1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * @fileoverview functions used for visualizing GIS data
6 * @requires vendor/jquery/jquery.svg.js
7 * @requires vendor/jquery/jquery.mousewheel.js
8 * @requires vendor/jquery/jquery.event.drag-2.2.js
24 * Zooms and pans the visualization.
26 function zoomAndPan () {
27 var g = svg.getElementById('groupPanel');
32 g.setAttribute('transform', 'translate(' + x + ', ' + y + ') scale(' + scale + ')');
35 $('circle.vector').each(function () {
36 id = $(this).attr('id');
37 circle = svg.getElementById(id);
38 $(svg).change(circle, {
40 'stroke-width' : (2 / scale)
45 $('polyline.vector').each(function () {
46 id = $(this).attr('id');
47 line = svg.getElementById(id);
49 'stroke-width' : (2 / scale)
54 $('path.vector').each(function () {
55 id = $(this).attr('id');
56 polygon = svg.getElementById(id);
57 $(svg).change(polygon, {
58 'stroke-width' : (0.5 / scale)
64 * Initially loads either SVG or OSM visualization based on the choice.
66 function selectVisualization () {
67 if ($('#choice').prop('checked') !== true) {
68 $('#openlayersmap').hide();
70 $('#placeholder').hide();
75 * Adds necessary styles to the div that coontains the openStreetMap.
77 function styleOSM () {
78 var $placeholder = $('#placeholder');
80 'border' : '1px solid #aaa',
81 'width' : $placeholder.width(),
82 'height' : $placeholder.height(),
85 $('#openlayersmap').css(cssObj);
89 * Loads the SVG element and make a reference to it.
92 var $placeholder = $('#placeholder');
95 onLoad: function (svg_ref) {
100 // Removes the second SVG element unnecessarily added due to the above command
101 $placeholder.find('svg:nth-child(2)').remove();
105 * Adds controllers for zooming and panning.
107 function addZoomPanControllers () {
108 var $placeholder = $('#placeholder');
109 if ($('#placeholder').find('svg').length > 0) {
110 var pmaThemeImage = $('#pmaThemeImage').val();
111 // add panning arrows
112 $('<img class="button" id="left_arrow" src="' + pmaThemeImage + 'west-mini.png">').appendTo($placeholder);
113 $('<img class="button" id="right_arrow" src="' + pmaThemeImage + 'east-mini.png">').appendTo($placeholder);
114 $('<img class="button" id="up_arrow" src="' + pmaThemeImage + 'north-mini.png">').appendTo($placeholder);
115 $('<img class="button" id="down_arrow" src="' + pmaThemeImage + 'south-mini.png">').appendTo($placeholder);
116 // add zooming controls
117 $('<img class="button" id="zoom_in" src="' + pmaThemeImage + 'zoom-plus-mini.png">').appendTo($placeholder);
118 $('<img class="button" id="zoom_world" src="' + pmaThemeImage + 'zoom-world-mini.png">').appendTo($placeholder);
119 $('<img class="button" id="zoom_out" src="' + pmaThemeImage + 'zoom-minus-mini.png">').appendTo($placeholder);
124 * Resizes the GIS visualization to fit into the space available.
126 function resizeGISVisualization () {
127 var $placeholder = $('#placeholder');
128 var old_width = $placeholder.width();
129 var visWidth = $('#div_view_options').width() - 48;
131 // Assign new value for width
132 $placeholder.width(visWidth);
133 $('svg').attr('width', visWidth);
135 // Assign the offset created due to resizing to defaultX and center the svg.
136 defaultX = (visWidth - old_width) / 2;
143 * Initialize the GIS visualization.
145 function initGISVisualization () {
146 // Loads either SVG or OSM visualization based on the choice
147 selectVisualization();
148 // Resizes the GIS visualization to fit into the space available
149 resizeGISVisualization();
150 if (typeof OpenLayers !== 'undefined') {
151 // Configure OpenLayers
152 OpenLayers._getScriptLocation = function () {
153 return './js/vendor/openlayers/';
155 // Adds necessary styles to the div that coontains the openStreetMap
157 // Draws openStreetMap with openLayers
160 // Loads the SVG element and make a reference to it
162 // Adds controllers for zooming and panning
163 addZoomPanControllers();
167 function getRelativeCoords (e) {
168 var position = $('#placeholder').offset();
170 x : e.pageX - position.left,
171 y : e.pageY - position.top
176 * Ajax handlers for GIS visualization page
178 * Actions Ajaxified here:
180 * Zooming in and zooming out on mousewheel movement.
181 * Panning the visualization on dragging.
182 * Zooming in on double clicking.
183 * Zooming out on clicking the zoom out button.
184 * Panning on clicking the arrow buttons.
185 * Displaying tooltips for GIS objects.
189 * Unbind all event handlers before tearing down a page
191 AJAX.registerTeardown('tbl_gis_visualization.js', function () {
192 $(document).off('click', '#choice');
193 $(document).off('mousewheel', '#placeholder');
194 $(document).off('dragstart', 'svg');
195 $(document).off('mouseup', 'svg');
196 $(document).off('drag', 'svg');
197 $(document).off('dblclick', '#placeholder');
198 $(document).off('click', '#zoom_in');
199 $(document).off('click', '#zoom_world');
200 $(document).off('click', '#zoom_out');
201 $(document).off('click', '#left_arrow');
202 $(document).off('click', '#right_arrow');
203 $(document).off('click', '#up_arrow');
204 $(document).off('click', '#down_arrow');
205 $('.vector').off('mousemove').off('mouseout');
208 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').on('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').on('mouseout', function (event) {
363 $('#tooltip').remove();