tbl_select.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. * Unbind all event handlers before tearing down a page
  16. */
  17. AJAX.registerTeardown('tbl_select.js', function() {
  18. $('#togglesearchformlink').unbind('click');
  19. $("#tbl_search_form.ajax").die('submit');
  20. $('select.geom_func').unbind('change');
  21. $('span.open_search_gis_editor').die('click');
  22. });
  23. AJAX.registerOnload('tbl_select.js', function() {
  24. /**
  25. * Prepare a div containing a link, otherwise it's incorrectly displayed
  26. * after a couple of clicks
  27. */
  28. $('<div id="togglesearchformdiv"><a id="togglesearchformlink"></a></div>')
  29. .insertAfter('#tbl_search_form')
  30. // don't show it until we have results on-screen
  31. .hide();
  32. $('#togglesearchformlink')
  33. .html(PMA_messages['strShowSearchCriteria'])
  34. .bind('click', function() {
  35. var $link = $(this);
  36. $('#tbl_search_form').slideToggle();
  37. if ($link.text() == PMA_messages['strHideSearchCriteria']) {
  38. $link.text(PMA_messages['strShowSearchCriteria']);
  39. } else {
  40. $link.text(PMA_messages['strHideSearchCriteria']);
  41. }
  42. // avoid default click action
  43. return false;
  44. });
  45. /**
  46. * Ajax event handler for Table Search
  47. */
  48. $("#tbl_search_form.ajax").live('submit', function(event) {
  49. var unaryFunctions = [
  50. 'IS NULL',
  51. 'IS NOT NULL',
  52. "= ''",
  53. "!= ''"];
  54. // jQuery object to reuse
  55. $search_form = $(this);
  56. event.preventDefault();
  57. // empty previous search results while we are waiting for new results
  58. $("#sqlqueryresults").empty();
  59. var $msgbox = PMA_ajaxShowMessage(PMA_messages['strSearching'], false);
  60. PMA_prepareForAjaxRequest($search_form);
  61. var values = {};
  62. $search_form.find(':input').each(function() {
  63. var $input = $(this);
  64. if ($input.attr('type') == 'checkbox' || $input.attr('type') == 'radio') {
  65. if ($input.is(':checked')) {
  66. values[this.name] = $input.val();
  67. }
  68. } else {
  69. values[this.name] = $input.val();
  70. }
  71. });
  72. var columnCount = $('select[name="columnsToDisplay[]"] option').length;
  73. // Submit values only for the columns that have unary column operator or a search criteria
  74. for (var a = 0; a < columnCount; a++) {
  75. if ($.inArray(values['criteriaColumnOperators[' + a + ']'], unaryFunctions) >= 0) {
  76. continue;
  77. }
  78. if (values['criteriaValues[' + a + ']'] == '' || values['criteriaValues[' + a + ']'] == null) {
  79. delete values['criteriaValues[' + a + ']'];
  80. delete values['criteriaColumnOperators[' + a + ']'];
  81. delete values['criteriaColumnNames[' + a + ']'];
  82. delete values['criteriaColumnTypes[' + a + ']'];
  83. delete values['criteriaColumnCollations[' + a + ']'];
  84. }
  85. }
  86. // If all columns are selected, use a single parameter to indicate that
  87. if (values['columnsToDisplay[]'] != null) {
  88. if (values['columnsToDisplay[]'].length == columnCount) {
  89. delete values['columnsToDisplay[]'];
  90. values['displayAllColumns'] = true;
  91. }
  92. } else {
  93. values['displayAllColumns'] = true;
  94. }
  95. $.post($search_form.attr('action'), values, function(data) {
  96. PMA_ajaxRemoveMessage($msgbox);
  97. if (data.success == true) {
  98. if (data.sql_query != null) { // zero rows
  99. $("#sqlqueryresults").html(data.sql_query);
  100. } else { // results found
  101. $("#sqlqueryresults").html(data.message);
  102. $("#sqlqueryresults").trigger('makegrid');
  103. }
  104. $('#tbl_search_form')
  105. // workaround for bug #3168569 - Issue on toggling the "Hide search criteria" in chrome.
  106. .slideToggle()
  107. .hide();
  108. $('#togglesearchformlink')
  109. // always start with the Show message
  110. .text(PMA_messages['strShowSearchCriteria']);
  111. $('#togglesearchformdiv')
  112. // now it's time to show the div containing the link
  113. .show();
  114. // needed for the display options slider in the results
  115. PMA_init_slider();
  116. } else {
  117. $("#sqlqueryresults").html(data.error);
  118. }
  119. }); // end $.post()
  120. });
  121. // Following section is related to the 'function based search' for geometry data types.
  122. // Initialy hide all the open_gis_editor spans
  123. $('span.open_search_gis_editor').hide();
  124. $('select.geom_func').bind('change', function() {
  125. var $geomFuncSelector = $(this);
  126. var binaryFunctions = [
  127. 'Contains',
  128. 'Crosses',
  129. 'Disjoint',
  130. 'Equals',
  131. 'Intersects',
  132. 'Overlaps',
  133. 'Touches',
  134. 'Within',
  135. 'MBRContains',
  136. 'MBRDisjoint',
  137. 'MBREquals',
  138. 'MBRIntersects',
  139. 'MBROverlaps',
  140. 'MBRTouches',
  141. 'MBRWithin',
  142. 'ST_Contains',
  143. 'ST_Crosses',
  144. 'ST_Disjoint',
  145. 'ST_Equals',
  146. 'ST_Intersects',
  147. 'ST_Overlaps',
  148. 'ST_Touches',
  149. 'ST_Within'
  150. ];
  151. var tempArray = [
  152. 'Envelope',
  153. 'EndPoint',
  154. 'StartPoint',
  155. 'ExteriorRing',
  156. 'Centroid',
  157. 'PointOnSurface'
  158. ];
  159. var outputGeomFunctions = binaryFunctions.concat(tempArray);
  160. // If the chosen function takes two geometry objects as parameters
  161. var $operator = $geomFuncSelector.parents('tr').find('td:nth-child(5)').find('select');
  162. if ($.inArray($geomFuncSelector.val(), binaryFunctions) >= 0){
  163. $operator.prop('readonly', true);
  164. } else {
  165. $operator.prop('readonly', false);
  166. }
  167. // if the chosen function's output is a geometry, enable GIS editor
  168. var $editorSpan = $geomFuncSelector.parents('tr').find('span.open_search_gis_editor');
  169. if ($.inArray($geomFuncSelector.val(), outputGeomFunctions) >= 0){
  170. $editorSpan.show();
  171. } else {
  172. $editorSpan.hide();
  173. }
  174. });
  175. $('span.open_search_gis_editor').live('click', function(event) {
  176. event.preventDefault();
  177. var $span = $(this);
  178. // Current value
  179. var value = $span.parent('td').children("input[type='text']").val();
  180. // Field name
  181. var field = 'Parameter';
  182. // Column type
  183. var geom_func = $span.parents('tr').find('.geom_func').val();
  184. if (geom_func == 'Envelope') {
  185. var type = 'polygon';
  186. } else if (geom_func == 'ExteriorRing') {
  187. var type = 'linestring';
  188. } else {
  189. var type = 'point';
  190. }
  191. // Names of input field and null checkbox
  192. var input_name = $span.parent('td').children("input[type='text']").attr('name');
  193. //Token
  194. var token = $("input[name='token']").val();
  195. openGISEditor();
  196. if (!gisEditorLoaded) {
  197. loadJSAndGISEditor(value, field, type, input_name, token);
  198. } else {
  199. loadGISEditor(value, field, type, input_name, token);
  200. }
  201. });
  202. });