/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* @fileoverview functions used in GIS data editor
*
* @requires jQuery
*
*/
var gisEditorLoaded = false;
/**
* Closes the GIS data editor and perform necessary clean up work.
*/
function closeGISEditor(){
$("#popup_background").fadeOut("fast");
$("#gis_editor").fadeOut("fast", function () {
$(this).html('');
});
}
/**
* Prepares the HTML recieved via AJAX.
*/
function prepareJSVersion() {
// Change the text on the submit button
$("#gis_editor input[name='gis_data[save]']")
.val(PMA_messages['strCopy'])
.insertAfter($('#gis_data_textarea'))
.before('
');
// Add close and cancel links
$('#gis_data_editor').prepend('' + PMA_messages['strClose'] + '');
$(' ' + PMA_messages['strCancel'] + '')
.insertAfter($("input[name='gis_data[save]']"));
// Remove the unnecessary text
$('div#gis_data_output p').remove();
// Remove 'add' buttons and add links
$('#gis_editor input.add').each(function(e) {
var $button = $(this);
$button.addClass('addJs').removeClass('add');
var classes = $button.attr('class');
$button.replaceWith(
'+ ' + $button.val() + ''
);
});
}
/**
* Returns the HTML for a data point.
*
* @param pointNumber point number
* @param prefix prefix of the name
* @returns the HTML for a data point
*/
function addDataPoint(pointNumber, prefix) {
return '
' + $.sprintf(PMA_messages['strPointN'], (pointNumber + 1)) + ':'
+ ''
+ ''
+ ''
+ '';
}
/**
* Initialize the visualization in the GIS data editor.
*/
function initGISEditorVisualization() {
// Loads either SVG or OSM visualization based on the choice
selectVisualization();
// Adds necessary styles to the div that coontains the openStreetMap
styleOSM();
// Loads the SVG element and make a reference to it
loadSVG();
// Adds controllers for zooming and panning
addZoomPanControllers();
zoomAndPan();
}
/**
* Loads JavaScript files and the GIS editor.
*
* @param value current value of the geometry field
* @param field field name
* @param type geometry type
* @param input_name name of the input field
* @param token token
*/
function loadJSAndGISEditor(value, field, type, input_name, token) {
var head = document.getElementsByTagName('head')[0];
var script;
// Loads a set of small JS file needed for the GIS editor
var smallScripts = [ 'js/jquery/jquery.svg.js',
'js/jquery/jquery.mousewheel.js',
'js/jquery/jquery.event.drag-2.0.js',
'js/tbl_gis_visualization.js' ];
for (i = 0; i < smallScripts.length; i++) {
script = document.createElement('script');
script.type = 'text/javascript';
script.src = smallScripts[i];
head.appendChild(script);
}
// OpenLayers.js is BIG and takes time. So asynchronous loading would not work.
// Load the JS and do a callback to load the content for the GIS Editor.
script = document.createElement('script');
script.type = 'text/javascript';
script.onreadystatechange = function() {
if (this.readyState == 'complete') {
loadGISEditor(value, field, type, input_name, token);
}
};
script.onload = function() {
loadGISEditor(value, field, type, input_name, token);
};
script.src = 'js/openlayers/OpenLayers.js';
head.appendChild(script);
gisEditorLoaded = true;
}
/**
* Loads the GIS editor via AJAX
*
* @param value current value of the geometry field
* @param field field name
* @param type geometry type
* @param input_name name of the input field
* @param token token
*/
function loadGISEditor(value, field, type, input_name, token) {
var $gis_editor = $("#gis_editor");
$.post('gis_data_editor.php', {
'field' : field,
'value' : value,
'type' : type,
'input_name' : input_name,
'get_gis_editor' : true,
'token' : token
}, function(data) {
if (data.success == true) {
$gis_editor.html(data.gis_editor);
initGISEditorVisualization();
prepareJSVersion();
} else {
PMA_ajaxShowMessage(data.error, false);
}
}, 'json');
}
/**
* Opens up the dialog for the GIS data editor.
*/
function openGISEditor() {
// Center the popup
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupWidth = windowWidth * 0.9;
var popupHeight = windowHeight * 0.9;
var popupOffsetTop = windowHeight / 2 - popupHeight / 2;
var popupOffsetLeft = windowWidth / 2 - popupWidth / 2;
var $gis_editor = $("#gis_editor");
var $backgrouond = $("#popup_background");
$gis_editor.css({"top": popupOffsetTop, "left": popupOffsetLeft, "width": popupWidth, "height": popupHeight});
$backgrouond.css({"opacity":"0.7"});
$gis_editor.append('