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