PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/apps/phpmyadmin3.2.0.1/js/common.js

http://iprn.googlecode.com/
JavaScript | 453 lines | 266 code | 53 blank | 134 comment | 98 complexity | bb6a17724e356485b6273bedc1133bc7 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, LGPL-2.0, Apache-2.0, MPL-2.0-no-copyleft-exception, GPL-3.0
  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: common.js 12022 2008-11-28 14:35:17Z nijel $
  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. if (window.frame_navigation.document.getElementById(db) == null) {
  94. // db is unknown, reload complete left frame
  95. refreshNavigation();
  96. } else {
  97. unmarkDbTable(old_db);
  98. markDbTable(db);
  99. }
  100. // TODO: add code to expand db in lightview mode
  101. // refresh querywindow
  102. refreshQuerywindow();
  103. }
  104. }
  105. /**
  106. * sets current selected table (called from navigation.php)
  107. *
  108. * @param string table name
  109. */
  110. function setTable(new_table) {
  111. //alert('setTable(' + new_table + ')');
  112. if (new_table != table) {
  113. // table has changed
  114. //alert( new_table + '(' + new_table.length + ') : ' + table );
  115. table = new_table;
  116. if (window.frame_navigation.document.getElementById(db + '.' + table) == null
  117. && table != '') {
  118. // table is unknown, reload complete left frame
  119. refreshNavigation();
  120. }
  121. // TODO: add code to expand table in lightview mode
  122. // refresh querywindow
  123. refreshQuerywindow();
  124. }
  125. }
  126. /**
  127. * reloads main frame
  128. *
  129. * @uses goTo()
  130. * @uses opendb_url
  131. * @uses token
  132. * @uses db
  133. * @uses server
  134. * @uses table
  135. * @uses lang
  136. * @uses collation_connection
  137. * @uses encodeURIComponent()
  138. * @param string url name of page to be loaded
  139. */
  140. function refreshMain(url) {
  141. if (! url) {
  142. if (db) {
  143. url = opendb_url;
  144. } else {
  145. url = 'main.php';
  146. }
  147. }
  148. //alert(db);
  149. goTo(url + '?server=' + encodeURIComponent(server) +
  150. '&token=' + encodeURIComponent(token) +
  151. '&db=' + encodeURIComponent(db) +
  152. '&table=' + encodeURIComponent(table) +
  153. '&lang=' + encodeURIComponent(lang) +
  154. '&collation_connection=' + encodeURIComponent(collation_connection),
  155. 'main');
  156. }
  157. /**
  158. * reloads navigation frame
  159. *
  160. * @uses goTo()
  161. * @uses token
  162. * @uses db
  163. * @uses server
  164. * @uses table
  165. * @uses lang
  166. * @uses collation_connection
  167. * @uses encodeURIComponent()
  168. */
  169. function refreshNavigation() {
  170. goTo('navigation.php?server=' + encodeURIComponent(server) +
  171. '&token=' + encodeURIComponent(token) +
  172. '&db=' + encodeURIComponent(db) +
  173. '&table=' + encodeURIComponent(table) +
  174. '&lang=' + encodeURIComponent(lang) +
  175. '&collation_connection=' + encodeURIComponent(collation_connection)
  176. );
  177. }
  178. /**
  179. * adds class to element
  180. */
  181. function addClass(element, classname)
  182. {
  183. if (element != null) {
  184. element.className += ' ' + classname;
  185. //alert('set class: ' + classname + ', now: ' + element.className);
  186. }
  187. }
  188. /**
  189. * removes class from element
  190. */
  191. function removeClass(element, classname)
  192. {
  193. if (element != null) {
  194. element.className = element.className.replace(' ' + classname, '');
  195. // if there is no other class anem there is no leading space
  196. element.className = element.className.replace(classname, '');
  197. //alert('removed class: ' + classname + ', now: ' + element.className);
  198. }
  199. }
  200. function unmarkDbTable(db, table)
  201. {
  202. var element_reference = window.frame_navigation.document.getElementById(db);
  203. if (element_reference != null) {
  204. //alert('remove from: ' + db);
  205. removeClass(element_reference.parentNode, 'marked');
  206. }
  207. element_reference = window.frame_navigation.document.getElementById(db + '.' + table);
  208. if (element_reference != null) {
  209. //alert('remove from: ' + db + '.' + table);
  210. removeClass(element_reference.parentNode, 'marked');
  211. }
  212. }
  213. function markDbTable(db, table)
  214. {
  215. var element_reference = window.frame_navigation.document.getElementById(db);
  216. if (element_reference != null) {
  217. addClass(element_reference.parentNode, 'marked');
  218. // scrolldown
  219. element_reference.focus();
  220. // opera marks the text, we dont want this ...
  221. element_reference.blur();
  222. }
  223. element_reference = window.frame_navigation.document.getElementById(db + '.' + table);
  224. if (element_reference != null) {
  225. addClass(element_reference.parentNode, 'marked');
  226. // scrolldown
  227. element_reference.focus();
  228. // opera marks the text, we dont want this ...
  229. element_reference.blur();
  230. }
  231. // return to main frame ...
  232. window.frame_content.focus();
  233. }
  234. /**
  235. * sets current selected server, table and db (called from libraries/footer.inc.php)
  236. */
  237. function setAll( new_lang, new_collation_connection, new_server, new_db, new_table, new_token ) {
  238. //alert('setAll( ' + new_lang + ', ' + new_collation_connection + ', ' + new_server + ', ' + new_db + ', ' + new_table + ', ' + new_token + ' )');
  239. if (new_server != server || new_lang != lang
  240. || new_collation_connection != collation_connection) {
  241. // something important has changed
  242. server = new_server;
  243. db = new_db;
  244. table = new_table;
  245. collation_connection = new_collation_connection;
  246. lang = new_lang;
  247. token = new_token;
  248. refreshNavigation();
  249. } else if (new_db != db || new_table != table) {
  250. // save new db and table
  251. var old_db = db;
  252. var old_table = table;
  253. db = new_db;
  254. table = new_table;
  255. if (window.frame_navigation.document.getElementById(db) == null
  256. && window.frame_navigation.document.getElementById(db + '.' + table) == null ) {
  257. // table or db is unknown, reload complete left frame
  258. refreshNavigation();
  259. } else {
  260. unmarkDbTable(old_db, old_table);
  261. markDbTable(db, table);
  262. }
  263. // TODO: add code to expand db in lightview mode
  264. // refresh querywindow
  265. refreshQuerywindow();
  266. }
  267. }
  268. function reload_querywindow(db, table, sql_query)
  269. {
  270. if ( ! querywindow.closed && querywindow.location ) {
  271. if ( ! querywindow.document.sqlform.LockFromUpdate
  272. || ! querywindow.document.sqlform.LockFromUpdate.checked ) {
  273. querywindow.document.getElementById('hiddenqueryform').db.value = db;
  274. querywindow.document.getElementById('hiddenqueryform').table.value = table;
  275. if (sql_query) {
  276. querywindow.document.getElementById('hiddenqueryform').sql_query.value = sql_query;
  277. }
  278. querywindow.document.getElementById('hiddenqueryform').submit();
  279. }
  280. }
  281. }
  282. /**
  283. * brings query window to front and inserts query to be edited
  284. */
  285. function focus_querywindow(sql_query)
  286. {
  287. /* if ( querywindow && !querywindow.closed && querywindow.location) { */
  288. if ( !querywindow || querywindow.closed || !querywindow.location) {
  289. // we need first to open the window and cannot pass the query with it
  290. // as we dont know if the query exceeds max url length
  291. /* url = 'querywindow.php?' + common_query + '&db=' + db + '&table=' + table + '&sql_query=SELECT * FROM'; */
  292. query_to_load = sql_query;
  293. open_querywindow();
  294. insertQuery(0);
  295. } else {
  296. //var querywindow = querywindow;
  297. if ( querywindow.document.getElementById('hiddenqueryform').querydisplay_tab != 'sql' ) {
  298. querywindow.document.getElementById('hiddenqueryform').querydisplay_tab.value = "sql";
  299. querywindow.document.getElementById('hiddenqueryform').sql_query.value = sql_query;
  300. querywindow.document.getElementById('hiddenqueryform').submit();
  301. querywindow.focus();
  302. } else {
  303. querywindow.focus();
  304. }
  305. }
  306. return true;
  307. }
  308. /**
  309. * inserts query string into query window textarea
  310. * called from script tag in querywindow
  311. */
  312. function insertQuery() {
  313. if (query_to_load != '' && querywindow.document && querywindow.document.getElementById && querywindow.document.getElementById('sqlquery')) {
  314. querywindow.document.getElementById('sqlquery').value = query_to_load;
  315. query_to_load = '';
  316. return true;
  317. }
  318. return false;
  319. }
  320. function open_querywindow( url ) {
  321. if ( ! url ) {
  322. url = 'querywindow.php?' + common_query + '&db=' + encodeURIComponent(db) + '&table=' + encodeURIComponent(table);
  323. }
  324. if (!querywindow.closed && querywindow.location) {
  325. goTo( url, 'query' );
  326. querywindow.focus();
  327. } else {
  328. querywindow = window.open( url + '&init=1', '',
  329. 'toolbar=0,location=0,directories=0,status=1,menubar=0,' +
  330. 'scrollbars=yes,resizable=yes,' +
  331. 'width=' + querywindow_width + ',' +
  332. 'height=' + querywindow_height );
  333. }
  334. if ( ! querywindow.opener ) {
  335. querywindow.opener = window.window;
  336. }
  337. if ( window.focus ) {
  338. querywindow.focus();
  339. }
  340. return true;
  341. }
  342. function refreshQuerywindow( url ) {
  343. if ( ! querywindow.closed && querywindow.location ) {
  344. if ( ! querywindow.document.sqlform.LockFromUpdate
  345. || ! querywindow.document.sqlform.LockFromUpdate.checked ) {
  346. open_querywindow( url )
  347. }
  348. }
  349. }
  350. /**
  351. * opens new url in target frame, with default being left frame
  352. * valid is 'main' and 'querywindow' all others leads to 'left'
  353. *
  354. * @param string targeturl new url to load
  355. * @param string target frame where to load the new url
  356. */
  357. function goTo(targeturl, target) {
  358. //alert(targeturl);
  359. if ( target == 'main' ) {
  360. target = window.frame_content;
  361. } else if ( target == 'query' ) {
  362. target = querywindow;
  363. //return open_querywindow( targeturl );
  364. } else if ( ! target ) {
  365. target = window.frame_navigation;
  366. }
  367. if ( target ) {
  368. if ( target.location.href == targeturl ) {
  369. return true;
  370. } else if ( target.location.href == pma_absolute_uri + targeturl ) {
  371. return true;
  372. }
  373. if ( safari_browser ) {
  374. target.location.href = targeturl;
  375. } else {
  376. target.location.replace(targeturl);
  377. }
  378. }
  379. return true;
  380. }
  381. // opens selected db in main frame
  382. function openDb(new_db) {
  383. //alert('opendb(' + new_db + ')');
  384. setDb(new_db);
  385. setTable('');
  386. refreshMain(opendb_url);
  387. return true;
  388. }
  389. function updateTableTitle( table_link_id, new_title ) {
  390. //alert('updateTableTitle');
  391. if ( window.parent.frame_navigation.document.getElementById(table_link_id) ) {
  392. var left = window.parent.frame_navigation.document;
  393. var link = left.getElementById(table_link_id);
  394. link.title = window.parent.pma_text_default_tab + ': ' + new_title;
  395. var link = left.getElementById('quick_' + table_link_id);
  396. link.title = window.parent.pma_text_left_default_tab + ': ' + new_title;
  397. return true;
  398. }
  399. return false;
  400. }