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');
30 g.setAttribute('transform', 'translate(' + x + ', ' + y + ') scale(' + scale + ')');
33 $('circle.vector').each(function () {
34 id = $(this).attr('id');
35 circle = svg.getElementById(id);
38 "stroke-width" : (2 / scale)
43 $('polyline.vector').each(function () {
44 id = $(this).attr('id');
45 line = svg.getElementById(id);
47 "stroke-width" : (2 / scale)
52 $('path.vector').each(function () {
53 id = $(this).attr('id');
54 polygon = svg.getElementById(id);
56 "stroke-width" : (0.5 / scale)
62 * Initially loads either SVG or OSM visualization based on the choice.
64 function selectVisualization() {
65 if ($('#choice').prop('checked') !== true) {
66 $('#openlayersmap').hide();
68 $('#placeholder').hide();
73 * Adds necessary styles to the div that coontains the openStreetMap.
76 var $placeholder = $('#placeholder');
78 'border' : '1px solid #aaa',
79 'width' : $placeholder.width(),
80 'height' : $placeholder.height(),
83 $('#openlayersmap').css(cssObj);
87 * Loads the SVG element and make a reference to it.
90 var $placeholder = $('#placeholder');
93 onLoad: function (svg_ref) {
98 // Removes the second SVG element unnecessarily added due to the above command
99 $placeholder.find('svg:nth-child(2)').remove();
103 * Adds controllers for zooming and panning.
105 function addZoomPanControllers() {
106 var $placeholder = $('#placeholder');
107 if ($("#placeholder svg").length > 0) {
108 var pmaThemeImage = $('#pmaThemeImage').val();
109 // add panning arrows
110 $('<img class="button" id="left_arrow" src="' + pmaThemeImage + 'west-mini.png">').appendTo($placeholder);
111 $('<img class="button" id="right_arrow" src="' + pmaThemeImage + 'east-mini.png">').appendTo($placeholder);
112 $('<img class="button" id="up_arrow" src="' + pmaThemeImage + 'north-mini.png">').appendTo($placeholder);
113 $('<img class="button" id="down_arrow" src="' + pmaThemeImage + 'south-mini.png">').appendTo($placeholder);
114 // add zooming controls
115 $('<img class="button" id="zoom_in" src="' + pmaThemeImage + 'zoom-plus-mini.png">').appendTo($placeholder);
116 $('<img class="button" id="zoom_world" src="' + pmaThemeImage + 'zoom-world-mini.png">').appendTo($placeholder);
117 $('<img class="button" id="zoom_out" src="' + pmaThemeImage + 'zoom-minus-mini.png">').appendTo($placeholder);
122 * Resizes the GIS visualization to fit into the space available.
124 function resizeGISVisualization() {
125 var $placeholder = $('#placeholder');
126 var old_width = $placeholder.width();
127 var visWidth = $('#div_view_options').width() - 48;
129 // Assign new value for width
130 $placeholder.width(visWidth);
131 $('svg').attr('width', visWidth);
133 // Assign the offset created due to resizing to defaultX and center the svg.
134 defaultX = (visWidth - old_width) / 2;
141 * Initialize the GIS visualization.
143 function initGISVisualization() {
144 // Loads either SVG or OSM visualization based on the choice
145 selectVisualization();
146 // Resizes the GIS visualization to fit into the space available
147 resizeGISVisualization();
148 // Adds necessary styles to the div that coontains the openStreetMap
150 // Draws openStreetMap with openLayers
152 // Loads the SVG element and make a reference to it
154 // Adds controllers for zooming and panning
155 addZoomPanControllers();
159 function getRelativeCoords(e) {
160 var position = $('#placeholder').offset();
162 x : e.pageX - position.left,
163 y : e.pageY - position.top
168 * Ajax handlers for GIS visualization page
170 * Actions Ajaxified here:
172 * Zooming in and zooming out on mousewheel movement.
173 * Panning the visualization on dragging.
174 * Zooming in on double clicking.
175 * Zooming out on clicking the zoom out button.
176 * Panning on clicking the arrow buttons.
177 * Displaying tooltips for GIS objects.
181 * Unbind all event handlers before tearing down a page
183 AJAX.registerTeardown('tbl_gis_visualization.js', function () {
184 $('#choice').die('click');
185 $('#placeholder').die('mousewheel');
186 $('svg').die('dragstart');
187 $('svg').die('mouseup');
188 $('svg').die('drag');
189 $('#placeholder').die('dblclick');
190 $('#zoom_in').die('click');
191 $('#zoom_world').die('click');
192 $('#zoom_out').die('click');
193 $('#left_arrow').die('click');
194 $('#right_arrow').die('click');
195 $('#up_arrow').die('click');
196 $('#down_arrow').die('click');
197 $('.vector').unbind('mousemove').unbind('mouseout');
200 AJAX.registerOnload('tbl_gis_visualization.js', function () {
202 // If we are in GIS visualization, initialize it
203 if ($('table.gis_table').length > 0) {
204 initGISVisualization();
207 $('#choice').live('click', function () {
208 if ($(this).prop('checked') === false) {
209 $('#placeholder').show();
210 $('#openlayersmap').hide();
212 $('#placeholder').hide();
213 $('#openlayersmap').show();
217 $('#placeholder').live('mousewheel', function (event, delta) {
218 var relCoords = getRelativeCoords(event);
222 // zooming in keeping the position under mouse pointer unmoved.
223 x = relCoords.x - (relCoords.x - x) * zoomFactor;
224 y = relCoords.y - (relCoords.y - y) * zoomFactor;
229 // zooming out keeping the position under mouse pointer unmoved.
230 x = relCoords.x - (relCoords.x - x) / zoomFactor;
231 y = relCoords.y - (relCoords.y - y) / zoomFactor;
240 $('svg').live('dragstart', function (event, dd) {
241 $('#placeholder').addClass('placeholderDrag');
242 dragX = Math.round(dd.offsetX);
243 dragY = Math.round(dd.offsetY);
246 $('svg').live('mouseup', function (event) {
247 $('#placeholder').removeClass('placeholderDrag');
250 $('svg').live('drag', function (event, dd) {
251 newX = Math.round(dd.offsetX);
254 newY = Math.round(dd.offsetY);
260 $('#placeholder').live('dblclick', function (event) {
262 // zooming in keeping the position under mouse pointer unmoved.
263 var relCoords = getRelativeCoords(event);
264 x = relCoords.x - (relCoords.x - x) * zoomFactor;
265 y = relCoords.y - (relCoords.y - y) * zoomFactor;
269 $('#zoom_in').live('click', function (e) {
274 width = $('#placeholder svg').attr('width');
275 height = $('#placeholder svg').attr('height');
276 // zooming in keeping the center unmoved.
277 x = width / 2 - (width / 2 - x) * zoomFactor;
278 y = height / 2 - (height / 2 - y) * zoomFactor;
282 $('#zoom_world').live('click', function (e) {
290 $('#zoom_out').live('click', function (e) {
295 width = $('#placeholder svg').attr('width');
296 height = $('#placeholder svg').attr('height');
297 // zooming out keeping the center unmoved.
298 x = width / 2 - (width / 2 - x) / zoomFactor;
299 y = height / 2 - (height / 2 - y) / zoomFactor;
303 $('#left_arrow').live('click', function (e) {
309 $('#right_arrow').live('click', function (e) {
315 $('#up_arrow').live('click', function (e) {
321 $('#down_arrow').live('click', function (e) {
328 * Detect the mousemove event and show tooltips.
330 $('.vector').bind('mousemove', function (event) {
331 var contents = $.trim(escapeHtml($(this).attr('name')));
332 $("#tooltip").remove();
333 if (contents !== '') {
334 $('<div id="tooltip">' + contents + '</div>').css({
335 position : 'absolute',
336 top : event.pageY + 10,
337 left : event.pageX + 10,
338 border : '1px solid #fdd',
340 'background-color' : '#fee',
342 }).appendTo("body").fadeIn(200);
347 * Detect the mouseout event and hide tooltips.
349 $('.vector').bind('mouseout', function (event) {
350 $("#tooltip").remove();