Fixed the NULL not applied after clearing nullable field
[phpmyadmin.git] / js / src / gis_data_editor.js
blobfad50b43063a5e1c6bf7d3af712c7ed54ac589f2
1 /**
2  * @fileoverview    functions used in GIS data editor
3  *
4  * @requires    jQuery
5  *
6  */
8 /* global addZoomPanControllers, storeGisSvgRef, selectVisualization, styleOSM, zoomAndPan */ // js/table/gis_visualization.js
9 /* global themeImagePath */ // templates/javascript/variables.twig
11 // eslint-disable-next-line no-unused-vars
12 var gisEditorLoaded = false;
14 /**
15  * Closes the GIS data editor and perform necessary clean up work.
16  */
17 function closeGISEditor () {
18     $('#popup_background').fadeOut('fast');
19     $('#gis_editor').fadeOut('fast', function () {
20         $(this).empty();
21     });
24 /**
25  * Prepares the HTML received via AJAX.
26  */
27 function prepareJSVersion () {
28     // Change the text on the submit button
29     $('#gis_editor').find('input[name=\'gis_data[save]\']')
30         .val(Messages.strCopy)
31         .insertAfter($('#gis_data_textarea'))
32         .before('<br><br>');
34     // Add close and cancel links
35     $('#gis_data_editor').prepend('<a class="close_gis_editor" href="#">' + Messages.strClose + '</a>');
36     $('<a class="cancel_gis_editor" href="#"> ' + Messages.strCancel + '</a>')
37         .insertAfter($('input[name=\'gis_data[save]\']'));
39     // Remove the unnecessary text
40     $('div#gis_data_output p').remove();
42     // Remove 'add' buttons and add links
43     $('#gis_editor').find('input.add').each(function () {
44         var $button = $(this);
45         $button.addClass('addJs').removeClass('add');
46         var classes = $button.attr('class');
47         $button.replaceWith(
48             '<a class="' + classes +
49             '" name="' + $button.attr('name') +
50             '" href="#">+ ' + $button.val() + '</a>'
51         );
52     });
55 /**
56  * Returns the HTML for a data point.
57  *
58  * @param {number} pointNumber point number
59  * @param {string} prefix      prefix of the name
60  * @return {string} the HTML for a data point
61  */
62 function addDataPoint (pointNumber, prefix) {
63     return '<br>' +
64         Functions.sprintf(Messages.strPointN, (pointNumber + 1)) + ': ' +
65         '<label for="x">' + Messages.strX + '</label>' +
66         '<input type="text" name="' + prefix + '[' + pointNumber + '][x]" value="">' +
67         '<label for="y">' + Messages.strY + '</label>' +
68         '<input type="text" name="' + prefix + '[' + pointNumber + '][y]" value="">';
71 /**
72  * Initialize the visualization in the GIS data editor.
73  */
74 function initGISEditorVisualization () {
75     storeGisSvgRef();
76     // Loads either SVG or OSM visualization based on the choice
77     selectVisualization();
78     // Adds necessary styles to the div that contains the openStreetMap
79     styleOSM();
80     // Adds controllers for zooming and panning
81     addZoomPanControllers();
82     zoomAndPan();
85 /**
86  * Loads JavaScript files and the GIS editor.
87  *
88  * @param value      current value of the geometry field
89  * @param field      field name
90  * @param type       geometry type
91  * @param inputName name of the input field
92  * @param token      token
93  */
94 // eslint-disable-next-line no-unused-vars
95 function loadJSAndGISEditor (value, field, type, inputName) {
96     var head = document.getElementsByTagName('head')[0];
97     var script;
99     script = document.createElement('script');
100     script.type = 'text/javascript';
101     script.src = 'js/dist/table/gis_visualization.js';
102     head.appendChild(script);
104     // OpenLayers.js is BIG and takes time. So asynchronous loading would not work.
105     // Load the JS and do a callback to load the content for the GIS Editor.
106     script = document.createElement('script');
107     script.type = 'text/javascript';
109     script.onreadystatechange = function () {
110         if (this.readyState === 'complete') {
111             loadGISEditor(value, field, type, inputName);
112         }
113     };
114     script.onload = function () {
115         loadGISEditor(value, field, type, inputName);
116     };
117     script.onerror = function () {
118         loadGISEditor(value, field, type, inputName);
119     };
121     script.src = 'js/vendor/openlayers/OpenLayers.js';
122     head.appendChild(script);
124     gisEditorLoaded = true;
128  * Loads the GIS editor via AJAX
130  * @param value      current value of the geometry field
131  * @param field      field name
132  * @param type       geometry type
133  * @param inputName name of the input field
134  */
135 function loadGISEditor (value, field, type, inputName) {
136     var $gisEditor = $('#gis_editor');
137     $.post('index.php?route=/gis-data-editor', {
138         'field' : field,
139         'value' : value,
140         'type' : type,
141         'input_name' : inputName,
142         'get_gis_editor' : true,
143         'ajax_request': true,
144         'server': CommonParams.get('server')
145     }, function (data) {
146         if (typeof data !== 'undefined' && data.success === true) {
147             $gisEditor.html(data.gis_editor);
148             initGISEditorVisualization();
149             prepareJSVersion();
150         } else {
151             Functions.ajaxShowMessage(data.error, false);
152         }
153     }, 'json');
157  * Opens up the dialog for the GIS data editor.
158  */
159 // eslint-disable-next-line no-unused-vars
160 function openGISEditor () {
161     // Center the popup
162     var windowWidth = document.documentElement.clientWidth;
163     var windowHeight = document.documentElement.clientHeight;
164     var popupWidth = windowWidth * 0.9;
165     var popupHeight = windowHeight * 0.9;
166     var popupOffsetTop = windowHeight / 2 - popupHeight / 2;
167     var popupOffsetLeft = windowWidth / 2 - popupWidth / 2;
169     var $gisEditor = $('#gis_editor');
170     var $background = $('#popup_background');
172     $gisEditor.css({ 'top': popupOffsetTop, 'left': popupOffsetLeft, 'width': popupWidth, 'height': popupHeight });
173     $background.css({ 'opacity' : '0.7' });
175     $gisEditor.append(
176         '<div id="gis_data_editor">' +
177         '<img class="ajaxIcon" id="loadingMonitorIcon" src="' +
178         themeImagePath + 'ajax_clock_small.gif" alt="">' +
179         '</div>'
180     );
182     // Make it appear
183     $background.fadeIn('fast');
184     $gisEditor.fadeIn('fast');
188  * Prepare and insert the GIS data in Well Known Text format
189  * to the input field.
190  */
191 function insertDataAndClose () {
192     var $form = $('form#gis_data_editor_form');
193     var inputName = $form.find('input[name=\'input_name\']').val();
195     var argsep = CommonParams.get('arg_separator');
196     $.post('index.php?route=/gis-data-editor', $form.serialize() + argsep + 'generate=true' + argsep + 'ajax_request=true', function (data) {
197         if (typeof data !== 'undefined' && data.success === true) {
198             $('input[name=\'' + inputName + '\']').val(data.result);
199         } else {
200             Functions.ajaxShowMessage(data.error, false);
201         }
202     }, 'json');
203     closeGISEditor();
207  * Unbind all event handlers before tearing down a page
208  */
209 AJAX.registerTeardown('gis_data_editor.js', function () {
210     $(document).off('click', '#gis_editor input[name=\'gis_data[save]\']');
211     $(document).off('submit', '#gis_editor');
212     $(document).off('change', '#gis_editor input[type=\'text\']');
213     $(document).off('change', '#gis_editor select.gis_type');
214     $(document).off('click', '#gis_editor a.close_gis_editor, #gis_editor a.cancel_gis_editor');
215     $(document).off('click', '#gis_editor a.addJs.addPoint');
216     $(document).off('click', '#gis_editor a.addLine.addJs');
217     $(document).off('click', '#gis_editor a.addJs.addPolygon');
218     $(document).off('click', '#gis_editor a.addJs.addGeom');
221 AJAX.registerOnload('gis_data_editor.js', function () {
222     /**
223      * Prepares and insert the GIS data to the input field on clicking 'copy'.
224      */
225     $(document).on('click', '#gis_editor input[name=\'gis_data[save]\']', function (event) {
226         event.preventDefault();
227         insertDataAndClose();
228     });
230     /**
231      * Prepares and insert the GIS data to the input field on pressing 'enter'.
232      */
233     $(document).on('submit', '#gis_editor', function (event) {
234         event.preventDefault();
235         insertDataAndClose();
236     });
238     /**
239      * Trigger asynchronous calls on data change and update the output.
240      */
241     $(document).on('change', '#gis_editor input[type=\'text\']', function () {
242         var $form = $('form#gis_data_editor_form');
243         var argsep = CommonParams.get('arg_separator');
244         $.post('index.php?route=/gis-data-editor', $form.serialize() + argsep + 'generate=true' + argsep + 'ajax_request=true', function (data) {
245             if (typeof data !== 'undefined' && data.success === true) {
246                 $('#gis_data_textarea').val(data.result);
247                 $('#placeholder').empty().removeClass('hasSVG').html(data.visualization);
248                 $('#openlayersmap').empty();
249                 /* TODO: the gis_data_editor should rather return JSON than JS code to eval */
250                 // eslint-disable-next-line no-eval
251                 eval(data.openLayers);
252                 initGISEditorVisualization();
253             } else {
254                 Functions.ajaxShowMessage(data.error, false);
255             }
256         }, 'json');
257     });
259     /**
260      * Update the form on change of the GIS type.
261      */
262     $(document).on('change', '#gis_editor select.gis_type', function () {
263         var $gisEditor = $('#gis_editor');
264         var $form = $('form#gis_data_editor_form');
266         var argsep = CommonParams.get('arg_separator');
267         $.post('index.php?route=/gis-data-editor', $form.serialize() + argsep + 'get_gis_editor=true' + argsep + 'ajax_request=true', function (data) {
268             if (typeof data !== 'undefined' && data.success === true) {
269                 $gisEditor.html(data.gis_editor);
270                 initGISEditorVisualization();
271                 prepareJSVersion();
272             } else {
273                 Functions.ajaxShowMessage(data.error, false);
274             }
275         }, 'json');
276     });
278     /**
279      * Handles closing of the GIS data editor.
280      */
281     $(document).on('click', '#gis_editor a.close_gis_editor, #gis_editor a.cancel_gis_editor', function () {
282         closeGISEditor();
283     });
285     /**
286      * Handles adding data points
287      */
288     $(document).on('click', '#gis_editor a.addJs.addPoint', function () {
289         var $a = $(this);
290         var name = $a.attr('name');
291         // Eg. name = gis_data[0][MULTIPOINT][add_point] => prefix = gis_data[0][MULTIPOINT]
292         var prefix = name.substr(0, name.length - 11);
293         // Find the number of points
294         var $noOfPointsInput = $('input[name=\'' + prefix + '[no_of_points]' + '\']');
295         var noOfPoints = parseInt($noOfPointsInput.val(), 10);
296         // Add the new data point
297         var html = addDataPoint(noOfPoints, prefix);
298         $a.before(html);
299         $noOfPointsInput.val(noOfPoints + 1);
300     });
302     /**
303      * Handles adding linestrings and inner rings
304      */
305     $(document).on('click', '#gis_editor a.addLine.addJs', function () {
306         var $a = $(this);
307         var name = $a.attr('name');
309         // Eg. name = gis_data[0][MULTILINESTRING][add_line] => prefix = gis_data[0][MULTILINESTRING]
310         var prefix = name.substr(0, name.length - 10);
311         var type = prefix.slice(prefix.lastIndexOf('[') + 1, prefix.lastIndexOf(']'));
313         // Find the number of lines
314         var $noOfLinesInput = $('input[name=\'' + prefix + '[no_of_lines]' + '\']');
315         var noOfLines = parseInt($noOfLinesInput.val(), 10);
317         // Add the new linesting of inner ring based on the type
318         var html = '<br>';
319         var noOfPoints;
320         if (type === 'MULTILINESTRING') {
321             html += Messages.strLineString + ' ' + (noOfLines + 1) + ':';
322             noOfPoints = 2;
323         } else {
324             html += Messages.strInnerRing + ' ' + noOfLines + ':';
325             noOfPoints = 4;
326         }
327         html += '<input type="hidden" name="' + prefix + '[' + noOfLines + '][no_of_points]" value="' + noOfPoints + '">';
328         for (var i = 0; i < noOfPoints; i++) {
329             html += addDataPoint(i, (prefix + '[' + noOfLines + ']'));
330         }
331         html += '<a class="addPoint addJs" name="' + prefix + '[' + noOfLines + '][add_point]" href="#">+ ' +
332             Messages.strAddPoint + '</a><br>';
334         $a.before(html);
335         $noOfLinesInput.val(noOfLines + 1);
336     });
338     /**
339      * Handles adding polygons
340      */
341     $(document).on('click', '#gis_editor a.addJs.addPolygon', function () {
342         var $a = $(this);
343         var name = $a.attr('name');
344         // Eg. name = gis_data[0][MULTIPOLYGON][add_polygon] => prefix = gis_data[0][MULTIPOLYGON]
345         var prefix = name.substr(0, name.length - 13);
346         // Find the number of polygons
347         var $noOfPolygonsInput = $('input[name=\'' + prefix + '[no_of_polygons]' + '\']');
348         var noOfPolygons = parseInt($noOfPolygonsInput.val(), 10);
350         // Add the new polygon
351         var html = Messages.strPolygon + ' ' + (noOfPolygons + 1) + ':<br>';
352         html += '<input type="hidden" name="' + prefix + '[' + noOfPolygons + '][no_of_lines]" value="1">' +
353             '<br>' + Messages.strOuterRing + ':' +
354             '<input type="hidden" name="' + prefix + '[' + noOfPolygons + '][0][no_of_points]" value="4">';
355         for (var i = 0; i < 4; i++) {
356             html += addDataPoint(i, (prefix + '[' + noOfPolygons + '][0]'));
357         }
358         html += '<a class="addPoint addJs" name="' + prefix + '[' + noOfPolygons + '][0][add_point]" href="#">+ ' +
359             Messages.strAddPoint + '</a><br>' +
360             '<a class="addLine addJs" name="' + prefix + '[' + noOfPolygons + '][add_line]" href="#">+ ' +
361             Messages.strAddInnerRing + '</a><br><br>';
363         $a.before(html);
364         $noOfPolygonsInput.val(noOfPolygons + 1);
365     });
367     /**
368      * Handles adding geoms
369      */
370     $(document).on('click', '#gis_editor a.addJs.addGeom', function () {
371         var $a = $(this);
372         var prefix = 'gis_data[GEOMETRYCOLLECTION]';
373         // Find the number of geoms
374         var $noOfGeomsInput = $('input[name=\'' + prefix + '[geom_count]' + '\']');
375         var noOfGeoms = parseInt($noOfGeomsInput.val(), 10);
377         var html1 = Messages.strGeometry + ' ' + (noOfGeoms + 1) + ':<br>';
378         var $geomType = $('select[name=\'gis_data[' + (noOfGeoms - 1) + '][gis_type]\']').clone();
379         $geomType.attr('name', 'gis_data[' + noOfGeoms + '][gis_type]').val('POINT');
380         var html2 = '<br>' + Messages.strPoint + ' :' +
381             '<label for="x"> ' + Messages.strX + ' </label>' +
382             '<input type="text" name="gis_data[' + noOfGeoms + '][POINT][x]" value="">' +
383             '<label for="y"> ' + Messages.strY + ' </label>' +
384             '<input type="text" name="gis_data[' + noOfGeoms + '][POINT][y]" value="">' +
385             '<br><br>';
387         $a.before(html1);
388         $geomType.insertBefore($a);
389         $a.before(html2);
390         $noOfGeomsInput.val(noOfGeoms + 1);
391     });