1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * @fileoverview functions used for visualizing GIS data
6 * @requires jquery/jquery.svg.js
7 * @requires jquery/jquery.mousewheel.js
8 * @requires jquery/jquery.event.drag-2.2.js
24 * Zooms and pans the visualization.
28 var g = svg.getElementById('groupPanel');
33 g.setAttribute('transform', 'translate(' + x + ', ' + y + ') scale(' + scale + ')');
36 $('circle.vector').each(function () {
37 id = $(this).attr('id');
38 circle = svg.getElementById(id);
41 "stroke-width" : (2 / scale)
46 $('polyline.vector').each(function () {
47 id = $(this).attr('id');
48 line = svg.getElementById(id);
50 "stroke-width" : (2 / scale)
55 $('path.vector').each(function () {
56 id = $(this).attr('id');
57 polygon = svg.getElementById(id);
59 "stroke-width" : (0.5 / scale)
65 * Initially loads either SVG or OSM visualization based on the choice.
67 function selectVisualization() {
68 if ($('#choice').prop('checked') !== true) {
69 $('#openlayersmap').hide();
71 $('#placeholder').hide();
76 * Adds necessary styles to the div that coontains the openStreetMap.
79 var $placeholder = $('#placeholder');
81 'border' : '1px solid #aaa',
82 'width' : $placeholder.width(),
83 'height' : $placeholder.height(),
86 $('#openlayersmap').css(cssObj);
90 * Loads the SVG element and make a reference to it.
93 var $placeholder = $('#placeholder');
96 onLoad: function (svg_ref) {
101 // Removes the second SVG element unnecessarily added due to the above command
102 $placeholder.find('svg:nth-child(2)').remove();
106 * Adds controllers for zooming and panning.
108 function addZoomPanControllers() {
109 var $placeholder = $('#placeholder');
110 if ($("#placeholder").find("svg").length > 0) {
111 var pmaThemeImage = $('#pmaThemeImage').val();
112 // add panning arrows
113 $('<img class="button" id="left_arrow" src="' + pmaThemeImage + 'west-mini.png">').appendTo($placeholder);
114 $('<img class="button" id="right_arrow" src="' + pmaThemeImage + 'east-mini.png">').appendTo($placeholder);
115 $('<img class="button" id="up_arrow" src="' + pmaThemeImage + 'north-mini.png">').appendTo($placeholder);
116 $('<img class="button" id="down_arrow" src="' + pmaThemeImage + 'south-mini.png">').appendTo($placeholder);
117 // add zooming controls
118 $('<img class="button" id="zoom_in" src="' + pmaThemeImage + 'zoom-plus-mini.png">').appendTo($placeholder);
119 $('<img class="button" id="zoom_world" src="' + pmaThemeImage + 'zoom-world-mini.png">').appendTo($placeholder);
120 $('<img class="button" id="zoom_out" src="' + pmaThemeImage + 'zoom-minus-mini.png">').appendTo($placeholder);
125 * Resizes the GIS visualization to fit into the space available.
127 function resizeGISVisualization() {
128 var $placeholder = $('#placeholder');
129 var old_width = $placeholder.width();
130 var visWidth = $('#div_view_options').width() - 48;
132 // Assign new value for width
133 $placeholder.width(visWidth);
134 $('svg').attr('width', visWidth);
136 // Assign the offset created due to resizing to defaultX and center the svg.
137 defaultX = (visWidth - old_width) / 2;
144 * Initialize the GIS visualization.
146 function initGISVisualization() {
147 // Loads either SVG or OSM visualization based on the choice
148 selectVisualization();
149 // Resizes the GIS visualization to fit into the space available
150 resizeGISVisualization();
151 if (typeof OpenLayers !== 'undefined') {
152 // Configure OpenLayers
153 OpenLayers._getScriptLocation = function() {return './js/openlayers/';};
154 // Adds necessary styles to the div that coontains the openStreetMap
156 // Draws openStreetMap with openLayers
159 // Loads the SVG element and make a reference to it
161 // Adds controllers for zooming and panning
162 addZoomPanControllers();
166 function getRelativeCoords(e) {
167 var position = $('#placeholder').offset();
169 x : e.pageX - position.left,
170 y : e.pageY - position.top
175 * Ajax handlers for GIS visualization page
177 * Actions Ajaxified here:
179 * Zooming in and zooming out on mousewheel movement.
180 * Panning the visualization on dragging.
181 * Zooming in on double clicking.
182 * Zooming out on clicking the zoom out button.
183 * Panning on clicking the arrow buttons.
184 * Displaying tooltips for GIS objects.
188 * Unbind all event handlers before tearing down a page
190 AJAX.registerTeardown('tbl_gis_visualization.js', function () {
191 $(document).off('click', '#choice');
192 $(document).off('mousewheel', '#placeholder');
193 $(document).off('dragstart', 'svg');
194 $(document).off('mouseup', 'svg');
195 $(document).off('drag', 'svg');
196 $(document).off('dblclick', '#placeholder');
197 $(document).off('click', '#zoom_in');
198 $(document).off('click', '#zoom_world');
199 $(document).off('click', '#zoom_out');
200 $(document).off('click', '#left_arrow');
201 $(document).off('click', '#right_arrow');
202 $(document).off('click', '#up_arrow');
203 $(document).off('click', '#down_arrow');
204 $('.vector').unbind('mousemove').unbind('mouseout');
207 AJAX.registerOnload('tbl_gis_visualization.js', function () {
209 // If we are in GIS visualization, initialize it
210 if ($('#gis_div').length > 0) {
211 initGISVisualization();
214 if (typeof OpenLayers === 'undefined') {
215 $('#choice, #labelChoice').hide();
217 $(document).on('click', '#choice', function () {
218 if ($(this).prop('checked') === false) {
219 $('#placeholder').show();
220 $('#openlayersmap').hide();
222 $('#placeholder').hide();
223 $('#openlayersmap').show();
227 $(document).on('mousewheel', '#placeholder', function (event, delta) {
228 event.preventDefault();
229 var relCoords = getRelativeCoords(event);
233 // zooming in keeping the position under mouse pointer unmoved.
234 x = relCoords.x - (relCoords.x - x) * zoomFactor;
235 y = relCoords.y - (relCoords.y - y) * zoomFactor;
240 // zooming out keeping the position under mouse pointer unmoved.
241 x = relCoords.x - (relCoords.x - x) / zoomFactor;
242 y = relCoords.y - (relCoords.y - y) / zoomFactor;
251 $(document).on('dragstart', 'svg', function (event, dd) {
252 $('#placeholder').addClass('placeholderDrag');
253 dragX = Math.round(dd.offsetX);
254 dragY = Math.round(dd.offsetY);
257 $(document).on('mouseup', 'svg', function (event) {
258 $('#placeholder').removeClass('placeholderDrag');
261 $(document).on('drag', 'svg', function (event, dd) {
262 var newX = Math.round(dd.offsetX);
265 var newY = Math.round(dd.offsetY);
271 $(document).on('dblclick', '#placeholder', function (event) {
273 // zooming in keeping the position under mouse pointer unmoved.
274 var relCoords = getRelativeCoords(event);
275 x = relCoords.x - (relCoords.x - x) * zoomFactor;
276 y = relCoords.y - (relCoords.y - y) * zoomFactor;
280 $(document).on('click', '#zoom_in', function (e) {
285 var $placeholder = $('#placeholder').find('svg');
286 width = $placeholder.attr('width');
287 height = $placeholder.attr('height');
288 // zooming in keeping the center unmoved.
289 x = width / 2 - (width / 2 - x) * zoomFactor;
290 y = height / 2 - (height / 2 - y) * zoomFactor;
294 $(document).on('click', '#zoom_world', function (e) {
302 $(document).on('click', '#zoom_out', function (e) {
307 var $placeholder = $('#placeholder').find('svg');
308 width = $placeholder.attr('width');
309 height = $placeholder.attr('height');
310 // zooming out keeping the center unmoved.
311 x = width / 2 - (width / 2 - x) / zoomFactor;
312 y = height / 2 - (height / 2 - y) / zoomFactor;
316 $(document).on('click', '#left_arrow', function (e) {
322 $(document).on('click', '#right_arrow', function (e) {
328 $(document).on('click', '#up_arrow', function (e) {
334 $(document).on('click', '#down_arrow', function (e) {
341 * Detect the mousemove event and show tooltips.
343 $('.vector').bind('mousemove', function (event) {
344 var contents = $.trim(escapeHtml($(this).attr('name')));
345 $("#tooltip").remove();
346 if (contents !== '') {
347 $('<div id="tooltip">' + contents + '</div>').css({
348 position : 'absolute',
349 top : event.pageY + 10,
350 left : event.pageX + 10,
351 border : '1px solid #fdd',
353 'background-color' : '#fee',
355 }).appendTo("body").fadeIn(200);
360 * Detect the mouseout event and hide tooltips.
362 $('.vector').bind('mouseout', function (event) {
363 $("#tooltip").remove();