2 * @fileoverview functions used in GIS data editor
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;
15 * Closes the GIS data editor and perform necessary clean up work.
17 function closeGISEditor () {
18 $('#popup_background').fadeOut('fast');
19 $('#gis_editor').fadeOut('fast', function () {
25 * Prepares the HTML received via AJAX.
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'))
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');
48 '<a class="' + classes +
49 '" name="' + $button.attr('name') +
50 '" href="#">+ ' + $button.val() + '</a>'
56 * Returns the HTML for a data point.
58 * @param {number} pointNumber point number
59 * @param {string} prefix prefix of the name
60 * @return {string} the HTML for a data point
62 function addDataPoint (pointNumber, prefix) {
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="">';
72 * Initialize the visualization in the GIS data editor.
74 function initGISEditorVisualization () {
76 // Loads either SVG or OSM visualization based on the choice
77 selectVisualization();
78 // Adds necessary styles to the div that contains the openStreetMap
80 // Adds controllers for zooming and panning
81 addZoomPanControllers();
86 * Loads JavaScript files and the GIS editor.
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
94 // eslint-disable-next-line no-unused-vars
95 function loadJSAndGISEditor (value, field, type, inputName) {
96 var head = document.getElementsByTagName('head')[0];
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);
114 script.onload = function () {
115 loadGISEditor(value, field, type, inputName);
117 script.onerror = function () {
118 loadGISEditor(value, field, type, inputName);
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
135 function loadGISEditor (value, field, type, inputName) {
136 var $gisEditor = $('#gis_editor');
137 $.post('index.php?route=/gis-data-editor', {
141 'input_name' : inputName,
142 'get_gis_editor' : true,
143 'ajax_request': true,
144 'server': CommonParams.get('server')
146 if (typeof data !== 'undefined' && data.success === true) {
147 $gisEditor.html(data.gis_editor);
148 initGISEditorVisualization();
151 Functions.ajaxShowMessage(data.error, false);
157 * Opens up the dialog for the GIS data editor.
159 // eslint-disable-next-line no-unused-vars
160 function openGISEditor () {
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' });
176 '<div id="gis_data_editor">' +
177 '<img class="ajaxIcon" id="loadingMonitorIcon" src="' +
178 themeImagePath + 'ajax_clock_small.gif" alt="">' +
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.
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);
200 Functions.ajaxShowMessage(data.error, false);
207 * Unbind all event handlers before tearing down a page
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 () {
223 * Prepares and insert the GIS data to the input field on clicking 'copy'.
225 $(document).on('click', '#gis_editor input[name=\'gis_data[save]\']', function (event) {
226 event.preventDefault();
227 insertDataAndClose();
231 * Prepares and insert the GIS data to the input field on pressing 'enter'.
233 $(document).on('submit', '#gis_editor', function (event) {
234 event.preventDefault();
235 insertDataAndClose();
239 * Trigger asynchronous calls on data change and update the output.
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();
254 Functions.ajaxShowMessage(data.error, false);
260 * Update the form on change of the GIS type.
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();
273 Functions.ajaxShowMessage(data.error, false);
279 * Handles closing of the GIS data editor.
281 $(document).on('click', '#gis_editor a.close_gis_editor, #gis_editor a.cancel_gis_editor', function () {
286 * Handles adding data points
288 $(document).on('click', '#gis_editor a.addJs.addPoint', function () {
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);
299 $noOfPointsInput.val(noOfPoints + 1);
303 * Handles adding linestrings and inner rings
305 $(document).on('click', '#gis_editor a.addLine.addJs', function () {
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
320 if (type === 'MULTILINESTRING') {
321 html += Messages.strLineString + ' ' + (noOfLines + 1) + ':';
324 html += Messages.strInnerRing + ' ' + noOfLines + ':';
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 + ']'));
331 html += '<a class="addPoint addJs" name="' + prefix + '[' + noOfLines + '][add_point]" href="#">+ ' +
332 Messages.strAddPoint + '</a><br>';
335 $noOfLinesInput.val(noOfLines + 1);
339 * Handles adding polygons
341 $(document).on('click', '#gis_editor a.addJs.addPolygon', function () {
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]'));
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>';
364 $noOfPolygonsInput.val(noOfPolygons + 1);
368 * Handles adding geoms
370 $(document).on('click', '#gis_editor a.addJs.addGeom', function () {
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="">' +
388 $geomType.insertBefore($a);
390 $noOfGeomsInput.val(noOfGeoms + 1);