You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

525 lines
21 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. Copyright (c) 2010 Nik Kalyani nik@kalyani.com http://www.kalyani.com
  3. Modified work Copyright (c) 2016 Jon Burrows subwaymap@jonburrows.co.uk https://jonburrows.co.uk
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. (function ($) {
  21. var plugin = {
  22. defaults: {
  23. debug: false,
  24. grid: false
  25. },
  26. options: {
  27. },
  28. identity: function (type) {
  29. if (type === undefined) type = "name";
  30. switch (type.toLowerCase()) {
  31. case "version": return "1.0.0"; break;
  32. default: return "subwayMap Plugin"; break;
  33. }
  34. },
  35. _debug: function (s) {
  36. if (this.options.debug)
  37. this._log(s);
  38. },
  39. _log: function () {
  40. if (window.console && window.console.log)
  41. window.console.log('[subwayMap] ' + Array.prototype.join.call(arguments, ' '));
  42. },
  43. _supportsCanvas: function () {
  44. var canvas = $("<canvas></canvas>");
  45. if (canvas[0].getContext)
  46. return true;
  47. else
  48. return false;
  49. },
  50. _getCanvasLayer: function (el, overlay) {
  51. this.layer++;
  52. var canvas = $("<canvas style='position:absolute;z-Index:" + ((overlay ? 2000 : 1000) + this.layer) + "' width='" + this.options.pixelWidth + "' height='" + this.options.pixelHeight + "'></canvas>");
  53. el.append(canvas);
  54. return (canvas[0].getContext("2d"));
  55. },
  56. _render: function (el) {
  57. this.layer = -1;
  58. var rows = el.attr("data-rows");
  59. if (rows === undefined)
  60. rows = 10;
  61. else
  62. rows = parseInt(rows);
  63. var columns = el.attr("data-columns");
  64. if (columns === undefined)
  65. columns = 10;
  66. else
  67. columns = parseInt(columns);
  68. var scale = el.attr("data-cellSize");
  69. if (scale === undefined)
  70. scale = 100;
  71. else
  72. scale = parseInt(scale);
  73. var lineWidth = el.attr("data-lineWidth");
  74. if (lineWidth === undefined)
  75. lineWidth = 10;
  76. else
  77. lineWidth = parseInt(lineWidth);
  78. var textClass = el.attr("data-textClass");
  79. if (textClass === undefined) textClass = "";
  80. var grid = el.attr("data-grid");
  81. if ((grid === undefined) || (grid.toLowerCase() == "false"))
  82. grid = false;
  83. else
  84. grid = true;
  85. var legendId = el.attr("data-legendId");
  86. if (legendId === undefined) legendId = "";
  87. var gridNumbers = el.attr("data-gridNumbers");
  88. if ((gridNumbers === undefined) || (gridNumbers.toLowerCase() == "false"))
  89. gridNumbers = false;
  90. else
  91. gridNumbers = true;
  92. var reverseMarkers = el.attr("data-reverseMarkers");
  93. if ((reverseMarkers === undefined) || (reverseMarkers.toLowerCase() == "false"))
  94. reverseMarkers = false;
  95. else
  96. reverseMarkers = true;
  97. this.options.pixelWidth = columns * scale;
  98. this.options.pixelHeight = rows * scale;
  99. //el.css("width", this.options.pixelWidth);
  100. //el.css("height", this.options.pixelHeight);
  101. var self = this;
  102. var lineLabels = [];
  103. var supportsCanvas = $("<canvas></canvas>")[0].getContext;
  104. if (supportsCanvas) {
  105. if (grid) this._drawGrid(el, scale, gridNumbers);
  106. $(el).children("ul").each(function (index) {
  107. var ul = $(this);
  108. var color = $(ul).attr("data-color");
  109. if (color === undefined) color = "#000000";
  110. var outline = $(ul).attr("data-outline");
  111. if (outline != undefined && ((outline === true) || (outline.toLowerCase() == "true")))
  112. outline = true;
  113. else
  114. outline = false;
  115. var dotted = $(ul).attr("data-dotted");
  116. if (dotted != undefined && ((dotted === true) || (dotted.toLowerCase() == "true")))
  117. dotted = true;
  118. else
  119. dotted = false;
  120. var lineTextClass = $(ul).attr("data-textClass");
  121. if (lineTextClass === undefined) lineTextClass = "";
  122. var shiftCoords = $(ul).attr("data-shiftCoords");
  123. if (shiftCoords === undefined) shiftCoords = "";
  124. var shiftX = 0.00;
  125. var shiftY = 0.00;
  126. if (shiftCoords.indexOf(",") > -1) {
  127. shiftX = parseInt(shiftCoords.split(",")[0]) * lineWidth/scale;
  128. shiftY = parseInt(shiftCoords.split(",")[1]) * lineWidth/scale;
  129. }
  130. var lineLabel = $(ul).attr("data-label");
  131. if (lineLabel === undefined)
  132. lineLabel = "Line " + index;
  133. lineLabels[lineLabels.length] = {label: lineLabel, color: color, outline: outline, dotted: dotted };
  134. var nodes = [];
  135. $(ul).children("li").each(function () {
  136. var coords = $(this).attr("data-coords");
  137. if (coords === undefined) coords = "";
  138. var dir = $(this).attr("data-dir");
  139. if (dir === undefined) dir = "";
  140. var labelPos = $(this).attr("data-labelPos");
  141. if (labelPos === undefined) labelPos = "s";
  142. var marker = $(this).attr("data-marker");
  143. if (marker == undefined) marker = "";
  144. var markerInfo = $(this).attr("data-markerInfo");
  145. if (markerInfo == undefined) markerInfo = "";
  146. var anchor = $(this).children("a:first-child");
  147. var label = $(this).text();
  148. if (label === undefined) label = "";
  149. var link = "";
  150. var title = "";
  151. if (anchor != undefined) {
  152. link = $(anchor).attr("href");
  153. if (link === undefined) link = "";
  154. title = $(anchor).attr("title");
  155. if (title === undefined) title = "";
  156. }
  157. self._debug("Coords=" + coords + "; Dir=" + dir + "; Link=" + link + "; Label=" + label + "; labelPos=" + labelPos + "; Marker=" + marker);
  158. var x = "";
  159. var y = "";
  160. if (coords.indexOf(",") > -1) {
  161. x = Number(coords.split(",")[0]) + (marker.indexOf("interchange") > -1 ? 0 : shiftX);
  162. y = Number(coords.split(",")[1]) + (marker.indexOf("interchange") > -1 ? 0 : shiftY);
  163. }
  164. nodes[nodes.length] = { x: x, y: y, direction: dir, marker: marker, markerInfo: markerInfo, link: link, title: title, label: label, labelPos: labelPos };
  165. });
  166. if (nodes.length > 0) {
  167. self._drawLine(el, scale, rows, columns, color, (lineTextClass != "" ? lineTextClass : textClass), lineWidth, nodes, reverseMarkers, dotted);
  168. if (outline === true)
  169. self._drawLine(el, scale, rows, columns, '#FFFFFF', false, lineWidth - 2, nodes, reverseMarkers, dotted);
  170. }
  171. $(ul).remove();
  172. });
  173. if ((lineLabels.length > 0) && (legendId != ""))
  174. {
  175. var legend = $("#" + legendId);
  176. for(var line=0; line<lineLabels.length; line++) {
  177. // Prepare SVG param for dotted lines
  178. var dottedSVGParam = "";
  179. if (lineLabels[line].dotted === true)
  180. dottedSVGParam = "stroke-dasharray='5, 5'";
  181. // Create a SVG line
  182. var lineSVG = "<line x1='0' y1='3' x2='100' y2='3' stroke-width='"+ (lineWidth + 2) +"' stroke='" + lineLabels[line].color + "' "+ dottedSVGParam +" />";
  183. // We create a second SVG white line to create the outline effect in the legend if required by the "outline" param
  184. if (lineLabels[line].outline === true)
  185. lineSVG += "<line x1='0' y1='4' x2='100' y2='4' stroke-width='"+ ( (lineWidth + 2) / 2 ) +"' stroke='#FFFFFF' "+ dottedSVGParam +" />";
  186. legend.append("<div><span style='float:left; display:bock;width:100px;height:" + lineWidth + "px;'><svg>" + lineSVG + "</svg></span>" + lineLabels[line].label + "</div>");
  187. }
  188. }
  189. }
  190. },
  191. _drawLine: function (el, scale, rows, columns, color, textClass, width, nodes, reverseMarkers, dotted) {
  192. var ctx = this._getCanvasLayer(el, false);
  193. ctx.beginPath();
  194. ctx.moveTo(nodes[0].x * scale, nodes[0].y * scale);
  195. var markers = [];
  196. var lineNodes = [];
  197. var node;
  198. for(node = 0; node < nodes.length; node++)
  199. {
  200. if (nodes[node].marker.indexOf("@") != 0)
  201. lineNodes[lineNodes.length] = nodes[node];
  202. }
  203. for (var lineNode = 0; lineNode < lineNodes.length; lineNode++) {
  204. if (lineNode < (lineNodes.length - 1)) {
  205. var nextNode = lineNodes[lineNode + 1];
  206. var currNode = lineNodes[lineNode];
  207. // Correction for edges so lines are not running off campus
  208. var xCorr = 0;
  209. var yCorr = 0;
  210. if (nextNode.x == 0) xCorr = width / 2;
  211. if (nextNode.x == columns) xCorr = -1 * width / 2;
  212. if (nextNode.y == 0) yCorr = width / 2;
  213. if (nextNode.y == rows) yCorr = -1 * width / 2;
  214. var xVal = 0;
  215. var yVal = 0;
  216. var direction = "";
  217. var xDiff = Math.round(Math.abs(currNode.x - nextNode.x));
  218. var yDiff = Math.round(Math.abs(currNode.y - nextNode.y));
  219. if ((xDiff == 0) || (yDiff == 0)) {
  220. // Horizontal or Vertical
  221. ctx.lineTo((nextNode.x * scale) + xCorr, (nextNode.y * scale) + yCorr);
  222. }
  223. else if ((xDiff == 1) && (yDiff == 1)) {
  224. // 90 degree turn
  225. if (nextNode.direction != "")
  226. direction = nextNode.direction.toLowerCase();
  227. switch (direction) {
  228. case "s": xVal = 0; yVal = scale; break;
  229. case "e": xVal = scale; yVal = 0; break;
  230. case "w": xVal = -1 * scale; yVal = 0; break;
  231. default: xVal = 0; yVal = -1 * scale; break;
  232. }
  233. ctx.quadraticCurveTo((currNode.x * scale) + xVal, (currNode.y * scale) + yVal,
  234. (nextNode.x * scale) + xCorr, (nextNode.y * scale) + yCorr);
  235. }
  236. else if (xDiff == yDiff) {
  237. // Symmetric, angular with curves at both ends
  238. if (nextNode.x < currNode.x) {
  239. if (nextNode.y < currNode.y)
  240. direction = "nw";
  241. else
  242. direction = "sw";
  243. }
  244. else {
  245. if (nextNode.y < currNode.y)
  246. direction = "ne";
  247. else
  248. direction = "se";
  249. }
  250. var dirVal = 1;
  251. switch (direction) {
  252. case "nw": xVal = -1 * (scale / 2); yVal = 1; dirVal = 1; break;
  253. case "sw": xVal = -1 * (scale / 2); yVal = -1; dirVal = 1; break;
  254. case "se": xVal = (scale / 2); yVal = -1; dirVal = -1; break;
  255. case "ne": xVal = (scale / 2); yVal = 1; dirVal = -1; break;
  256. }
  257. this._debug((currNode.x * scale) + xVal + ", " + (currNode.y * scale) + "; " + (nextNode.x + (dirVal * xDiff / 2)) * scale + ", " +
  258. (nextNode.y + (yVal * xDiff / 2)) * scale);
  259. ctx.bezierCurveTo(
  260. (currNode.x * scale) + xVal, (currNode.y * scale),
  261. (currNode.x * scale) + xVal, (currNode.y * scale),
  262. (nextNode.x + (dirVal * xDiff / 2)) * scale, (nextNode.y + (yVal * xDiff / 2)) * scale);
  263. ctx.bezierCurveTo(
  264. (nextNode.x * scale) + (dirVal * scale / 2), (nextNode.y) * scale,
  265. (nextNode.x * scale) + (dirVal * scale / 2), (nextNode.y) * scale,
  266. nextNode.x * scale, nextNode.y * scale);
  267. }
  268. else
  269. ctx.lineTo(nextNode.x * scale, nextNode.y * scale);
  270. }
  271. }
  272. if (dotted === true)
  273. ctx.setLineDash([5, 5]);
  274. ctx.strokeStyle = color;
  275. ctx.lineWidth = width;
  276. ctx.stroke();
  277. ctx = this._getCanvasLayer(el, true);
  278. for (node = 0; node < nodes.length; node++) {
  279. if (textClass != false)
  280. this._drawMarker(el, ctx, scale, color, textClass, width, nodes[node], reverseMarkers);
  281. }
  282. },
  283. _drawMarker: function (el, ctx, scale, color, textClass, width, data, reverseMarkers) {
  284. if (data.label == "") return;
  285. if (data.marker == "") data.marker = "station";
  286. // Scale coordinates for rendering
  287. var x = data.x * scale;
  288. var y = data.y * scale;
  289. // Keep it simple -- black on white, or white on black
  290. var fgColor = "#000000";
  291. var bgColor = "#ffffff";
  292. if (reverseMarkers)
  293. {
  294. fgColor = "#ffffff";
  295. bgColor = "#000000";
  296. }
  297. // Render station and interchange icons
  298. ctx.strokeStyle = fgColor;
  299. ctx.fillStyle = bgColor;
  300. ctx.beginPath();
  301. switch(data.marker.toLowerCase())
  302. {
  303. case "interchange":
  304. case "@interchange":
  305. ctx.lineWidth = width;
  306. if (data.markerInfo == "")
  307. ctx.arc(x, y, width * 0.7, 0, Math.PI * 2, true);
  308. else
  309. {
  310. var mDir = data.markerInfo.substr(0,1).toLowerCase();
  311. var mSize = parseInt(data.markerInfo.substr(1,10));
  312. if (((mDir == "v") || (mDir == "h")) && (mSize > 1))
  313. {
  314. if (mDir == "v")
  315. {
  316. ctx.arc(x, y, width * 0.7,290 * Math.PI/180, 250 * Math.PI/180, false);
  317. ctx.arc(x, y-(width*mSize), width * 0.7,110 * Math.PI/180, 70 * Math.PI/180, false);
  318. }
  319. else
  320. {
  321. ctx.arc(x, y, width * 0.7,20 * Math.PI/180, 340 * Math.PI/180, false);
  322. ctx.arc(x+(width*mSize), y, width * 0.7,200 * Math.PI/180, 160 * Math.PI/180, false);
  323. }
  324. }
  325. else
  326. ctx.arc(x, y, width * 0.7, 0, Math.PI * 2, true);
  327. }
  328. break;
  329. case "station":
  330. case "@station":
  331. ctx.lineWidth = width/2;
  332. ctx.arc(x, y, width/2, 0, Math.PI * 2, true);
  333. break;
  334. }
  335. ctx.closePath();
  336. ctx.stroke();
  337. ctx.fill();
  338. // Render text labels and hyperlinks
  339. var pos = "";
  340. var offset = width + 4;
  341. var topOffset = 0;
  342. var centerOffset = "-50px";
  343. switch(data.labelPos.toLowerCase())
  344. {
  345. case "n":
  346. pos = "text-align: center; margin: 0 0 " + offset + "px " + centerOffset;
  347. topOffset = offset * 2;
  348. break;
  349. case "w":
  350. pos = "text-align: right; margin:0 " + offset + "px 0 -" + (100 + offset) + "px";
  351. topOffset = offset;
  352. break;
  353. case "e":
  354. pos = "text-align: left; margin:0 0 0 " + offset + "px";
  355. topOffset = offset;
  356. break;
  357. case "s":
  358. pos = "text-align: center; margin:" + offset + "px 0 0 " + centerOffset;
  359. break;
  360. case "se":
  361. pos = "text-align: left; margin:" + offset + "px 0 0 " + offset + "px";
  362. break;
  363. case "ne":
  364. pos = "text-align: left; padding-left: " + offset + "px; margin: 0 0 " + offset + "px 0";
  365. topOffset = offset * 2;
  366. break;
  367. case "sw":
  368. pos = "text-align: right; margin:" + offset + "px 0 0 -" + (100 + offset) + "px";
  369. topOffset = offset;
  370. break;
  371. case "nw":
  372. pos = "text-align: right; margin: -" + offset + "px 0 0 -" + (100 + offset) + "px";
  373. topOffset = offset;
  374. break;
  375. }
  376. var style = (textClass != "" ? "class='" + textClass + "' " : "") + "style='" + (textClass == "" ? "font-size:8pt;font-family:Verdana,Arial,Helvetica,Sans Serif;text-decoration:none;" : "") + "width:100px;" + (pos != "" ? pos : "") + ";position:absolute;top:" + (y + el.position().top - (topOffset > 0 ? topOffset : 0)) + "px;left:" + (x + el.position().left) + "px;z-index:3000;'";
  377. if (data.link != "")
  378. $("<a " + style + " title='" + data.title.replace(/\\n/g,"<br />") + "' href='" + data.link + "' target='_new'>" + data.label.replace(/\\n/g,"<br />") + "</span>").appendTo(el);
  379. else
  380. $("<span " + style + ">" + data.label.replace(/\\n/g,"<br />") + "</span>").appendTo(el);
  381. },
  382. _drawGrid: function (el, scale, gridNumbers) {
  383. var ctx = this._getCanvasLayer(el, false);
  384. ctx.fillStyle = "#000";
  385. ctx.beginPath();
  386. var counter = 0;
  387. for (var x = 0.5; x < this.options.pixelWidth; x += scale) {
  388. if (gridNumbers)
  389. {
  390. ctx.moveTo(x, 0);
  391. ctx.fillText(counter++, x-15, 10);
  392. }
  393. ctx.moveTo(x, 0);
  394. ctx.lineTo(x, this.options.pixelHeight);
  395. }
  396. ctx.moveTo(this.options.pixelWidth - 0.5, 0);
  397. ctx.lineTo(this.options.pixelWidth - 0.5, this.options.pixelHeight);
  398. counter = 0;
  399. for (var y = 0.5; y < this.options.pixelHeight; y += scale) {
  400. if (gridNumbers)
  401. {
  402. ctx.moveTo(0, y);
  403. ctx.fillText(counter++, 0, y-15);
  404. }
  405. ctx.moveTo(0, y);
  406. ctx.lineTo(this.options.pixelWidth, y);
  407. }
  408. ctx.moveTo(0, this.options.pixelHeight - 0.5);
  409. ctx.lineTo(this.options.pixelWidth, this.options.pixelHeight - 0.5);
  410. ctx.strokeStyle = "#eee";
  411. ctx.lineWidth = 1;
  412. ctx.stroke();
  413. ctx.fill();
  414. ctx.closePath();
  415. }
  416. };
  417. var methods = {
  418. init: function (options) {
  419. plugin.options = $.extend({}, plugin.defaults, options);
  420. // iterate and reformat each matched element
  421. return this.each(function (index) {
  422. plugin.options = $.meta
  423. ? $.extend(plugin.options, $(this).data())
  424. : plugin.options;
  425. plugin._debug("BEGIN: " + plugin.identity() + " for element " + index);
  426. plugin._render($(this));
  427. plugin._debug("END: " + plugin.identity() + " for element " + index);
  428. });
  429. },
  430. drawLine: function (data) {
  431. plugin._drawLine(data.element, data.scale, data.rows, data.columns, data.color, data.width, data.nodes);
  432. }
  433. };
  434. $.fn.subwayMap = function (method) {
  435. // Method calling logic
  436. if (methods[method]) {
  437. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  438. } else if (typeof method === 'object' || !method) {
  439. return methods.init.apply(this, arguments);
  440. } else {
  441. $.error('Method ' + method + ' does not exist on jQuery.tooltip');
  442. }
  443. };
  444. })(jQuery);