PageRenderTime 59ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/phpmyadmin/js/common.js

http://github.com/jyr/MNPP
JavaScript | 456 lines | 266 code | 53 blank | 137 comment | 99 complexity | 0625106c808d4de1fc5ed14c796e96f1 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, LGPL-2.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0, BSD-3-Clause, GPL-3.0, BSD-2-Clause
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * common functions used for communicating between main, navigation and querywindow
  4. *
  5. * @version $Id$
  6. */
  7. /**
  8. * holds the browser query window
  9. */
  10. var querywindow = '';
  11. /**
  12. * holds the query to be load from a new query window
  13. */
  14. var query_to_load = '';
  15. /**
  16. * attach a function to object event
  17. *
  18. * <code>
  19. * addEvent(window, 'load', PMA_initPage);
  20. * </code>
  21. * @param object or id
  22. * @param string event type (load, mouseover, focus, ...)
  23. * @param function to be attached
  24. */
  25. function addEvent(obj, type, fn)
  26. {
  27. if (obj.attachEvent) {
  28. obj['e' + type + fn] = fn;
  29. obj[type + fn] = function() {obj['e' + type + fn](window.event);}
  30. obj.attachEvent('on' + type, obj[type + fn]);
  31. } else {
  32. obj.addEventListener(type, fn, false);
  33. }
  34. }
  35. /**
  36. * detach/remove a function from an object event
  37. *
  38. * @param object or id
  39. * @param event type (load, mouseover, focus, ...)
  40. * @param function naem of function to be attached
  41. */
  42. function removeEvent(obj, type, fn)
  43. {
  44. if (obj.detachEvent) {
  45. obj.detachEvent('on' + type, obj[type + fn]);
  46. obj[type + fn] = null;
  47. } else {
  48. obj.removeEventListener(type, fn, false);
  49. }
  50. }
  51. /**
  52. * get DOM elements by html class
  53. *
  54. * @param string class_name - name of class
  55. * @param node node - search only sub nodes of this node (optional)
  56. * @param string tag - search only these tags (optional)
  57. */
  58. function getElementsByClassName(class_name, node, tag)
  59. {
  60. var classElements = new Array();
  61. if (node == null) {
  62. node = document;
  63. }
  64. if (tag == null) {
  65. tag = '*';
  66. }
  67. var j = 0, teststr;
  68. var els = node.getElementsByTagName(tag);
  69. var elsLen = els.length;
  70. for (i = 0; i < elsLen; i++) {
  71. if (els[i].className.indexOf(class_name) != -1) {
  72. teststr = "," + els[i].className.split(" ").join(",") + ",";
  73. if (teststr.indexOf("," + class_name + ",") != -1) {
  74. classElements[j] = els[i];
  75. j++;
  76. }
  77. }
  78. }
  79. return classElements;
  80. }
  81. /**
  82. * sets current selected db
  83. *
  84. * @param string db name
  85. */
  86. function setDb(new_db) {
  87. //alert('setDb(' + new_db + ')');
  88. if (new_db != db) {
  89. // db has changed
  90. //alert( new_db + '(' + new_db.length + ') : ' + db );
  91. var old_db = db;
  92. db = new_db;
  93. // the db name as an id exists only when LeftFrameLight is false
  94. if (window.frame_navigation.document.getElementById(db) == null) {
  95. // happens when LeftFrameLight is true
  96. // db is unknown, reload complete left frame
  97. refreshNavigation();
  98. } else {
  99. // happens when LeftFrameLight is false
  100. unmarkDbTable(old_db);
  101. markDbTable(db);
  102. }
  103. // TODO: add code to expand db in lightview mode
  104. // refresh querywindow
  105. refreshQuerywindow();
  106. }
  107. }
  108. /**
  109. * sets current selected table (called from navigation.php)
  110. *
  111. * @param string table name
  112. */
  113. function setTable(new_table) {
  114. //alert('setTable(' + new_table + ')');
  115. if (new_table != table) {
  116. // table has changed
  117. //alert( new_table + '(' + new_table.length + ') : ' + table );
  118. table = new_table;
  119. if (window.frame_navigation.document.getElementById(db + '.' + table) == null
  120. && table != '') {
  121. // table is unknown, reload complete left frame
  122. refreshNavigation();
  123. }
  124. // TODO: add code to expand table in lightview mode
  125. // refresh querywindow
  126. refreshQuerywindow();
  127. }
  128. }
  129. /**
  130. * reloads main frame
  131. *
  132. * @uses goTo()
  133. * @uses opendb_url
  134. * @uses token
  135. * @uses db
  136. * @uses server
  137. * @uses table
  138. * @uses lang
  139. * @uses collation_connection
  140. * @uses encodeURIComponent()
  141. * @param string url name of page to be loaded
  142. */
  143. function refreshMain(url) {
  144. if (! url) {
  145. if (db) {
  146. url = opendb_url;
  147. } else {
  148. url = 'main.php';
  149. }
  150. }
  151. //alert(db);
  152. goTo(url + '?server=' + encodeURIComponent(server) +
  153. '&token=' + encodeURIComponent(token) +
  154. '&db=' + encodeURIComponent(db) +
  155. '&table=' + encodeURIComponent(table) +
  156. '&lang=' + encodeURIComponent(lang) +
  157. '&collation_connection=' + encodeURIComponent(collation_connection),
  158. 'main');
  159. }
  160. /**
  161. * reloads navigation frame
  162. *
  163. * @uses goTo()
  164. * @uses token
  165. * @uses db
  166. * @uses server
  167. * @uses table
  168. * @uses lang
  169. * @uses collation_connection
  170. * @uses encodeURIComponent()
  171. */
  172. function refreshNavigation() {
  173. goTo('navigation.php?server=' + encodeURIComponent(server) +
  174. '&token=' + encodeURIComponent(token) +
  175. '&db=' + encodeURIComponent(db) +
  176. '&table=' + encodeURIComponent(table) +
  177. '&lang=' + encodeURIComponent(lang) +
  178. '&collation_connection=' + encodeURIComponent(collation_connection)
  179. );
  180. }
  181. /**
  182. * adds class to element
  183. */
  184. function addClass(element, classname)
  185. {
  186. if (element != null) {
  187. element.className += ' ' + classname;
  188. //alert('set class: ' + classname + ', now: ' + element.className);
  189. }
  190. }
  191. /**
  192. * removes class from element
  193. */
  194. function removeClass(element, classname)
  195. {
  196. if (element != null) {
  197. element.className = element.className.replace(' ' + classname, '');
  198. // if there is no other class anem there is no leading space
  199. element.className = element.className.replace(classname, '');
  200. //alert('removed class: ' + classname + ', now: ' + element.className);
  201. }
  202. }
  203. function unmarkDbTable(db, table)
  204. {
  205. var element_reference = window.frame_navigation.document.getElementById(db);
  206. if (element_reference != null) {
  207. //alert('remove from: ' + db);
  208. removeClass(element_reference.parentNode, 'marked');
  209. }
  210. element_reference = window.frame_navigation.document.getElementById(db + '.' + table);
  211. if (element_reference != null) {
  212. //alert('remove from: ' + db + '.' + table);
  213. removeClass(element_reference.parentNode, 'marked');
  214. }
  215. }
  216. function markDbTable(db, table)
  217. {
  218. var element_reference = window.frame_navigation.document.getElementById(db);
  219. if (element_reference != null) {
  220. addClass(element_reference.parentNode, 'marked');
  221. // scrolldown
  222. element_reference.focus();
  223. // opera marks the text, we dont want this ...
  224. element_reference.blur();
  225. }
  226. element_reference = window.frame_navigation.document.getElementById(db + '.' + table);
  227. if (element_reference != null) {
  228. addClass(element_reference.parentNode, 'marked');
  229. // scrolldown
  230. element_reference.focus();
  231. // opera marks the text, we dont want this ...
  232. element_reference.blur();
  233. }
  234. // return to main frame ...
  235. window.frame_content.focus();
  236. }
  237. /**
  238. * sets current selected server, table and db (called from libraries/footer.inc.php)
  239. */
  240. function setAll( new_lang, new_collation_connection, new_server, new_db, new_table, new_token ) {
  241. //alert('setAll( ' + new_lang + ', ' + new_collation_connection + ', ' + new_server + ', ' + new_db + ', ' + new_table + ', ' + new_token + ' )');
  242. if (new_server != server || new_lang != lang
  243. || new_collation_connection != collation_connection) {
  244. // something important has changed
  245. server = new_server;
  246. db = new_db;
  247. table = new_table;
  248. collation_connection = new_collation_connection;
  249. lang = new_lang;
  250. token = new_token;
  251. refreshNavigation();
  252. } else if (new_db != db || new_table != table) {
  253. // save new db and table
  254. var old_db = db;
  255. var old_table = table;
  256. db = new_db;
  257. table = new_table;
  258. if (window.frame_navigation.document.getElementById(db) == null
  259. && window.frame_navigation.document.getElementById(db + '.' + table) == null ) {
  260. // table or db is unknown, reload complete left frame
  261. refreshNavigation();
  262. } else {
  263. unmarkDbTable(old_db, old_table);
  264. markDbTable(db, table);
  265. }
  266. // TODO: add code to expand db in lightview mode
  267. // refresh querywindow
  268. refreshQuerywindow();
  269. }
  270. }
  271. function reload_querywindow(db, table, sql_query)
  272. {
  273. if ( ! querywindow.closed && querywindow.location ) {
  274. if ( ! querywindow.document.sqlform.LockFromUpdate
  275. || ! querywindow.document.sqlform.LockFromUpdate.checked ) {
  276. querywindow.document.getElementById('hiddenqueryform').db.value = db;
  277. querywindow.document.getElementById('hiddenqueryform').table.value = table;
  278. if (sql_query) {
  279. querywindow.document.getElementById('hiddenqueryform').sql_query.value = sql_query;
  280. }
  281. querywindow.document.getElementById('hiddenqueryform').submit();
  282. }
  283. }
  284. }
  285. /**
  286. * brings query window to front and inserts query to be edited
  287. */
  288. function focus_querywindow(sql_query)
  289. {
  290. /* if ( querywindow && !querywindow.closed && querywindow.location) { */
  291. if ( !querywindow || querywindow.closed || !querywindow.location) {
  292. // we need first to open the window and cannot pass the query with it
  293. // as we dont know if the query exceeds max url length
  294. /* url = 'querywindow.php?' + common_query + '&db=' + db + '&table=' + table + '&sql_query=SELECT * FROM'; */
  295. query_to_load = sql_query;
  296. open_querywindow();
  297. insertQuery(0);
  298. } else {
  299. //var querywindow = querywindow;
  300. if ( querywindow.document.getElementById('hiddenqueryform').querydisplay_tab != 'sql' ) {
  301. querywindow.document.getElementById('hiddenqueryform').querydisplay_tab.value = "sql";
  302. querywindow.document.getElementById('hiddenqueryform').sql_query.value = sql_query;
  303. querywindow.document.getElementById('hiddenqueryform').submit();
  304. querywindow.focus();
  305. } else {
  306. querywindow.focus();
  307. }
  308. }
  309. return true;
  310. }
  311. /**
  312. * inserts query string into query window textarea
  313. * called from script tag in querywindow
  314. */
  315. function insertQuery() {
  316. if (query_to_load != '' && querywindow.document && querywindow.document.getElementById && querywindow.document.getElementById('sqlquery')) {
  317. querywindow.document.getElementById('sqlquery').value = query_to_load;
  318. query_to_load = '';
  319. return true;
  320. }
  321. return false;
  322. }
  323. function open_querywindow( url ) {
  324. if ( ! url ) {
  325. url = 'querywindow.php?' + common_query + '&db=' + encodeURIComponent(db) + '&table=' + encodeURIComponent(table);
  326. }
  327. if (!querywindow.closed && querywindow.location) {
  328. goTo( url, 'query' );
  329. querywindow.focus();
  330. } else {
  331. querywindow = window.open( url + '&init=1', '',
  332. 'toolbar=0,location=0,directories=0,status=1,menubar=0,' +
  333. 'scrollbars=yes,resizable=yes,' +
  334. 'width=' + querywindow_width + ',' +
  335. 'height=' + querywindow_height );
  336. }
  337. if ( ! querywindow.opener ) {
  338. querywindow.opener = window.window;
  339. }
  340. if ( window.focus ) {
  341. querywindow.focus();
  342. }
  343. return true;
  344. }
  345. function refreshQuerywindow( url ) {
  346. if ( ! querywindow.closed && querywindow.location ) {
  347. if ( ! querywindow.document.sqlform.LockFromUpdate
  348. || ! querywindow.document.sqlform.LockFromUpdate.checked ) {
  349. open_querywindow( url )
  350. }
  351. }
  352. }
  353. /**
  354. * opens new url in target frame, with default being left frame
  355. * valid is 'main' and 'querywindow' all others leads to 'left'
  356. *
  357. * @param string targeturl new url to load
  358. * @param string target frame where to load the new url
  359. */
  360. function goTo(targeturl, target) {
  361. //alert(targeturl);
  362. if ( target == 'main' ) {
  363. target = window.frame_content;
  364. } else if ( target == 'query' ) {
  365. target = querywindow;
  366. //return open_querywindow( targeturl );
  367. } else if ( ! target ) {
  368. target = window.frame_navigation;
  369. }
  370. if ( target ) {
  371. if ( target.location.href == targeturl ) {
  372. return true;
  373. } else if ( target.location.href == pma_absolute_uri + targeturl ) {
  374. return true;
  375. }
  376. if ( safari_browser ) {
  377. target.location.href = targeturl;
  378. } else {
  379. target.location.replace(targeturl);
  380. }
  381. }
  382. return true;
  383. }
  384. // opens selected db in main frame
  385. function openDb(new_db) {
  386. //alert('opendb(' + new_db + ')');
  387. setDb(new_db);
  388. setTable('');
  389. refreshMain(opendb_url);
  390. return true;
  391. }
  392. function updateTableTitle( table_link_id, new_title ) {
  393. //alert('updateTableTitle');
  394. if ( window.parent.frame_navigation.document && window.parent.frame_navigation.document.getElementById(table_link_id) ) {
  395. var left = window.parent.frame_navigation.document;
  396. var link = left.getElementById(table_link_id);
  397. link.title = window.parent.pma_text_default_tab + ': ' + new_title;
  398. var link = left.getElementById('quick_' + table_link_id);
  399. link.title = window.parent.pma_text_left_default_tab + ': ' + new_title;
  400. return true;
  401. }
  402. return false;
  403. }