tbl_gis_visualization.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* vim: set expandtab sw=4 ts=4 sts=4: */
  2. /**
  3. * @fileoverview functions used for visualizing GIS data
  4. *
  5. * @requires jquery
  6. * @requires jquery/jquery.svg.js
  7. * @requires jquery/jquery.mousewheel.js
  8. * @requires jquery/jquery.event.drag-2.2.js
  9. */
  10. // Constants
  11. var zoomFactor = 1.5;
  12. var defaultX = 0;
  13. var defaultY = 0;
  14. // Variables
  15. var x;
  16. var y;
  17. var scale = 1;
  18. var svg;
  19. /**
  20. * Zooms and pans the visualization.
  21. */
  22. function zoomAndPan()
  23. {
  24. var g = svg.getElementById('groupPanel');
  25. g.setAttribute('transform', 'translate(' + x + ', ' + y + ') scale(' + scale + ')');
  26. var id;
  27. var circle;
  28. $('circle.vector').each(function() {
  29. id = $(this).attr('id');
  30. circle = svg.getElementById(id);
  31. svg.change(circle, {
  32. r : (3 / scale),
  33. "stroke-width" : (2 / scale)
  34. });
  35. });
  36. var line;
  37. $('polyline.vector').each(function() {
  38. id = $(this).attr('id');
  39. line = svg.getElementById(id);
  40. svg.change(line, {
  41. "stroke-width" : (2 / scale)
  42. });
  43. });
  44. var polygon;
  45. $('path.vector').each(function() {
  46. id = $(this).attr('id');
  47. polygon = svg.getElementById(id);
  48. svg.change(polygon, {
  49. "stroke-width" : (0.5 / scale)
  50. });
  51. });
  52. }
  53. /**
  54. * Initially loads either SVG or OSM visualization based on the choice.
  55. */
  56. function selectVisualization() {
  57. if ($('#choice').prop('checked') != true) {
  58. $('#openlayersmap').hide();
  59. } else {
  60. $('#placeholder').hide();
  61. }
  62. }
  63. /**
  64. * Adds necessary styles to the div that coontains the openStreetMap.
  65. */
  66. function styleOSM() {
  67. var $placeholder = $('#placeholder');
  68. var cssObj = {
  69. 'border' : '1px solid #aaa',
  70. 'width' : $placeholder.width(),
  71. 'height' : $placeholder.height(),
  72. 'float' : 'right'
  73. };
  74. $('#openlayersmap').css(cssObj);
  75. }
  76. /**
  77. * Loads the SVG element and make a reference to it.
  78. */
  79. function loadSVG() {
  80. var $placeholder = $('#placeholder');
  81. $placeholder.svg({
  82. onLoad: function(svg_ref) {
  83. svg = svg_ref;
  84. }
  85. });
  86. // Removes the second SVG element unnecessarily added due to the above command
  87. $placeholder.find('svg:nth-child(2)').remove();
  88. }
  89. /**
  90. * Adds controllers for zooming and panning.
  91. */
  92. function addZoomPanControllers() {
  93. var $placeholder = $('#placeholder');
  94. if ($("#placeholder svg").length > 0) {
  95. var pmaThemeImage = $('#pmaThemeImage').val();
  96. // add panning arrows
  97. $('<img class="button" id="left_arrow" src="' + pmaThemeImage + 'west-mini.png">').appendTo($placeholder);
  98. $('<img class="button" id="right_arrow" src="' + pmaThemeImage + 'east-mini.png">').appendTo($placeholder);
  99. $('<img class="button" id="up_arrow" src="' + pmaThemeImage + 'north-mini.png">').appendTo($placeholder);
  100. $('<img class="button" id="down_arrow" src="' + pmaThemeImage + 'south-mini.png">').appendTo($placeholder);
  101. // add zooming controls
  102. $('<img class="button" id="zoom_in" src="' + pmaThemeImage + 'zoom-plus-mini.png">').appendTo($placeholder);
  103. $('<img class="button" id="zoom_world" src="' + pmaThemeImage + 'zoom-world-mini.png">').appendTo($placeholder);
  104. $('<img class="button" id="zoom_out" src="' + pmaThemeImage + 'zoom-minus-mini.png">').appendTo($placeholder);
  105. }
  106. }
  107. /**
  108. * Resizes the GIS visualization to fit into the space available.
  109. */
  110. function resizeGISVisualization() {
  111. var $placeholder = $('#placeholder');
  112. var old_width = $placeholder.width();
  113. var visWidth = $('#div_view_options').width() - 48;
  114. // Assign new value for width
  115. $placeholder.width(visWidth);
  116. $('svg').attr('width', visWidth);
  117. // Assign the offset created due to resizing to defaultX and center the svg.
  118. defaultX = (visWidth - old_width) / 2;
  119. x = defaultX;
  120. y = 0;
  121. scale = 1;
  122. }
  123. /**
  124. * Initialize the GIS visualization.
  125. */
  126. function initGISVisualization() {
  127. // Loads either SVG or OSM visualization based on the choice
  128. selectVisualization();
  129. // Resizes the GIS visualization to fit into the space available
  130. resizeGISVisualization();
  131. // Adds necessary styles to the div that coontains the openStreetMap
  132. styleOSM();
  133. // Draws openStreetMap with openLayers
  134. drawOpenLayers();
  135. // Loads the SVG element and make a reference to it
  136. loadSVG();
  137. // Adds controllers for zooming and panning
  138. addZoomPanControllers();
  139. zoomAndPan();
  140. }
  141. function getRelativeCoords(e) {
  142. var position = $('#placeholder').offset();
  143. return {
  144. x : e.pageX - position.left,
  145. y : e.pageY - position.top
  146. };
  147. }
  148. /**
  149. * Ajax handlers for GIS visualization page
  150. *
  151. * Actions Ajaxified here:
  152. *
  153. * Zooming in and zooming out on mousewheel movement.
  154. * Panning the visualization on dragging.
  155. * Zooming in on double clicking.
  156. * Zooming out on clicking the zoom out button.
  157. * Panning on clicking the arrow buttons.
  158. * Displaying tooltips for GIS objects.
  159. */
  160. /**
  161. * Unbind all event handlers before tearing down a page
  162. */
  163. AJAX.registerTeardown('tbl_gis_visualization.js', function() {
  164. $('#choice').die('click');
  165. $('#placeholder').die('mousewheel');
  166. $('svg').die('dragstart');
  167. $('svg').die('mouseup');
  168. $('svg').die('drag');
  169. $('#placeholder').die('dblclick');
  170. $('#zoom_in').die('click');
  171. $('#zoom_world').die('click');
  172. $('#zoom_out').die('click');
  173. $('#left_arrow').die('click');
  174. $('#right_arrow').die('click');
  175. $('#up_arrow').die('click');
  176. $('#down_arrow').die('click');
  177. $('.vector').unbind('mousemove').unbind('mouseout');
  178. });
  179. AJAX.registerOnload('tbl_gis_visualization.js', function() {
  180. // If we are in GIS visualization, initialize it
  181. if ($('table.gis_table').length > 0) {
  182. initGISVisualization();
  183. }
  184. $('#choice').live('click', function() {
  185. if ($(this).prop('checked') == false) {
  186. $('#placeholder').show();
  187. $('#openlayersmap').hide();
  188. } else {
  189. $('#placeholder').hide();
  190. $('#openlayersmap').show();
  191. }
  192. });
  193. $('#placeholder').live('mousewheel', function(event, delta) {
  194. var relCoords = getRelativeCoords(event);
  195. if (delta > 0) {
  196. //zoom in
  197. scale *= zoomFactor;
  198. // zooming in keeping the position under mouse pointer unmoved.
  199. x = relCoords.x - (relCoords.x - x) * zoomFactor;
  200. y = relCoords.y - (relCoords.y - y) * zoomFactor;
  201. zoomAndPan();
  202. } else {
  203. //zoom out
  204. scale /= zoomFactor;
  205. // zooming out keeping the position under mouse pointer unmoved.
  206. x = relCoords.x - (relCoords.x - x) / zoomFactor;
  207. y = relCoords.y - (relCoords.y - y) / zoomFactor;
  208. zoomAndPan();
  209. }
  210. return true;
  211. });
  212. var dragX = 0; var dragY = 0;
  213. $('svg').live('dragstart', function(event, dd) {
  214. $('#placeholder').addClass('placeholderDrag');
  215. dragX = Math.round(dd.offsetX);
  216. dragY = Math.round(dd.offsetY);
  217. });
  218. $('svg').live('mouseup', function(event) {
  219. $('#placeholder').removeClass('placeholderDrag');
  220. });
  221. $('svg').live('drag', function(event, dd) {
  222. newX = Math.round(dd.offsetX);
  223. x += newX - dragX;
  224. dragX = newX;
  225. newY = Math.round(dd.offsetY);
  226. y += newY - dragY;
  227. dragY = newY;
  228. zoomAndPan();
  229. });
  230. $('#placeholder').live('dblclick', function(event) {
  231. scale *= zoomFactor;
  232. // zooming in keeping the position under mouse pointer unmoved.
  233. var relCoords = getRelativeCoords(event);
  234. x = relCoords.x - (relCoords.x - x) * zoomFactor;
  235. y = relCoords.y - (relCoords.y - y) * zoomFactor;
  236. zoomAndPan();
  237. });
  238. $('#zoom_in').live('click', function(e) {
  239. e.preventDefault();
  240. //zoom in
  241. scale *= zoomFactor;
  242. width = $('#placeholder svg').attr('width');
  243. height = $('#placeholder svg').attr('height');
  244. // zooming in keeping the center unmoved.
  245. x = width / 2 - (width / 2 - x) * zoomFactor;
  246. y = height / 2 - (height / 2 - y) * zoomFactor;
  247. zoomAndPan();
  248. });
  249. $('#zoom_world').live('click', function(e) {
  250. e.preventDefault();
  251. scale = 1;
  252. x = defaultX;
  253. y = defaultY;
  254. zoomAndPan();
  255. });
  256. $('#zoom_out').live('click', function(e) {
  257. e.preventDefault();
  258. //zoom out
  259. scale /= zoomFactor;
  260. width = $('#placeholder svg').attr('width');
  261. height = $('#placeholder svg').attr('height');
  262. // zooming out keeping the center unmoved.
  263. x = width / 2 - (width / 2 - x) / zoomFactor;
  264. y = height / 2 - (height / 2 - y) / zoomFactor;
  265. zoomAndPan();
  266. });
  267. $('#left_arrow').live('click', function(e) {
  268. e.preventDefault();
  269. x += 100;
  270. zoomAndPan();
  271. });
  272. $('#right_arrow').live('click', function(e) {
  273. e.preventDefault();
  274. x -= 100;
  275. zoomAndPan();
  276. });
  277. $('#up_arrow').live('click', function(e) {
  278. e.preventDefault();
  279. y += 100;
  280. zoomAndPan();
  281. });
  282. $('#down_arrow').live('click', function(e) {
  283. e.preventDefault();
  284. y -= 100;
  285. zoomAndPan();
  286. });
  287. /**
  288. * Detect the mousemove event and show tooltips.
  289. */
  290. $('.vector').bind('mousemove', function(event) {
  291. var contents = $.trim(escapeHtml($(this).attr('name')));
  292. $("#tooltip").remove();
  293. if (contents != '') {
  294. $('<div id="tooltip">' + contents + '</div>').css({
  295. position : 'absolute',
  296. top : event.pageY + 10,
  297. left : event.pageX + 10,
  298. border : '1px solid #fdd',
  299. padding : '2px',
  300. 'background-color' : '#fee',
  301. opacity : 0.90
  302. }).appendTo("body").fadeIn(200);
  303. }
  304. });
  305. /**
  306. * Detect the mouseout event and hide tooltips.
  307. */
  308. $('.vector').bind('mouseout', function(event) {
  309. $("#tooltip").remove();
  310. });
  311. });