/*
Copyright (c) 2010 Nik Kalyani nik@kalyani.com http://www.kalyani.com
Modified work Copyright (c) 2016 Jon Burrows subwaymap@jonburrows.co.uk https://jonburrows.co.uk
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
(function ($) {
var plugin = {
defaults: {
debug: false,
grid: false
},
options: {
},
identity: function (type) {
if (type === undefined) type = "name";
switch (type.toLowerCase()) {
case "version": return "1.0.0"; break;
default: return "subwayMap Plugin"; break;
}
},
_debug: function (s) {
if (this.options.debug)
this._log(s);
},
_log: function () {
if (window.console && window.console.log)
window.console.log('[subwayMap] ' + Array.prototype.join.call(arguments, ' '));
},
_supportsCanvas: function () {
var canvas = $("");
if (canvas[0].getContext)
return true;
else
return false;
},
_getCanvasLayer: function (el, overlay) {
this.layer++;
var canvas = $("");
el.append(canvas);
return (canvas[0].getContext("2d"));
},
_render: function (el) {
this.layer = -1;
var rows = el.attr("data-rows");
if (rows === undefined)
rows = 10;
else
rows = parseInt(rows);
var columns = el.attr("data-columns");
if (columns === undefined)
columns = 10;
else
columns = parseInt(columns);
var scale = el.attr("data-cellSize");
if (scale === undefined)
scale = 100;
else
scale = parseInt(scale);
var lineWidth = el.attr("data-lineWidth");
if (lineWidth === undefined)
lineWidth = 10;
else
lineWidth = parseInt(lineWidth);
var textClass = el.attr("data-textClass");
if (textClass === undefined) textClass = "";
var grid = el.attr("data-grid");
if ((grid === undefined) || (grid.toLowerCase() == "false"))
grid = false;
else
grid = true;
var legendId = el.attr("data-legendId");
if (legendId === undefined) legendId = "";
var gridNumbers = el.attr("data-gridNumbers");
if ((gridNumbers === undefined) || (gridNumbers.toLowerCase() == "false"))
gridNumbers = false;
else
gridNumbers = true;
var reverseMarkers = el.attr("data-reverseMarkers");
if ((reverseMarkers === undefined) || (reverseMarkers.toLowerCase() == "false"))
reverseMarkers = false;
else
reverseMarkers = true;
this.options.pixelWidth = columns * scale;
this.options.pixelHeight = rows * scale;
//el.css("width", this.options.pixelWidth);
//el.css("height", this.options.pixelHeight);
var self = this;
var lineLabels = [];
var supportsCanvas = $("")[0].getContext;
if (supportsCanvas) {
if (grid) this._drawGrid(el, scale, gridNumbers);
$(el).children("ul").each(function (index) {
var ul = $(this);
var color = $(ul).attr("data-color");
if (color === undefined) color = "#000000";
var outline = $(ul).attr("data-outline");
if (outline != undefined && ((outline === true) || (outline.toLowerCase() == "true")))
outline = true;
else
outline = false;
var dotted = $(ul).attr("data-dotted");
if (dotted != undefined && ((dotted === true) || (dotted.toLowerCase() == "true")))
dotted = true;
else
dotted = false;
var lineTextClass = $(ul).attr("data-textClass");
if (lineTextClass === undefined) lineTextClass = "";
var shiftCoords = $(ul).attr("data-shiftCoords");
if (shiftCoords === undefined) shiftCoords = "";
var shiftX = 0.00;
var shiftY = 0.00;
if (shiftCoords.indexOf(",") > -1) {
shiftX = parseInt(shiftCoords.split(",")[0]) * lineWidth/scale;
shiftY = parseInt(shiftCoords.split(",")[1]) * lineWidth/scale;
}
var lineLabel = $(ul).attr("data-label");
if (lineLabel === undefined)
lineLabel = "Line " + index;
lineLabels[lineLabels.length] = {label: lineLabel, color: color, outline: outline, dotted: dotted };
var nodes = [];
$(ul).children("li").each(function () {
var coords = $(this).attr("data-coords");
if (coords === undefined) coords = "";
var dir = $(this).attr("data-dir");
if (dir === undefined) dir = "";
var labelPos = $(this).attr("data-labelPos");
if (labelPos === undefined) labelPos = "s";
var marker = $(this).attr("data-marker");
if (marker == undefined) marker = "";
var markerInfo = $(this).attr("data-markerInfo");
if (markerInfo == undefined) markerInfo = "";
var anchor = $(this).children("a:first-child");
var label = $(this).text();
if (label === undefined) label = "";
var link = "";
var title = "";
if (anchor != undefined) {
link = $(anchor).attr("href");
if (link === undefined) link = "";
title = $(anchor).attr("title");
if (title === undefined) title = "";
}
self._debug("Coords=" + coords + "; Dir=" + dir + "; Link=" + link + "; Label=" + label + "; labelPos=" + labelPos + "; Marker=" + marker);
var x = "";
var y = "";
if (coords.indexOf(",") > -1) {
x = Number(coords.split(",")[0]) + (marker.indexOf("interchange") > -1 ? 0 : shiftX);
y = Number(coords.split(",")[1]) + (marker.indexOf("interchange") > -1 ? 0 : shiftY);
}
nodes[nodes.length] = { x: x, y: y, direction: dir, marker: marker, markerInfo: markerInfo, link: link, title: title, label: label, labelPos: labelPos };
});
if (nodes.length > 0) {
self._drawLine(el, scale, rows, columns, color, (lineTextClass != "" ? lineTextClass : textClass), lineWidth, nodes, reverseMarkers, dotted);
if (outline === true)
self._drawLine(el, scale, rows, columns, '#FFFFFF', false, lineWidth - 2, nodes, reverseMarkers, dotted);
}
$(ul).remove();
});
if ((lineLabels.length > 0) && (legendId != ""))
{
var legend = $("#" + legendId);
for(var line=0; line