2 * @fileoverview functions used in GIS data editor
9 * Closes the GIS data editor and perform necessary clean up work.
11 function closeGISEditor(){
12 $("#popup_background").fadeOut("fast");
13 $("#gis_editor").fadeOut("fast");
14 $("#gis_editor").html('');
18 * Prepares the HTML recieved via AJAX.
20 function prepareJSVersion() {
21 // Hide 'Go' buttons associated with the dropdowns
24 // Change the text on the submit button
25 $("input[name='gis_data[save]']")
26 .attr('value', PMA_messages['strCopy'])
27 .insertAfter($('#gis_data_textarea'))
30 // Add close and cancel links
31 $('#gis_data_editor').prepend('<a class="close_gis_editor">' + PMA_messages['strClose'] + '</a>');
32 $('<a class="cancel_gis_editor"> ' + PMA_messages['strCancel'] + '</a>')
33 .insertAfter($("input[name='gis_data[save]']"));
35 // Remove the unnecessary text
36 $('div#gis_data_output p').remove();
38 // Remove 'add' buttons and add links
39 $('.add').each(function(e) {
40 var $button = $(this);
41 $button.addClass('addJs').removeClass('add');
42 var classes = $button.attr('class');
44 .after('<a class="' + classes + '" name="' + $button.attr('name')
45 + '">+ ' + $button.attr('value') + '</a>')
51 * Returns the HTML for a data point.
53 * @param pointNumber point number
54 * @param prefix prefix of the name
55 * @returns the HTML for a data point
57 function addDataPoint(pointNumber, prefix) {
58 return '<br>' + PMA_messages['strPoint'] + (pointNumber + 1) + ':'
59 + '<label for="x"> ' + PMA_messages['strX'] + ' </label>'
60 + '<input type="text" name="' + prefix + '[' + pointNumber + '][x]" value="">'
61 + '<label for="y"> ' + PMA_messages['strY'] + ' </label>'
62 + '<input type="text" name="' + prefix + '[' + pointNumber + '][y]" value="">';
66 * Initialize the visualization in the GIS data editor.
68 function initGISEditorVisualization() {
69 // Loads either SVG or OSM visualization based on the choice
70 selectVisualization();
71 // Adds necessary styles to the div that coontains the openStreetMap
73 // Loads the SVG element and make a reference to it
75 // Adds controllers for zooming and panning
76 addZoomPanControllers();
81 * Opens up the GIS data editor.
83 * @param value current value of the geometry field
84 * @param field field name
85 * @param type geometry type
86 * @param input_name name of the input field
89 function openGISEditor(value, field, type, input_name, token) {
91 var windowWidth = document.documentElement.clientWidth;
92 var windowHeight = document.documentElement.clientHeight;
93 var popupWidth = windowWidth * 0.9;
94 var popupHeight = windowHeight * 0.9;
95 var popupOffsetTop = windowHeight / 2 - popupHeight / 2;
96 var popupOffsetLeft = windowWidth / 2 - popupWidth / 2;
97 var $gis_editor = $("#gis_editor");
98 $gis_editor.css({"top": popupOffsetTop, "left": popupOffsetLeft, "width": popupWidth, "height": popupHeight});
100 $.post('gis_data_editor.php', {
104 'input_name' : input_name,
105 'get_gis_editor' : true,
108 if(data.success == true) {
109 $gis_editor.html(data.gis_editor);
110 initGISEditorVisualization();
113 PMA_ajaxShowMessage(data.error);
118 $("#popup_background").css({"opacity":"0.7"});
119 $("#popup_background").fadeIn("fast");
120 $gis_editor.fadeIn("fast");
124 * Prepare and insert the GIS data in Well Known Text format
125 * to the input field.
127 function insertDataAndClose() {
128 var $form = $('form#gis_data_editor_form');
129 var input_name = $form.find("input[name='input_name']").val();
131 $.post('gis_data_editor.php', $form.serialize() + "&generate=true", function(data) {
132 if(data.success == true) {
133 $("input[name='" + input_name + "']").val(data.result);
135 PMA_ajaxShowMessage(data.error);
141 $(document).ready(function() {
143 // Remove the class that is added due to the URL being too long.
144 $('.open_gis_editor a').removeClass('formLinkSubmit');
147 * Prepares and insert the GIS data to the input field on clicking 'copy'.
149 $("input[name='gis_data[save]']").live('click', function(event) {
150 event.preventDefault();
151 insertDataAndClose();
155 * Prepares and insert the GIS data to the input field on pressing 'enter'.
157 $('#gis_editor').live('submit', function(event) {
158 event.preventDefault();
159 insertDataAndClose();
163 * Trigger asynchronous calls on data change and update the output.
165 $('#gis_editor').find("input[type='text']").live('change', function() {
166 var $form = $('form#gis_data_editor_form');
167 $.post('gis_data_editor.php', $form.serialize() + "&generate=true", function(data) {
168 if(data.success == true) {
169 $('#gis_data_textarea').val(data.result);
170 $('#placeholder').empty().removeClass('hasSVG').html(data.visualization);
171 $('#openlayersmap').empty();
172 eval(data.openLayers);
173 initGISEditorVisualization();
175 PMA_ajaxShowMessage(data.error);
181 * Update the form on change of the GIS type.
183 $(".gis_type").live('change', function(event) {
184 var $gis_editor = $("#gis_editor");
185 var $form = $('form#gis_data_editor_form');
187 $.post('gis_data_editor.php', $form.serialize() + "&get_gis_editor=true", function(data) {
188 if(data.success == true) {
189 $gis_editor.html(data.gis_editor);
190 initGISEditorVisualization();
193 PMA_ajaxShowMessage(data.error);
199 * Handles closing of the GIS data editor.
201 $('.close_gis_editor, .cancel_gis_editor').live('click', function() {
206 * Handles adding data points
208 $('.addJs.addPoint').live('click', function() {
210 var name = $a.attr('name');
211 // Eg. name = gis_data[0][MULTIPOINT][add_point] => prefix = gis_data[0][MULTIPOINT]
212 var prefix = name.substr(0, name.length - 11);
213 // Find the number of points
214 var $noOfPointsInput = $("input[name='" + prefix + "[no_of_points]" + "']");
215 var noOfPoints = parseInt($noOfPointsInput.attr('value'));
216 // Add the new data point
217 var html = addDataPoint(noOfPoints, prefix);
219 $noOfPointsInput.attr('value', noOfPoints + 1);
223 * Handles adding linestrings and inner rings
225 $('.addLine.addJs').live('click', function() {
227 var name = $a.attr('name');
229 // Eg. name = gis_data[0][MULTILINESTRING][add_line] => prefix = gis_data[0][MULTILINESTRING]
230 var prefix = name.substr(0, name.length - 10);
231 var type = prefix.slice(prefix.lastIndexOf('[') + 1, prefix.lastIndexOf(']'));
233 // Find the number of lines
234 var $noOfLinesInput = $("input[name='" + prefix + "[no_of_lines]" + "']");
235 var noOfLines = parseInt($noOfLinesInput.attr('value'));
237 // Add the new linesting of inner ring based on the type
239 if (type == 'MULTILINESTRING') {
240 html += PMA_messages['strLineString'] + (noOfLines + 1) + ':';
243 html += PMA_messages['strInnerRing'] + noOfLines + ':';
246 html += '<input type="hidden" name="' + prefix + '[' + noOfLines + '][no_of_points]" value="' + noOfPoints + '">';
247 for (i = 0; i < noOfPoints; i++) {
248 html += addDataPoint(i, (prefix + '[' + noOfLines + ']'));
250 html += '<a class="addPoint addJs" name="' + prefix + '[' + noOfLines + '][add_point]">+ '
251 + PMA_messages['strAddPoint'] + '</a><br>';
254 $noOfLinesInput.attr('value', noOfLines + 1);
258 * Handles adding polygons
260 $('.addJs.addPolygon').live('click', function() {
262 var name = $a.attr('name');
263 // Eg. name = gis_data[0][MULTIPOLYGON][add_polygon] => prefix = gis_data[0][MULTIPOLYGON]
264 var prefix = name.substr(0, name.length - 13);
265 // Find the number of polygons
266 var $noOfPolygonsInput = $("input[name='" + prefix + "[no_of_polygons]" + "']");
267 var noOfPolygons = parseInt($noOfPolygonsInput.attr('value'));
269 // Add the new polygon
270 var html = PMA_messages['strPolygon'] + (noOfPolygons + 1) + ':<br>';
271 html += '<input type="hidden" name="' + prefix + '[' + noOfPolygons + '][no_of_lines]" value="1">';
272 + '<br>' + PMA_messages['strOuterRing'] + ':';
273 + '<input type="hidden" name="' + prefix + '[' + noOfPolygons + '][0][no_of_points]" value="4">';
274 for (i = 0; i < 4; i++) {
275 html += addDataPoint(i, (prefix + '[' + noOfPolygons + '][0]'));
277 html += '<a class="addPoint addJs" name="' + prefix + '[' + noOfPolygons + '][0][add_point]">+ '
278 + PMA_messages['strAddPoint'] + '</a><br>'
279 + '<a class="addLine addJs" name="' + prefix + '[' + noOfPolygons + '][add_line]">+ '
280 + PMA_messages['strAddInnerRing'] + '</a><br><br>';
283 $noOfPolygonsInput.attr('value', noOfPolygons + 1);
287 * Handles adding geoms
289 $('.addJs.addGeom').live('click', function() {
291 var prefix = 'gis_data[GEOMETRYCOLLECTION]';
292 // Find the number of geoms
293 var $noOfGeomsInput = $("input[name='" + prefix + "[geom_count]" + "']");
294 var noOfGeoms = parseInt($noOfGeomsInput.attr('value'));
296 var html1 = PMA_messages['strGeometry'] + (noOfGeoms + 1) + ':<br>';
297 var $geomType = $("select[name='gis_data[" + (noOfGeoms - 1) + "][gis_type]']").clone();
298 $geomType.attr('name', 'gis_data[' + noOfGeoms + '][gis_type]').val('POINT');
299 var html2 = '<br>' + PMA_messages['strPoint'] + ' :'
300 + '<label for="x"> ' + PMA_messages['strX'] + ' </label>'
301 + '<input type="text" name="gis_data[' + noOfGeoms + '][POINT][x]" value="">'
302 + '<label for="y"> ' + PMA_messages['strY'] + ' </label>'
303 + '<input type="text" name="gis_data[' + noOfGeoms + '][POINT][y]" value="">'
306 $a.before(html1); $geomType.insertBefore($a); $a.before(html2);
307 $noOfGeomsInput.attr('value', noOfGeoms + 1);