tbl_select.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * @fileoverview JavaScript functions used on tbl_select.php
  4. *
  5. * @requires jQuery
  6. * @requires js/functions.js
  7. */
  8. /**
  9. * Ajax event handlers for this page
  10. *
  11. * Actions ajaxified here:
  12. * Table search
  13. */
  14. /**
  15. * Checks if given data-type is numeric or date.
  16. *
  17. * @param string data_type Column data-type
  18. *
  19. * @return bool|string
  20. */
  21. function PMA_checkIfDataTypeNumericOrDate (data_type) {
  22. // To test for numeric data-types.
  23. var numeric_re = new RegExp(
  24. 'TINYINT|SMALLINT|MEDIUMINT|INT|BIGINT|DECIMAL|FLOAT|DOUBLE|REAL',
  25. 'i'
  26. );
  27. // To test for date data-types.
  28. var date_re = new RegExp(
  29. 'DATETIME|DATE|TIMESTAMP|TIME|YEAR',
  30. 'i'
  31. );
  32. // Return matched data-type
  33. if (numeric_re.test(data_type)) {
  34. return numeric_re.exec(data_type)[0];
  35. }
  36. if (date_re.test(data_type)) {
  37. return date_re.exec(data_type)[0];
  38. }
  39. return false;
  40. }
  41. /**
  42. * Unbind all event handlers before tearing down a page
  43. */
  44. AJAX.registerTeardown('tbl_select.js', function () {
  45. $('#togglesearchformlink').off('click');
  46. $(document).off('submit', '#tbl_search_form.ajax');
  47. $('select.geom_func').off('change');
  48. $(document).off('click', 'span.open_search_gis_editor');
  49. $('body').off('change', 'select[name*="criteriaColumnOperators"]'); // Fix for bug #13778, changed 'click' to 'change'
  50. });
  51. AJAX.registerOnload('tbl_select.js', function () {
  52. /**
  53. * Prepare a div containing a link, otherwise it's incorrectly displayed
  54. * after a couple of clicks
  55. */
  56. $('<div id="togglesearchformdiv"><a id="togglesearchformlink"></a></div>')
  57. .insertAfter('#tbl_search_form')
  58. // don't show it until we have results on-screen
  59. .hide();
  60. $('#togglesearchformlink')
  61. .html(PMA_messages.strShowSearchCriteria)
  62. .on('click', function () {
  63. var $link = $(this);
  64. $('#tbl_search_form').slideToggle();
  65. if ($link.text() === PMA_messages.strHideSearchCriteria) {
  66. $link.text(PMA_messages.strShowSearchCriteria);
  67. } else {
  68. $link.text(PMA_messages.strHideSearchCriteria);
  69. }
  70. // avoid default click action
  71. return false;
  72. });
  73. var tableRows = $('#fieldset_table_qbe select');
  74. $.each(tableRows, function (index, item) {
  75. $(item).on('change', function () {
  76. changeValueFieldType(this, index);
  77. });
  78. });
  79. /**
  80. * Ajax event handler for Table search
  81. */
  82. $(document).on('submit', '#tbl_search_form.ajax', function (event) {
  83. var unaryFunctions = [
  84. 'IS NULL',
  85. 'IS NOT NULL',
  86. '= \'\'',
  87. '!= \'\''
  88. ];
  89. var geomUnaryFunctions = [
  90. 'IsEmpty',
  91. 'IsSimple',
  92. 'IsRing',
  93. 'IsClosed',
  94. ];
  95. // jQuery object to reuse
  96. var $search_form = $(this);
  97. event.preventDefault();
  98. // empty previous search results while we are waiting for new results
  99. $('#sqlqueryresultsouter').empty();
  100. var $msgbox = PMA_ajaxShowMessage(PMA_messages.strSearching, false);
  101. PMA_prepareForAjaxRequest($search_form);
  102. var values = {};
  103. $search_form.find(':input').each(function () {
  104. var $input = $(this);
  105. if ($input.attr('type') === 'checkbox' || $input.attr('type') === 'radio') {
  106. if ($input.is(':checked')) {
  107. values[this.name] = $input.val();
  108. }
  109. } else {
  110. values[this.name] = $input.val();
  111. }
  112. });
  113. var columnCount = $('select[name="columnsToDisplay[]"] option').length;
  114. // Submit values only for the columns that have unary column operator or a search criteria
  115. for (var a = 0; a < columnCount; a++) {
  116. if ($.inArray(values['criteriaColumnOperators[' + a + ']'], unaryFunctions) >= 0) {
  117. continue;
  118. }
  119. if (values['geom_func[' + a + ']'] &&
  120. $.isArray(values['geom_func[' + a + ']'], geomUnaryFunctions) >= 0) {
  121. continue;
  122. }
  123. if (values['criteriaValues[' + a + ']'] === '' || values['criteriaValues[' + a + ']'] === null) {
  124. delete values['criteriaValues[' + a + ']'];
  125. delete values['criteriaColumnOperators[' + a + ']'];
  126. delete values['criteriaColumnNames[' + a + ']'];
  127. delete values['criteriaColumnTypes[' + a + ']'];
  128. delete values['criteriaColumnCollations[' + a + ']'];
  129. }
  130. }
  131. // If all columns are selected, use a single parameter to indicate that
  132. if (values['columnsToDisplay[]'] !== null) {
  133. if (values['columnsToDisplay[]'].length === columnCount) {
  134. delete values['columnsToDisplay[]'];
  135. values.displayAllColumns = true;
  136. }
  137. } else {
  138. values.displayAllColumns = true;
  139. }
  140. $.post($search_form.attr('action'), values, function (data) {
  141. PMA_ajaxRemoveMessage($msgbox);
  142. if (typeof data !== 'undefined' && data.success === true) {
  143. if (typeof data.sql_query !== 'undefined') { // zero rows
  144. $('#sqlqueryresultsouter').html(data.sql_query);
  145. } else { // results found
  146. $('#sqlqueryresultsouter').html(data.message);
  147. $('.sqlqueryresults').trigger('makegrid').trigger('stickycolumns');
  148. }
  149. $('#tbl_search_form')
  150. // workaround for bug #3168569 - Issue on toggling the "Hide search criteria" in chrome.
  151. .slideToggle()
  152. .hide();
  153. $('#togglesearchformlink')
  154. // always start with the Show message
  155. .text(PMA_messages.strShowSearchCriteria);
  156. $('#togglesearchformdiv')
  157. // now it's time to show the div containing the link
  158. .show();
  159. // needed for the display options slider in the results
  160. PMA_init_slider();
  161. $('html, body').animate({ scrollTop: 0 }, 'fast');
  162. } else {
  163. $('#sqlqueryresultsouter').html(data.error);
  164. }
  165. PMA_highlightSQL($('#sqlqueryresultsouter'));
  166. }); // end $.post()
  167. });
  168. // Following section is related to the 'function based search' for geometry data types.
  169. // Initialy hide all the open_gis_editor spans
  170. $('span.open_search_gis_editor').hide();
  171. $('select.geom_func').bind('change', function () {
  172. var $geomFuncSelector = $(this);
  173. var binaryFunctions = [
  174. 'Contains',
  175. 'Crosses',
  176. 'Disjoint',
  177. 'Equals',
  178. 'Intersects',
  179. 'Overlaps',
  180. 'Touches',
  181. 'Within',
  182. 'MBRContains',
  183. 'MBRDisjoint',
  184. 'MBREquals',
  185. 'MBRIntersects',
  186. 'MBROverlaps',
  187. 'MBRTouches',
  188. 'MBRWithin',
  189. 'ST_Contains',
  190. 'ST_Crosses',
  191. 'ST_Disjoint',
  192. 'ST_Equals',
  193. 'ST_Intersects',
  194. 'ST_Overlaps',
  195. 'ST_Touches',
  196. 'ST_Within'
  197. ];
  198. var tempArray = [
  199. 'Envelope',
  200. 'EndPoint',
  201. 'StartPoint',
  202. 'ExteriorRing',
  203. 'Centroid',
  204. 'PointOnSurface'
  205. ];
  206. var outputGeomFunctions = binaryFunctions.concat(tempArray);
  207. // If the chosen function takes two geometry objects as parameters
  208. var $operator = $geomFuncSelector.parents('tr').find('td:nth-child(5)').find('select');
  209. if ($.inArray($geomFuncSelector.val(), binaryFunctions) >= 0) {
  210. $operator.prop('readonly', true);
  211. } else {
  212. $operator.prop('readonly', false);
  213. }
  214. // if the chosen function's output is a geometry, enable GIS editor
  215. var $editorSpan = $geomFuncSelector.parents('tr').find('span.open_search_gis_editor');
  216. if ($.inArray($geomFuncSelector.val(), outputGeomFunctions) >= 0) {
  217. $editorSpan.show();
  218. } else {
  219. $editorSpan.hide();
  220. }
  221. });
  222. $(document).on('click', 'span.open_search_gis_editor', function (event) {
  223. event.preventDefault();
  224. var $span = $(this);
  225. // Current value
  226. var value = $span.parent('td').children('input[type=\'text\']').val();
  227. // Field name
  228. var field = 'Parameter';
  229. // Column type
  230. var geom_func = $span.parents('tr').find('.geom_func').val();
  231. var type;
  232. if (geom_func === 'Envelope') {
  233. type = 'polygon';
  234. } else if (geom_func === 'ExteriorRing') {
  235. type = 'linestring';
  236. } else {
  237. type = 'point';
  238. }
  239. // Names of input field and null checkbox
  240. var input_name = $span.parent('td').children('input[type=\'text\']').attr('name');
  241. // Token
  242. openGISEditor();
  243. if (!gisEditorLoaded) {
  244. loadJSAndGISEditor(value, field, type, input_name);
  245. } else {
  246. loadGISEditor(value, field, type, input_name);
  247. }
  248. });
  249. /**
  250. * Ajax event handler for Range-Search.
  251. */
  252. $('body').on('change', 'select[name*="criteriaColumnOperators"]', function () { // Fix for bug #13778, changed 'click' to 'change'
  253. $source_select = $(this);
  254. // Get the column name.
  255. var column_name = $(this)
  256. .closest('tr')
  257. .find('th:first')
  258. .text();
  259. // Get the data-type of column excluding size.
  260. var data_type = $(this)
  261. .closest('tr')
  262. .find('td[data-type]')
  263. .attr('data-type');
  264. data_type = PMA_checkIfDataTypeNumericOrDate(data_type);
  265. // Get the operator.
  266. var operator = $(this).val();
  267. if ((operator === 'BETWEEN' || operator === 'NOT BETWEEN')
  268. && data_type
  269. ) {
  270. var $msgbox = PMA_ajaxShowMessage();
  271. $.ajax({
  272. url: 'tbl_select.php',
  273. type: 'POST',
  274. data: {
  275. server: PMA_commonParams.get('server'),
  276. ajax_request: 1,
  277. db: $('input[name="db"]').val(),
  278. table: $('input[name="table"]').val(),
  279. column: column_name,
  280. range_search: 1
  281. },
  282. success: function (response) {
  283. PMA_ajaxRemoveMessage($msgbox);
  284. if (response.success) {
  285. // Get the column min value.
  286. var min = response.column_data.min
  287. ? '(' + PMA_messages.strColumnMin +
  288. ' ' + response.column_data.min + ')'
  289. : '';
  290. // Get the column max value.
  291. var max = response.column_data.max
  292. ? '(' + PMA_messages.strColumnMax +
  293. ' ' + response.column_data.max + ')'
  294. : '';
  295. var button_options = {};
  296. button_options[PMA_messages.strGo] = function () {
  297. var min_value = $('#min_value').val();
  298. var max_value = $('#max_value').val();
  299. var final_value = '';
  300. if (min_value.length && max_value.length) {
  301. final_value = min_value + ', ' +
  302. max_value;
  303. }
  304. var $target_field = $source_select.closest('tr')
  305. .find('[name*="criteriaValues"]');
  306. // If target field is a select list.
  307. if ($target_field.is('select')) {
  308. $target_field.val(final_value);
  309. var $options = $target_field.find('option');
  310. var $closest_min = null;
  311. var $closest_max = null;
  312. // Find closest min and max value.
  313. $options.each(function () {
  314. if (
  315. $closest_min === null
  316. || Math.abs($(this).val() - min_value) < Math.abs($closest_min.val() - min_value)
  317. ) {
  318. $closest_min = $(this);
  319. }
  320. if (
  321. $closest_max === null
  322. || Math.abs($(this).val() - max_value) < Math.abs($closest_max.val() - max_value)
  323. ) {
  324. $closest_max = $(this);
  325. }
  326. });
  327. $closest_min.attr('selected', 'selected');
  328. $closest_max.attr('selected', 'selected');
  329. } else {
  330. $target_field.val(final_value);
  331. }
  332. $(this).dialog('close');
  333. };
  334. button_options[PMA_messages.strCancel] = function () {
  335. $(this).dialog('close');
  336. };
  337. // Display dialog box.
  338. $('<div/>').append(
  339. '<fieldset>' +
  340. '<legend>' + operator + '</legend>' +
  341. '<label for="min_value">' + PMA_messages.strMinValue +
  342. '</label>' +
  343. '<input type="text" id="min_value" />' + '<br>' +
  344. '<span class="small_font">' + min + '</span>' + '<br>' +
  345. '<label for="max_value">' + PMA_messages.strMaxValue +
  346. '</label>' +
  347. '<input type="text" id="max_value" />' + '<br>' +
  348. '<span class="small_font">' + max + '</span>' +
  349. '</fieldset>'
  350. ).dialog({
  351. minWidth: 500,
  352. maxHeight: 400,
  353. modal: true,
  354. buttons: button_options,
  355. title: PMA_messages.strRangeSearch,
  356. open: function () {
  357. // Add datepicker wherever required.
  358. PMA_addDatepicker($('#min_value'), data_type);
  359. PMA_addDatepicker($('#max_value'), data_type);
  360. },
  361. close: function () {
  362. $(this).remove();
  363. }
  364. });
  365. } else {
  366. PMA_ajaxShowMessage(response.error);
  367. }
  368. },
  369. error: function (response) {
  370. PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest);
  371. }
  372. });
  373. }
  374. });
  375. var windowwidth = $(window).width();
  376. $('.jsresponsive').css('max-width', (windowwidth - 69) + 'px');
  377. });