tbl_gis_visualization.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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;
  213. var dragY = 0;
  214. $('svg').live('dragstart', function (event, dd) {
  215. $('#placeholder').addClass('placeholderDrag');
  216. dragX = Math.round(dd.offsetX);
  217. dragY = Math.round(dd.offsetY);
  218. });
  219. $('svg').live('mouseup', function (event) {
  220. $('#placeholder').removeClass('placeholderDrag');
  221. });
  222. $('svg').live('drag', function (event, dd) {
  223. newX = Math.round(dd.offsetX);
  224. x += newX - dragX;
  225. dragX = newX;
  226. newY = Math.round(dd.offsetY);
  227. y += newY - dragY;
  228. dragY = newY;
  229. zoomAndPan();
  230. });
  231. $('#placeholder').live('dblclick', function (event) {
  232. scale *= zoomFactor;
  233. // zooming in keeping the position under mouse pointer unmoved.
  234. var relCoords = getRelativeCoords(event);
  235. x = relCoords.x - (relCoords.x - x) * zoomFactor;
  236. y = relCoords.y - (relCoords.y - y) * zoomFactor;
  237. zoomAndPan();
  238. });
  239. $('#zoom_in').live('click', function (e) {
  240. e.preventDefault();
  241. //zoom in
  242. scale *= zoomFactor;
  243. width = $('#placeholder svg').attr('width');
  244. height = $('#placeholder svg').attr('height');
  245. // zooming in keeping the center unmoved.
  246. x = width / 2 - (width / 2 - x) * zoomFactor;
  247. y = height / 2 - (height / 2 - y) * zoomFactor;
  248. zoomAndPan();
  249. });
  250. $('#zoom_world').live('click', function (e) {
  251. e.preventDefault();
  252. scale = 1;
  253. x = defaultX;
  254. y = defaultY;
  255. zoomAndPan();
  256. });
  257. $('#zoom_out').live('click', function (e) {
  258. e.preventDefault();
  259. //zoom out
  260. scale /= zoomFactor;
  261. width = $('#placeholder svg').attr('width');
  262. height = $('#placeholder svg').attr('height');
  263. // zooming out keeping the center unmoved.
  264. x = width / 2 - (width / 2 - x) / zoomFactor;
  265. y = height / 2 - (height / 2 - y) / zoomFactor;
  266. zoomAndPan();
  267. });
  268. $('#left_arrow').live('click', function (e) {
  269. e.preventDefault();
  270. x += 100;
  271. zoomAndPan();
  272. });
  273. $('#right_arrow').live('click', function (e) {
  274. e.preventDefault();
  275. x -= 100;
  276. zoomAndPan();
  277. });
  278. $('#up_arrow').live('click', function (e) {
  279. e.preventDefault();
  280. y += 100;
  281. zoomAndPan();
  282. });
  283. $('#down_arrow').live('click', function (e) {
  284. e.preventDefault();
  285. y -= 100;
  286. zoomAndPan();
  287. });
  288. /**
  289. * Detect the mousemove event and show tooltips.
  290. */
  291. $('.vector').bind('mousemove', function (event) {
  292. var contents = $.trim(escapeHtml($(this).attr('name')));
  293. $("#tooltip").remove();
  294. if (contents !== '') {
  295. $('<div id="tooltip">' + contents + '</div>').css({
  296. position : 'absolute',
  297. top : event.pageY + 10,
  298. left : event.pageX + 10,
  299. border : '1px solid #fdd',
  300. padding : '2px',
  301. 'background-color' : '#fee',
  302. opacity : 0.90
  303. }).appendTo("body").fadeIn(200);
  304. }
  305. });
  306. /**
  307. * Detect the mouseout event and hide tooltips.
  308. */
  309. $('.vector').bind('mouseout', function (event) {
  310. $("#tooltip").remove();
  311. });
  312. });