-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsubway.min.js
25 lines (23 loc) · 11.4 KB
/
subway.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
* jQuery throttle / debounce - v1.1 - 3/7/2010
* http://benalman.com/projects/jquery-throttle-debounce-plugin/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
/*
* ScaleRaphael 0.8 by Zevan Rosser 2010
* For use with Raphael library : www.raphaeljs.com
* Licensed under the MIT license.
*
* www.shapevent.com/scaleraphael/
*/
/*
* OLsubwaymap
* Licensed under the MIT License (MIT). (http://opensource.org/licenses/MIT)
* Copyright © 2013 openlectures LLP (http://openlectures.org/).
*
* https://github.com/openlectures/OLsubway.js
*/
(function(e,t){var n=e.jQuery||e.Cowboy||(e.Cowboy={}),r;n.throttle=r=function(e,r,i,s){function a(){function p(){u=+(new Date);i.apply(n,l)}function v(){o=t}var n=this,a=+(new Date)-u,l=arguments;if(s&&!o){p()}o&&clearTimeout(o);if(s===t&&a>e){p()}else{if(r!==true){o=setTimeout(s?v:p,s===t?e-a:e)}}}var o,u=0;if(typeof r!=="boolean"){s=i;i=r;r=t}if(n.guid){a.guid=i.guid=i.guid||n.guid++}return a};n.debounce=function(e,n,i){return i===t?r(e,n,false):r(e,i,n!==false)}})(this);(function(){window.ScaleRaphael=function(e,t,n){var r=document.getElementById(e);if(!r.style.position)r.style.position="relative";r.style.width=t+"px";r.style.height=n+"px";r.style.overflow="hidden";var i;if(Raphael.type==="VML"){r.innerHTML="<rvml:group style='position : absolute; width: 1000px; height: 1000px; top: 0px; left: 0px' coordsize='1000,1000' class='rvml' id='vmlgroup'></rvml:group>";i=r.getElementsByTagName("rvml:group")[0]}else{r.innerHTML="<div id='svggroup'></div>";i=r.getElementsByTagName("div")[0]}var s=new Raphael(i,t,n);var o;if(Raphael.type==="SVG"){s.canvas.setAttribute("viewBox","0 0 "+t+" "+n)}else{o=r.getElementsByTagName("div")[0]}s.changeSize=function(e,u,a,f){f=!f;var l=e/t;var c=u/n;var h=l<c?l:c;var p=parseInt(n*h);var d=parseInt(t*h);if(Raphael.type==="VML"){var v=r.getElementsByTagName("textpath");for(var m in v){var g=v[m];if(g.style){if(!g._fontSize){var y=g.style.font.split("px");g._fontSize=parseInt(y[0]);g._font=y[1]}g.style.font=g._fontSize*h+"px"+g._font}}var b;if(d<p){b=d*1e3/t}else{b=p*1e3/n}b=parseInt(b);i.style.width=b+"px";i.style.height=b+"px";if(f){i.style.left=parseInt((e-d)/2)+"px";i.style.top=parseInt((u-p)/2)+"px"}o.style.overflow="visible"}if(f){d=e;p=u}r.style.width=d+"px";r.style.height=p+"px";s.setSize(d,p);if(a){r.style.position="absolute";r.style.left=parseInt((e-d)/2)+"px";r.style.top=parseInt((u-p)/2)+"px"}};s.scaleAll=function(e){s.changeSize(t*e,n*e)};s.changeSize(t,n);s.w=t;s.h=n;return s}})();(function(e){"use strict";function m(e,t){this.x=e;this.y=t}function g(e,t,n,r,i){this.name=e;this.color=t;this.fontSize=n;this.fontColor=r;this.sharp=i;this.edges=[]}function y(e,t){m.call(this,e.x,e.y);this.pt=t}function b(e,t){this.color=e;this.segments=new Array(E(t))}function w(e,t,n,r,i,s){this.name=t;this.href=n;this.labelDir=r;this.labelTer=i;this.terminals=[];this.links=s;this.ID=e;this.elements=[[],[],[]]}function E(e){return new m(e.x*t,e.y*t)}function S(e,n){var r=e.x,i=e.y;var s=0;if(e.x-n.x>0){r-=a*t}else if(e.x-n.x<0){r+=a*t;s=180}if(e.y-n.y>0){i-=a*t;s=90}else if(e.y-n.y<0){i+=a*t;s=270}return[new m(r,i),s]}function x(e,t){var n=t.set();for(var r=0;r<e.length;r++){n.push(e[r])}return n}var t=20;var n=.45;var r=.4;var i=.55;var s=.1;var o=14;var u="#bbb";var a=.7;var f=40;var l="M0,0 l0,0.5 0.6,-0.5 -0.6,-0.5z";var c=["#000","#eee"];var h=["#03f","#709"];var p=2*r*i;var d=r-s;var v=p-s*2.4;if(typeof Object.create==="undefined"){Object.create=function(e){function t(){}t.prototype=e;return new t}}m.prototype.toString=function(){return this.x+","+this.y};g.prototype.addEdge=function(e){e=E(e);this.edges.push(e)};g.prototype.centroid=function(){if(this.edges.length<1){throw new Error("Polygon has no points!")}this.edges.push(this.edges[0]);var e=0,t=0,n=0;for(var r=0;r<this.edges.length-1;r++){e+=(this.edges[r].x+this.edges[r+1].x)*(this.edges[r].x*this.edges[r+1].y-this.edges[r+1].x*this.edges[r].y);t+=(this.edges[r].y+this.edges[r+1].y)*(this.edges[r].x*this.edges[r+1].y-this.edges[r+1].x*this.edges[r].y);n+=this.edges[r].x*this.edges[r+1].y-this.edges[r+1].x*this.edges[r].y}this.edges.pop();return new m(1/(3*n)*e,1/(3*n)*t)};g.prototype.paint=function(e){if(this.edges.length>2){var t="M"+this.edges[0].toString()+" ";if(typeof this.sharp==="undefined"||!this.sharp){t+="R"}for(var n=1;n<this.edges.length;n++){t+=this.edges[n].toString()+" "}this.background=e.path(t+"z").attr({fill:this.color,stroke:"none"});var r=this.centroid();e.text(r.x,r.y,this.name).attr({"font-size":this.fontSize,fill:this.fontColor})}};y.prototype=Object.create(m.prototype);y.prototype.toString=function(){return this.pt+" "+m.prototype.toString.call(this)};b.prototype.addSegment=function(e,t){e=E(e);if(typeof t==="undefined"){this.segments.push(e)}else if(t.toUpperCase()==="N"||t.toUpperCase()==="S"){this.segments.push(new y(e,new m(e.x,this.segments[this.segments.length-1].y)))}else if(t==="E"||t==="W"){this.segments.push(new y(e,new m(this.segments[this.segments.length-1].x,e.y)))}};b.prototype.paint=function(e){this.arrowHead(e);var r="M"+this.segments[0]+" ";for(var i=1;i<this.segments.length;i++){if(this.segments[i]instanceof y){r+="Q"+this.segments[i]+" "}else{r+="L"+this.segments[i]+" "}}e.path(r).attr({stroke:this.color,"stroke-width":n*t})};b.prototype.arrowHead=function(e){if(this.segments.length>1){var n=this.segments.pop();var r;if(n instanceof y){r=S(n,n.pt);n=new y(r[0],n.pt)}else{r=S(n,this.segments[this.segments.length-1]);n=new m(r[0].x,r[0].y)}this.segments.push(n);e.path(l).attr({fill:this.color,stroke:"none"}).transform("T"+n.x+","+n.y+"S"+t+"R"+r[1])}};w.prototype.addTerminal=function(e){this.terminals.push(E(e))};w.prototype.linkGlow=function(){this.elements[2].show()};w.prototype.linkUnGlow=function(){this.elements[2].hide()};w.prototype.paint=function(e){var n=0;var i;for(var s=0;s<this.terminals.length;s++){i=e.circle(this.terminals[s].x,this.terminals[s].y,r*t).attr("fill",c[0]);this.elements[0].push(i);this.elements[1].push(i.glow({width:t/2,color:h[0]}));this.elements[2].push(i.glow({width:t/2,color:h[1]}));if(n!==0){i=e.path("M"+n+" L"+this.terminals[s]).attr({stroke:c[0],"stroke-width":t*p});this.elements[0].push(i);this.elements[1].push(i.glow({width:t*p/2*1.8,opacity:.8,color:h[0]}));this.elements[2].push(i.glow({width:t*p/2*1.8,opacity:.8,color:h[1]}))}n=this.terminals[s]}n=0;for(s=0;s<this.terminals.length;s++){i=e.circle(this.terminals[s].x,this.terminals[s].y,t*d).attr("fill",c[1]);this.elements[0].push(i);if(n!==0){i=e.path("M"+n+" L"+this.terminals[s]).attr({stroke:c[1],"stroke-width":t*v});this.elements[0].push(i)}n=this.terminals[s]}for(s=0;s<this.terminals.length;s++){this.elements[0].push(e.text(this.terminals[s].x,this.terminals[s].y,this.ID+1).attr({"font-size":d*t*1.8,"font-weight":"bolder"}))}var o=this.printLabel(e);this.elements[0].push(o);for(s=0;s<this.elements.length;s++){this.elements[s]=x(this.elements[s],e)}this.elements[1].hide();this.elements[2].hide();var u=this.elements[1];var a=this.links;this.elements[0].mouseover(function(){u.show();o.attr("font-weight","bolder");for(var e in a){if(typeof a[e]!=="undefined"){a[e].linkGlow()}}}).mouseout(function(){u.hide();o.attr("font-weight","normal");for(var e in a){if(typeof a[e]!=="undefined"){a[e].linkUnGlow()}}});this.elements[0].attr("href",this.href)};w.prototype.printLabel=function(e){if(typeof this.labelTer==="undefined"){this.labelTer=1}if(typeof this.labelDir==="undefined"){this.labelDir="S"}if(this.labelTer<0||this.terminals.length<this.labelTer){throw new Error("Invalid terminal number!")}var n=this.terminals[this.labelTer-1].x,r=this.terminals[this.labelTer-1].y;var i="middle";switch(this.labelDir.toUpperCase()){case"N":r-=t;break;case"NW":case"WN":n-=t/2;r-=t/2;i="end";break;case"W":n-=t/2;i="end";break;case"SW":case"WS":n-=t/2;r+=t/2;i="end";break;case"S":r+=t;break;case"SE":case"ES":n+=t/2;r+=t/2;i="start";break;case"E":n+=t/2;i="start";break;case"NE":case"EN":n+=t/2;r-=t/2;i="start";break}var s=e.text(n,r,this.name).attr({"text-anchor":i,"font-size":o});return s};e.OLSubway=function(){this.paintQueue=[];this.stations=[];this.islands=[];this.boundBox=new m(0,0);this.display="subway";this.data=undefined};e.OLSubway.prototype.reOrderStations=function(){for(var e=2;e>=0;e--){for(var t=0;t<this.stations.length;t++){this.stations[t].elements[e].toFront()}}};e.OLSubway.prototype.maxCoord=function(e){this.boundBox.x=Math.max(this.boundBox.x,Math.floor(e.x));this.boundBox.y=Math.max(this.boundBox.y,Math.floor(e.y))};e.OLSubway.prototype.canvasResize=function(){var e=$("#"+this.display).parent();this.paper.changeSize(e.width(),e.width(),false,true)};e.OLSubway.prototype.create=function(e,t){if(typeof e==="undefined"){return}if(typeof t==="undefined"){t=e}if(document.readyState==="complete"){this.createWrapped(e,t)}else{var n=this;var r=function(){n.createWrapped(e,t)};if(window.addEventListener){window.addEventListener("load",r)}else if(window.attachEvent){window.attachEvent("onload",r)}}};e.OLSubway.prototype.createWrapped=function(e,n){this.display=e;this.data="#"+n;var r=$(this.data);var i=this;$(".subway-islands",r).each(function(e,t){var n=new g($(t).data("island-name"),$(t).data("background-color"),$(t).data("font-size"),$(t).data("font-color"),$(t).data("sharp"));$(t).children().each(function(e,t){var r=$(t).data("edge").split(",");var s=new m(parseFloat(r[0]),parseFloat(r[1]));n.addEdge(s);i.maxCoord(s)});i.islands.push(n);i.paintQueue.push(n)});$(".subway-tracks",r).each(function(e,t){var n=$(t).data("start-point").split(",");var r=new m(parseInt(n[0],10),parseInt(n[1],10));var s=new b($(t).data("color"),r);i.maxCoord(r);$(t).children().each(function(e,t){var n=$(t).data("dest").split(",");r=new m(parseInt(n[0],10),parseInt(n[1],10));s.addSegment(r,$(t).data("turn"));i.maxCoord(r)});i.paintQueue.push(s)});$("#subway-stations",r).children().each(function(e,t){var n=$(t).text().replace(/\\n/g,"\n");var r=$(t).children("a").first().attr("href");var s=$(t).data("label-dir");var o=$(t).data("label-ter");var u=$(t).data("link");var a;if(typeof u!=="undefined"){u=u.toString().split(",");for(a=0;a<u.length;a++){u[a]=parseInt(u[a]-1,10)}}var f=new w(i.stations.length,n,r,s,o,u);var l=$(t).data("pos").split(/[,;]/);for(a=1;a<=l.length;a+=2){var c=new m(parseInt(l[a-1],10),parseInt(l[a],10));f.addTerminal(c);i.maxCoord(c)}i.stations.push(f);i.paintQueue.push(f)});for(var s=0;s<this.stations.length;s++){var o=this.stations[s];for(var a=0;typeof o.links!=="undefined"&&a<o.links.length;a++){if(s===o.links[a]){o.links[a]=undefined}else{o.links[a]=this.stations[o.links[a]]}}}var l=this.boundBox.x+1,c=this.boundBox.y+1;this.paper=new ScaleRaphael(this.display,l*t,c*t);for(s=0;s<this.paintQueue.length;s++){this.paintQueue[s].paint(this.paper)}this.reOrderStations();if($(this.data).data("debug")){for(s=0;s<=l;s++){this.paper.path("M"+s*t+", 0 L"+s*t+", "+c*t).attr({stroke:u,"stroke-width":2}).toBack()}for(s=0;s<=c;s++){this.paper.path("M0, "+s*t+" L"+l*t+", "+s*t).attr({stroke:u,"stroke-width":2}).toBack()}for(s=0;s<this.islands.length;s++){this.islands[s].background.toBack()}}this.canvasResize();var h;if(typeof $.debounce==="undefined"){h=Cowboy}else{h=$}var p=window.onresize;var d=h.debounce(f,function(){i.canvasResize()});window.onresize=function(){d();if(typeof p==="function"){p()}};if("#"+this.display!==this.data){$(this.data).hide()}};e.OLSubway.prototype.destroy=function(){this.paper.remove();this.paintQueue=[];this.stations=[];this.islands=[];this.boundBox=new m(0,0);this.display="subway";this.data=undefined}})(this)