sql.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * @fileoverview functions used wherever an sql query form is used
  4. *
  5. * @requires jQuery
  6. * @requires js/functions.js
  7. *
  8. */
  9. var $data_a;
  10. /**
  11. * decode a string URL_encoded
  12. *
  13. * @param string str
  14. * @return string the URL-decoded string
  15. */
  16. function PMA_urldecode(str)
  17. {
  18. return decodeURIComponent(str.replace(/\+/g, '%20'));
  19. }
  20. /**
  21. * endecode a string URL_decoded
  22. *
  23. * @param string str
  24. * @return string the URL-encoded string
  25. */
  26. function PMA_urlencode(str)
  27. {
  28. return encodeURIComponent(str).replace(/\%20/g, '+');
  29. }
  30. /**
  31. * Get the field name for the current field. Required to construct the query
  32. * for grid editing
  33. *
  34. * @param $this_field jQuery object that points to the current field's tr
  35. */
  36. function getFieldName($this_field)
  37. {
  38. var this_field_index = $this_field.index();
  39. // ltr or rtl direction does not impact how the DOM was generated
  40. // check if the action column in the left exist
  41. var left_action_exist = !$('#table_results').find('th:first').hasClass('draggable');
  42. // number of column span for checkbox and Actions
  43. var left_action_skip = left_action_exist ? $('#table_results').find('th:first').attr('colspan') - 1 : 0;
  44. var field_name = $('#table_results').find('thead').find('th:eq('+ (this_field_index - left_action_skip) + ') a').text();
  45. // happens when just one row (headings contain no a)
  46. if ("" == field_name) {
  47. var $heading = $('#table_results').find('thead').find('th:eq('+ (this_field_index - left_action_skip) + ')').children('span');
  48. // may contain column comment enclosed in a span - detach it temporarily to read the column name
  49. var $tempColComment = $heading.children().detach();
  50. field_name = $heading.text();
  51. // re-attach the column comment
  52. $heading.append($tempColComment);
  53. }
  54. field_name = $.trim(field_name);
  55. return field_name;
  56. }
  57. /**
  58. * Unbind all event handlers before tearing down a page
  59. */
  60. AJAX.registerTeardown('sql.js', function() {
  61. $('a.delete_row.ajax').unbind('click');
  62. $('#bookmarkQueryForm').die('submit');
  63. $('input#bkm_label').unbind('keyup');
  64. $("#sqlqueryresults").die('makegrid');
  65. $("#togglequerybox").unbind('click');
  66. $("#button_submit_query").die('click');
  67. $("input[name=bookmark_variable]").unbind("keypress");
  68. $("#sqlqueryform.ajax").die('submit');
  69. $("input[name=navig].ajax").die('click');
  70. $("#pageselector").die('change');
  71. $("#table_results.ajax").find("a[title=Sort]").die('click');
  72. $("#displayOptionsForm.ajax").die('submit');
  73. $('a.browse_foreign').die('click');
  74. $('th.column_heading.pointer').die('hover');
  75. $('th.column_heading.marker').die('click');
  76. });
  77. /**
  78. * @description <p>Ajax scripts for sql and browse pages</p>
  79. *
  80. * Actions ajaxified here:
  81. * <ul>
  82. * <li>Retrieve results of an SQL query</li>
  83. * <li>Paginate the results table</li>
  84. * <li>Sort the results table</li>
  85. * <li>Change table according to display options</li>
  86. * <li>Grid editing of data</li>
  87. * <li>Saving a bookmark</li>
  88. * </ul>
  89. *
  90. * @name document.ready
  91. * @memberOf jQuery
  92. */
  93. AJAX.registerOnload('sql.js', function() {
  94. // Delete row from SQL results
  95. $('a.delete_row.ajax').click(function (e) {
  96. e.preventDefault();
  97. var question = $.sprintf(PMA_messages['strDoYouReally'], escapeHtml($(this).closest('td').find('div').text()));
  98. var $link = $(this);
  99. $link.PMA_confirm(question, $link.attr('href'), function (url) {
  100. $msgbox = PMA_ajaxShowMessage();
  101. $.get(url, {'ajax_request':true, 'is_js_confirmed': true}, function (data) {
  102. if (data.success) {
  103. PMA_ajaxShowMessage(data.message);
  104. $link.closest('tr').remove();
  105. } else {
  106. PMA_ajaxShowMessage(data.error, false);
  107. }
  108. })
  109. });
  110. });
  111. // Ajaxification for 'Bookmark this SQL query'
  112. $('#bookmarkQueryForm').live('submit', function (e) {
  113. e.preventDefault();
  114. PMA_ajaxShowMessage();
  115. $.post($(this).attr('action'), 'ajax_request=1&' + $(this).serialize(), function (data) {
  116. if (data.success) {
  117. PMA_ajaxShowMessage(data.message);
  118. } else {
  119. PMA_ajaxShowMessage(data.error, false);
  120. }
  121. });
  122. });
  123. /* Hides the bookmarkoptions checkboxes when the bookmark label is empty */
  124. $('input#bkm_label').keyup(function() {
  125. $('input#id_bkm_all_users, input#id_bkm_replace')
  126. .parent()
  127. .toggle($(this).val().length > 0);
  128. }).trigger('keyup');
  129. /**
  130. * Attach the {@link makegrid} function to a custom event, which will be
  131. * triggered manually everytime the table of results is reloaded
  132. * @memberOf jQuery
  133. */
  134. $("#sqlqueryresults").live('makegrid', function() {
  135. PMA_makegrid($('#table_results')[0]);
  136. });
  137. /**
  138. * Append the "Show/Hide query box" message to the query input form
  139. *
  140. * @memberOf jQuery
  141. * @name appendToggleSpan
  142. */
  143. // do not add this link more than once
  144. if (! $('#sqlqueryform').find('a').is('#togglequerybox')) {
  145. $('<a id="togglequerybox"></a>')
  146. .html(PMA_messages['strHideQueryBox'])
  147. .appendTo("#sqlqueryform")
  148. // initially hidden because at this point, nothing else
  149. // appears under the link
  150. .hide();
  151. // Attach the toggling of the query box visibility to a click
  152. $("#togglequerybox").bind('click', function() {
  153. var $link = $(this);
  154. $link.siblings().slideToggle("fast");
  155. if ($link.text() == PMA_messages['strHideQueryBox']) {
  156. $link.text(PMA_messages['strShowQueryBox']);
  157. // cheap trick to add a spacer between the menu tabs
  158. // and "Show query box"; feel free to improve!
  159. $('#togglequerybox_spacer').remove();
  160. $link.before('<br id="togglequerybox_spacer" />');
  161. } else {
  162. $link.text(PMA_messages['strHideQueryBox']);
  163. }
  164. // avoid default click action
  165. return false;
  166. });
  167. }
  168. /**
  169. * Event handler for sqlqueryform.ajax button_submit_query
  170. *
  171. * @memberOf jQuery
  172. */
  173. $("#button_submit_query").live('click', function(event) {
  174. var $form = $(this).closest("form");
  175. // the Go button related to query submission was clicked,
  176. // instead of the one related to Bookmarks, so empty the
  177. // id_bookmark selector to avoid misinterpretation in
  178. // import.php about what needs to be done
  179. $form.find("select[name=id_bookmark]").val("");
  180. // let normal event propagation happen
  181. });
  182. /**
  183. * Event handler for hitting enter on sqlqueryform bookmark_variable
  184. * (the Variable textfield in Bookmarked SQL query section)
  185. *
  186. * @memberOf jQuery
  187. */
  188. $("input[name=bookmark_variable]").bind("keypress", function(event) {
  189. // force the 'Enter Key' to implicitly click the #button_submit_bookmark
  190. var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
  191. if (keycode == 13) { // keycode for enter key
  192. // When you press enter in the sqlqueryform, which
  193. // has 2 submit buttons, the default is to run the
  194. // #button_submit_query, because of the tabindex
  195. // attribute.
  196. // This submits #button_submit_bookmark instead,
  197. // because when you are in the Bookmarked SQL query
  198. // section and hit enter, you expect it to do the
  199. // same action as the Go button in that section.
  200. $("#button_submit_bookmark").click();
  201. return false;
  202. } else {
  203. return true;
  204. }
  205. });
  206. /**
  207. * Ajax Event handler for 'SQL Query Submit'
  208. *
  209. * @see PMA_ajaxShowMessage()
  210. * @memberOf jQuery
  211. * @name sqlqueryform_submit
  212. */
  213. $("#sqlqueryform.ajax").live('submit', function(event) {
  214. event.preventDefault();
  215. var $form = $(this);
  216. if (! checkSqlQuery($form[0])) {
  217. return false;
  218. }
  219. // remove any div containing a previous error message
  220. $('div.error').remove();
  221. var $msgbox = PMA_ajaxShowMessage();
  222. var $sqlqueryresults = $('#sqlqueryresults');
  223. PMA_prepareForAjaxRequest($form);
  224. $.post($form.attr('action'), $form.serialize() , function(data) {
  225. if (data.success == true) {
  226. // success happens if the query returns rows or not
  227. //
  228. // fade out previous messages, if any
  229. $('div.success, div.sqlquery_message').fadeOut();
  230. if ($('#result_query').length) {
  231. $('#result_query').remove();
  232. }
  233. // show a message that stays on screen
  234. if (typeof data.action_bookmark != 'undefined') {
  235. // view only
  236. if ('1' == data.action_bookmark) {
  237. $('#sqlquery').text(data.sql_query);
  238. // send to codemirror if possible
  239. setQuery(data.sql_query);
  240. }
  241. // delete
  242. if ('2' == data.action_bookmark) {
  243. $("#id_bookmark option[value='" + data.id_bookmark + "']").remove();
  244. }
  245. $sqlqueryresults
  246. .show()
  247. .html(data.message);
  248. } else if (typeof data.sql_query != 'undefined') {
  249. $('<div class="sqlquery_message"></div>')
  250. .html(data.sql_query)
  251. .insertBefore('#sqlqueryform');
  252. // unnecessary div that came from data.sql_query
  253. $('div.notice').remove();
  254. } else {
  255. $sqlqueryresults
  256. .show()
  257. .html(data.message);
  258. }
  259. if (typeof data.ajax_reload != 'undefined') {
  260. if (data.ajax_reload.reload) {
  261. if (data.ajax_reload.table_name) {
  262. PMA_commonParams.set('table', data.ajax_reload.table_name);
  263. PMA_commonActions.refreshMain();
  264. } else {
  265. PMA_reloadNavigation();
  266. }
  267. }
  268. } else if (typeof data.reload != 'undefined') {
  269. // this happens if a USE or DROP command was typed
  270. PMA_commonActions.setDb(data.db);
  271. var url;
  272. if (data.db) {
  273. if (data.table) {
  274. url = 'table_sql.php';
  275. } else {
  276. url = 'db_sql.php';
  277. }
  278. } else {
  279. url = 'server_sql.php';
  280. }
  281. PMA_commonActions.refreshMain(url, function () {
  282. if ($('#result_query').length) {
  283. $('#result_query').remove();
  284. }
  285. if (data.sql_query) {
  286. $('<div id="result_query"></div>')
  287. .html(data.sql_query)
  288. .prependTo('#page_content');
  289. }
  290. });
  291. }
  292. $sqlqueryresults.show().trigger('makegrid');
  293. $('#togglequerybox').show();
  294. PMA_init_slider();
  295. if (typeof data.action_bookmark == 'undefined') {
  296. if ( $('#sqlqueryform input[name="retain_query_box"]').is(':checked') != true ) {
  297. if ($("#togglequerybox").siblings(":visible").length > 0) {
  298. $("#togglequerybox").trigger('click');
  299. }
  300. }
  301. }
  302. } else if (data.success == false ) {
  303. // show an error message that stays on screen
  304. $('#sqlqueryform').before(data.error);
  305. $sqlqueryresults.hide();
  306. }
  307. PMA_ajaxRemoveMessage($msgbox);
  308. }); // end $.post()
  309. }); // end SQL Query submit
  310. /**
  311. * Paginate results with Page Selector dropdown
  312. * @memberOf jQuery
  313. * @name paginate_dropdown_change
  314. */
  315. $("#pageselector").live('change', function(event) {
  316. var $form = $(this).parent("form");
  317. $form.submit();
  318. }); // end Paginate results with Page Selector
  319. /**
  320. * Ajax Event handler for the display options
  321. * @memberOf jQuery
  322. * @name displayOptionsForm_submit
  323. */
  324. $("#displayOptionsForm.ajax").live('submit', function(event) {
  325. event.preventDefault();
  326. $form = $(this);
  327. $.post($form.attr('action'), $form.serialize() + '&ajax_request=true' , function(data) {
  328. $("#sqlqueryresults")
  329. .html(data.message)
  330. .trigger('makegrid');
  331. PMA_init_slider();
  332. }); // end $.post()
  333. }); //end displayOptionsForm handler
  334. }); // end $()
  335. /**
  336. * Starting from some th, change the class of all td under it.
  337. * If isAddClass is specified, it will be used to determine whether to add or remove the class.
  338. */
  339. function PMA_changeClassForColumn($this_th, newclass, isAddClass)
  340. {
  341. // index 0 is the th containing the big T
  342. var th_index = $this_th.index();
  343. var has_big_t = !$this_th.closest('tr').children(':first').hasClass('column_heading');
  344. // .eq() is zero-based
  345. if (has_big_t) {
  346. th_index--;
  347. }
  348. var $tds = $this_th.closest('table').find('tbody tr').find('td.data:eq('+th_index+')');
  349. if (isAddClass == undefined) {
  350. $tds.toggleClass(newclass);
  351. } else {
  352. $tds.toggleClass(newclass, isAddClass);
  353. }
  354. }
  355. AJAX.registerOnload('sql.js', function() {
  356. $('a.browse_foreign').live('click', function(e) {
  357. e.preventDefault();
  358. window.open(this.href, 'foreigners', 'width=640,height=240,scrollbars=yes,resizable=yes');
  359. $anchor = $(this);
  360. $anchor.addClass('browse_foreign_clicked');
  361. });
  362. /**
  363. * vertical column highlighting in horizontal mode when hovering over the column header
  364. */
  365. $('th.column_heading.pointer').live('hover', function(e) {
  366. PMA_changeClassForColumn($(this), 'hover', e.type == 'mouseenter');
  367. });
  368. /**
  369. * vertical column marking in horizontal mode when clicking the column header
  370. */
  371. $('th.column_heading.marker').live('click', function() {
  372. PMA_changeClassForColumn($(this), 'marked');
  373. });
  374. /**
  375. * create resizable table
  376. */
  377. $("#sqlqueryresults").trigger('makegrid');
  378. });
  379. /*
  380. * Profiling Chart
  381. */
  382. function makeProfilingChart()
  383. {
  384. if ($('#profilingchart').length == 0
  385. || $('#profilingchart').html().length != 0
  386. ) {
  387. return;
  388. }
  389. var data = [];
  390. $.each(jQuery.parseJSON($('#profilingChartData').html()),function(key,value) {
  391. data.push([key,parseFloat(value)]);
  392. });
  393. // Remove chart and data divs contents
  394. $('#profilingchart').html('').show();
  395. $('#profilingChartData').html('');
  396. PMA_createProfilingChartJqplot('profilingchart', data);
  397. }