dashing-beanstalk 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +9 -0
  3. data/bin/dashing +124 -0
  4. data/javascripts/batman.jquery.js +163 -0
  5. data/javascripts/batman.js +13680 -0
  6. data/javascripts/dashing.coffee +125 -0
  7. data/javascripts/es5-shim.js +1021 -0
  8. data/javascripts/jquery.js +4 -0
  9. data/lib/dashing.rb +160 -0
  10. data/lib/scheduler.rb +58 -0
  11. data/templates/dashboard/%name%.erb.tt +7 -0
  12. data/templates/job/%name%.rb +4 -0
  13. data/templates/project/.gitignore +2 -0
  14. data/templates/project/Gemfile +6 -0
  15. data/templates/project/README.md +1 -0
  16. data/templates/project/assets/fonts/fontawesome-webfont.eot +0 -0
  17. data/templates/project/assets/fonts/fontawesome-webfont.svg +399 -0
  18. data/templates/project/assets/fonts/fontawesome-webfont.ttf +0 -0
  19. data/templates/project/assets/fonts/fontawesome-webfont.woff +0 -0
  20. data/templates/project/assets/images/logo.png +0 -0
  21. data/templates/project/assets/javascripts/application.coffee +25 -0
  22. data/templates/project/assets/javascripts/d3-3.2.8.js +5 -0
  23. data/templates/project/assets/javascripts/dashing.gridster.coffee +37 -0
  24. data/templates/project/assets/javascripts/gridster/jquery.gridster.js +3263 -0
  25. data/templates/project/assets/javascripts/gridster/jquery.leanModal.min.js +5 -0
  26. data/templates/project/assets/javascripts/jquery.knob.js +646 -0
  27. data/templates/project/assets/javascripts/rickshaw-1.4.3.min.js +2 -0
  28. data/templates/project/assets/stylesheets/application.scss +257 -0
  29. data/templates/project/assets/stylesheets/font-awesome.css +1479 -0
  30. data/templates/project/assets/stylesheets/jquery.gridster.css +57 -0
  31. data/templates/project/config.ru +18 -0
  32. data/templates/project/dashboards/layout.erb +32 -0
  33. data/templates/project/dashboards/sample.erb +25 -0
  34. data/templates/project/dashboards/sampletv.erb +56 -0
  35. data/templates/project/jobs/buzzwords.rb +9 -0
  36. data/templates/project/jobs/convergence.rb +14 -0
  37. data/templates/project/jobs/sample.rb +13 -0
  38. data/templates/project/jobs/twitter.rb +28 -0
  39. data/templates/project/lib/.empty_directory +1 -0
  40. data/templates/project/public/404.html +26 -0
  41. data/templates/project/public/favicon.ico +0 -0
  42. data/templates/project/widgets/clock/clock.coffee +18 -0
  43. data/templates/project/widgets/clock/clock.html +2 -0
  44. data/templates/project/widgets/clock/clock.scss +13 -0
  45. data/templates/project/widgets/comments/comments.coffee +24 -0
  46. data/templates/project/widgets/comments/comments.html +7 -0
  47. data/templates/project/widgets/comments/comments.scss +33 -0
  48. data/templates/project/widgets/graph/graph.coffee +36 -0
  49. data/templates/project/widgets/graph/graph.html +5 -0
  50. data/templates/project/widgets/graph/graph.scss +65 -0
  51. data/templates/project/widgets/iframe/iframe.coffee +9 -0
  52. data/templates/project/widgets/iframe/iframe.html +1 -0
  53. data/templates/project/widgets/iframe/iframe.scss +8 -0
  54. data/templates/project/widgets/image/image.coffee +9 -0
  55. data/templates/project/widgets/image/image.html +1 -0
  56. data/templates/project/widgets/image/image.scss +13 -0
  57. data/templates/project/widgets/list/list.coffee +6 -0
  58. data/templates/project/widgets/list/list.html +18 -0
  59. data/templates/project/widgets/list/list.scss +60 -0
  60. data/templates/project/widgets/meter/meter.coffee +14 -0
  61. data/templates/project/widgets/meter/meter.html +7 -0
  62. data/templates/project/widgets/meter/meter.scss +35 -0
  63. data/templates/project/widgets/number/number.coffee +24 -0
  64. data/templates/project/widgets/number/number.html +11 -0
  65. data/templates/project/widgets/number/number.scss +39 -0
  66. data/templates/project/widgets/text/text.coffee +1 -0
  67. data/templates/project/widgets/text/text.html +7 -0
  68. data/templates/project/widgets/text/text.scss +32 -0
  69. data/templates/widget/%name%/%name%.coffee.tt +9 -0
  70. data/templates/widget/%name%/%name%.html +1 -0
  71. data/templates/widget/%name%/%name%.scss.tt +3 -0
  72. metadata +272 -0
@@ -0,0 +1,25 @@
1
+ # dashing.js is located in the dashing framework
2
+ # It includes jquery & batman for you.
3
+ #= require dashing.js
4
+
5
+ #= require_directory .
6
+ #= require_tree ../../widgets
7
+
8
+ console.log("Yeah! The dashboard has started!")
9
+
10
+ Dashing.on 'ready', ->
11
+ Dashing.widget_margins ||= [5, 5]
12
+ Dashing.widget_base_dimensions ||= [300, 360]
13
+ Dashing.numColumns ||= 4
14
+
15
+ contentWidth = (Dashing.widget_base_dimensions[0] + Dashing.widget_margins[0] * 2) * Dashing.numColumns
16
+
17
+ Batman.setImmediate ->
18
+ $('.gridster').width(contentWidth)
19
+ $('.gridster ul:first').gridster
20
+ widget_margins: Dashing.widget_margins
21
+ widget_base_dimensions: Dashing.widget_base_dimensions
22
+ avoid_overlapped_widgets: !Dashing.customGridsterLayout
23
+ draggable:
24
+ stop: Dashing.showGridsterInstructions
25
+ start: -> Dashing.currentWidgetPositions = Dashing.getWidgetPositions()
@@ -0,0 +1,5 @@
1
+ d3=function(){function n(n){return null!=n&&!isNaN(n)}function t(n){return n.length}function e(n){for(var t=1;n*t%1;)t*=10;return t}function r(n,t){try{for(var e in t)Object.defineProperty(n.prototype,e,{value:t[e],enumerable:!1})}catch(r){n.prototype=t}}function i(){}function u(){}function a(n,t,e){return function(){var r=e.apply(t,arguments);return r===t?n:r}}function o(n,t){if(t in n)return t;t=t.charAt(0).toUpperCase()+t.substring(1);for(var e=0,r=Ca.length;r>e;++e){var i=Ca[e]+t;if(i in n)return i}}function c(n){for(var t=n.length,e=new Array(t);t--;)e[t]=n[t];return e}function l(n){return Array.prototype.slice.call(n)}function s(){}function f(){}function h(n){function t(){for(var t,r=e,i=-1,u=r.length;++i<u;)(t=r[i].on)&&t.apply(this,arguments);return n}var e=[],r=new i;return t.on=function(t,i){var u,a=r.get(t);return arguments.length<2?a&&a.on:(a&&(a.on=null,e=e.slice(0,u=e.indexOf(a)).concat(e.slice(u+1)),r.remove(t)),i&&e.push(r.set(t,{on:i})),n)},t}function g(){ya.event.preventDefault()}function p(){for(var n,t=ya.event;n=t.sourceEvent;)t=n;return t}function m(n){for(var t=new f,e=0,r=arguments.length;++e<r;)t[arguments[e]]=h(t);return t.of=function(e,r){return function(i){try{var u=i.sourceEvent=ya.event;i.target=n,ya.event=i,t[i.type].apply(e,r)}finally{ya.event=u}}},t}function d(n){return La(n,Ya),n}function v(n){return"function"==typeof n?n:function(){return Ha(n,this)}}function y(n){return"function"==typeof n?n:function(){return Fa(n,this)}}function M(n,t){function e(){this.removeAttribute(n)}function r(){this.removeAttributeNS(n.space,n.local)}function i(){this.setAttribute(n,t)}function u(){this.setAttributeNS(n.space,n.local,t)}function a(){var e=t.apply(this,arguments);null==e?this.removeAttribute(n):this.setAttribute(n,e)}function o(){var e=t.apply(this,arguments);null==e?this.removeAttributeNS(n.space,n.local):this.setAttributeNS(n.space,n.local,e)}return n=ya.ns.qualify(n),null==t?n.local?r:e:"function"==typeof t?n.local?o:a:n.local?u:i}function x(n){return n.trim().replace(/\s+/g," ")}function b(n){return new RegExp("(?:^|\\s+)"+ya.requote(n)+"(?:\\s+|$)","g")}function _(n,t){function e(){for(var e=-1;++e<i;)n[e](this,t)}function r(){for(var e=-1,r=t.apply(this,arguments);++e<i;)n[e](this,r)}n=n.trim().split(/\s+/).map(w);var i=n.length;return"function"==typeof t?r:e}function w(n){var t=b(n);return function(e,r){if(i=e.classList)return r?i.add(n):i.remove(n);var i=e.getAttribute("class")||"";r?(t.lastIndex=0,t.test(i)||e.setAttribute("class",x(i+" "+n))):e.setAttribute("class",x(i.replace(t," ")))}}function S(n,t,e){function r(){this.style.removeProperty(n)}function i(){this.style.setProperty(n,t,e)}function u(){var r=t.apply(this,arguments);null==r?this.style.removeProperty(n):this.style.setProperty(n,r,e)}return null==t?r:"function"==typeof t?u:i}function E(n,t){function e(){delete this[n]}function r(){this[n]=t}function i(){var e=t.apply(this,arguments);null==e?delete this[n]:this[n]=e}return null==t?e:"function"==typeof t?i:r}function k(n){return"function"==typeof n?n:(n=ya.ns.qualify(n)).local?function(){return Ma.createElementNS(n.space,n.local)}:function(){return Ma.createElementNS(this.namespaceURI,n)}}function A(n){return{__data__:n}}function N(n){return function(){return Oa(this,n)}}function q(n){return arguments.length||(n=ya.ascending),function(t,e){return t&&e?n(t.__data__,e.__data__):!t-!e}}function T(n,t){for(var e=0,r=n.length;r>e;e++)for(var i,u=n[e],a=0,o=u.length;o>a;a++)(i=u[a])&&t(i,a,e);return n}function C(n){return La(n,Ua),n}function z(n){var t,e;return function(r,i,u){var a,o=n[u].update,c=o.length;for(u!=e&&(e=u,t=0),i>=t&&(t=i+1);!(a=o[t])&&++t<c;);return a}}function D(n,t,e){function r(){var t=this[a];t&&(this.removeEventListener(n,t,t.$),delete this[a])}function i(){var i=c(t,za(arguments));r.call(this),this.addEventListener(n,this[a]=i,i.$=e),i._=t}function u(){var t,e=new RegExp("^__on([^.]+)"+ya.requote(n)+"$");for(var r in this)if(t=r.match(e)){var i=this[r];this.removeEventListener(t[1],i,i.$),delete this[r]}}var a="__on"+n,o=n.indexOf("."),c=j;o>0&&(n=n.substring(0,o));var l=Va.get(n);return l&&(n=l,c=L),o?t?i:r:t?s:u}function j(n,t){return function(e){var r=ya.event;ya.event=e,t[0]=this.__data__;try{n.apply(this,t)}finally{ya.event=r}}}function L(n,t){var e=j(n,t);return function(n){var t=this,r=n.relatedTarget;r&&(r===t||8&r.compareDocumentPosition(t))||e.call(t,n)}}function H(){var n=".dragsuppress-"+ ++Za,t="touchmove"+n,e="selectstart"+n,r="dragstart"+n,i="click"+n,u=ya.select(ba).on(t,g).on(e,g).on(r,g),a=xa.style,o=a[Xa];return a[Xa]="none",function(t){function e(){u.on(i,null)}u.on(n,null),a[Xa]=o,t&&(u.on(i,function(){g(),e()},!0),setTimeout(e,0))}}function F(n,t){var e=n.ownerSVGElement||n;if(e.createSVGPoint){var r=e.createSVGPoint();if(0>Ba&&(ba.scrollX||ba.scrollY)){e=ya.select("body").append("svg").style({position:"absolute",top:0,left:0,margin:0,padding:0,border:"none"},"important");var i=e[0][0].getScreenCTM();Ba=!(i.f||i.e),e.remove()}return Ba?(r.x=t.pageX,r.y=t.pageY):(r.x=t.clientX,r.y=t.clientY),r=r.matrixTransform(n.getScreenCTM().inverse()),[r.x,r.y]}var u=n.getBoundingClientRect();return[t.clientX-u.left-n.clientLeft,t.clientY-u.top-n.clientTop]}function P(){}function O(n,t,e){return new Y(n,t,e)}function Y(n,t,e){this.h=n,this.s=t,this.l=e}function R(n,t,e){function r(n){return n>360?n-=360:0>n&&(n+=360),60>n?u+(a-u)*n/60:180>n?a:240>n?u+(a-u)*(240-n)/60:u}function i(n){return Math.round(255*r(n))}var u,a;return n=isNaN(n)?0:(n%=360)<0?n+360:n,t=isNaN(t)?0:0>t?0:t>1?1:t,e=0>e?0:e>1?1:e,a=.5>=e?e*(1+t):e+t-e*t,u=2*e-a,at(i(n+120),i(n),i(n-120))}function U(n){return n>0?1:0>n?-1:0}function I(n){return n>1?0:-1>n?Ka:Math.acos(n)}function V(n){return n>1?Ka/2:-1>n?-Ka/2:Math.asin(n)}function X(n){return(Math.exp(n)-Math.exp(-n))/2}function Z(n){return(Math.exp(n)+Math.exp(-n))/2}function B(n){return(n=Math.sin(n/2))*n}function $(n,t,e){return new W(n,t,e)}function W(n,t,e){this.h=n,this.c=t,this.l=e}function J(n,t,e){return isNaN(n)&&(n=0),isNaN(t)&&(t=0),G(e,Math.cos(n*=to)*t,Math.sin(n)*t)}function G(n,t,e){return new K(n,t,e)}function K(n,t,e){this.l=n,this.a=t,this.b=e}function Q(n,t,e){var r=(n+16)/116,i=r+t/500,u=r-e/200;return i=tt(i)*uo,r=tt(r)*ao,u=tt(u)*oo,at(rt(3.2404542*i-1.5371385*r-.4985314*u),rt(-.969266*i+1.8760108*r+.041556*u),rt(.0556434*i-.2040259*r+1.0572252*u))}function nt(n,t,e){return n>0?$(Math.atan2(e,t)*eo,Math.sqrt(t*t+e*e),n):$(0/0,0/0,n)}function tt(n){return n>.206893034?n*n*n:(n-4/29)/7.787037}function et(n){return n>.008856?Math.pow(n,1/3):7.787037*n+4/29}function rt(n){return Math.round(255*(.00304>=n?12.92*n:1.055*Math.pow(n,1/2.4)-.055))}function it(n){return at(n>>16,255&n>>8,255&n)}function ut(n){return it(n)+""}function at(n,t,e){return new ot(n,t,e)}function ot(n,t,e){this.r=n,this.g=t,this.b=e}function ct(n){return 16>n?"0"+Math.max(0,n).toString(16):Math.min(255,n).toString(16)}function lt(n,t,e){var r,i,u,a=0,o=0,c=0;if(r=/([a-z]+)\((.*)\)/i.exec(n))switch(i=r[2].split(","),r[1]){case"hsl":return e(parseFloat(i[0]),parseFloat(i[1])/100,parseFloat(i[2])/100);case"rgb":return t(gt(i[0]),gt(i[1]),gt(i[2]))}return(u=so.get(n))?t(u.r,u.g,u.b):(null!=n&&"#"===n.charAt(0)&&(4===n.length?(a=n.charAt(1),a+=a,o=n.charAt(2),o+=o,c=n.charAt(3),c+=c):7===n.length&&(a=n.substring(1,3),o=n.substring(3,5),c=n.substring(5,7)),a=parseInt(a,16),o=parseInt(o,16),c=parseInt(c,16)),t(a,o,c))}function st(n,t,e){var r,i,u=Math.min(n/=255,t/=255,e/=255),a=Math.max(n,t,e),o=a-u,c=(a+u)/2;return o?(i=.5>c?o/(a+u):o/(2-a-u),r=n==a?(t-e)/o+(e>t?6:0):t==a?(e-n)/o+2:(n-t)/o+4,r*=60):(r=0/0,i=c>0&&1>c?0:r),O(r,i,c)}function ft(n,t,e){n=ht(n),t=ht(t),e=ht(e);var r=et((.4124564*n+.3575761*t+.1804375*e)/uo),i=et((.2126729*n+.7151522*t+.072175*e)/ao),u=et((.0193339*n+.119192*t+.9503041*e)/oo);return G(116*i-16,500*(r-i),200*(i-u))}function ht(n){return(n/=255)<=.04045?n/12.92:Math.pow((n+.055)/1.055,2.4)}function gt(n){var t=parseFloat(n);return"%"===n.charAt(n.length-1)?Math.round(2.55*t):t}function pt(n){return"function"==typeof n?n:function(){return n}}function mt(n){return n}function dt(n){return function(t,e,r){return 2===arguments.length&&"function"==typeof e&&(r=e,e=null),vt(t,e,n,r)}}function vt(n,t,e,r){function i(){var n,t=c.status;if(!t&&c.responseText||t>=200&&300>t||304===t){try{n=e.call(u,c)}catch(r){return a.error.call(u,r),void 0}a.load.call(u,n)}else a.error.call(u,c)}var u={},a=ya.dispatch("progress","load","error"),o={},c=new XMLHttpRequest,l=null;return!ba.XDomainRequest||"withCredentials"in c||!/^(http(s)?:)?\/\//.test(n)||(c=new XDomainRequest),"onload"in c?c.onload=c.onerror=i:c.onreadystatechange=function(){c.readyState>3&&i()},c.onprogress=function(n){var t=ya.event;ya.event=n;try{a.progress.call(u,c)}finally{ya.event=t}},u.header=function(n,t){return n=(n+"").toLowerCase(),arguments.length<2?o[n]:(null==t?delete o[n]:o[n]=t+"",u)},u.mimeType=function(n){return arguments.length?(t=null==n?null:n+"",u):t},u.responseType=function(n){return arguments.length?(l=n,u):l},u.response=function(n){return e=n,u},["get","post"].forEach(function(n){u[n]=function(){return u.send.apply(u,[n].concat(za(arguments)))}}),u.send=function(e,r,i){if(2===arguments.length&&"function"==typeof r&&(i=r,r=null),c.open(e,n,!0),null==t||"accept"in o||(o.accept=t+",*/*"),c.setRequestHeader)for(var a in o)c.setRequestHeader(a,o[a]);return null!=t&&c.overrideMimeType&&c.overrideMimeType(t),null!=l&&(c.responseType=l),null!=i&&u.on("error",i).on("load",function(n){i(null,n)}),c.send(null==r?null:r),u},u.abort=function(){return c.abort(),u},ya.rebind(u,a,"on"),null==r?u:u.get(yt(r))}function yt(n){return 1===n.length?function(t,e){n(null==t?e:null)}:n}function Mt(){var n=bt(),t=_t()-n;t>24?(isFinite(t)&&(clearTimeout(po),po=setTimeout(Mt,t)),go=0):(go=1,vo(Mt))}function xt(n,t,e){var r=arguments.length;2>r&&(t=0),3>r&&(e=Date.now()),mo.callback=n,mo.time=e+t}function bt(){var n=Date.now();for(mo=fo;mo;)n>=mo.time&&(mo.flush=mo.callback(n-mo.time)),mo=mo.next;return n}function _t(){for(var n,t=fo,e=1/0;t;)t.flush?t=n?n.next=t.next:fo=t.next:(t.time<e&&(e=t.time),t=(n=t).next);return ho=n,e}function wt(n,t){var e=Math.pow(10,3*Math.abs(8-t));return{scale:t>8?function(n){return n/e}:function(n){return n*e},symbol:n}}function St(n,t){return t-(n?Math.ceil(Math.log(n)/Math.LN10):1)}function Et(n){return n+""}function kt(){}function At(n,t,e){var r=e.s=n+t,i=r-n,u=r-i;e.t=n-u+(t-i)}function Nt(n,t){n&&qo.hasOwnProperty(n.type)&&qo[n.type](n,t)}function qt(n,t,e){var r,i=-1,u=n.length-e;for(t.lineStart();++i<u;)r=n[i],t.point(r[0],r[1]);t.lineEnd()}function Tt(n,t){var e=-1,r=n.length;for(t.polygonStart();++e<r;)qt(n[e],t,1);t.polygonEnd()}function Ct(){function n(n,t){n*=to,t=t*to/2+Ka/4;var e=n-r,a=Math.cos(t),o=Math.sin(t),c=u*o,l=i*a+c*Math.cos(e),s=c*Math.sin(e);Co.add(Math.atan2(s,l)),r=n,i=a,u=o}var t,e,r,i,u;zo.point=function(a,o){zo.point=n,r=(t=a)*to,i=Math.cos(o=(e=o)*to/2+Ka/4),u=Math.sin(o)},zo.lineEnd=function(){n(t,e)}}function zt(n){var t=n[0],e=n[1],r=Math.cos(e);return[r*Math.cos(t),r*Math.sin(t),Math.sin(e)]}function Dt(n,t){return n[0]*t[0]+n[1]*t[1]+n[2]*t[2]}function jt(n,t){return[n[1]*t[2]-n[2]*t[1],n[2]*t[0]-n[0]*t[2],n[0]*t[1]-n[1]*t[0]]}function Lt(n,t){n[0]+=t[0],n[1]+=t[1],n[2]+=t[2]}function Ht(n,t){return[n[0]*t,n[1]*t,n[2]*t]}function Ft(n){var t=Math.sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]);n[0]/=t,n[1]/=t,n[2]/=t}function Pt(n){return[Math.atan2(n[1],n[0]),V(n[2])]}function Ot(n,t){return Math.abs(n[0]-t[0])<Qa&&Math.abs(n[1]-t[1])<Qa}function Yt(n,t){n*=to;var e=Math.cos(t*=to);Rt(e*Math.cos(n),e*Math.sin(n),Math.sin(t))}function Rt(n,t,e){++Do,Lo+=(n-Lo)/Do,Ho+=(t-Ho)/Do,Fo+=(e-Fo)/Do}function Ut(){function n(n,i){n*=to;var u=Math.cos(i*=to),a=u*Math.cos(n),o=u*Math.sin(n),c=Math.sin(i),l=Math.atan2(Math.sqrt((l=e*c-r*o)*l+(l=r*a-t*c)*l+(l=t*o-e*a)*l),t*a+e*o+r*c);jo+=l,Po+=l*(t+(t=a)),Oo+=l*(e+(e=o)),Yo+=l*(r+(r=c)),Rt(t,e,r)}var t,e,r;Vo.point=function(i,u){i*=to;var a=Math.cos(u*=to);t=a*Math.cos(i),e=a*Math.sin(i),r=Math.sin(u),Vo.point=n,Rt(t,e,r)}}function It(){Vo.point=Yt}function Vt(){function n(n,t){n*=to;var e=Math.cos(t*=to),a=e*Math.cos(n),o=e*Math.sin(n),c=Math.sin(t),l=i*c-u*o,s=u*a-r*c,f=r*o-i*a,h=Math.sqrt(l*l+s*s+f*f),g=r*a+i*o+u*c,p=h&&-I(g)/h,m=Math.atan2(h,g);Ro+=p*l,Uo+=p*s,Io+=p*f,jo+=m,Po+=m*(r+(r=a)),Oo+=m*(i+(i=o)),Yo+=m*(u+(u=c)),Rt(r,i,u)}var t,e,r,i,u;Vo.point=function(a,o){t=a,e=o,Vo.point=n,a*=to;var c=Math.cos(o*=to);r=c*Math.cos(a),i=c*Math.sin(a),u=Math.sin(o),Rt(r,i,u)},Vo.lineEnd=function(){n(t,e),Vo.lineEnd=It,Vo.point=Yt}}function Xt(){return!0}function Zt(n,t,e,r,i){var u=[],a=[];if(n.forEach(function(n){if(!((t=n.length-1)<=0)){var t,e=n[0],r=n[t];if(Ot(e,r)){i.lineStart();for(var o=0;t>o;++o)i.point((e=n[o])[0],e[1]);return i.lineEnd(),void 0}var c={point:e,points:n,other:null,visited:!1,entry:!0,subject:!0},l={point:e,points:[e],other:c,visited:!1,entry:!1,subject:!1};c.other=l,u.push(c),a.push(l),c={point:r,points:[r],other:null,visited:!1,entry:!1,subject:!0},l={point:r,points:[r],other:c,visited:!1,entry:!0,subject:!1},c.other=l,u.push(c),a.push(l)}}),a.sort(t),Bt(u),Bt(a),u.length){if(e)for(var o=1,c=!e(a[0].point),l=a.length;l>o;++o)a[o].entry=c=!c;for(var s,f,h,g=u[0];;){for(s=g;s.visited;)if((s=s.next)===g)return;f=s.points,i.lineStart();do{if(s.visited=s.other.visited=!0,s.entry){if(s.subject)for(var o=0;o<f.length;o++)i.point((h=f[o])[0],h[1]);else r(s.point,s.next.point,1,i);s=s.next}else{if(s.subject){f=s.prev.points;for(var o=f.length;--o>=0;)i.point((h=f[o])[0],h[1])}else r(s.point,s.prev.point,-1,i);s=s.prev}s=s.other,f=s.points}while(!s.visited);i.lineEnd()}}}function Bt(n){if(t=n.length){for(var t,e,r=0,i=n[0];++r<t;)i.next=e=n[r],e.prev=i,i=e;i.next=e=n[0],e.prev=i}}function $t(n,t,e,r){return function(i){function u(t,e){n(t,e)&&i.point(t,e)}function a(n,t){m.point(n,t)}function o(){d.point=a,m.lineStart()}function c(){d.point=u,m.lineEnd()}function l(n,t){y.point(n,t),p.push([n,t])}function s(){y.lineStart(),p=[]}function f(){l(p[0][0],p[0][1]),y.lineEnd();var n,t=y.clean(),e=v.buffer(),r=e.length;if(p.pop(),g.push(p),p=null,r){if(1&t){n=e[0];var u,r=n.length-1,a=-1;for(i.lineStart();++a<r;)i.point((u=n[a])[0],u[1]);return i.lineEnd(),void 0}r>1&&2&t&&e.push(e.pop().concat(e.shift())),h.push(e.filter(Wt))}}var h,g,p,m=t(i),d={point:u,lineStart:o,lineEnd:c,polygonStart:function(){d.point=l,d.lineStart=s,d.lineEnd=f,h=[],g=[],i.polygonStart()},polygonEnd:function(){d.point=u,d.lineStart=o,d.lineEnd=c,h=ya.merge(h),h.length?Zt(h,Gt,null,e,i):r(g)&&(i.lineStart(),e(null,null,1,i),i.lineEnd()),i.polygonEnd(),h=g=null},sphere:function(){i.polygonStart(),i.lineStart(),e(null,null,1,i),i.lineEnd(),i.polygonEnd()}},v=Jt(),y=t(v);return d}}function Wt(n){return n.length>1}function Jt(){var n,t=[];return{lineStart:function(){t.push(n=[])},point:function(t,e){n.push([t,e])},lineEnd:s,buffer:function(){var e=t;return t=[],n=null,e},rejoin:function(){t.length>1&&t.push(t.pop().concat(t.shift()))}}}function Gt(n,t){return((n=n.point)[0]<0?n[1]-Ka/2-Qa:Ka/2-n[1])-((t=t.point)[0]<0?t[1]-Ka/2-Qa:Ka/2-t[1])}function Kt(n,t){var e=n[0],r=n[1],i=[Math.sin(e),-Math.cos(e),0],u=0,a=!1,o=!1,c=0;Co.reset();for(var l=0,s=t.length;s>l;++l){var f=t[l],h=f.length;if(h){for(var g=f[0],p=g[0],m=g[1]/2+Ka/4,d=Math.sin(m),v=Math.cos(m),y=1;;){y===h&&(y=0),n=f[y];var M=n[0],x=n[1]/2+Ka/4,b=Math.sin(x),_=Math.cos(x),w=M-p,S=Math.abs(w)>Ka,E=d*b;if(Co.add(Math.atan2(E*Math.sin(w),v*_+E*Math.cos(w))),Math.abs(x)<Qa&&(o=!0),u+=S?w+(w>=0?2:-2)*Ka:w,S^p>=e^M>=e){var k=jt(zt(g),zt(n));Ft(k);var A=jt(i,k);Ft(A);var N=(S^w>=0?-1:1)*V(A[2]);r>N&&(c+=S^w>=0?1:-1)}if(!y++)break;p=M,d=b,v=_,g=n}Math.abs(u)>Qa&&(a=!0)}}return(!o&&!a&&0>Co||-Qa>u)^1&c}function Qt(n){var t,e=0/0,r=0/0,i=0/0;return{lineStart:function(){n.lineStart(),t=1},point:function(u,a){var o=u>0?Ka:-Ka,c=Math.abs(u-e);Math.abs(c-Ka)<Qa?(n.point(e,r=(r+a)/2>0?Ka/2:-Ka/2),n.point(i,r),n.lineEnd(),n.lineStart(),n.point(o,r),n.point(u,r),t=0):i!==o&&c>=Ka&&(Math.abs(e-i)<Qa&&(e-=i*Qa),Math.abs(u-o)<Qa&&(u-=o*Qa),r=ne(e,r,u,a),n.point(i,r),n.lineEnd(),n.lineStart(),n.point(o,r),t=0),n.point(e=u,r=a),i=o},lineEnd:function(){n.lineEnd(),e=r=0/0},clean:function(){return 2-t}}}function ne(n,t,e,r){var i,u,a=Math.sin(n-e);return Math.abs(a)>Qa?Math.atan((Math.sin(t)*(u=Math.cos(r))*Math.sin(e)-Math.sin(r)*(i=Math.cos(t))*Math.sin(n))/(i*u*a)):(t+r)/2}function te(n,t,e,r){var i;if(null==n)i=e*Ka/2,r.point(-Ka,i),r.point(0,i),r.point(Ka,i),r.point(Ka,0),r.point(Ka,-i),r.point(0,-i),r.point(-Ka,-i),r.point(-Ka,0),r.point(-Ka,i);else if(Math.abs(n[0]-t[0])>Qa){var u=(n[0]<t[0]?1:-1)*Ka;i=e*u/2,r.point(-u,i),r.point(0,i),r.point(u,i)}else r.point(t[0],t[1])}function ee(n){return Kt(Zo,n)}function re(n){function t(n,t){return Math.cos(n)*Math.cos(t)>a}function e(n){var e,u,a,c,s;return{lineStart:function(){c=a=!1,s=1},point:function(f,h){var g,p=[f,h],m=t(f,h),d=o?m?0:i(f,h):m?i(f+(0>f?Ka:-Ka),h):0;if(!e&&(c=a=m)&&n.lineStart(),m!==a&&(g=r(e,p),(Ot(e,g)||Ot(p,g))&&(p[0]+=Qa,p[1]+=Qa,m=t(p[0],p[1]))),m!==a)s=0,m?(n.lineStart(),g=r(p,e),n.point(g[0],g[1])):(g=r(e,p),n.point(g[0],g[1]),n.lineEnd()),e=g;else if(l&&e&&o^m){var v;d&u||!(v=r(p,e,!0))||(s=0,o?(n.lineStart(),n.point(v[0][0],v[0][1]),n.point(v[1][0],v[1][1]),n.lineEnd()):(n.point(v[1][0],v[1][1]),n.lineEnd(),n.lineStart(),n.point(v[0][0],v[0][1])))}!m||e&&Ot(e,p)||n.point(p[0],p[1]),e=p,a=m,u=d},lineEnd:function(){a&&n.lineEnd(),e=null},clean:function(){return s|(c&&a)<<1}}}function r(n,t,e){var r=zt(n),i=zt(t),u=[1,0,0],o=jt(r,i),c=Dt(o,o),l=o[0],s=c-l*l;if(!s)return!e&&n;var f=a*c/s,h=-a*l/s,g=jt(u,o),p=Ht(u,f),m=Ht(o,h);Lt(p,m);var d=g,v=Dt(p,d),y=Dt(d,d),M=v*v-y*(Dt(p,p)-1);if(!(0>M)){var x=Math.sqrt(M),b=Ht(d,(-v-x)/y);if(Lt(b,p),b=Pt(b),!e)return b;var _,w=n[0],S=t[0],E=n[1],k=t[1];w>S&&(_=w,w=S,S=_);var A=S-w,N=Math.abs(A-Ka)<Qa,q=N||Qa>A;if(!N&&E>k&&(_=E,E=k,k=_),q?N?E+k>0^b[1]<(Math.abs(b[0]-w)<Qa?E:k):E<=b[1]&&b[1]<=k:A>Ka^(w<=b[0]&&b[0]<=S)){var T=Ht(d,(-v+x)/y);return Lt(T,p),[b,Pt(T)]}}}function i(t,e){var r=o?n:Ka-n,i=0;return-r>t?i|=1:t>r&&(i|=2),-r>e?i|=4:e>r&&(i|=8),i}function u(n){return Kt(c,n)}var a=Math.cos(n),o=a>0,c=[n,0],l=Math.abs(a)>Qa,s=Ne(n,6*to);return $t(t,e,s,u)}function ie(n,t,e,r){function i(r,i){return Math.abs(r[0]-n)<Qa?i>0?0:3:Math.abs(r[0]-e)<Qa?i>0?2:1:Math.abs(r[1]-t)<Qa?i>0?1:0:i>0?3:2}function u(n,t){return a(n.point,t.point)}function a(n,t){var e=i(n,1),r=i(t,1);return e!==r?e-r:0===e?t[1]-n[1]:1===e?n[0]-t[0]:2===e?n[1]-t[1]:t[0]-n[0]}function o(i,u){var a=u[0]-i[0],o=u[1]-i[1],c=[0,1];return Math.abs(a)<Qa&&Math.abs(o)<Qa?n<=i[0]&&i[0]<=e&&t<=i[1]&&i[1]<=r:ue(n-i[0],a,c)&&ue(i[0]-e,-a,c)&&ue(t-i[1],o,c)&&ue(i[1]-r,-o,c)?(c[1]<1&&(u[0]=i[0]+c[1]*a,u[1]=i[1]+c[1]*o),c[0]>0&&(i[0]+=c[0]*a,i[1]+=c[0]*o),!0):!1}return function(c){function l(u){var a=i(u,-1),o=s([0===a||3===a?n:e,a>1?r:t]);return o}function s(n){for(var t=0,e=M.length,r=n[1],i=0;e>i;++i)for(var u,a=1,o=M[i],c=o.length,l=o[0];c>a;++a)u=o[a],l[1]<=r?u[1]>r&&f(l,u,n)>0&&++t:u[1]<=r&&f(l,u,n)<0&&--t,l=u;return 0!==t}function f(n,t,e){return(t[0]-n[0])*(e[1]-n[1])-(e[0]-n[0])*(t[1]-n[1])}function h(u,o,c,l){var s=0,f=0;if(null==u||(s=i(u,c))!==(f=i(o,c))||a(u,o)<0^c>0){do l.point(0===s||3===s?n:e,s>1?r:t);while((s=(s+c+4)%4)!==f)}else l.point(o[0],o[1])}function g(i,u){return i>=n&&e>=i&&u>=t&&r>=u}function p(n,t){g(n,t)&&c.point(n,t)}function m(){T.point=v,M&&M.push(x=[]),A=!0,k=!1,S=E=0/0}function d(){y&&(v(b,_),w&&k&&q.rejoin(),y.push(q.buffer())),T.point=p,k&&c.lineEnd()}function v(n,t){n=Math.max(-Bo,Math.min(Bo,n)),t=Math.max(-Bo,Math.min(Bo,t));var e=g(n,t);if(M&&x.push([n,t]),A)b=n,_=t,w=e,A=!1,e&&(c.lineStart(),c.point(n,t));else if(e&&k)c.point(n,t);else{var r=[S,E],i=[n,t];o(r,i)?(k||(c.lineStart(),c.point(r[0],r[1])),c.point(i[0],i[1]),e||c.lineEnd()):e&&(c.lineStart(),c.point(n,t))}S=n,E=t,k=e}var y,M,x,b,_,w,S,E,k,A,N=c,q=Jt(),T={point:p,lineStart:m,lineEnd:d,polygonStart:function(){c=q,y=[],M=[]},polygonEnd:function(){c=N,(y=ya.merge(y)).length?(c.polygonStart(),Zt(y,u,l,h,c),c.polygonEnd()):s([n,t])&&(c.polygonStart(),c.lineStart(),h(null,null,1,c),c.lineEnd(),c.polygonEnd()),y=M=x=null}};return T}}function ue(n,t,e){if(Math.abs(t)<Qa)return 0>=n;var r=n/t;if(t>0){if(r>e[1])return!1;r>e[0]&&(e[0]=r)}else{if(r<e[0])return!1;r<e[1]&&(e[1]=r)}return!0}function ae(n,t){function e(e,r){return e=n(e,r),t(e[0],e[1])}return n.invert&&t.invert&&(e.invert=function(e,r){return e=t.invert(e,r),e&&n.invert(e[0],e[1])}),e}function oe(n){var t=0,e=Ka/3,r=be(n),i=r(t,e);return i.parallels=function(n){return arguments.length?r(t=n[0]*Ka/180,e=n[1]*Ka/180):[180*(t/Ka),180*(e/Ka)]},i}function ce(n,t){function e(n,t){var e=Math.sqrt(u-2*i*Math.sin(t))/i;return[e*Math.sin(n*=i),a-e*Math.cos(n)]}var r=Math.sin(n),i=(r+Math.sin(t))/2,u=1+r*(2*i-r),a=Math.sqrt(u)/i;return e.invert=function(n,t){var e=a-t;return[Math.atan2(n,e)/i,V((u-(n*n+e*e)*i*i)/(2*i))]},e}function le(){function n(n,t){Wo+=i*n-r*t,r=n,i=t}var t,e,r,i;nc.point=function(u,a){nc.point=n,t=r=u,e=i=a},nc.lineEnd=function(){n(t,e)}}function se(n,t){Jo>n&&(Jo=n),n>Ko&&(Ko=n),Go>t&&(Go=t),t>Qo&&(Qo=t)}function fe(){function n(n,t){a.push("M",n,",",t,u)}function t(n,t){a.push("M",n,",",t),o.point=e}function e(n,t){a.push("L",n,",",t)}function r(){o.point=n}function i(){a.push("Z")}var u=he(4.5),a=[],o={point:n,lineStart:function(){o.point=t},lineEnd:r,polygonStart:function(){o.lineEnd=i},polygonEnd:function(){o.lineEnd=r,o.point=n},pointRadius:function(n){return u=he(n),o},result:function(){if(a.length){var n=a.join("");return a=[],n}}};return o}function he(n){return"m0,"+n+"a"+n+","+n+" 0 1,1 0,"+-2*n+"a"+n+","+n+" 0 1,1 0,"+2*n+"z"}function ge(n,t){Lo+=n,Ho+=t,++Fo}function pe(){function n(n,r){var i=n-t,u=r-e,a=Math.sqrt(i*i+u*u);Po+=a*(t+n)/2,Oo+=a*(e+r)/2,Yo+=a,ge(t=n,e=r)}var t,e;ec.point=function(r,i){ec.point=n,ge(t=r,e=i)}}function me(){ec.point=ge}function de(){function n(n,t){var e=n-r,u=t-i,a=Math.sqrt(e*e+u*u);Po+=a*(r+n)/2,Oo+=a*(i+t)/2,Yo+=a,a=i*n-r*t,Ro+=a*(r+n),Uo+=a*(i+t),Io+=3*a,ge(r=n,i=t)}var t,e,r,i;ec.point=function(u,a){ec.point=n,ge(t=r=u,e=i=a)},ec.lineEnd=function(){n(t,e)}}function ve(n){function t(t,e){n.moveTo(t,e),n.arc(t,e,a,0,2*Ka)}function e(t,e){n.moveTo(t,e),o.point=r}function r(t,e){n.lineTo(t,e)}function i(){o.point=t}function u(){n.closePath()}var a=4.5,o={point:t,lineStart:function(){o.point=e},lineEnd:i,polygonStart:function(){o.lineEnd=u},polygonEnd:function(){o.lineEnd=i,o.point=t},pointRadius:function(n){return a=n,o},result:s};return o}function ye(n){function t(t){function r(e,r){e=n(e,r),t.point(e[0],e[1])}function i(){M=0/0,S.point=a,t.lineStart()}function a(r,i){var a=zt([r,i]),o=n(r,i);e(M,x,y,b,_,w,M=o[0],x=o[1],y=r,b=a[0],_=a[1],w=a[2],u,t),t.point(M,x)}function o(){S.point=r,t.lineEnd()}function c(){i(),S.point=l,S.lineEnd=s}function l(n,t){a(f=n,h=t),g=M,p=x,m=b,d=_,v=w,S.point=a}function s(){e(M,x,y,b,_,w,g,p,f,m,d,v,u,t),S.lineEnd=o,o()}var f,h,g,p,m,d,v,y,M,x,b,_,w,S={point:r,lineStart:i,lineEnd:o,polygonStart:function(){t.polygonStart(),S.lineStart=c},polygonEnd:function(){t.polygonEnd(),S.lineStart=i}};return S}function e(t,u,a,o,c,l,s,f,h,g,p,m,d,v){var y=s-t,M=f-u,x=y*y+M*M;if(x>4*r&&d--){var b=o+g,_=c+p,w=l+m,S=Math.sqrt(b*b+_*_+w*w),E=Math.asin(w/=S),k=Math.abs(Math.abs(w)-1)<Qa?(a+h)/2:Math.atan2(_,b),A=n(k,E),N=A[0],q=A[1],T=N-t,C=q-u,z=M*T-y*C;(z*z/x>r||Math.abs((y*T+M*C)/x-.5)>.3||i>o*g+c*p+l*m)&&(e(t,u,a,o,c,l,N,q,k,b/=S,_/=S,w,d,v),v.point(N,q),e(N,q,k,b,_,w,s,f,h,g,p,m,d,v))}}var r=.5,i=Math.cos(30*to),u=16;return t.precision=function(n){return arguments.length?(u=(r=n*n)>0&&16,t):Math.sqrt(r)},t}function Me(n){var t=ye(function(t,e){return n([t*eo,e*eo])});return function(n){return n=t(n),{point:function(t,e){n.point(t*to,e*to)},sphere:function(){n.sphere()},lineStart:function(){n.lineStart()},lineEnd:function(){n.lineEnd()},polygonStart:function(){n.polygonStart()},polygonEnd:function(){n.polygonEnd()}}}}function xe(n){return be(function(){return n})()}function be(n){function t(n){return n=o(n[0]*to,n[1]*to),[n[0]*h+c,l-n[1]*h]}function e(n){return n=o.invert((n[0]-c)/h,(l-n[1])/h),n&&[n[0]*eo,n[1]*eo]}function r(){o=ae(a=Se(v,y,M),u);var n=u(m,d);return c=g-n[0]*h,l=p+n[1]*h,i()}function i(){return s&&(s.valid=!1,s=null),t}var u,a,o,c,l,s,f=ye(function(n,t){return n=u(n,t),[n[0]*h+c,l-n[1]*h]}),h=150,g=480,p=250,m=0,d=0,v=0,y=0,M=0,x=Xo,b=mt,_=null,w=null;return t.stream=function(n){return s&&(s.valid=!1),s=_e(a,x(f(b(n)))),s.valid=!0,s},t.clipAngle=function(n){return arguments.length?(x=null==n?(_=n,Xo):re((_=+n)*to),i()):_},t.clipExtent=function(n){return arguments.length?(w=n,b=null==n?mt:ie(n[0][0],n[0][1],n[1][0],n[1][1]),i()):w},t.scale=function(n){return arguments.length?(h=+n,r()):h},t.translate=function(n){return arguments.length?(g=+n[0],p=+n[1],r()):[g,p]},t.center=function(n){return arguments.length?(m=n[0]%360*to,d=n[1]%360*to,r()):[m*eo,d*eo]},t.rotate=function(n){return arguments.length?(v=n[0]%360*to,y=n[1]%360*to,M=n.length>2?n[2]%360*to:0,r()):[v*eo,y*eo,M*eo]},ya.rebind(t,f,"precision"),function(){return u=n.apply(this,arguments),t.invert=u.invert&&e,r()}}function _e(n,t){return{point:function(e,r){r=n(e*to,r*to),e=r[0],t.point(e>Ka?e-2*Ka:-Ka>e?e+2*Ka:e,r[1])},sphere:function(){t.sphere()},lineStart:function(){t.lineStart()},lineEnd:function(){t.lineEnd()},polygonStart:function(){t.polygonStart()},polygonEnd:function(){t.polygonEnd()}}}function we(n,t){return[n,t]}function Se(n,t,e){return n?t||e?ae(ke(n),Ae(t,e)):ke(n):t||e?Ae(t,e):we}function Ee(n){return function(t,e){return t+=n,[t>Ka?t-2*Ka:-Ka>t?t+2*Ka:t,e]}}function ke(n){var t=Ee(n);return t.invert=Ee(-n),t}function Ae(n,t){function e(n,t){var e=Math.cos(t),o=Math.cos(n)*e,c=Math.sin(n)*e,l=Math.sin(t),s=l*r+o*i;return[Math.atan2(c*u-s*a,o*r-l*i),V(s*u+c*a)]}var r=Math.cos(n),i=Math.sin(n),u=Math.cos(t),a=Math.sin(t);return e.invert=function(n,t){var e=Math.cos(t),o=Math.cos(n)*e,c=Math.sin(n)*e,l=Math.sin(t),s=l*u-c*a;return[Math.atan2(c*u+l*a,o*r+s*i),V(s*r-o*i)]},e}function Ne(n,t){var e=Math.cos(n),r=Math.sin(n);return function(i,u,a,o){null!=i?(i=qe(e,i),u=qe(e,u),(a>0?u>i:i>u)&&(i+=2*a*Ka)):(i=n+2*a*Ka,u=n);for(var c,l=a*t,s=i;a>0?s>u:u>s;s-=l)o.point((c=Pt([e,-r*Math.cos(s),-r*Math.sin(s)]))[0],c[1])}}function qe(n,t){var e=zt(t);e[0]-=n,Ft(e);var r=I(-e[1]);return((-e[2]<0?-r:r)+2*Math.PI-Qa)%(2*Math.PI)}function Te(n,t,e){var r=ya.range(n,t-Qa,e).concat(t);return function(n){return r.map(function(t){return[n,t]})}}function Ce(n,t,e){var r=ya.range(n,t-Qa,e).concat(t);return function(n){return r.map(function(t){return[t,n]})}}function ze(n){return n.source}function De(n){return n.target}function je(n,t,e,r){var i=Math.cos(t),u=Math.sin(t),a=Math.cos(r),o=Math.sin(r),c=i*Math.cos(n),l=i*Math.sin(n),s=a*Math.cos(e),f=a*Math.sin(e),h=2*Math.asin(Math.sqrt(B(r-t)+i*a*B(e-n))),g=1/Math.sin(h),p=h?function(n){var t=Math.sin(n*=h)*g,e=Math.sin(h-n)*g,r=e*c+t*s,i=e*l+t*f,a=e*u+t*o;return[Math.atan2(i,r)*eo,Math.atan2(a,Math.sqrt(r*r+i*i))*eo]}:function(){return[n*eo,t*eo]};return p.distance=h,p}function Le(){function n(n,i){var u=Math.sin(i*=to),a=Math.cos(i),o=Math.abs((n*=to)-t),c=Math.cos(o);rc+=Math.atan2(Math.sqrt((o=a*Math.sin(o))*o+(o=r*u-e*a*c)*o),e*u+r*a*c),t=n,e=u,r=a}var t,e,r;ic.point=function(i,u){t=i*to,e=Math.sin(u*=to),r=Math.cos(u),ic.point=n},ic.lineEnd=function(){ic.point=ic.lineEnd=s}}function He(n,t){function e(t,e){var r=Math.cos(t),i=Math.cos(e),u=n(r*i);return[u*i*Math.sin(t),u*Math.sin(e)]}return e.invert=function(n,e){var r=Math.sqrt(n*n+e*e),i=t(r),u=Math.sin(i),a=Math.cos(i);return[Math.atan2(n*u,r*a),Math.asin(r&&e*u/r)]},e}function Fe(n,t){function e(n,t){var e=Math.abs(Math.abs(t)-Ka/2)<Qa?0:a/Math.pow(i(t),u);return[e*Math.sin(u*n),a-e*Math.cos(u*n)]}var r=Math.cos(n),i=function(n){return Math.tan(Ka/4+n/2)},u=n===t?Math.sin(n):Math.log(r/Math.cos(t))/Math.log(i(t)/i(n)),a=r*Math.pow(i(n),u)/u;return u?(e.invert=function(n,t){var e=a-t,r=U(u)*Math.sqrt(n*n+e*e);return[Math.atan2(n,e)/u,2*Math.atan(Math.pow(a/r,1/u))-Ka/2]},e):Oe}function Pe(n,t){function e(n,t){var e=u-t;return[e*Math.sin(i*n),u-e*Math.cos(i*n)]}var r=Math.cos(n),i=n===t?Math.sin(n):(r-Math.cos(t))/(t-n),u=r/i+n;return Math.abs(i)<Qa?we:(e.invert=function(n,t){var e=u-t;return[Math.atan2(n,e)/i,u-U(i)*Math.sqrt(n*n+e*e)]},e)}function Oe(n,t){return[n,Math.log(Math.tan(Ka/4+t/2))]}function Ye(n){var t,e=xe(n),r=e.scale,i=e.translate,u=e.clipExtent;return e.scale=function(){var n=r.apply(e,arguments);return n===e?t?e.clipExtent(null):e:n},e.translate=function(){var n=i.apply(e,arguments);return n===e?t?e.clipExtent(null):e:n},e.clipExtent=function(n){var a=u.apply(e,arguments);if(a===e){if(t=null==n){var o=Ka*r(),c=i();u([[c[0]-o,c[1]-o],[c[0]+o,c[1]+o]])}}else t&&(a=null);return a},e.clipExtent(null)}function Re(n,t){var e=Math.cos(t)*Math.sin(n);return[Math.log((1+e)/(1-e))/2,Math.atan2(Math.tan(t),Math.cos(n))]}function Ue(n){function t(t){function a(){l.push("M",u(n(s),o))}for(var c,l=[],s=[],f=-1,h=t.length,g=pt(e),p=pt(r);++f<h;)i.call(this,c=t[f],f)?s.push([+g.call(this,c,f),+p.call(this,c,f)]):s.length&&(a(),s=[]);return s.length&&a(),l.length?l.join(""):null}var e=Ie,r=Ve,i=Xt,u=Xe,a=u.key,o=.7;return t.x=function(n){return arguments.length?(e=n,t):e},t.y=function(n){return arguments.length?(r=n,t):r},t.defined=function(n){return arguments.length?(i=n,t):i},t.interpolate=function(n){return arguments.length?(a="function"==typeof n?u=n:(u=sc.get(n)||Xe).key,t):a},t.tension=function(n){return arguments.length?(o=n,t):o},t}function Ie(n){return n[0]}function Ve(n){return n[1]}function Xe(n){return n.join("L")}function Ze(n){return Xe(n)+"Z"}function Be(n){for(var t=0,e=n.length,r=n[0],i=[r[0],",",r[1]];++t<e;)i.push("H",(r[0]+(r=n[t])[0])/2,"V",r[1]);return e>1&&i.push("H",r[0]),i.join("")}function $e(n){for(var t=0,e=n.length,r=n[0],i=[r[0],",",r[1]];++t<e;)i.push("V",(r=n[t])[1],"H",r[0]);return i.join("")}function We(n){for(var t=0,e=n.length,r=n[0],i=[r[0],",",r[1]];++t<e;)i.push("H",(r=n[t])[0],"V",r[1]);return i.join("")}function Je(n,t){return n.length<4?Xe(n):n[1]+Qe(n.slice(1,n.length-1),nr(n,t))}function Ge(n,t){return n.length<3?Xe(n):n[0]+Qe((n.push(n[0]),n),nr([n[n.length-2]].concat(n,[n[1]]),t))}function Ke(n,t){return n.length<3?Xe(n):n[0]+Qe(n,nr(n,t))}function Qe(n,t){if(t.length<1||n.length!=t.length&&n.length!=t.length+2)return Xe(n);var e=n.length!=t.length,r="",i=n[0],u=n[1],a=t[0],o=a,c=1;if(e&&(r+="Q"+(u[0]-2*a[0]/3)+","+(u[1]-2*a[1]/3)+","+u[0]+","+u[1],i=n[1],c=2),t.length>1){o=t[1],u=n[c],c++,r+="C"+(i[0]+a[0])+","+(i[1]+a[1])+","+(u[0]-o[0])+","+(u[1]-o[1])+","+u[0]+","+u[1];for(var l=2;l<t.length;l++,c++)u=n[c],o=t[l],r+="S"+(u[0]-o[0])+","+(u[1]-o[1])+","+u[0]+","+u[1]}if(e){var s=n[c];r+="Q"+(u[0]+2*o[0]/3)+","+(u[1]+2*o[1]/3)+","+s[0]+","+s[1]}return r}function nr(n,t){for(var e,r=[],i=(1-t)/2,u=n[0],a=n[1],o=1,c=n.length;++o<c;)e=u,u=a,a=n[o],r.push([i*(a[0]-e[0]),i*(a[1]-e[1])]);return r}function tr(n){if(n.length<3)return Xe(n);var t=1,e=n.length,r=n[0],i=r[0],u=r[1],a=[i,i,i,(r=n[1])[0]],o=[u,u,u,r[1]],c=[i,",",u,"L",ur(gc,a),",",ur(gc,o)];for(n.push(n[e-1]);++t<=e;)r=n[t],a.shift(),a.push(r[0]),o.shift(),o.push(r[1]),ar(c,a,o);return n.pop(),c.push("L",r),c.join("")}function er(n){if(n.length<4)return Xe(n);for(var t,e=[],r=-1,i=n.length,u=[0],a=[0];++r<3;)t=n[r],u.push(t[0]),a.push(t[1]);for(e.push(ur(gc,u)+","+ur(gc,a)),--r;++r<i;)t=n[r],u.shift(),u.push(t[0]),a.shift(),a.push(t[1]),ar(e,u,a);return e.join("")}function rr(n){for(var t,e,r=-1,i=n.length,u=i+4,a=[],o=[];++r<4;)e=n[r%i],a.push(e[0]),o.push(e[1]);for(t=[ur(gc,a),",",ur(gc,o)],--r;++r<u;)e=n[r%i],a.shift(),a.push(e[0]),o.shift(),o.push(e[1]),ar(t,a,o);return t.join("")}function ir(n,t){var e=n.length-1;if(e)for(var r,i,u=n[0][0],a=n[0][1],o=n[e][0]-u,c=n[e][1]-a,l=-1;++l<=e;)r=n[l],i=l/e,r[0]=t*r[0]+(1-t)*(u+i*o),r[1]=t*r[1]+(1-t)*(a+i*c);return tr(n)}function ur(n,t){return n[0]*t[0]+n[1]*t[1]+n[2]*t[2]+n[3]*t[3]
2
+ }function ar(n,t,e){n.push("C",ur(fc,t),",",ur(fc,e),",",ur(hc,t),",",ur(hc,e),",",ur(gc,t),",",ur(gc,e))}function or(n,t){return(t[1]-n[1])/(t[0]-n[0])}function cr(n){for(var t=0,e=n.length-1,r=[],i=n[0],u=n[1],a=r[0]=or(i,u);++t<e;)r[t]=(a+(a=or(i=u,u=n[t+1])))/2;return r[t]=a,r}function lr(n){for(var t,e,r,i,u=[],a=cr(n),o=-1,c=n.length-1;++o<c;)t=or(n[o],n[o+1]),Math.abs(t)<1e-6?a[o]=a[o+1]=0:(e=a[o]/t,r=a[o+1]/t,i=e*e+r*r,i>9&&(i=3*t/Math.sqrt(i),a[o]=i*e,a[o+1]=i*r));for(o=-1;++o<=c;)i=(n[Math.min(c,o+1)][0]-n[Math.max(0,o-1)][0])/(6*(1+a[o]*a[o])),u.push([i||0,a[o]*i||0]);return u}function sr(n){return n.length<3?Xe(n):n[0]+Qe(n,lr(n))}function fr(n,t,e,r){var i,u,a,o,c,l,s;return i=r[n],u=i[0],a=i[1],i=r[t],o=i[0],c=i[1],i=r[e],l=i[0],s=i[1],(s-a)*(o-u)-(c-a)*(l-u)>0}function hr(n,t,e){return(e[0]-t[0])*(n[1]-t[1])<(e[1]-t[1])*(n[0]-t[0])}function gr(n,t,e,r){var i=n[0],u=e[0],a=t[0]-i,o=r[0]-u,c=n[1],l=e[1],s=t[1]-c,f=r[1]-l,h=(o*(c-l)-f*(i-u))/(f*a-o*s);return[i+h*a,c+h*s]}function pr(n){var t=n[0],e=n[n.length-1];return!(t[0]-e[0]||t[1]-e[1])}function mr(n,t){var e={list:n.map(function(n,t){return{index:t,x:n[0],y:n[1]}}).sort(function(n,t){return n.y<t.y?-1:n.y>t.y?1:n.x<t.x?-1:n.x>t.x?1:0}),bottomSite:null},r={list:[],leftEnd:null,rightEnd:null,init:function(){r.leftEnd=r.createHalfEdge(null,"l"),r.rightEnd=r.createHalfEdge(null,"l"),r.leftEnd.r=r.rightEnd,r.rightEnd.l=r.leftEnd,r.list.unshift(r.leftEnd,r.rightEnd)},createHalfEdge:function(n,t){return{edge:n,side:t,vertex:null,l:null,r:null}},insert:function(n,t){t.l=n,t.r=n.r,n.r.l=t,n.r=t},leftBound:function(n){var t=r.leftEnd;do t=t.r;while(t!=r.rightEnd&&i.rightOf(t,n));return t=t.l},del:function(n){n.l.r=n.r,n.r.l=n.l,n.edge=null},right:function(n){return n.r},left:function(n){return n.l},leftRegion:function(n){return null==n.edge?e.bottomSite:n.edge.region[n.side]},rightRegion:function(n){return null==n.edge?e.bottomSite:n.edge.region[mc[n.side]]}},i={bisect:function(n,t){var e={region:{l:n,r:t},ep:{l:null,r:null}},r=t.x-n.x,i=t.y-n.y,u=r>0?r:-r,a=i>0?i:-i;return e.c=n.x*r+n.y*i+.5*(r*r+i*i),u>a?(e.a=1,e.b=i/r,e.c/=r):(e.b=1,e.a=r/i,e.c/=i),e},intersect:function(n,t){var e=n.edge,r=t.edge;if(!e||!r||e.region.r==r.region.r)return null;var i=e.a*r.b-e.b*r.a;if(Math.abs(i)<1e-10)return null;var u,a,o=(e.c*r.b-r.c*e.b)/i,c=(r.c*e.a-e.c*r.a)/i,l=e.region.r,s=r.region.r;l.y<s.y||l.y==s.y&&l.x<s.x?(u=n,a=e):(u=t,a=r);var f=o>=a.region.r.x;return f&&"l"===u.side||!f&&"r"===u.side?null:{x:o,y:c}},rightOf:function(n,t){var e=n.edge,r=e.region.r,i=t.x>r.x;if(i&&"l"===n.side)return 1;if(!i&&"r"===n.side)return 0;if(1===e.a){var u=t.y-r.y,a=t.x-r.x,o=0,c=0;if(!i&&e.b<0||i&&e.b>=0?c=o=u>=e.b*a:(c=t.x+t.y*e.b>e.c,e.b<0&&(c=!c),c||(o=1)),!o){var l=r.x-e.region.l.x;c=e.b*(a*a-u*u)<l*u*(1+2*a/l+e.b*e.b),e.b<0&&(c=!c)}}else{var s=e.c-e.a*t.x,f=t.y-s,h=t.x-r.x,g=s-r.y;c=f*f>h*h+g*g}return"l"===n.side?c:!c},endPoint:function(n,e,r){n.ep[e]=r,n.ep[mc[e]]&&t(n)},distance:function(n,t){var e=n.x-t.x,r=n.y-t.y;return Math.sqrt(e*e+r*r)}},u={list:[],insert:function(n,t,e){n.vertex=t,n.ystar=t.y+e;for(var r=0,i=u.list,a=i.length;a>r;r++){var o=i[r];if(!(n.ystar>o.ystar||n.ystar==o.ystar&&t.x>o.vertex.x))break}i.splice(r,0,n)},del:function(n){for(var t=0,e=u.list,r=e.length;r>t&&e[t]!=n;++t);e.splice(t,1)},empty:function(){return 0===u.list.length},nextEvent:function(n){for(var t=0,e=u.list,r=e.length;r>t;++t)if(e[t]==n)return e[t+1];return null},min:function(){var n=u.list[0];return{x:n.vertex.x,y:n.ystar}},extractMin:function(){return u.list.shift()}};r.init(),e.bottomSite=e.list.shift();for(var a,o,c,l,s,f,h,g,p,m,d,v,y,M=e.list.shift();;)if(u.empty()||(a=u.min()),M&&(u.empty()||M.y<a.y||M.y==a.y&&M.x<a.x))o=r.leftBound(M),c=r.right(o),h=r.rightRegion(o),v=i.bisect(h,M),f=r.createHalfEdge(v,"l"),r.insert(o,f),m=i.intersect(o,f),m&&(u.del(o),u.insert(o,m,i.distance(m,M))),o=f,f=r.createHalfEdge(v,"r"),r.insert(o,f),m=i.intersect(f,c),m&&u.insert(f,m,i.distance(m,M)),M=e.list.shift();else{if(u.empty())break;o=u.extractMin(),l=r.left(o),c=r.right(o),s=r.right(c),h=r.leftRegion(o),g=r.rightRegion(c),d=o.vertex,i.endPoint(o.edge,o.side,d),i.endPoint(c.edge,c.side,d),r.del(o),u.del(c),r.del(c),y="l",h.y>g.y&&(p=h,h=g,g=p,y="r"),v=i.bisect(h,g),f=r.createHalfEdge(v,y),r.insert(l,f),i.endPoint(v,mc[y],d),m=i.intersect(l,f),m&&(u.del(l),u.insert(l,m,i.distance(m,h))),m=i.intersect(f,s),m&&u.insert(f,m,i.distance(m,h))}for(o=r.right(r.leftEnd);o!=r.rightEnd;o=r.right(o))t(o.edge)}function dr(n){return n.x}function vr(n){return n.y}function yr(){return{leaf:!0,nodes:[],point:null,x:null,y:null}}function Mr(n,t,e,r,i,u){if(!n(t,e,r,i,u)){var a=.5*(e+i),o=.5*(r+u),c=t.nodes;c[0]&&Mr(n,c[0],e,r,a,o),c[1]&&Mr(n,c[1],a,r,i,o),c[2]&&Mr(n,c[2],e,o,a,u),c[3]&&Mr(n,c[3],a,o,i,u)}}function xr(n,t){n=ya.rgb(n),t=ya.rgb(t);var e=n.r,r=n.g,i=n.b,u=t.r-e,a=t.g-r,o=t.b-i;return function(n){return"#"+ct(Math.round(e+u*n))+ct(Math.round(r+a*n))+ct(Math.round(i+o*n))}}function br(n,t){var e,r={},i={};for(e in n)e in t?r[e]=Sr(n[e],t[e]):i[e]=n[e];for(e in t)e in n||(i[e]=t[e]);return function(n){for(e in r)i[e]=r[e](n);return i}}function _r(n,t){return t-=n=+n,function(e){return n+t*e}}function wr(n,t){var e,r,i,u,a,o=0,c=0,l=[],s=[];for(n+="",t+="",dc.lastIndex=0,r=0;e=dc.exec(t);++r)e.index&&l.push(t.substring(o,c=e.index)),s.push({i:l.length,x:e[0]}),l.push(null),o=dc.lastIndex;for(o<t.length&&l.push(t.substring(o)),r=0,u=s.length;(e=dc.exec(n))&&u>r;++r)if(a=s[r],a.x==e[0]){if(a.i)if(null==l[a.i+1])for(l[a.i-1]+=a.x,l.splice(a.i,1),i=r+1;u>i;++i)s[i].i--;else for(l[a.i-1]+=a.x+l[a.i+1],l.splice(a.i,2),i=r+1;u>i;++i)s[i].i-=2;else if(null==l[a.i+1])l[a.i]=a.x;else for(l[a.i]=a.x+l[a.i+1],l.splice(a.i+1,1),i=r+1;u>i;++i)s[i].i--;s.splice(r,1),u--,r--}else a.x=_r(parseFloat(e[0]),parseFloat(a.x));for(;u>r;)a=s.pop(),null==l[a.i+1]?l[a.i]=a.x:(l[a.i]=a.x+l[a.i+1],l.splice(a.i+1,1)),u--;return 1===l.length?null==l[0]?(a=s[0].x,function(n){return a(n)+""}):function(){return t}:function(n){for(r=0;u>r;++r)l[(a=s[r]).i]=a.x(n);return l.join("")}}function Sr(n,t){for(var e,r=ya.interpolators.length;--r>=0&&!(e=ya.interpolators[r](n,t)););return e}function Er(n,t){var e,r=[],i=[],u=n.length,a=t.length,o=Math.min(n.length,t.length);for(e=0;o>e;++e)r.push(Sr(n[e],t[e]));for(;u>e;++e)i[e]=n[e];for(;a>e;++e)i[e]=t[e];return function(n){for(e=0;o>e;++e)i[e]=r[e](n);return i}}function kr(n){return function(t){return 0>=t?0:t>=1?1:n(t)}}function Ar(n){return function(t){return 1-n(1-t)}}function Nr(n){return function(t){return.5*(.5>t?n(2*t):2-n(2-2*t))}}function qr(n){return n*n}function Tr(n){return n*n*n}function Cr(n){if(0>=n)return 0;if(n>=1)return 1;var t=n*n,e=t*n;return 4*(.5>n?e:3*(n-t)+e-.75)}function zr(n){return function(t){return Math.pow(t,n)}}function Dr(n){return 1-Math.cos(n*Ka/2)}function jr(n){return Math.pow(2,10*(n-1))}function Lr(n){return 1-Math.sqrt(1-n*n)}function Hr(n,t){var e;return arguments.length<2&&(t=.45),arguments.length?e=t/(2*Ka)*Math.asin(1/n):(n=1,e=t/4),function(r){return 1+n*Math.pow(2,10*-r)*Math.sin(2*(r-e)*Ka/t)}}function Fr(n){return n||(n=1.70158),function(t){return t*t*((n+1)*t-n)}}function Pr(n){return 1/2.75>n?7.5625*n*n:2/2.75>n?7.5625*(n-=1.5/2.75)*n+.75:2.5/2.75>n?7.5625*(n-=2.25/2.75)*n+.9375:7.5625*(n-=2.625/2.75)*n+.984375}function Or(n,t){n=ya.hcl(n),t=ya.hcl(t);var e=n.h,r=n.c,i=n.l,u=t.h-e,a=t.c-r,o=t.l-i;return isNaN(a)&&(a=0,r=isNaN(r)?t.c:r),isNaN(u)?(u=0,e=isNaN(e)?t.h:e):u>180?u-=360:-180>u&&(u+=360),function(n){return J(e+u*n,r+a*n,i+o*n)+""}}function Yr(n,t){n=ya.hsl(n),t=ya.hsl(t);var e=n.h,r=n.s,i=n.l,u=t.h-e,a=t.s-r,o=t.l-i;return isNaN(a)&&(a=0,r=isNaN(r)?t.s:r),isNaN(u)?(u=0,e=isNaN(e)?t.h:e):u>180?u-=360:-180>u&&(u+=360),function(n){return R(e+u*n,r+a*n,i+o*n)+""}}function Rr(n,t){n=ya.lab(n),t=ya.lab(t);var e=n.l,r=n.a,i=n.b,u=t.l-e,a=t.a-r,o=t.b-i;return function(n){return Q(e+u*n,r+a*n,i+o*n)+""}}function Ur(n,t){return t-=n,function(e){return Math.round(n+t*e)}}function Ir(n){var t=[n.a,n.b],e=[n.c,n.d],r=Xr(t),i=Vr(t,e),u=Xr(Zr(e,t,-i))||0;t[0]*e[1]<e[0]*t[1]&&(t[0]*=-1,t[1]*=-1,r*=-1,i*=-1),this.rotate=(r?Math.atan2(t[1],t[0]):Math.atan2(-e[0],e[1]))*eo,this.translate=[n.e,n.f],this.scale=[r,u],this.skew=u?Math.atan2(i,u)*eo:0}function Vr(n,t){return n[0]*t[0]+n[1]*t[1]}function Xr(n){var t=Math.sqrt(Vr(n,n));return t&&(n[0]/=t,n[1]/=t),t}function Zr(n,t,e){return n[0]+=e*t[0],n[1]+=e*t[1],n}function Br(n,t){var e,r=[],i=[],u=ya.transform(n),a=ya.transform(t),o=u.translate,c=a.translate,l=u.rotate,s=a.rotate,f=u.skew,h=a.skew,g=u.scale,p=a.scale;return o[0]!=c[0]||o[1]!=c[1]?(r.push("translate(",null,",",null,")"),i.push({i:1,x:_r(o[0],c[0])},{i:3,x:_r(o[1],c[1])})):c[0]||c[1]?r.push("translate("+c+")"):r.push(""),l!=s?(l-s>180?s+=360:s-l>180&&(l+=360),i.push({i:r.push(r.pop()+"rotate(",null,")")-2,x:_r(l,s)})):s&&r.push(r.pop()+"rotate("+s+")"),f!=h?i.push({i:r.push(r.pop()+"skewX(",null,")")-2,x:_r(f,h)}):h&&r.push(r.pop()+"skewX("+h+")"),g[0]!=p[0]||g[1]!=p[1]?(e=r.push(r.pop()+"scale(",null,",",null,")"),i.push({i:e-4,x:_r(g[0],p[0])},{i:e-2,x:_r(g[1],p[1])})):(1!=p[0]||1!=p[1])&&r.push(r.pop()+"scale("+p+")"),e=i.length,function(n){for(var t,u=-1;++u<e;)r[(t=i[u]).i]=t.x(n);return r.join("")}}function $r(n,t){return t=t-(n=+n)?1/(t-n):0,function(e){return(e-n)*t}}function Wr(n,t){return t=t-(n=+n)?1/(t-n):0,function(e){return Math.max(0,Math.min(1,(e-n)*t))}}function Jr(n){for(var t=n.source,e=n.target,r=Kr(t,e),i=[t];t!==r;)t=t.parent,i.push(t);for(var u=i.length;e!==r;)i.splice(u,0,e),e=e.parent;return i}function Gr(n){for(var t=[],e=n.parent;null!=e;)t.push(n),n=e,e=e.parent;return t.push(n),t}function Kr(n,t){if(n===t)return n;for(var e=Gr(n),r=Gr(t),i=e.pop(),u=r.pop(),a=null;i===u;)a=i,i=e.pop(),u=r.pop();return a}function Qr(n){n.fixed|=2}function ni(n){n.fixed&=-7}function ti(n){n.fixed|=4,n.px=n.x,n.py=n.y}function ei(n){n.fixed&=-5}function ri(n,t,e){var r=0,i=0;if(n.charge=0,!n.leaf)for(var u,a=n.nodes,o=a.length,c=-1;++c<o;)u=a[c],null!=u&&(ri(u,t,e),n.charge+=u.charge,r+=u.charge*u.cx,i+=u.charge*u.cy);if(n.point){n.leaf||(n.point.x+=Math.random()-.5,n.point.y+=Math.random()-.5);var l=t*e[n.point.index];n.charge+=n.pointCharge=l,r+=l*n.point.x,i+=l*n.point.y}n.cx=r/n.charge,n.cy=i/n.charge}function ii(n,t){return ya.rebind(n,t,"sort","children","value"),n.nodes=n,n.links=ci,n}function ui(n){return n.children}function ai(n){return n.value}function oi(n,t){return t.value-n.value}function ci(n){return ya.merge(n.map(function(n){return(n.children||[]).map(function(t){return{source:n,target:t}})}))}function li(n){return n.x}function si(n){return n.y}function fi(n,t,e){n.y0=t,n.y=e}function hi(n){return ya.range(n.length)}function gi(n){for(var t=-1,e=n[0].length,r=[];++t<e;)r[t]=0;return r}function pi(n){for(var t,e=1,r=0,i=n[0][1],u=n.length;u>e;++e)(t=n[e][1])>i&&(r=e,i=t);return r}function mi(n){return n.reduce(di,0)}function di(n,t){return n+t[1]}function vi(n,t){return yi(n,Math.ceil(Math.log(t.length)/Math.LN2+1))}function yi(n,t){for(var e=-1,r=+n[0],i=(n[1]-r)/t,u=[];++e<=t;)u[e]=i*e+r;return u}function Mi(n){return[ya.min(n),ya.max(n)]}function xi(n,t){return n.parent==t.parent?1:2}function bi(n){var t=n.children;return t&&t.length?t[0]:n._tree.thread}function _i(n){var t,e=n.children;return e&&(t=e.length)?e[t-1]:n._tree.thread}function wi(n,t){var e=n.children;if(e&&(i=e.length))for(var r,i,u=-1;++u<i;)t(r=wi(e[u],t),n)>0&&(n=r);return n}function Si(n,t){return n.x-t.x}function Ei(n,t){return t.x-n.x}function ki(n,t){return n.depth-t.depth}function Ai(n,t){function e(n,r){var i=n.children;if(i&&(a=i.length))for(var u,a,o=null,c=-1;++c<a;)u=i[c],e(u,o),o=u;t(n,r)}e(n,null)}function Ni(n){for(var t,e=0,r=0,i=n.children,u=i.length;--u>=0;)t=i[u]._tree,t.prelim+=e,t.mod+=e,e+=t.shift+(r+=t.change)}function qi(n,t,e){n=n._tree,t=t._tree;var r=e/(t.number-n.number);n.change+=r,t.change-=r,t.shift+=e,t.prelim+=e,t.mod+=e}function Ti(n,t,e){return n._tree.ancestor.parent==t.parent?n._tree.ancestor:e}function Ci(n,t){return n.value-t.value}function zi(n,t){var e=n._pack_next;n._pack_next=t,t._pack_prev=n,t._pack_next=e,e._pack_prev=t}function Di(n,t){n._pack_next=t,t._pack_prev=n}function ji(n,t){var e=t.x-n.x,r=t.y-n.y,i=n.r+t.r;return.999*i*i>e*e+r*r}function Li(n){function t(n){s=Math.min(n.x-n.r,s),f=Math.max(n.x+n.r,f),h=Math.min(n.y-n.r,h),g=Math.max(n.y+n.r,g)}if((e=n.children)&&(l=e.length)){var e,r,i,u,a,o,c,l,s=1/0,f=-1/0,h=1/0,g=-1/0;if(e.forEach(Hi),r=e[0],r.x=-r.r,r.y=0,t(r),l>1&&(i=e[1],i.x=i.r,i.y=0,t(i),l>2))for(u=e[2],Oi(r,i,u),t(u),zi(r,u),r._pack_prev=u,zi(u,i),i=r._pack_next,a=3;l>a;a++){Oi(r,i,u=e[a]);var p=0,m=1,d=1;for(o=i._pack_next;o!==i;o=o._pack_next,m++)if(ji(o,u)){p=1;break}if(1==p)for(c=r._pack_prev;c!==o._pack_prev&&!ji(c,u);c=c._pack_prev,d++);p?(d>m||m==d&&i.r<r.r?Di(r,i=o):Di(r=c,i),a--):(zi(r,u),i=u,t(u))}var v=(s+f)/2,y=(h+g)/2,M=0;for(a=0;l>a;a++)u=e[a],u.x-=v,u.y-=y,M=Math.max(M,u.r+Math.sqrt(u.x*u.x+u.y*u.y));n.r=M,e.forEach(Fi)}}function Hi(n){n._pack_next=n._pack_prev=n}function Fi(n){delete n._pack_next,delete n._pack_prev}function Pi(n,t,e,r){var i=n.children;if(n.x=t+=r*n.x,n.y=e+=r*n.y,n.r*=r,i)for(var u=-1,a=i.length;++u<a;)Pi(i[u],t,e,r)}function Oi(n,t,e){var r=n.r+e.r,i=t.x-n.x,u=t.y-n.y;if(r&&(i||u)){var a=t.r+e.r,o=i*i+u*u;a*=a,r*=r;var c=.5+(r-a)/(2*o),l=Math.sqrt(Math.max(0,2*a*(r+o)-(r-=o)*r-a*a))/(2*o);e.x=n.x+c*i+l*u,e.y=n.y+c*u-l*i}else e.x=n.x+r,e.y=n.y}function Yi(n){return 1+ya.max(n,function(n){return n.y})}function Ri(n){return n.reduce(function(n,t){return n+t.x},0)/n.length}function Ui(n){var t=n.children;return t&&t.length?Ui(t[0]):n}function Ii(n){var t,e=n.children;return e&&(t=e.length)?Ii(e[t-1]):n}function Vi(n){return{x:n.x,y:n.y,dx:n.dx,dy:n.dy}}function Xi(n,t){var e=n.x+t[3],r=n.y+t[0],i=n.dx-t[1]-t[3],u=n.dy-t[0]-t[2];return 0>i&&(e+=i/2,i=0),0>u&&(r+=u/2,u=0),{x:e,y:r,dx:i,dy:u}}function Zi(n){var t=n[0],e=n[n.length-1];return e>t?[t,e]:[e,t]}function Bi(n){return n.rangeExtent?n.rangeExtent():Zi(n.range())}function $i(n,t,e,r){var i=e(n[0],n[1]),u=r(t[0],t[1]);return function(n){return u(i(n))}}function Wi(n,t){var e,r=0,i=n.length-1,u=n[r],a=n[i];return u>a&&(e=r,r=i,i=e,e=u,u=a,a=e),n[r]=t.floor(u),n[i]=t.ceil(a),n}function Ji(n){return n?{floor:function(t){return Math.floor(t/n)*n},ceil:function(t){return Math.ceil(t/n)*n}}:kc}function Gi(n,t,e,r){var i=[],u=[],a=0,o=Math.min(n.length,t.length)-1;for(n[o]<n[0]&&(n=n.slice().reverse(),t=t.slice().reverse());++a<=o;)i.push(e(n[a-1],n[a])),u.push(r(t[a-1],t[a]));return function(t){var e=ya.bisect(n,t,1,o)-1;return u[e](i[e](t))}}function Ki(n,t,e,r){function i(){var i=Math.min(n.length,t.length)>2?Gi:$i,c=r?Wr:$r;return a=i(n,t,c,e),o=i(t,n,c,Sr),u}function u(n){return a(n)}var a,o;return u.invert=function(n){return o(n)},u.domain=function(t){return arguments.length?(n=t.map(Number),i()):n},u.range=function(n){return arguments.length?(t=n,i()):t},u.rangeRound=function(n){return u.range(n).interpolate(Ur)},u.clamp=function(n){return arguments.length?(r=n,i()):r},u.interpolate=function(n){return arguments.length?(e=n,i()):e},u.ticks=function(t){return ru(n,t)},u.tickFormat=function(t,e){return iu(n,t,e)},u.nice=function(t){return nu(n,t),i()},u.copy=function(){return Ki(n,t,e,r)},i()}function Qi(n,t){return ya.rebind(n,t,"range","rangeRound","interpolate","clamp")}function nu(n,t){return Wi(n,Ji(t?eu(n,t)[2]:tu(n)))}function tu(n){var t=Zi(n),e=t[1]-t[0];return Math.pow(10,Math.round(Math.log(e)/Math.LN10)-1)}function eu(n,t){var e=Zi(n),r=e[1]-e[0],i=Math.pow(10,Math.floor(Math.log(r/t)/Math.LN10)),u=t/r*i;return.15>=u?i*=10:.35>=u?i*=5:.75>=u&&(i*=2),e[0]=Math.ceil(e[0]/i)*i,e[1]=Math.floor(e[1]/i)*i+.5*i,e[2]=i,e}function ru(n,t){return ya.range.apply(ya,eu(n,t))}function iu(n,t,e){var r=-Math.floor(Math.log(eu(n,t)[2])/Math.LN10+.01);return ya.format(e?e.replace(wo,function(n,t,e,i,u,a,o,c,l,s){return[t,e,i,u,a,o,c,l||"."+(r-2*("%"===s)),s].join("")}):",."+r+"f")}function uu(n,t,e,r){function i(n){return(e?Math.log(0>n?0:n):-Math.log(n>0?0:-n))/Math.log(t)}function u(n){return e?Math.pow(t,n):-Math.pow(t,-n)}function a(t){return n(i(t))}return a.invert=function(t){return u(n.invert(t))},a.domain=function(t){return arguments.length?(e=t[0]>=0,n.domain((r=t.map(Number)).map(i)),a):r},a.base=function(e){return arguments.length?(t=+e,n.domain(r.map(i)),a):t},a.nice=function(){var t=Wi(r.map(i),e?Math:Nc);return n.domain(t),r=t.map(u),a},a.ticks=function(){var n=Zi(r),a=[],o=n[0],c=n[1],l=Math.floor(i(o)),s=Math.ceil(i(c)),f=t%1?2:t;if(isFinite(s-l)){if(e){for(;s>l;l++)for(var h=1;f>h;h++)a.push(u(l)*h);a.push(u(l))}else for(a.push(u(l));l++<s;)for(var h=f-1;h>0;h--)a.push(u(l)*h);for(l=0;a[l]<o;l++);for(s=a.length;a[s-1]>c;s--);a=a.slice(l,s)}return a},a.tickFormat=function(n,t){if(!arguments.length)return Ac;arguments.length<2?t=Ac:"function"!=typeof t&&(t=ya.format(t));var r,o=Math.max(.1,n/a.ticks().length),c=e?(r=1e-12,Math.ceil):(r=-1e-12,Math.floor);return function(n){return n/u(c(i(n)+r))<=o?t(n):""}},a.copy=function(){return uu(n.copy(),t,e,r)},Qi(a,n)}function au(n,t,e){function r(t){return n(i(t))}var i=ou(t),u=ou(1/t);return r.invert=function(t){return u(n.invert(t))},r.domain=function(t){return arguments.length?(n.domain((e=t.map(Number)).map(i)),r):e},r.ticks=function(n){return ru(e,n)},r.tickFormat=function(n,t){return iu(e,n,t)},r.nice=function(n){return r.domain(nu(e,n))},r.exponent=function(a){return arguments.length?(i=ou(t=a),u=ou(1/t),n.domain(e.map(i)),r):t},r.copy=function(){return au(n.copy(),t,e)},Qi(r,n)}function ou(n){return function(t){return 0>t?-Math.pow(-t,n):Math.pow(t,n)}}function cu(n,t){function e(t){return a[((u.get(t)||u.set(t,n.push(t)))-1)%a.length]}function r(t,e){return ya.range(n.length).map(function(n){return t+e*n})}var u,a,o;return e.domain=function(r){if(!arguments.length)return n;n=[],u=new i;for(var a,o=-1,c=r.length;++o<c;)u.has(a=r[o])||u.set(a,n.push(a));return e[t.t].apply(e,t.a)},e.range=function(n){return arguments.length?(a=n,o=0,t={t:"range",a:arguments},e):a},e.rangePoints=function(i,u){arguments.length<2&&(u=0);var c=i[0],l=i[1],s=(l-c)/(Math.max(1,n.length-1)+u);return a=r(n.length<2?(c+l)/2:c+s*u/2,s),o=0,t={t:"rangePoints",a:arguments},e},e.rangeBands=function(i,u,c){arguments.length<2&&(u=0),arguments.length<3&&(c=u);var l=i[1]<i[0],s=i[l-0],f=i[1-l],h=(f-s)/(n.length-u+2*c);return a=r(s+h*c,h),l&&a.reverse(),o=h*(1-u),t={t:"rangeBands",a:arguments},e},e.rangeRoundBands=function(i,u,c){arguments.length<2&&(u=0),arguments.length<3&&(c=u);var l=i[1]<i[0],s=i[l-0],f=i[1-l],h=Math.floor((f-s)/(n.length-u+2*c)),g=f-s-(n.length-u)*h;return a=r(s+Math.round(g/2),h),l&&a.reverse(),o=Math.round(h*(1-u)),t={t:"rangeRoundBands",a:arguments},e},e.rangeBand=function(){return o},e.rangeExtent=function(){return Zi(t.a[0])},e.copy=function(){return cu(n,t)},e.domain(n)}function lu(n,t){function e(){var e=0,u=t.length;for(i=[];++e<u;)i[e-1]=ya.quantile(n,e/u);return r}function r(n){return isNaN(n=+n)?void 0:t[ya.bisect(i,n)]}var i;return r.domain=function(t){return arguments.length?(n=t.filter(function(n){return!isNaN(n)}).sort(ya.ascending),e()):n},r.range=function(n){return arguments.length?(t=n,e()):t},r.quantiles=function(){return i},r.invertExtent=function(e){return e=t.indexOf(e),0>e?[0/0,0/0]:[e>0?i[e-1]:n[0],e<i.length?i[e]:n[n.length-1]]},r.copy=function(){return lu(n,t)},e()}function su(n,t,e){function r(t){return e[Math.max(0,Math.min(a,Math.floor(u*(t-n))))]}function i(){return u=e.length/(t-n),a=e.length-1,r}var u,a;return r.domain=function(e){return arguments.length?(n=+e[0],t=+e[e.length-1],i()):[n,t]},r.range=function(n){return arguments.length?(e=n,i()):e},r.invertExtent=function(t){return t=e.indexOf(t),t=0>t?0/0:t/u+n,[t,t+1/u]},r.copy=function(){return su(n,t,e)},i()}function fu(n,t){function e(e){return e>=e?t[ya.bisect(n,e)]:void 0}return e.domain=function(t){return arguments.length?(n=t,e):n},e.range=function(n){return arguments.length?(t=n,e):t},e.invertExtent=function(e){return e=t.indexOf(e),[n[e-1],n[e]]},e.copy=function(){return fu(n,t)},e}function hu(n){function t(n){return+n}return t.invert=t,t.domain=t.range=function(e){return arguments.length?(n=e.map(t),t):n},t.ticks=function(t){return ru(n,t)},t.tickFormat=function(t,e){return iu(n,t,e)},t.copy=function(){return hu(n)},t}function gu(n){return n.innerRadius}function pu(n){return n.outerRadius}function mu(n){return n.startAngle}function du(n){return n.endAngle}function vu(n){for(var t,e,r,i=-1,u=n.length;++i<u;)t=n[i],e=t[0],r=t[1]+Dc,t[0]=e*Math.cos(r),t[1]=e*Math.sin(r);return n}function yu(n){function t(t){function c(){m.push("M",o(n(v),f),s,l(n(d.reverse()),f),"Z")}for(var h,g,p,m=[],d=[],v=[],y=-1,M=t.length,x=pt(e),b=pt(i),_=e===r?function(){return g}:pt(r),w=i===u?function(){return p}:pt(u);++y<M;)a.call(this,h=t[y],y)?(d.push([g=+x.call(this,h,y),p=+b.call(this,h,y)]),v.push([+_.call(this,h,y),+w.call(this,h,y)])):d.length&&(c(),d=[],v=[]);return d.length&&c(),m.length?m.join(""):null}var e=Ie,r=Ie,i=0,u=Ve,a=Xt,o=Xe,c=o.key,l=o,s="L",f=.7;return t.x=function(n){return arguments.length?(e=r=n,t):r},t.x0=function(n){return arguments.length?(e=n,t):e},t.x1=function(n){return arguments.length?(r=n,t):r},t.y=function(n){return arguments.length?(i=u=n,t):u},t.y0=function(n){return arguments.length?(i=n,t):i},t.y1=function(n){return arguments.length?(u=n,t):u},t.defined=function(n){return arguments.length?(a=n,t):a},t.interpolate=function(n){return arguments.length?(c="function"==typeof n?o=n:(o=sc.get(n)||Xe).key,l=o.reverse||o,s=o.closed?"M":"L",t):c},t.tension=function(n){return arguments.length?(f=n,t):f},t}function Mu(n){return n.radius}function xu(n){return[n.x,n.y]}function bu(n){return function(){var t=n.apply(this,arguments),e=t[0],r=t[1]+Dc;return[e*Math.cos(r),e*Math.sin(r)]}}function _u(){return 64}function wu(){return"circle"}function Su(n){var t=Math.sqrt(n/Ka);return"M0,"+t+"A"+t+","+t+" 0 1,1 0,"+-t+"A"+t+","+t+" 0 1,1 0,"+t+"Z"}function Eu(n,t){return La(n,Yc),n.id=t,n}function ku(n,t,e,r){var i=n.id;return T(n,"function"==typeof e?function(n,u,a){n.__transition__[i].tween.set(t,r(e.call(n,n.__data__,u,a)))}:(e=r(e),function(n){n.__transition__[i].tween.set(t,e)}))}function Au(n){return null==n&&(n=""),function(){this.textContent=n}}function Nu(n,t,e,r){var u=n.__transition__||(n.__transition__={active:0,count:0}),a=u[e];if(!a){var o=r.time;a=u[e]={tween:new i,time:o,ease:r.ease,delay:r.delay,duration:r.duration},++u.count,ya.timer(function(r){function i(r){return u.active>e?l():(u.active=e,a.event&&a.event.start.call(n,s,t),a.tween.forEach(function(e,r){(r=r.call(n,s,t))&&p.push(r)}),c(r)?1:(xt(c,0,o),void 0))}function c(r){if(u.active!==e)return l();for(var i=(r-h)/g,o=f(i),c=p.length;c>0;)p[--c].call(n,o);return i>=1?(l(),a.event&&a.event.end.call(n,s,t),1):void 0}function l(){return--u.count?delete u[e]:delete n.__transition__,1}var s=n.__data__,f=a.ease,h=a.delay,g=a.duration,p=[];return r>=h?i(r):(xt(i,h,o),void 0)},0,o)}}function qu(n,t){n.attr("transform",function(n){return"translate("+t(n)+",0)"})}function Tu(n,t){n.attr("transform",function(n){return"translate(0,"+t(n)+")"})}function Cu(n,t,e){if(r=[],e&&t.length>1){for(var r,i,u,a=Zi(n.domain()),o=-1,c=t.length,l=(t[1]-t[0])/++e;++o<c;)for(i=e;--i>0;)(u=+t[o]-i*l)>=a[0]&&r.push(u);for(--o,i=0;++i<e&&(u=+t[o]+i*l)<a[1];)r.push(u)}return r}function zu(){this._=new Date(arguments.length>1?Date.UTC.apply(this,arguments):arguments[0])}function Du(n,t,e){function r(t){var e=n(t),r=u(e,1);return r-t>t-e?e:r}function i(e){return t(e=n(new Zc(e-1)),1),e}function u(n,e){return t(n=new Zc(+n),e),n}function a(n,r,u){var a=i(n),o=[];if(u>1)for(;r>a;)e(a)%u||o.push(new Date(+a)),t(a,1);else for(;r>a;)o.push(new Date(+a)),t(a,1);return o}function o(n,t,e){try{Zc=zu;var r=new zu;return r._=n,a(r,t,e)}finally{Zc=Date}}n.floor=n,n.round=r,n.ceil=i,n.offset=u,n.range=a;var c=n.utc=ju(n);return c.floor=c,c.round=ju(r),c.ceil=ju(i),c.offset=ju(u),c.range=o,n}function ju(n){return function(t,e){try{Zc=zu;var r=new zu;return r._=t,n(r,e)._}finally{Zc=Date}}}function Lu(n,t,e,r){for(var i,u,a=0,o=t.length,c=e.length;o>a;){if(r>=c)return-1;if(i=t.charCodeAt(a++),37===i){if(u=gl[t.charAt(a++)],!u||(r=u(n,e,r))<0)return-1}else if(i!=e.charCodeAt(r++))return-1}return r}function Hu(n){return new RegExp("^(?:"+n.map(ya.requote).join("|")+")","i")}function Fu(n){for(var t=new i,e=-1,r=n.length;++e<r;)t.set(n[e].toLowerCase(),e);return t}function Pu(n,t,e){var r=0>n?"-":"",i=(r?-n:n)+"",u=i.length;return r+(e>u?new Array(e-u+1).join(t)+i:i)}function Ou(n,t,e){il.lastIndex=0;var r=il.exec(t.substring(e));return r?(n.w=ul.get(r[0].toLowerCase()),e+r[0].length):-1}function Yu(n,t,e){el.lastIndex=0;var r=el.exec(t.substring(e));return r?(n.w=rl.get(r[0].toLowerCase()),e+r[0].length):-1}function Ru(n,t,e){pl.lastIndex=0;var r=pl.exec(t.substring(e,e+1));return r?(n.w=+r[0],e+r[0].length):-1}function Uu(n,t,e){pl.lastIndex=0;var r=pl.exec(t.substring(e));return r?(n.U=+r[0],e+r[0].length):-1}function Iu(n,t,e){pl.lastIndex=0;var r=pl.exec(t.substring(e));return r?(n.W=+r[0],e+r[0].length):-1}function Vu(n,t,e){cl.lastIndex=0;var r=cl.exec(t.substring(e));return r?(n.m=ll.get(r[0].toLowerCase()),e+r[0].length):-1}function Xu(n,t,e){al.lastIndex=0;var r=al.exec(t.substring(e));return r?(n.m=ol.get(r[0].toLowerCase()),e+r[0].length):-1}function Zu(n,t,e){return Lu(n,hl.c.toString(),t,e)}function Bu(n,t,e){return Lu(n,hl.x.toString(),t,e)}function $u(n,t,e){return Lu(n,hl.X.toString(),t,e)}function Wu(n,t,e){pl.lastIndex=0;var r=pl.exec(t.substring(e,e+4));return r?(n.y=+r[0],e+r[0].length):-1}function Ju(n,t,e){pl.lastIndex=0;var r=pl.exec(t.substring(e,e+2));return r?(n.y=Gu(+r[0]),e+r[0].length):-1}function Gu(n){return n+(n>68?1900:2e3)}function Ku(n,t,e){pl.lastIndex=0;var r=pl.exec(t.substring(e,e+2));return r?(n.m=r[0]-1,e+r[0].length):-1}function Qu(n,t,e){pl.lastIndex=0;var r=pl.exec(t.substring(e,e+2));return r?(n.d=+r[0],e+r[0].length):-1}function na(n,t,e){pl.lastIndex=0;var r=pl.exec(t.substring(e,e+3));return r?(n.j=+r[0],e+r[0].length):-1}function ta(n,t,e){pl.lastIndex=0;var r=pl.exec(t.substring(e,e+2));return r?(n.H=+r[0],e+r[0].length):-1}function ea(n,t,e){pl.lastIndex=0;var r=pl.exec(t.substring(e,e+2));return r?(n.M=+r[0],e+r[0].length):-1}function ra(n,t,e){pl.lastIndex=0;var r=pl.exec(t.substring(e,e+2));return r?(n.S=+r[0],e+r[0].length):-1}function ia(n,t,e){pl.lastIndex=0;var r=pl.exec(t.substring(e,e+3));return r?(n.L=+r[0],e+r[0].length):-1}function ua(n,t,e){var r=ml.get(t.substring(e,e+=2).toLowerCase());return null==r?-1:(n.p=r,e)}function aa(n){var t=n.getTimezoneOffset(),e=t>0?"-":"+",r=~~(Math.abs(t)/60),i=Math.abs(t)%60;return e+Pu(r,"0",2)+Pu(i,"0",2)}function oa(n,t,e){sl.lastIndex=0;var r=sl.exec(t.substring(e,e+1));return r?e+r[0].length:-1}function ca(n){return n.toISOString()}function la(n,t,e){function r(t){return n(t)}return r.invert=function(t){return sa(n.invert(t))},r.domain=function(t){return arguments.length?(n.domain(t),r):n.domain().map(sa)},r.nice=function(n){return r.domain(Wi(r.domain(),n))},r.ticks=function(e,i){var u=Zi(r.domain());if("function"!=typeof e){var a=u[1]-u[0],o=a/e,c=ya.bisect(vl,o);if(c==vl.length)return t.year(u,e);if(!c)return n.ticks(e).map(sa);o/vl[c-1]<vl[c]/o&&--c,e=t[c],i=e[1],e=e[0].range}return e(u[0],new Date(+u[1]+1),i)},r.tickFormat=function(){return e},r.copy=function(){return la(n.copy(),t,e)},Qi(r,n)}function sa(n){return new Date(n)}function fa(n){return function(t){for(var e=n.length-1,r=n[e];!r[1](t);)r=n[--e];return r[0](t)}}function ha(n){var t=new Date(n,0,1);return t.setFullYear(n),t}function ga(n){var t=n.getFullYear(),e=ha(t),r=ha(t+1);return t+(n-e)/(r-e)}function pa(n){var t=new Date(Date.UTC(n,0,1));return t.setUTCFullYear(n),t}function ma(n){var t=n.getUTCFullYear(),e=pa(t),r=pa(t+1);return t+(n-e)/(r-e)}function da(n){return JSON.parse(n.responseText)}function va(n){var t=Ma.createRange();return t.selectNode(Ma.body),t.createContextualFragment(n.responseText)}var ya={version:"3.2.8"};Date.now||(Date.now=function(){return+new Date});var Ma=document,xa=Ma.documentElement,ba=window;try{Ma.createElement("div").style.setProperty("opacity",0,"")}catch(_a){var wa=ba.Element.prototype,Sa=wa.setAttribute,Ea=wa.setAttributeNS,ka=ba.CSSStyleDeclaration.prototype,Aa=ka.setProperty;wa.setAttribute=function(n,t){Sa.call(this,n,t+"")},wa.setAttributeNS=function(n,t,e){Ea.call(this,n,t,e+"")},ka.setProperty=function(n,t,e){Aa.call(this,n,t+"",e)}}ya.ascending=function(n,t){return t>n?-1:n>t?1:n>=t?0:0/0},ya.descending=function(n,t){return n>t?-1:t>n?1:t>=n?0:0/0},ya.min=function(n,t){var e,r,i=-1,u=n.length;if(1===arguments.length){for(;++i<u&&!(null!=(e=n[i])&&e>=e);)e=void 0;for(;++i<u;)null!=(r=n[i])&&e>r&&(e=r)}else{for(;++i<u&&!(null!=(e=t.call(n,n[i],i))&&e>=e);)e=void 0;for(;++i<u;)null!=(r=t.call(n,n[i],i))&&e>r&&(e=r)}return e},ya.max=function(n,t){var e,r,i=-1,u=n.length;if(1===arguments.length){for(;++i<u&&!(null!=(e=n[i])&&e>=e);)e=void 0;for(;++i<u;)null!=(r=n[i])&&r>e&&(e=r)}else{for(;++i<u&&!(null!=(e=t.call(n,n[i],i))&&e>=e);)e=void 0;for(;++i<u;)null!=(r=t.call(n,n[i],i))&&r>e&&(e=r)}return e},ya.extent=function(n,t){var e,r,i,u=-1,a=n.length;if(1===arguments.length){for(;++u<a&&!(null!=(e=i=n[u])&&e>=e);)e=i=void 0;for(;++u<a;)null!=(r=n[u])&&(e>r&&(e=r),r>i&&(i=r))}else{for(;++u<a&&!(null!=(e=i=t.call(n,n[u],u))&&e>=e);)e=void 0;for(;++u<a;)null!=(r=t.call(n,n[u],u))&&(e>r&&(e=r),r>i&&(i=r))}return[e,i]},ya.sum=function(n,t){var e,r=0,i=n.length,u=-1;if(1===arguments.length)for(;++u<i;)isNaN(e=+n[u])||(r+=e);else for(;++u<i;)isNaN(e=+t.call(n,n[u],u))||(r+=e);return r},ya.mean=function(t,e){var r,i=t.length,u=0,a=-1,o=0;if(1===arguments.length)for(;++a<i;)n(r=t[a])&&(u+=(r-u)/++o);else for(;++a<i;)n(r=e.call(t,t[a],a))&&(u+=(r-u)/++o);return o?u:void 0},ya.quantile=function(n,t){var e=(n.length-1)*t+1,r=Math.floor(e),i=+n[r-1],u=e-r;return u?i+u*(n[r]-i):i},ya.median=function(t,e){return arguments.length>1&&(t=t.map(e)),t=t.filter(n),t.length?ya.quantile(t.sort(ya.ascending),.5):void 0},ya.bisector=function(n){return{left:function(t,e,r,i){for(arguments.length<3&&(r=0),arguments.length<4&&(i=t.length);i>r;){var u=r+i>>>1;n.call(t,t[u],u)<e?r=u+1:i=u}return r},right:function(t,e,r,i){for(arguments.length<3&&(r=0),arguments.length<4&&(i=t.length);i>r;){var u=r+i>>>1;e<n.call(t,t[u],u)?i=u:r=u+1}return r}}};var Na=ya.bisector(function(n){return n});ya.bisectLeft=Na.left,ya.bisect=ya.bisectRight=Na.right,ya.shuffle=function(n){for(var t,e,r=n.length;r;)e=0|Math.random()*r--,t=n[r],n[r]=n[e],n[e]=t;return n},ya.permute=function(n,t){for(var e=t.length,r=new Array(e);e--;)r[e]=n[t[e]];return r},ya.zip=function(){if(!(i=arguments.length))return[];for(var n=-1,e=ya.min(arguments,t),r=new Array(e);++n<e;)for(var i,u=-1,a=r[n]=new Array(i);++u<i;)a[u]=arguments[u][n];return r},ya.transpose=function(n){return ya.zip.apply(ya,n)},ya.keys=function(n){var t=[];for(var e in n)t.push(e);return t},ya.values=function(n){var t=[];for(var e in n)t.push(n[e]);return t},ya.entries=function(n){var t=[];for(var e in n)t.push({key:e,value:n[e]});return t},ya.merge=function(n){return Array.prototype.concat.apply([],n)},ya.range=function(n,t,r){if(arguments.length<3&&(r=1,arguments.length<2&&(t=n,n=0)),1/0===(t-n)/r)throw new Error("infinite range");var i,u=[],a=e(Math.abs(r)),o=-1;if(n*=a,t*=a,r*=a,0>r)for(;(i=n+r*++o)>t;)u.push(i/a);else for(;(i=n+r*++o)<t;)u.push(i/a);return u},ya.map=function(n){var t=new i;if(n instanceof i)n.forEach(function(n,e){t.set(n,e)});else for(var e in n)t.set(e,n[e]);return t},r(i,{has:function(n){return qa+n in this},get:function(n){return this[qa+n]},set:function(n,t){return this[qa+n]=t},remove:function(n){return n=qa+n,n in this&&delete this[n]},keys:function(){var n=[];return this.forEach(function(t){n.push(t)}),n},values:function(){var n=[];return this.forEach(function(t,e){n.push(e)}),n},entries:function(){var n=[];
3
+ return this.forEach(function(t,e){n.push({key:t,value:e})}),n},forEach:function(n){for(var t in this)t.charCodeAt(0)===Ta&&n.call(this,t.substring(1),this[t])}});var qa="\0",Ta=qa.charCodeAt(0);ya.nest=function(){function n(t,o,c){if(c>=a.length)return r?r.call(u,o):e?o.sort(e):o;for(var l,s,f,h,g=-1,p=o.length,m=a[c++],d=new i;++g<p;)(h=d.get(l=m(s=o[g])))?h.push(s):d.set(l,[s]);return t?(s=t(),f=function(e,r){s.set(e,n(t,r,c))}):(s={},f=function(e,r){s[e]=n(t,r,c)}),d.forEach(f),s}function t(n,e){if(e>=a.length)return n;var r=[],i=o[e++];return n.forEach(function(n,i){r.push({key:n,values:t(i,e)})}),i?r.sort(function(n,t){return i(n.key,t.key)}):r}var e,r,u={},a=[],o=[];return u.map=function(t,e){return n(e,t,0)},u.entries=function(e){return t(n(ya.map,e,0),0)},u.key=function(n){return a.push(n),u},u.sortKeys=function(n){return o[a.length-1]=n,u},u.sortValues=function(n){return e=n,u},u.rollup=function(n){return r=n,u},u},ya.set=function(n){var t=new u;if(n)for(var e=0,r=n.length;r>e;++e)t.add(n[e]);return t},r(u,{has:function(n){return qa+n in this},add:function(n){return this[qa+n]=!0,n},remove:function(n){return n=qa+n,n in this&&delete this[n]},values:function(){var n=[];return this.forEach(function(t){n.push(t)}),n},forEach:function(n){for(var t in this)t.charCodeAt(0)===Ta&&n.call(this,t.substring(1))}}),ya.behavior={},ya.rebind=function(n,t){for(var e,r=1,i=arguments.length;++r<i;)n[e=arguments[r]]=a(n,t,t[e]);return n};var Ca=["webkit","ms","moz","Moz","o","O"],za=l;try{za(xa.childNodes)[0].nodeType}catch(Da){za=c}ya.dispatch=function(){for(var n=new f,t=-1,e=arguments.length;++t<e;)n[arguments[t]]=h(n);return n},f.prototype.on=function(n,t){var e=n.indexOf("."),r="";if(e>=0&&(r=n.substring(e+1),n=n.substring(0,e)),n)return arguments.length<2?this[n].on(r):this[n].on(r,t);if(2===arguments.length){if(null==t)for(n in this)this.hasOwnProperty(n)&&this[n].on(r,null);return this}},ya.event=null,ya.requote=function(n){return n.replace(ja,"\\$&")};var ja=/[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g,La={}.__proto__?function(n,t){n.__proto__=t}:function(n,t){for(var e in t)n[e]=t[e]},Ha=function(n,t){return t.querySelector(n)},Fa=function(n,t){return t.querySelectorAll(n)},Pa=xa[o(xa,"matchesSelector")],Oa=function(n,t){return Pa.call(n,t)};"function"==typeof Sizzle&&(Ha=function(n,t){return Sizzle(n,t)[0]||null},Fa=function(n,t){return Sizzle.uniqueSort(Sizzle(n,t))},Oa=Sizzle.matchesSelector),ya.selection=function(){return Ia};var Ya=ya.selection.prototype=[];Ya.select=function(n){var t,e,r,i,u=[];n=v(n);for(var a=-1,o=this.length;++a<o;){u.push(t=[]),t.parentNode=(r=this[a]).parentNode;for(var c=-1,l=r.length;++c<l;)(i=r[c])?(t.push(e=n.call(i,i.__data__,c,a)),e&&"__data__"in i&&(e.__data__=i.__data__)):t.push(null)}return d(u)},Ya.selectAll=function(n){var t,e,r=[];n=y(n);for(var i=-1,u=this.length;++i<u;)for(var a=this[i],o=-1,c=a.length;++o<c;)(e=a[o])&&(r.push(t=za(n.call(e,e.__data__,o,i))),t.parentNode=e);return d(r)};var Ra={svg:"http://www.w3.org/2000/svg",xhtml:"http://www.w3.org/1999/xhtml",xlink:"http://www.w3.org/1999/xlink",xml:"http://www.w3.org/XML/1998/namespace",xmlns:"http://www.w3.org/2000/xmlns/"};ya.ns={prefix:Ra,qualify:function(n){var t=n.indexOf(":"),e=n;return t>=0&&(e=n.substring(0,t),n=n.substring(t+1)),Ra.hasOwnProperty(e)?{space:Ra[e],local:n}:n}},Ya.attr=function(n,t){if(arguments.length<2){if("string"==typeof n){var e=this.node();return n=ya.ns.qualify(n),n.local?e.getAttributeNS(n.space,n.local):e.getAttribute(n)}for(t in n)this.each(M(t,n[t]));return this}return this.each(M(n,t))},Ya.classed=function(n,t){if(arguments.length<2){if("string"==typeof n){var e=this.node(),r=(n=n.trim().split(/^|\s+/g)).length,i=-1;if(t=e.classList){for(;++i<r;)if(!t.contains(n[i]))return!1}else for(t=e.getAttribute("class");++i<r;)if(!b(n[i]).test(t))return!1;return!0}for(t in n)this.each(_(t,n[t]));return this}return this.each(_(n,t))},Ya.style=function(n,t,e){var r=arguments.length;if(3>r){if("string"!=typeof n){2>r&&(t="");for(e in n)this.each(S(e,n[e],t));return this}if(2>r)return ba.getComputedStyle(this.node(),null).getPropertyValue(n);e=""}return this.each(S(n,t,e))},Ya.property=function(n,t){if(arguments.length<2){if("string"==typeof n)return this.node()[n];for(t in n)this.each(E(t,n[t]));return this}return this.each(E(n,t))},Ya.text=function(n){return arguments.length?this.each("function"==typeof n?function(){var t=n.apply(this,arguments);this.textContent=null==t?"":t}:null==n?function(){this.textContent=""}:function(){this.textContent=n}):this.node().textContent},Ya.html=function(n){return arguments.length?this.each("function"==typeof n?function(){var t=n.apply(this,arguments);this.innerHTML=null==t?"":t}:null==n?function(){this.innerHTML=""}:function(){this.innerHTML=n}):this.node().innerHTML},Ya.append=function(n){return n=k(n),this.select(function(){return this.appendChild(n.apply(this,arguments))})},Ya.insert=function(n,t){return n=k(n),t=v(t),this.select(function(){return this.insertBefore(n.apply(this,arguments),t.apply(this,arguments))})},Ya.remove=function(){return this.each(function(){var n=this.parentNode;n&&n.removeChild(this)})},Ya.data=function(n,t){function e(n,e){var r,u,a,o=n.length,f=e.length,h=Math.min(o,f),g=new Array(f),p=new Array(f),m=new Array(o);if(t){var d,v=new i,y=new i,M=[];for(r=-1;++r<o;)d=t.call(u=n[r],u.__data__,r),v.has(d)?m[r]=u:v.set(d,u),M.push(d);for(r=-1;++r<f;)d=t.call(e,a=e[r],r),(u=v.get(d))?(g[r]=u,u.__data__=a):y.has(d)||(p[r]=A(a)),y.set(d,a),v.remove(d);for(r=-1;++r<o;)v.has(M[r])&&(m[r]=n[r])}else{for(r=-1;++r<h;)u=n[r],a=e[r],u?(u.__data__=a,g[r]=u):p[r]=A(a);for(;f>r;++r)p[r]=A(e[r]);for(;o>r;++r)m[r]=n[r]}p.update=g,p.parentNode=g.parentNode=m.parentNode=n.parentNode,c.push(p),l.push(g),s.push(m)}var r,u,a=-1,o=this.length;if(!arguments.length){for(n=new Array(o=(r=this[0]).length);++a<o;)(u=r[a])&&(n[a]=u.__data__);return n}var c=C([]),l=d([]),s=d([]);if("function"==typeof n)for(;++a<o;)e(r=this[a],n.call(r,r.parentNode.__data__,a));else for(;++a<o;)e(r=this[a],n);return l.enter=function(){return c},l.exit=function(){return s},l},Ya.datum=function(n){return arguments.length?this.property("__data__",n):this.property("__data__")},Ya.filter=function(n){var t,e,r,i=[];"function"!=typeof n&&(n=N(n));for(var u=0,a=this.length;a>u;u++){i.push(t=[]),t.parentNode=(e=this[u]).parentNode;for(var o=0,c=e.length;c>o;o++)(r=e[o])&&n.call(r,r.__data__,o)&&t.push(r)}return d(i)},Ya.order=function(){for(var n=-1,t=this.length;++n<t;)for(var e,r=this[n],i=r.length-1,u=r[i];--i>=0;)(e=r[i])&&(u&&u!==e.nextSibling&&u.parentNode.insertBefore(e,u),u=e);return this},Ya.sort=function(n){n=q.apply(this,arguments);for(var t=-1,e=this.length;++t<e;)this[t].sort(n);return this.order()},Ya.each=function(n){return T(this,function(t,e,r){n.call(t,t.__data__,e,r)})},Ya.call=function(n){var t=za(arguments);return n.apply(t[0]=this,t),this},Ya.empty=function(){return!this.node()},Ya.node=function(){for(var n=0,t=this.length;t>n;n++)for(var e=this[n],r=0,i=e.length;i>r;r++){var u=e[r];if(u)return u}return null},Ya.size=function(){var n=0;return this.each(function(){++n}),n};var Ua=[];ya.selection.enter=C,ya.selection.enter.prototype=Ua,Ua.append=Ya.append,Ua.empty=Ya.empty,Ua.node=Ya.node,Ua.call=Ya.call,Ua.size=Ya.size,Ua.select=function(n){for(var t,e,r,i,u,a=[],o=-1,c=this.length;++o<c;){r=(i=this[o]).update,a.push(t=[]),t.parentNode=i.parentNode;for(var l=-1,s=i.length;++l<s;)(u=i[l])?(t.push(r[l]=e=n.call(i.parentNode,u.__data__,l,o)),e.__data__=u.__data__):t.push(null)}return d(a)},Ua.insert=function(n,t){return arguments.length<2&&(t=z(this)),Ya.insert.call(this,n,t)},Ya.transition=function(){for(var n,t,e=Hc||++Rc,r=[],i=Fc||{time:Date.now(),ease:Cr,delay:0,duration:250},u=-1,a=this.length;++u<a;){r.push(n=[]);for(var o=this[u],c=-1,l=o.length;++c<l;)(t=o[c])&&Nu(t,c,e,i),n.push(t)}return Eu(r,e)},ya.select=function(n){var t=["string"==typeof n?Ha(n,Ma):n];return t.parentNode=xa,d([t])},ya.selectAll=function(n){var t=za("string"==typeof n?Fa(n,Ma):n);return t.parentNode=xa,d([t])};var Ia=ya.select(xa);Ya.on=function(n,t,e){var r=arguments.length;if(3>r){if("string"!=typeof n){2>r&&(t=!1);for(e in n)this.each(D(e,n[e],t));return this}if(2>r)return(r=this.node()["__on"+n])&&r._;e=!1}return this.each(D(n,t,e))};var Va=ya.map({mouseenter:"mouseover",mouseleave:"mouseout"});Va.forEach(function(n){"on"+n in Ma&&Va.remove(n)});var Xa=o(xa.style,"userSelect"),Za=0;ya.mouse=function(n){return F(n,p())};var Ba=/WebKit/.test(ba.navigator.userAgent)?-1:0;ya.touches=function(n,t){return arguments.length<2&&(t=p().touches),t?za(t).map(function(t){var e=F(n,t);return e.identifier=t.identifier,e}):[]},ya.behavior.drag=function(){function n(){this.on("mousedown.drag",a).on("touchstart.drag",o)}function t(){return ya.event.changedTouches[0].identifier}function e(n,t){return ya.touches(n).filter(function(n){return n.identifier===t})[0]}function r(n,t,e,r){return function(){function a(){if(!s)return o();var n=t(s,g),e=n[0]-m[0],r=n[1]-m[1];d|=e|r,m=n,f({type:"drag",x:n[0]+c[0],y:n[1]+c[1],dx:e,dy:r})}function o(){v.on(e+"."+p,null).on(r+"."+p,null),y(d&&ya.event.target===h),f({type:"dragend"})}var c,l=this,s=l.parentNode,f=i.of(l,arguments),h=ya.event.target,g=n(),p=null==g?"drag":"drag-"+g,m=t(s,g),d=0,v=ya.select(ba).on(e+"."+p,a).on(r+"."+p,o),y=H();u?(c=u.apply(l,arguments),c=[c.x-m[0],c.y-m[1]]):c=[0,0],f({type:"dragstart"})}}var i=m(n,"drag","dragstart","dragend"),u=null,a=r(s,ya.mouse,"mousemove","mouseup"),o=r(t,e,"touchmove","touchend");return n.origin=function(t){return arguments.length?(u=t,n):u},ya.rebind(n,i,"on")},ya.behavior.zoom=function(){function n(){this.on(w,o).on(Ja+".zoom",l).on(S,s).on("dblclick.zoom",f).on(k,c)}function t(n){return[(n[0]-x[0])/b,(n[1]-x[1])/b]}function e(n){return[n[0]*b+x[0],n[1]*b+x[1]]}function r(n){b=Math.max(_[0],Math.min(_[1],n))}function i(n,t){t=e(t),x[0]+=n[0]-t[0],x[1]+=n[1]-t[1]}function u(){v&&v.domain(d.range().map(function(n){return(n-x[0])/b}).map(d.invert)),M&&M.domain(y.range().map(function(n){return(n-x[1])/b}).map(y.invert))}function a(n){u(),n({type:"zoom",scale:b,translate:x})}function o(){function n(){c=1,i(ya.mouse(r),f),a(u)}function e(){l.on(S,ba===r?s:null).on(E,null),h(c&&ya.event.target===o)}var r=this,u=q.of(r,arguments),o=ya.event.target,c=0,l=ya.select(ba).on(S,n).on(E,e),f=t(ya.mouse(r)),h=H()}function c(){function n(){var n=ya.touches(h);return f=b,s={},n.forEach(function(n){s[n.identifier]=t(n)}),n}function e(){var t=Date.now(),e=n();if(1===e.length){if(500>t-p){var u=e[0],o=s[u.identifier];r(2*b),i(u,o),g(),a(m)}p=t}else if(e.length>1){var u=e[0],c=e[1],l=u[0]-c[0],f=u[1]-c[1];d=l*l+f*f}}function u(){var n=ya.touches(h),t=n[0],e=s[t.identifier];if(u=n[1]){var u,o=s[u.identifier],c=ya.event.scale;if(null==c){var l=(l=u[0]-t[0])*l+(l=u[1]-t[1])*l;c=d&&Math.sqrt(l/d)}t=[(t[0]+u[0])/2,(t[1]+u[1])/2],e=[(e[0]+o[0])/2,(e[1]+o[1])/2],r(c*f)}p=null,i(t,e),a(m)}function l(){ya.event.touches.length?n():(v.on(A,null).on(N,null),y.on(w,o).on(k,c),M())}var s,f,h=this,m=q.of(h,arguments),d=0,v=ya.select(ba).on(A,u).on(N,l),y=ya.select(h).on(w,null).on(k,e),M=H();e()}function l(){g(),h||(h=t(ya.mouse(this))),r(Math.pow(2,.002*$a())*b),i(ya.mouse(this),h),a(q.of(this,arguments))}function s(){h=null}function f(){var n=ya.mouse(this),e=t(n),u=Math.log(b)/Math.LN2;r(Math.pow(2,ya.event.shiftKey?Math.ceil(u)-1:Math.floor(u)+1)),i(n,e),a(q.of(this,arguments))}var h,p,d,v,y,M,x=[0,0],b=1,_=Wa,w="mousedown.zoom",S="mousemove.zoom",E="mouseup.zoom",k="touchstart.zoom",A="touchmove.zoom",N="touchend.zoom",q=m(n,"zoom");return n.translate=function(t){return arguments.length?(x=t.map(Number),u(),n):x},n.scale=function(t){return arguments.length?(b=+t,u(),n):b},n.scaleExtent=function(t){return arguments.length?(_=null==t?Wa:t.map(Number),n):_},n.x=function(t){return arguments.length?(v=t,d=t.copy(),x=[0,0],b=1,n):v},n.y=function(t){return arguments.length?(M=t,y=t.copy(),x=[0,0],b=1,n):M},ya.rebind(n,q,"on")};var $a,Wa=[0,1/0],Ja="onwheel"in Ma?($a=function(){return-ya.event.deltaY*(ya.event.deltaMode?120:1)},"wheel"):"onmousewheel"in Ma?($a=function(){return ya.event.wheelDelta},"mousewheel"):($a=function(){return-ya.event.detail},"MozMousePixelScroll");P.prototype.toString=function(){return this.rgb()+""},ya.hsl=function(n,t,e){return 1===arguments.length?n instanceof Y?O(n.h,n.s,n.l):lt(""+n,st,O):O(+n,+t,+e)};var Ga=Y.prototype=new P;Ga.brighter=function(n){return n=Math.pow(.7,arguments.length?n:1),O(this.h,this.s,this.l/n)},Ga.darker=function(n){return n=Math.pow(.7,arguments.length?n:1),O(this.h,this.s,n*this.l)},Ga.rgb=function(){return R(this.h,this.s,this.l)};var Ka=Math.PI,Qa=1e-6,no=Qa*Qa,to=Ka/180,eo=180/Ka;ya.hcl=function(n,t,e){return 1===arguments.length?n instanceof W?$(n.h,n.c,n.l):n instanceof K?nt(n.l,n.a,n.b):nt((n=ft((n=ya.rgb(n)).r,n.g,n.b)).l,n.a,n.b):$(+n,+t,+e)};var ro=W.prototype=new P;ro.brighter=function(n){return $(this.h,this.c,Math.min(100,this.l+io*(arguments.length?n:1)))},ro.darker=function(n){return $(this.h,this.c,Math.max(0,this.l-io*(arguments.length?n:1)))},ro.rgb=function(){return J(this.h,this.c,this.l).rgb()},ya.lab=function(n,t,e){return 1===arguments.length?n instanceof K?G(n.l,n.a,n.b):n instanceof W?J(n.l,n.c,n.h):ft((n=ya.rgb(n)).r,n.g,n.b):G(+n,+t,+e)};var io=18,uo=.95047,ao=1,oo=1.08883,co=K.prototype=new P;co.brighter=function(n){return G(Math.min(100,this.l+io*(arguments.length?n:1)),this.a,this.b)},co.darker=function(n){return G(Math.max(0,this.l-io*(arguments.length?n:1)),this.a,this.b)},co.rgb=function(){return Q(this.l,this.a,this.b)},ya.rgb=function(n,t,e){return 1===arguments.length?n instanceof ot?at(n.r,n.g,n.b):lt(""+n,at,R):at(~~n,~~t,~~e)};var lo=ot.prototype=new P;lo.brighter=function(n){n=Math.pow(.7,arguments.length?n:1);var t=this.r,e=this.g,r=this.b,i=30;return t||e||r?(t&&i>t&&(t=i),e&&i>e&&(e=i),r&&i>r&&(r=i),at(Math.min(255,~~(t/n)),Math.min(255,~~(e/n)),Math.min(255,~~(r/n)))):at(i,i,i)},lo.darker=function(n){return n=Math.pow(.7,arguments.length?n:1),at(~~(n*this.r),~~(n*this.g),~~(n*this.b))},lo.hsl=function(){return st(this.r,this.g,this.b)},lo.toString=function(){return"#"+ct(this.r)+ct(this.g)+ct(this.b)};var so=ya.map({aliceblue:15792383,antiquewhite:16444375,aqua:65535,aquamarine:8388564,azure:15794175,beige:16119260,bisque:16770244,black:0,blanchedalmond:16772045,blue:255,blueviolet:9055202,brown:10824234,burlywood:14596231,cadetblue:6266528,chartreuse:8388352,chocolate:13789470,coral:16744272,cornflowerblue:6591981,cornsilk:16775388,crimson:14423100,cyan:65535,darkblue:139,darkcyan:35723,darkgoldenrod:12092939,darkgray:11119017,darkgreen:25600,darkgrey:11119017,darkkhaki:12433259,darkmagenta:9109643,darkolivegreen:5597999,darkorange:16747520,darkorchid:10040012,darkred:9109504,darksalmon:15308410,darkseagreen:9419919,darkslateblue:4734347,darkslategray:3100495,darkslategrey:3100495,darkturquoise:52945,darkviolet:9699539,deeppink:16716947,deepskyblue:49151,dimgray:6908265,dimgrey:6908265,dodgerblue:2003199,firebrick:11674146,floralwhite:16775920,forestgreen:2263842,fuchsia:16711935,gainsboro:14474460,ghostwhite:16316671,gold:16766720,goldenrod:14329120,gray:8421504,green:32768,greenyellow:11403055,grey:8421504,honeydew:15794160,hotpink:16738740,indianred:13458524,indigo:4915330,ivory:16777200,khaki:15787660,lavender:15132410,lavenderblush:16773365,lawngreen:8190976,lemonchiffon:16775885,lightblue:11393254,lightcoral:15761536,lightcyan:14745599,lightgoldenrodyellow:16448210,lightgray:13882323,lightgreen:9498256,lightgrey:13882323,lightpink:16758465,lightsalmon:16752762,lightseagreen:2142890,lightskyblue:8900346,lightslategray:7833753,lightslategrey:7833753,lightsteelblue:11584734,lightyellow:16777184,lime:65280,limegreen:3329330,linen:16445670,magenta:16711935,maroon:8388608,mediumaquamarine:6737322,mediumblue:205,mediumorchid:12211667,mediumpurple:9662683,mediumseagreen:3978097,mediumslateblue:8087790,mediumspringgreen:64154,mediumturquoise:4772300,mediumvioletred:13047173,midnightblue:1644912,mintcream:16121850,mistyrose:16770273,moccasin:16770229,navajowhite:16768685,navy:128,oldlace:16643558,olive:8421376,olivedrab:7048739,orange:16753920,orangered:16729344,orchid:14315734,palegoldenrod:15657130,palegreen:10025880,paleturquoise:11529966,palevioletred:14381203,papayawhip:16773077,peachpuff:16767673,peru:13468991,pink:16761035,plum:14524637,powderblue:11591910,purple:8388736,red:16711680,rosybrown:12357519,royalblue:4286945,saddlebrown:9127187,salmon:16416882,sandybrown:16032864,seagreen:3050327,seashell:16774638,sienna:10506797,silver:12632256,skyblue:8900331,slateblue:6970061,slategray:7372944,slategrey:7372944,snow:16775930,springgreen:65407,steelblue:4620980,tan:13808780,teal:32896,thistle:14204888,tomato:16737095,turquoise:4251856,violet:15631086,wheat:16113331,white:16777215,whitesmoke:16119285,yellow:16776960,yellowgreen:10145074});so.forEach(function(n,t){so.set(n,it(t))}),ya.functor=pt,ya.xhr=dt(mt),ya.dsv=function(n,t){function e(n,e,u){arguments.length<3&&(u=e,e=null);var a=ya.xhr(n,t,u);return a.row=function(n){return arguments.length?a.response(null==(e=n)?r:i(n)):e},a.row(e)}function r(n){return e.parse(n.responseText)}function i(n){return function(t){return e.parse(t.responseText,n)}}function a(t){return t.map(o).join(n)}function o(n){return c.test(n)?'"'+n.replace(/\"/g,'""')+'"':n}var c=new RegExp('["'+n+"\n]"),l=n.charCodeAt(0);return e.parse=function(n,t){var r;return e.parseRows(n,function(n,e){if(r)return r(n,e-1);var i=new Function("d","return {"+n.map(function(n,t){return JSON.stringify(n)+": d["+t+"]"}).join(",")+"}");r=t?function(n,e){return t(i(n),e)}:i})},e.parseRows=function(n,t){function e(){if(s>=c)return a;if(i)return i=!1,u;var t=s;if(34===n.charCodeAt(t)){for(var e=t;e++<c;)if(34===n.charCodeAt(e)){if(34!==n.charCodeAt(e+1))break;++e}s=e+2;var r=n.charCodeAt(e+1);return 13===r?(i=!0,10===n.charCodeAt(e+2)&&++s):10===r&&(i=!0),n.substring(t+1,e).replace(/""/g,'"')}for(;c>s;){var r=n.charCodeAt(s++),o=1;if(10===r)i=!0;else if(13===r)i=!0,10===n.charCodeAt(s)&&(++s,++o);else if(r!==l)continue;return n.substring(t,s-o)}return n.substring(t)}for(var r,i,u={},a={},o=[],c=n.length,s=0,f=0;(r=e())!==a;){for(var h=[];r!==u&&r!==a;)h.push(r),r=e();(!t||(h=t(h,f++)))&&o.push(h)}return o},e.format=function(t){if(Array.isArray(t[0]))return e.formatRows(t);var r=new u,i=[];return t.forEach(function(n){for(var t in n)r.has(t)||i.push(r.add(t))}),[i.map(o).join(n)].concat(t.map(function(t){return i.map(function(n){return o(t[n])}).join(n)})).join("\n")},e.formatRows=function(n){return n.map(a).join("\n")},e},ya.csv=ya.dsv(",","text/csv"),ya.tsv=ya.dsv(" ","text/tab-separated-values");var fo,ho,go,po,mo,vo=ba[o(ba,"requestAnimationFrame")]||function(n){setTimeout(n,17)};ya.timer=function(n,t,e){var r=arguments.length;2>r&&(t=0),3>r&&(e=Date.now());var i=e+t,u={callback:n,time:i,next:null};ho?ho.next=u:fo=u,ho=u,go||(po=clearTimeout(po),go=1,vo(Mt))},ya.timer.flush=function(){bt(),_t()};var yo=".",Mo=",",xo=[3,3],bo="$",_o=["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"].map(wt);ya.formatPrefix=function(n,t){var e=0;return n&&(0>n&&(n*=-1),t&&(n=ya.round(n,St(n,t))),e=1+Math.floor(1e-12+Math.log(n)/Math.LN10),e=Math.max(-24,Math.min(24,3*Math.floor((0>=e?e+1:e-1)/3)))),_o[8+e/3]},ya.round=function(n,t){return t?Math.round(n*(t=Math.pow(10,t)))/t:Math.round(n)},ya.format=function(n){var t=wo.exec(n),e=t[1]||" ",r=t[2]||">",i=t[3]||"",u=t[4]||"",a=t[5],o=+t[6],c=t[7],l=t[8],s=t[9],f=1,h="",g=!1;switch(l&&(l=+l.substring(1)),(a||"0"===e&&"="===r)&&(a=e="0",r="=",c&&(o-=Math.floor((o-1)/4))),s){case"n":c=!0,s="g";break;case"%":f=100,h="%",s="f";break;case"p":f=100,h="%",s="r";break;case"b":case"o":case"x":case"X":"#"===u&&(u="0"+s.toLowerCase());case"c":case"d":g=!0,l=0;break;case"s":f=-1,s="r"}"#"===u?u="":"$"===u&&(u=bo),"r"!=s||l||(s="g"),null!=l&&("g"==s?l=Math.max(1,Math.min(21,l)):("e"==s||"f"==s)&&(l=Math.max(0,Math.min(20,l)))),s=So.get(s)||Et;var p=a&&c;return function(n){if(g&&n%1)return"";var t=0>n||0===n&&0>1/n?(n=-n,"-"):i;if(0>f){var m=ya.formatPrefix(n,l);n=m.scale(n),h=m.symbol}else n*=f;n=s(n,l);var d=n.lastIndexOf("."),v=0>d?n:n.substring(0,d),y=0>d?"":yo+n.substring(d+1);!a&&c&&(v=Eo(v));var M=u.length+v.length+y.length+(p?0:t.length),x=o>M?new Array(M=o-M+1).join(e):"";return p&&(v=Eo(x+v)),t+=u,n=v+y,("<"===r?t+n+x:">"===r?x+t+n:"^"===r?x.substring(0,M>>=1)+t+n+x.substring(M):t+(p?n:x+n))+h}};var wo=/(?:([^{])?([<>=^]))?([+\- ])?([$#])?(0)?(\d+)?(,)?(\.-?\d+)?([a-z%])?/i,So=ya.map({b:function(n){return n.toString(2)},c:function(n){return String.fromCharCode(n)},o:function(n){return n.toString(8)},x:function(n){return n.toString(16)},X:function(n){return n.toString(16).toUpperCase()},g:function(n,t){return n.toPrecision(t)},e:function(n,t){return n.toExponential(t)},f:function(n,t){return n.toFixed(t)},r:function(n,t){return(n=ya.round(n,St(n,t))).toFixed(Math.max(0,Math.min(20,St(n*(1+1e-15),t))))}}),Eo=mt;if(xo){var ko=xo.length;Eo=function(n){for(var t=n.length,e=[],r=0,i=xo[0];t>0&&i>0;)e.push(n.substring(t-=i,t+i)),i=xo[r=(r+1)%ko];return e.reverse().join(Mo)}}ya.geo={},kt.prototype={s:0,t:0,add:function(n){At(n,this.t,Ao),At(Ao.s,this.s,this),this.s?this.t+=Ao.t:this.s=Ao.t},reset:function(){this.s=this.t=0},valueOf:function(){return this.s}};var Ao=new kt;ya.geo.stream=function(n,t){n&&No.hasOwnProperty(n.type)?No[n.type](n,t):Nt(n,t)};var No={Feature:function(n,t){Nt(n.geometry,t)},FeatureCollection:function(n,t){for(var e=n.features,r=-1,i=e.length;++r<i;)Nt(e[r].geometry,t)}},qo={Sphere:function(n,t){t.sphere()},Point:function(n,t){var e=n.coordinates;t.point(e[0],e[1])},MultiPoint:function(n,t){for(var e,r=n.coordinates,i=-1,u=r.length;++i<u;)e=r[i],t.point(e[0],e[1])},LineString:function(n,t){qt(n.coordinates,t,0)},MultiLineString:function(n,t){for(var e=n.coordinates,r=-1,i=e.length;++r<i;)qt(e[r],t,0)},Polygon:function(n,t){Tt(n.coordinates,t)},MultiPolygon:function(n,t){for(var e=n.coordinates,r=-1,i=e.length;++r<i;)Tt(e[r],t)},GeometryCollection:function(n,t){for(var e=n.geometries,r=-1,i=e.length;++r<i;)Nt(e[r],t)}};ya.geo.area=function(n){return To=0,ya.geo.stream(n,zo),To};var To,Co=new kt,zo={sphere:function(){To+=4*Ka},point:s,lineStart:s,lineEnd:s,polygonStart:function(){Co.reset(),zo.lineStart=Ct},polygonEnd:function(){var n=2*Co;To+=0>n?4*Ka+n:n,zo.lineStart=zo.lineEnd=zo.point=s}};ya.geo.bounds=function(){function n(n,t){M.push(x=[s=n,h=n]),f>t&&(f=t),t>g&&(g=t)}function t(t,e){var r=zt([t*to,e*to]);if(v){var i=jt(v,r),u=[i[1],-i[0],0],a=jt(u,i);Ft(a),a=Pt(a);var c=t-p,l=c>0?1:-1,m=a[0]*eo*l,d=Math.abs(c)>180;if(d^(m>l*p&&l*t>m)){var y=a[1]*eo;y>g&&(g=y)}else if(m=(m+360)%360-180,d^(m>l*p&&l*t>m)){var y=-a[1]*eo;f>y&&(f=y)}else f>e&&(f=e),e>g&&(g=e);d?p>t?o(s,t)>o(s,h)&&(h=t):o(t,h)>o(s,h)&&(s=t):h>=s?(s>t&&(s=t),t>h&&(h=t)):t>p?o(s,t)>o(s,h)&&(h=t):o(t,h)>o(s,h)&&(s=t)}else n(t,e);v=r,p=t}function e(){b.point=t}function r(){x[0]=s,x[1]=h,b.point=n,v=null}function i(n,e){if(v){var r=n-p;y+=Math.abs(r)>180?r+(r>0?360:-360):r}else m=n,d=e;zo.point(n,e),t(n,e)}function u(){zo.lineStart()}function a(){i(m,d),zo.lineEnd(),Math.abs(y)>Qa&&(s=-(h=180)),x[0]=s,x[1]=h,v=null}function o(n,t){return(t-=n)<0?t+360:t}function c(n,t){return n[0]-t[0]}function l(n,t){return t[0]<=t[1]?t[0]<=n&&n<=t[1]:n<t[0]||t[1]<n}var s,f,h,g,p,m,d,v,y,M,x,b={point:n,lineStart:e,lineEnd:r,polygonStart:function(){b.point=i,b.lineStart=u,b.lineEnd=a,y=0,zo.polygonStart()},polygonEnd:function(){zo.polygonEnd(),b.point=n,b.lineStart=e,b.lineEnd=r,0>Co?(s=-(h=180),f=-(g=90)):y>Qa?g=90:-Qa>y&&(f=-90),x[0]=s,x[1]=h}};return function(n){g=h=-(s=f=1/0),M=[],ya.geo.stream(n,b);var t=M.length;if(t){M.sort(c);for(var e,r=1,i=M[0],u=[i];t>r;++r)e=M[r],l(e[0],i)||l(e[1],i)?(o(i[0],e[1])>o(i[0],i[1])&&(i[1]=e[1]),o(e[0],i[1])>o(i[0],i[1])&&(i[0]=e[0])):u.push(i=e);for(var a,e,p=-1/0,t=u.length-1,r=0,i=u[t];t>=r;i=e,++r)e=u[r],(a=o(i[1],e[0]))>p&&(p=a,s=e[0],h=i[1])}return M=x=null,1/0===s||1/0===f?[[0/0,0/0],[0/0,0/0]]:[[s,f],[h,g]]}}(),ya.geo.centroid=function(n){Do=jo=Lo=Ho=Fo=Po=Oo=Yo=Ro=Uo=Io=0,ya.geo.stream(n,Vo);var t=Ro,e=Uo,r=Io,i=t*t+e*e+r*r;return no>i&&(t=Po,e=Oo,r=Yo,Qa>jo&&(t=Lo,e=Ho,r=Fo),i=t*t+e*e+r*r,no>i)?[0/0,0/0]:[Math.atan2(e,t)*eo,V(r/Math.sqrt(i))*eo]};var Do,jo,Lo,Ho,Fo,Po,Oo,Yo,Ro,Uo,Io,Vo={sphere:s,point:Yt,lineStart:Ut,lineEnd:It,polygonStart:function(){Vo.lineStart=Vt},polygonEnd:function(){Vo.lineStart=Ut}},Xo=$t(Xt,Qt,te,ee),Zo=[-Ka,0],Bo=1e9;(ya.geo.conicEqualArea=function(){return oe(ce)}).raw=ce,ya.geo.albers=function(){return ya.geo.conicEqualArea().rotate([96,0]).center([-.6,38.7]).parallels([29.5,45.5]).scale(1070)},ya.geo.albersUsa=function(){function n(n){var u=n[0],a=n[1];return t=null,e(u,a),t||(r(u,a),t)||i(u,a),t}var t,e,r,i,u=ya.geo.albers(),a=ya.geo.conicEqualArea().rotate([154,0]).center([-2,58.5]).parallels([55,65]),o=ya.geo.conicEqualArea().rotate([157,0]).center([-3,19.9]).parallels([8,18]),c={point:function(n,e){t=[n,e]}};return n.invert=function(n){var t=u.scale(),e=u.translate(),r=(n[0]-e[0])/t,i=(n[1]-e[1])/t;return(i>=.12&&.234>i&&r>=-.425&&-.214>r?a:i>=.166&&.234>i&&r>=-.214&&-.115>r?o:u).invert(n)},n.stream=function(n){var t=u.stream(n),e=a.stream(n),r=o.stream(n);return{point:function(n,i){t.point(n,i),e.point(n,i),r.point(n,i)},sphere:function(){t.sphere(),e.sphere(),r.sphere()},lineStart:function(){t.lineStart(),e.lineStart(),r.lineStart()},lineEnd:function(){t.lineEnd(),e.lineEnd(),r.lineEnd()},polygonStart:function(){t.polygonStart(),e.polygonStart(),r.polygonStart()},polygonEnd:function(){t.polygonEnd(),e.polygonEnd(),r.polygonEnd()}}},n.precision=function(t){return arguments.length?(u.precision(t),a.precision(t),o.precision(t),n):u.precision()},n.scale=function(t){return arguments.length?(u.scale(t),a.scale(.35*t),o.scale(t),n.translate(u.translate())):u.scale()},n.translate=function(t){if(!arguments.length)return u.translate();var l=u.scale(),s=+t[0],f=+t[1];return e=u.translate(t).clipExtent([[s-.455*l,f-.238*l],[s+.455*l,f+.238*l]]).stream(c).point,r=a.translate([s-.307*l,f+.201*l]).clipExtent([[s-.425*l+Qa,f+.12*l+Qa],[s-.214*l-Qa,f+.234*l-Qa]]).stream(c).point,i=o.translate([s-.205*l,f+.212*l]).clipExtent([[s-.214*l+Qa,f+.166*l+Qa],[s-.115*l-Qa,f+.234*l-Qa]]).stream(c).point,n},n.scale(1070)};var $o,Wo,Jo,Go,Ko,Qo,nc={point:s,lineStart:s,lineEnd:s,polygonStart:function(){Wo=0,nc.lineStart=le},polygonEnd:function(){nc.lineStart=nc.lineEnd=nc.point=s,$o+=Math.abs(Wo/2)}},tc={point:se,lineStart:s,lineEnd:s,polygonStart:s,polygonEnd:s},ec={point:ge,lineStart:pe,lineEnd:me,polygonStart:function(){ec.lineStart=de},polygonEnd:function(){ec.point=ge,ec.lineStart=pe,ec.lineEnd=me}};ya.geo.path=function(){function n(n){return n&&("function"==typeof o&&u.pointRadius(+o.apply(this,arguments)),a&&a.valid||(a=i(u)),ya.geo.stream(n,a)),u.result()}function t(){return a=null,n}var e,r,i,u,a,o=4.5;return n.area=function(n){return $o=0,ya.geo.stream(n,i(nc)),$o},n.centroid=function(n){return Lo=Ho=Fo=Po=Oo=Yo=Ro=Uo=Io=0,ya.geo.stream(n,i(ec)),Io?[Ro/Io,Uo/Io]:Yo?[Po/Yo,Oo/Yo]:Fo?[Lo/Fo,Ho/Fo]:[0/0,0/0]},n.bounds=function(n){return Ko=Qo=-(Jo=Go=1/0),ya.geo.stream(n,i(tc)),[[Jo,Go],[Ko,Qo]]},n.projection=function(n){return arguments.length?(i=(e=n)?n.stream||Me(n):mt,t()):e},n.context=function(n){return arguments.length?(u=null==(r=n)?new fe:new ve(n),"function"!=typeof o&&u.pointRadius(o),t()):r},n.pointRadius=function(t){return arguments.length?(o="function"==typeof t?t:(u.pointRadius(+t),+t),n):o},n.projection(ya.geo.albersUsa()).context(null)},ya.geo.projection=xe,ya.geo.projectionMutator=be,(ya.geo.equirectangular=function(){return xe(we)}).raw=we.invert=we,ya.geo.rotation=function(n){function t(t){return t=n(t[0]*to,t[1]*to),t[0]*=eo,t[1]*=eo,t}return n=Se(n[0]%360*to,n[1]*to,n.length>2?n[2]*to:0),t.invert=function(t){return t=n.invert(t[0]*to,t[1]*to),t[0]*=eo,t[1]*=eo,t},t},ya.geo.circle=function(){function n(){var n="function"==typeof r?r.apply(this,arguments):r,t=Se(-n[0]*to,-n[1]*to,0).invert,i=[];return e(null,null,1,{point:function(n,e){i.push(n=t(n,e)),n[0]*=eo,n[1]*=eo}}),{type:"Polygon",coordinates:[i]}}var t,e,r=[0,0],i=6;return n.origin=function(t){return arguments.length?(r=t,n):r},n.angle=function(r){return arguments.length?(e=Ne((t=+r)*to,i*to),n):t},n.precision=function(r){return arguments.length?(e=Ne(t*to,(i=+r)*to),n):i},n.angle(90)},ya.geo.distance=function(n,t){var e,r=(t[0]-n[0])*to,i=n[1]*to,u=t[1]*to,a=Math.sin(r),o=Math.cos(r),c=Math.sin(i),l=Math.cos(i),s=Math.sin(u),f=Math.cos(u);return Math.atan2(Math.sqrt((e=f*a)*e+(e=l*s-c*f*o)*e),c*s+l*f*o)},ya.geo.graticule=function(){function n(){return{type:"MultiLineString",coordinates:t()}}function t(){return ya.range(Math.ceil(u/d)*d,i,d).map(h).concat(ya.range(Math.ceil(l/v)*v,c,v).map(g)).concat(ya.range(Math.ceil(r/p)*p,e,p).filter(function(n){return Math.abs(n%d)>Qa}).map(s)).concat(ya.range(Math.ceil(o/m)*m,a,m).filter(function(n){return Math.abs(n%v)>Qa}).map(f))}var e,r,i,u,a,o,c,l,s,f,h,g,p=10,m=p,d=90,v=360,y=2.5;return n.lines=function(){return t().map(function(n){return{type:"LineString",coordinates:n}})},n.outline=function(){return{type:"Polygon",coordinates:[h(u).concat(g(c).slice(1),h(i).reverse().slice(1),g(l).reverse().slice(1))]}},n.extent=function(t){return arguments.length?n.majorExtent(t).minorExtent(t):n.minorExtent()},n.majorExtent=function(t){return arguments.length?(u=+t[0][0],i=+t[1][0],l=+t[0][1],c=+t[1][1],u>i&&(t=u,u=i,i=t),l>c&&(t=l,l=c,c=t),n.precision(y)):[[u,l],[i,c]]},n.minorExtent=function(t){return arguments.length?(r=+t[0][0],e=+t[1][0],o=+t[0][1],a=+t[1][1],r>e&&(t=r,r=e,e=t),o>a&&(t=o,o=a,a=t),n.precision(y)):[[r,o],[e,a]]},n.step=function(t){return arguments.length?n.majorStep(t).minorStep(t):n.minorStep()},n.majorStep=function(t){return arguments.length?(d=+t[0],v=+t[1],n):[d,v]},n.minorStep=function(t){return arguments.length?(p=+t[0],m=+t[1],n):[p,m]},n.precision=function(t){return arguments.length?(y=+t,s=Te(o,a,90),f=Ce(r,e,y),h=Te(l,c,90),g=Ce(u,i,y),n):y},n.majorExtent([[-180,-90+Qa],[180,90-Qa]]).minorExtent([[-180,-80-Qa],[180,80+Qa]])},ya.geo.greatArc=function(){function n(){return{type:"LineString",coordinates:[t||r.apply(this,arguments),e||i.apply(this,arguments)]}}var t,e,r=ze,i=De;return n.distance=function(){return ya.geo.distance(t||r.apply(this,arguments),e||i.apply(this,arguments))},n.source=function(e){return arguments.length?(r=e,t="function"==typeof e?null:e,n):r},n.target=function(t){return arguments.length?(i=t,e="function"==typeof t?null:t,n):i},n.precision=function(){return arguments.length?n:0},n},ya.geo.interpolate=function(n,t){return je(n[0]*to,n[1]*to,t[0]*to,t[1]*to)},ya.geo.length=function(n){return rc=0,ya.geo.stream(n,ic),rc};var rc,ic={sphere:s,point:s,lineStart:Le,lineEnd:s,polygonStart:s,polygonEnd:s},uc=He(function(n){return Math.sqrt(2/(1+n))},function(n){return 2*Math.asin(n/2)});(ya.geo.azimuthalEqualArea=function(){return xe(uc)}).raw=uc;var ac=He(function(n){var t=Math.acos(n);return t&&t/Math.sin(t)},mt);(ya.geo.azimuthalEquidistant=function(){return xe(ac)}).raw=ac,(ya.geo.conicConformal=function(){return oe(Fe)}).raw=Fe,(ya.geo.conicEquidistant=function(){return oe(Pe)}).raw=Pe;var oc=He(function(n){return 1/n},Math.atan);(ya.geo.gnomonic=function(){return xe(oc)}).raw=oc,Oe.invert=function(n,t){return[n,2*Math.atan(Math.exp(t))-Ka/2]},(ya.geo.mercator=function(){return Ye(Oe)}).raw=Oe;var cc=He(function(){return 1},Math.asin);(ya.geo.orthographic=function(){return xe(cc)}).raw=cc;var lc=He(function(n){return 1/(1+n)},function(n){return 2*Math.atan(n)});(ya.geo.stereographic=function(){return xe(lc)}).raw=lc,Re.invert=function(n,t){return[Math.atan2(X(n),Math.cos(t)),V(Math.sin(t)/Z(n))]},(ya.geo.transverseMercator=function(){return Ye(Re)}).raw=Re,ya.geom={},ya.svg={},ya.svg.line=function(){return Ue(mt)};var sc=ya.map({linear:Xe,"linear-closed":Ze,step:Be,"step-before":$e,"step-after":We,basis:tr,"basis-open":er,"basis-closed":rr,bundle:ir,cardinal:Ke,"cardinal-open":Je,"cardinal-closed":Ge,monotone:sr});
4
+ sc.forEach(function(n,t){t.key=n,t.closed=/-closed$/.test(n)});var fc=[0,2/3,1/3,0],hc=[0,1/3,2/3,0],gc=[0,1/6,2/3,1/6];ya.geom.hull=function(n){function t(n){if(n.length<3)return[];var t,i,u,a,o,c,l,s,f,h,g,p,m=pt(e),d=pt(r),v=n.length,y=v-1,M=[],x=[],b=0;if(m===Ie&&r===Ve)t=n;else for(u=0,t=[];v>u;++u)t.push([+m.call(this,i=n[u],u),+d.call(this,i,u)]);for(u=1;v>u;++u)(t[u][1]<t[b][1]||t[u][1]==t[b][1]&&t[u][0]<t[b][0])&&(b=u);for(u=0;v>u;++u)u!==b&&(c=t[u][1]-t[b][1],o=t[u][0]-t[b][0],M.push({angle:Math.atan2(c,o),index:u}));for(M.sort(function(n,t){return n.angle-t.angle}),g=M[0].angle,h=M[0].index,f=0,u=1;y>u;++u){if(a=M[u].index,g==M[u].angle){if(o=t[h][0]-t[b][0],c=t[h][1]-t[b][1],l=t[a][0]-t[b][0],s=t[a][1]-t[b][1],o*o+c*c>=l*l+s*s){M[u].index=-1;continue}M[f].index=-1}g=M[u].angle,f=u,h=a}for(x.push(b),u=0,a=0;2>u;++a)M[a].index>-1&&(x.push(M[a].index),u++);for(p=x.length;y>a;++a)if(!(M[a].index<0)){for(;!fr(x[p-2],x[p-1],M[a].index,t);)--p;x[p++]=M[a].index}var _=[];for(u=p-1;u>=0;--u)_.push(n[x[u]]);return _}var e=Ie,r=Ve;return arguments.length?t(n):(t.x=function(n){return arguments.length?(e=n,t):e},t.y=function(n){return arguments.length?(r=n,t):r},t)},ya.geom.polygon=function(n){return La(n,pc),n};var pc=ya.geom.polygon.prototype=[];pc.area=function(){for(var n,t=-1,e=this.length,r=this[e-1],i=0;++t<e;)n=r,r=this[t],i+=n[1]*r[0]-n[0]*r[1];return.5*i},pc.centroid=function(n){var t,e,r=-1,i=this.length,u=0,a=0,o=this[i-1];for(arguments.length||(n=-1/(6*this.area()));++r<i;)t=o,o=this[r],e=t[0]*o[1]-o[0]*t[1],u+=(t[0]+o[0])*e,a+=(t[1]+o[1])*e;return[u*n,a*n]},pc.clip=function(n){for(var t,e,r,i,u,a,o=pr(n),c=-1,l=this.length-pr(this),s=this[l-1];++c<l;){for(t=n.slice(),n.length=0,i=this[c],u=t[(r=t.length-o)-1],e=-1;++e<r;)a=t[e],hr(a,s,i)?(hr(u,s,i)||n.push(gr(u,a,s,i)),n.push(a)):hr(u,s,i)&&n.push(gr(u,a,s,i)),u=a;o&&n.push(n[0]),s=i}return n},ya.geom.delaunay=function(n){var t=n.map(function(){return[]}),e=[];return mr(n,function(e){t[e.region.l.index].push(n[e.region.r.index])}),t.forEach(function(t,r){var i=n[r],u=i[0],a=i[1];t.forEach(function(n){n.angle=Math.atan2(n[0]-u,n[1]-a)}),t.sort(function(n,t){return n.angle-t.angle});for(var o=0,c=t.length-1;c>o;o++)e.push([i,t[o],t[o+1]])}),e},ya.geom.voronoi=function(n){function t(n){var t,u,a,o=n.map(function(){return[]}),c=pt(e),l=pt(r),s=n.length,f=1e6;if(c===Ie&&l===Ve)t=n;else for(t=new Array(s),a=0;s>a;++a)t[a]=[+c.call(this,u=n[a],a),+l.call(this,u,a)];if(mr(t,function(n){var t,e,r,i,u,a;1===n.a&&n.b>=0?(t=n.ep.r,e=n.ep.l):(t=n.ep.l,e=n.ep.r),1===n.a?(u=t?t.y:-f,r=n.c-n.b*u,a=e?e.y:f,i=n.c-n.b*a):(r=t?t.x:-f,u=n.c-n.a*r,i=e?e.x:f,a=n.c-n.a*i);var c=[r,u],l=[i,a];o[n.region.l.index].push(c,l),o[n.region.r.index].push(c,l)}),o=o.map(function(n,e){var r=t[e][0],i=t[e][1],u=n.map(function(n){return Math.atan2(n[0]-r,n[1]-i)}),a=ya.range(n.length).sort(function(n,t){return u[n]-u[t]});return a.filter(function(n,t){return!t||u[n]-u[a[t-1]]>Qa}).map(function(t){return n[t]})}),o.forEach(function(n,e){var r=n.length;if(!r)return n.push([-f,-f],[-f,f],[f,f],[f,-f]);if(!(r>2)){var i=t[e],u=n[0],a=n[1],o=i[0],c=i[1],l=u[0],s=u[1],h=a[0],g=a[1],p=Math.abs(h-l),m=g-s;if(Math.abs(m)<Qa){var d=s>c?-f:f;n.push([-f,d],[f,d])}else if(Qa>p){var v=l>o?-f:f;n.push([v,-f],[v,f])}else{var d=(l-o)*(g-s)>(h-l)*(s-c)?f:-f,y=Math.abs(m)-p;Math.abs(y)<Qa?n.push([0>m?d:-d,d]):(y>0&&(d*=-1),n.push([-f,d],[f,d]))}}}),i)for(a=0;s>a;++a)i.clip(o[a]);for(a=0;s>a;++a)o[a].point=n[a];return o}var e=Ie,r=Ve,i=null;return arguments.length?t(n):(t.x=function(n){return arguments.length?(e=n,t):e},t.y=function(n){return arguments.length?(r=n,t):r},t.clipExtent=function(n){if(!arguments.length)return i&&[i[0],i[2]];if(null==n)i=null;else{var e=+n[0][0],r=+n[0][1],u=+n[1][0],a=+n[1][1];i=ya.geom.polygon([[e,r],[e,a],[u,a],[u,r]])}return t},t.size=function(n){return arguments.length?t.clipExtent(n&&[[0,0],n]):i&&i[2]},t.links=function(n){var t,i,u,a=n.map(function(){return[]}),o=[],c=pt(e),l=pt(r),s=n.length;if(c===Ie&&l===Ve)t=n;else for(t=new Array(s),u=0;s>u;++u)t[u]=[+c.call(this,i=n[u],u),+l.call(this,i,u)];return mr(t,function(t){var e=t.region.l.index,r=t.region.r.index;a[e][r]||(a[e][r]=a[r][e]=!0,o.push({source:n[e],target:n[r]}))}),o},t.triangles=function(n){if(e===Ie&&r===Ve)return ya.geom.delaunay(n);for(var t,i=new Array(c),u=pt(e),a=pt(r),o=-1,c=n.length;++o<c;)(i[o]=[+u.call(this,t=n[o],o),+a.call(this,t,o)]).data=t;return ya.geom.delaunay(i).map(function(n){return n.map(function(n){return n.data})})},t)};var mc={l:"r",r:"l"};ya.geom.quadtree=function(n,t,e,r,i){function u(n){function u(n,t,e,r,i,u,a,o){if(!isNaN(e)&&!isNaN(r))if(n.leaf){var c=n.x,s=n.y;if(null!=c)if(Math.abs(c-e)+Math.abs(s-r)<.01)l(n,t,e,r,i,u,a,o);else{var f=n.point;n.x=n.y=n.point=null,l(n,f,c,s,i,u,a,o),l(n,t,e,r,i,u,a,o)}else n.x=e,n.y=r,n.point=t}else l(n,t,e,r,i,u,a,o)}function l(n,t,e,r,i,a,o,c){var l=.5*(i+o),s=.5*(a+c),f=e>=l,h=r>=s,g=(h<<1)+f;n.leaf=!1,n=n.nodes[g]||(n.nodes[g]=yr()),f?i=l:o=l,h?a=s:c=s,u(n,t,e,r,i,a,o,c)}var s,f,h,g,p,m,d,v,y,M=pt(o),x=pt(c);if(null!=t)m=t,d=e,v=r,y=i;else if(v=y=-(m=d=1/0),f=[],h=[],p=n.length,a)for(g=0;p>g;++g)s=n[g],s.x<m&&(m=s.x),s.y<d&&(d=s.y),s.x>v&&(v=s.x),s.y>y&&(y=s.y),f.push(s.x),h.push(s.y);else for(g=0;p>g;++g){var b=+M(s=n[g],g),_=+x(s,g);m>b&&(m=b),d>_&&(d=_),b>v&&(v=b),_>y&&(y=_),f.push(b),h.push(_)}var w=v-m,S=y-d;w>S?y=d+w:v=m+S;var E=yr();if(E.add=function(n){u(E,n,+M(n,++g),+x(n,g),m,d,v,y)},E.visit=function(n){Mr(n,E,m,d,v,y)},g=-1,null==t){for(;++g<p;)u(E,n[g],f[g],h[g],m,d,v,y);--g}else n.forEach(E.add);return f=h=n=s=null,E}var a,o=Ie,c=Ve;return(a=arguments.length)?(o=dr,c=vr,3===a&&(i=e,r=t,e=t=0),u(n)):(u.x=function(n){return arguments.length?(o=n,u):o},u.y=function(n){return arguments.length?(c=n,u):c},u.extent=function(n){return arguments.length?(null==n?t=e=r=i=null:(t=+n[0][0],e=+n[0][1],r=+n[1][0],i=+n[1][1]),u):null==t?null:[[t,e],[r,i]]},u.size=function(n){return arguments.length?(null==n?t=e=r=i=null:(t=e=0,r=+n[0],i=+n[1]),u):null==t?null:[r-t,i-e]},u)},ya.interpolateRgb=xr,ya.interpolateObject=br,ya.interpolateNumber=_r,ya.interpolateString=wr;var dc=/[-+]?(?:\d+\.?\d*|\.?\d+)(?:[eE][-+]?\d+)?/g;ya.interpolate=Sr,ya.interpolators=[function(n,t){var e=typeof t;return("string"===e?so.has(t)||/^(#|rgb\(|hsl\()/.test(t)?xr:wr:t instanceof P?xr:"object"===e?Array.isArray(t)?Er:br:_r)(n,t)}],ya.interpolateArray=Er;var vc=function(){return mt},yc=ya.map({linear:vc,poly:zr,quad:function(){return qr},cubic:function(){return Tr},sin:function(){return Dr},exp:function(){return jr},circle:function(){return Lr},elastic:Hr,back:Fr,bounce:function(){return Pr}}),Mc=ya.map({"in":mt,out:Ar,"in-out":Nr,"out-in":function(n){return Nr(Ar(n))}});ya.ease=function(n){var t=n.indexOf("-"),e=t>=0?n.substring(0,t):n,r=t>=0?n.substring(t+1):"in";return e=yc.get(e)||vc,r=Mc.get(r)||mt,kr(r(e.apply(null,Array.prototype.slice.call(arguments,1))))},ya.interpolateHcl=Or,ya.interpolateHsl=Yr,ya.interpolateLab=Rr,ya.interpolateRound=Ur,ya.transform=function(n){var t=Ma.createElementNS(ya.ns.prefix.svg,"g");return(ya.transform=function(n){if(null!=n){t.setAttribute("transform",n);var e=t.transform.baseVal.consolidate()}return new Ir(e?e.matrix:xc)})(n)},Ir.prototype.toString=function(){return"translate("+this.translate+")rotate("+this.rotate+")skewX("+this.skew+")scale("+this.scale+")"};var xc={a:1,b:0,c:0,d:1,e:0,f:0};ya.interpolateTransform=Br,ya.layout={},ya.layout.bundle=function(){return function(n){for(var t=[],e=-1,r=n.length;++e<r;)t.push(Jr(n[e]));return t}},ya.layout.chord=function(){function n(){var n,l,f,h,g,p={},m=[],d=ya.range(u),v=[];for(e=[],r=[],n=0,h=-1;++h<u;){for(l=0,g=-1;++g<u;)l+=i[h][g];m.push(l),v.push(ya.range(u)),n+=l}for(a&&d.sort(function(n,t){return a(m[n],m[t])}),o&&v.forEach(function(n,t){n.sort(function(n,e){return o(i[t][n],i[t][e])})}),n=(2*Ka-s*u)/n,l=0,h=-1;++h<u;){for(f=l,g=-1;++g<u;){var y=d[h],M=v[y][g],x=i[y][M],b=l,_=l+=x*n;p[y+"-"+M]={index:y,subindex:M,startAngle:b,endAngle:_,value:x}}r[y]={index:y,startAngle:f,endAngle:l,value:(l-f)/n},l+=s}for(h=-1;++h<u;)for(g=h-1;++g<u;){var w=p[h+"-"+g],S=p[g+"-"+h];(w.value||S.value)&&e.push(w.value<S.value?{source:S,target:w}:{source:w,target:S})}c&&t()}function t(){e.sort(function(n,t){return c((n.source.value+n.target.value)/2,(t.source.value+t.target.value)/2)})}var e,r,i,u,a,o,c,l={},s=0;return l.matrix=function(n){return arguments.length?(u=(i=n)&&i.length,e=r=null,l):i},l.padding=function(n){return arguments.length?(s=n,e=r=null,l):s},l.sortGroups=function(n){return arguments.length?(a=n,e=r=null,l):a},l.sortSubgroups=function(n){return arguments.length?(o=n,e=null,l):o},l.sortChords=function(n){return arguments.length?(c=n,e&&t(),l):c},l.chords=function(){return e||n(),e},l.groups=function(){return r||n(),r},l},ya.layout.force=function(){function n(n){return function(t,e,r,i){if(t.point!==n){var u=t.cx-n.x,a=t.cy-n.y,o=1/Math.sqrt(u*u+a*a);if(m>(i-e)*o){var c=t.charge*o*o;return n.px-=u*c,n.py-=a*c,!0}if(t.point&&isFinite(o)){var c=t.pointCharge*o*o;n.px-=u*c,n.py-=a*c}}return!t.charge}}function t(n){n.px=ya.event.x,n.py=ya.event.y,o.resume()}var e,r,i,u,a,o={},c=ya.dispatch("start","tick","end"),l=[1,1],s=.9,f=bc,h=_c,g=-30,p=.1,m=.8,d=[],v=[];return o.tick=function(){if((r*=.99)<.005)return c.end({type:"end",alpha:r=0}),!0;var t,e,o,f,h,m,y,M,x,b=d.length,_=v.length;for(e=0;_>e;++e)o=v[e],f=o.source,h=o.target,M=h.x-f.x,x=h.y-f.y,(m=M*M+x*x)&&(m=r*u[e]*((m=Math.sqrt(m))-i[e])/m,M*=m,x*=m,h.x-=M*(y=f.weight/(h.weight+f.weight)),h.y-=x*y,f.x+=M*(y=1-y),f.y+=x*y);if((y=r*p)&&(M=l[0]/2,x=l[1]/2,e=-1,y))for(;++e<b;)o=d[e],o.x+=(M-o.x)*y,o.y+=(x-o.y)*y;if(g)for(ri(t=ya.geom.quadtree(d),r,a),e=-1;++e<b;)(o=d[e]).fixed||t.visit(n(o));for(e=-1;++e<b;)o=d[e],o.fixed?(o.x=o.px,o.y=o.py):(o.x-=(o.px-(o.px=o.x))*s,o.y-=(o.py-(o.py=o.y))*s);c.tick({type:"tick",alpha:r})},o.nodes=function(n){return arguments.length?(d=n,o):d},o.links=function(n){return arguments.length?(v=n,o):v},o.size=function(n){return arguments.length?(l=n,o):l},o.linkDistance=function(n){return arguments.length?(f="function"==typeof n?n:+n,o):f},o.distance=o.linkDistance,o.linkStrength=function(n){return arguments.length?(h="function"==typeof n?n:+n,o):h},o.friction=function(n){return arguments.length?(s=+n,o):s},o.charge=function(n){return arguments.length?(g="function"==typeof n?n:+n,o):g},o.gravity=function(n){return arguments.length?(p=+n,o):p},o.theta=function(n){return arguments.length?(m=+n,o):m},o.alpha=function(n){return arguments.length?(n=+n,r?r=n>0?n:0:n>0&&(c.start({type:"start",alpha:r=n}),ya.timer(o.tick)),o):r},o.start=function(){function n(n,r){for(var i,u=t(e),a=-1,o=u.length;++a<o;)if(!isNaN(i=u[a][n]))return i;return Math.random()*r}function t(){if(!c){for(c=[],r=0;p>r;++r)c[r]=[];for(r=0;m>r;++r){var n=v[r];c[n.source.index].push(n.target),c[n.target.index].push(n.source)}}return c[e]}var e,r,c,s,p=d.length,m=v.length,y=l[0],M=l[1];for(e=0;p>e;++e)(s=d[e]).index=e,s.weight=0;for(e=0;m>e;++e)s=v[e],"number"==typeof s.source&&(s.source=d[s.source]),"number"==typeof s.target&&(s.target=d[s.target]),++s.source.weight,++s.target.weight;for(e=0;p>e;++e)s=d[e],isNaN(s.x)&&(s.x=n("x",y)),isNaN(s.y)&&(s.y=n("y",M)),isNaN(s.px)&&(s.px=s.x),isNaN(s.py)&&(s.py=s.y);if(i=[],"function"==typeof f)for(e=0;m>e;++e)i[e]=+f.call(this,v[e],e);else for(e=0;m>e;++e)i[e]=f;if(u=[],"function"==typeof h)for(e=0;m>e;++e)u[e]=+h.call(this,v[e],e);else for(e=0;m>e;++e)u[e]=h;if(a=[],"function"==typeof g)for(e=0;p>e;++e)a[e]=+g.call(this,d[e],e);else for(e=0;p>e;++e)a[e]=g;return o.resume()},o.resume=function(){return o.alpha(.1)},o.stop=function(){return o.alpha(0)},o.drag=function(){return e||(e=ya.behavior.drag().origin(mt).on("dragstart.force",Qr).on("drag.force",t).on("dragend.force",ni)),arguments.length?(this.on("mouseover.force",ti).on("mouseout.force",ei).call(e),void 0):e},ya.rebind(o,c,"on")};var bc=20,_c=1;ya.layout.hierarchy=function(){function n(t,a,o){var c=i.call(e,t,a);if(t.depth=a,o.push(t),c&&(l=c.length)){for(var l,s,f=-1,h=t.children=[],g=0,p=a+1;++f<l;)s=n(c[f],p,o),s.parent=t,h.push(s),g+=s.value;r&&h.sort(r),u&&(t.value=g)}else u&&(t.value=+u.call(e,t,a)||0);return t}function t(n,r){var i=n.children,a=0;if(i&&(o=i.length))for(var o,c=-1,l=r+1;++c<o;)a+=t(i[c],l);else u&&(a=+u.call(e,n,r)||0);return u&&(n.value=a),a}function e(t){var e=[];return n(t,0,e),e}var r=oi,i=ui,u=ai;return e.sort=function(n){return arguments.length?(r=n,e):r},e.children=function(n){return arguments.length?(i=n,e):i},e.value=function(n){return arguments.length?(u=n,e):u},e.revalue=function(n){return t(n,0),n},e},ya.layout.partition=function(){function n(t,e,r,i){var u=t.children;if(t.x=e,t.y=t.depth*i,t.dx=r,t.dy=i,u&&(a=u.length)){var a,o,c,l=-1;for(r=t.value?r/t.value:0;++l<a;)n(o=u[l],e,c=o.value*r,i),e+=c}}function t(n){var e=n.children,r=0;if(e&&(i=e.length))for(var i,u=-1;++u<i;)r=Math.max(r,t(e[u]));return 1+r}function e(e,u){var a=r.call(this,e,u);return n(a[0],0,i[0],i[1]/t(a[0])),a}var r=ya.layout.hierarchy(),i=[1,1];return e.size=function(n){return arguments.length?(i=n,e):i},ii(e,r)},ya.layout.pie=function(){function n(u){var a=u.map(function(e,r){return+t.call(n,e,r)}),o=+("function"==typeof r?r.apply(this,arguments):r),c=(("function"==typeof i?i.apply(this,arguments):i)-o)/ya.sum(a),l=ya.range(u.length);null!=e&&l.sort(e===wc?function(n,t){return a[t]-a[n]}:function(n,t){return e(u[n],u[t])});var s=[];return l.forEach(function(n){var t;s[n]={data:u[n],value:t=a[n],startAngle:o,endAngle:o+=t*c}}),s}var t=Number,e=wc,r=0,i=2*Ka;return n.value=function(e){return arguments.length?(t=e,n):t},n.sort=function(t){return arguments.length?(e=t,n):e},n.startAngle=function(t){return arguments.length?(r=t,n):r},n.endAngle=function(t){return arguments.length?(i=t,n):i},n};var wc={};ya.layout.stack=function(){function n(o,c){var l=o.map(function(e,r){return t.call(n,e,r)}),s=l.map(function(t){return t.map(function(t,e){return[u.call(n,t,e),a.call(n,t,e)]})}),f=e.call(n,s,c);l=ya.permute(l,f),s=ya.permute(s,f);var h,g,p,m=r.call(n,s,c),d=l.length,v=l[0].length;for(g=0;v>g;++g)for(i.call(n,l[0][g],p=m[g],s[0][g][1]),h=1;d>h;++h)i.call(n,l[h][g],p+=s[h-1][g][1],s[h][g][1]);return o}var t=mt,e=hi,r=gi,i=fi,u=li,a=si;return n.values=function(e){return arguments.length?(t=e,n):t},n.order=function(t){return arguments.length?(e="function"==typeof t?t:Sc.get(t)||hi,n):e},n.offset=function(t){return arguments.length?(r="function"==typeof t?t:Ec.get(t)||gi,n):r},n.x=function(t){return arguments.length?(u=t,n):u},n.y=function(t){return arguments.length?(a=t,n):a},n.out=function(t){return arguments.length?(i=t,n):i},n};var Sc=ya.map({"inside-out":function(n){var t,e,r=n.length,i=n.map(pi),u=n.map(mi),a=ya.range(r).sort(function(n,t){return i[n]-i[t]}),o=0,c=0,l=[],s=[];for(t=0;r>t;++t)e=a[t],c>o?(o+=u[e],l.push(e)):(c+=u[e],s.push(e));return s.reverse().concat(l)},reverse:function(n){return ya.range(n.length).reverse()},"default":hi}),Ec=ya.map({silhouette:function(n){var t,e,r,i=n.length,u=n[0].length,a=[],o=0,c=[];for(e=0;u>e;++e){for(t=0,r=0;i>t;t++)r+=n[t][e][1];r>o&&(o=r),a.push(r)}for(e=0;u>e;++e)c[e]=(o-a[e])/2;return c},wiggle:function(n){var t,e,r,i,u,a,o,c,l,s=n.length,f=n[0],h=f.length,g=[];for(g[0]=c=l=0,e=1;h>e;++e){for(t=0,i=0;s>t;++t)i+=n[t][e][1];for(t=0,u=0,o=f[e][0]-f[e-1][0];s>t;++t){for(r=0,a=(n[t][e][1]-n[t][e-1][1])/(2*o);t>r;++r)a+=(n[r][e][1]-n[r][e-1][1])/o;u+=a*n[t][e][1]}g[e]=c-=i?u/i*o:0,l>c&&(l=c)}for(e=0;h>e;++e)g[e]-=l;return g},expand:function(n){var t,e,r,i=n.length,u=n[0].length,a=1/i,o=[];for(e=0;u>e;++e){for(t=0,r=0;i>t;t++)r+=n[t][e][1];if(r)for(t=0;i>t;t++)n[t][e][1]/=r;else for(t=0;i>t;t++)n[t][e][1]=a}for(e=0;u>e;++e)o[e]=0;return o},zero:gi});ya.layout.histogram=function(){function n(n,u){for(var a,o,c=[],l=n.map(e,this),s=r.call(this,l,u),f=i.call(this,s,l,u),u=-1,h=l.length,g=f.length-1,p=t?1:1/h;++u<g;)a=c[u]=[],a.dx=f[u+1]-(a.x=f[u]),a.y=0;if(g>0)for(u=-1;++u<h;)o=l[u],o>=s[0]&&o<=s[1]&&(a=c[ya.bisect(f,o,1,g)-1],a.y+=p,a.push(n[u]));return c}var t=!0,e=Number,r=Mi,i=vi;return n.value=function(t){return arguments.length?(e=t,n):e},n.range=function(t){return arguments.length?(r=pt(t),n):r},n.bins=function(t){return arguments.length?(i="number"==typeof t?function(n){return yi(n,t)}:pt(t),n):i},n.frequency=function(e){return arguments.length?(t=!!e,n):t},n},ya.layout.tree=function(){function n(n,u){function a(n,t){var r=n.children,i=n._tree;if(r&&(u=r.length)){for(var u,o,l,s=r[0],f=s,h=-1;++h<u;)l=r[h],a(l,o),f=c(l,o,f),o=l;Ni(n);var g=.5*(s._tree.prelim+l._tree.prelim);t?(i.prelim=t._tree.prelim+e(n,t),i.mod=i.prelim-g):i.prelim=g}else t&&(i.prelim=t._tree.prelim+e(n,t))}function o(n,t){n.x=n._tree.prelim+t;var e=n.children;if(e&&(r=e.length)){var r,i=-1;for(t+=n._tree.mod;++i<r;)o(e[i],t)}}function c(n,t,r){if(t){for(var i,u=n,a=n,o=t,c=n.parent.children[0],l=u._tree.mod,s=a._tree.mod,f=o._tree.mod,h=c._tree.mod;o=_i(o),u=bi(u),o&&u;)c=bi(c),a=_i(a),a._tree.ancestor=n,i=o._tree.prelim+f-u._tree.prelim-l+e(o,u),i>0&&(qi(Ti(o,n,r),n,i),l+=i,s+=i),f+=o._tree.mod,l+=u._tree.mod,h+=c._tree.mod,s+=a._tree.mod;o&&!_i(a)&&(a._tree.thread=o,a._tree.mod+=f-s),u&&!bi(c)&&(c._tree.thread=u,c._tree.mod+=l-h,r=n)}return r}var l=t.call(this,n,u),s=l[0];Ai(s,function(n,t){n._tree={ancestor:n,prelim:0,mod:0,change:0,shift:0,number:t?t._tree.number+1:0}}),a(s),o(s,-s._tree.prelim);var f=wi(s,Ei),h=wi(s,Si),g=wi(s,ki),p=f.x-e(f,h)/2,m=h.x+e(h,f)/2,d=g.depth||1;return Ai(s,i?function(n){n.x*=r[0],n.y=n.depth*r[1],delete n._tree}:function(n){n.x=(n.x-p)/(m-p)*r[0],n.y=n.depth/d*r[1],delete n._tree}),l}var t=ya.layout.hierarchy().sort(null).value(null),e=xi,r=[1,1],i=!1;return n.separation=function(t){return arguments.length?(e=t,n):e},n.size=function(t){return arguments.length?(i=null==(r=t),n):i?null:r},n.nodeSize=function(t){return arguments.length?(i=null!=(r=t),n):i?r:null},ii(n,t)},ya.layout.pack=function(){function n(n,u){var a=e.call(this,n,u),o=a[0],c=i[0],l=i[1],s=null==t?Math.sqrt:"function"==typeof t?t:function(){return t};if(o.x=o.y=0,Ai(o,function(n){n.r=+s(n.value)}),Ai(o,Li),r){var f=r*(t?1:Math.max(2*o.r/c,2*o.r/l))/2;Ai(o,function(n){n.r+=f}),Ai(o,Li),Ai(o,function(n){n.r-=f})}return Pi(o,c/2,l/2,t?1:1/Math.max(2*o.r/c,2*o.r/l)),a}var t,e=ya.layout.hierarchy().sort(Ci),r=0,i=[1,1];return n.size=function(t){return arguments.length?(i=t,n):i},n.radius=function(e){return arguments.length?(t=null==e||"function"==typeof e?e:+e,n):t},n.padding=function(t){return arguments.length?(r=+t,n):r},ii(n,e)},ya.layout.cluster=function(){function n(n,u){var a,o=t.call(this,n,u),c=o[0],l=0;Ai(c,function(n){var t=n.children;t&&t.length?(n.x=Ri(t),n.y=Yi(t)):(n.x=a?l+=e(n,a):0,n.y=0,a=n)});var s=Ui(c),f=Ii(c),h=s.x-e(s,f)/2,g=f.x+e(f,s)/2;return Ai(c,i?function(n){n.x=(n.x-c.x)*r[0],n.y=(c.y-n.y)*r[1]}:function(n){n.x=(n.x-h)/(g-h)*r[0],n.y=(1-(c.y?n.y/c.y:1))*r[1]}),o}var t=ya.layout.hierarchy().sort(null).value(null),e=xi,r=[1,1],i=!1;return n.separation=function(t){return arguments.length?(e=t,n):e},n.size=function(t){return arguments.length?(i=null==(r=t),n):i?null:r},n.nodeSize=function(t){return arguments.length?(i=null!=(r=t),n):i?r:null},ii(n,t)},ya.layout.treemap=function(){function n(n,t){for(var e,r,i=-1,u=n.length;++i<u;)r=(e=n[i]).value*(0>t?0:t),e.area=isNaN(r)||0>=r?0:r}function t(e){var u=e.children;if(u&&u.length){var a,o,c,l=f(e),s=[],h=u.slice(),p=1/0,m="slice"===g?l.dx:"dice"===g?l.dy:"slice-dice"===g?1&e.depth?l.dy:l.dx:Math.min(l.dx,l.dy);for(n(h,l.dx*l.dy/e.value),s.area=0;(c=h.length)>0;)s.push(a=h[c-1]),s.area+=a.area,"squarify"!==g||(o=r(s,m))<=p?(h.pop(),p=o):(s.area-=s.pop().area,i(s,m,l,!1),m=Math.min(l.dx,l.dy),s.length=s.area=0,p=1/0);s.length&&(i(s,m,l,!0),s.length=s.area=0),u.forEach(t)}}function e(t){var r=t.children;if(r&&r.length){var u,a=f(t),o=r.slice(),c=[];for(n(o,a.dx*a.dy/t.value),c.area=0;u=o.pop();)c.push(u),c.area+=u.area,null!=u.z&&(i(c,u.z?a.dx:a.dy,a,!o.length),c.length=c.area=0);r.forEach(e)}}function r(n,t){for(var e,r=n.area,i=0,u=1/0,a=-1,o=n.length;++a<o;)(e=n[a].area)&&(u>e&&(u=e),e>i&&(i=e));return r*=r,t*=t,r?Math.max(t*i*p/r,r/(t*u*p)):1/0}function i(n,t,e,r){var i,u=-1,a=n.length,o=e.x,l=e.y,s=t?c(n.area/t):0;if(t==e.dx){for((r||s>e.dy)&&(s=e.dy);++u<a;)i=n[u],i.x=o,i.y=l,i.dy=s,o+=i.dx=Math.min(e.x+e.dx-o,s?c(i.area/s):0);i.z=!0,i.dx+=e.x+e.dx-o,e.y+=s,e.dy-=s}else{for((r||s>e.dx)&&(s=e.dx);++u<a;)i=n[u],i.x=o,i.y=l,i.dx=s,l+=i.dy=Math.min(e.y+e.dy-l,s?c(i.area/s):0);i.z=!1,i.dy+=e.y+e.dy-l,e.x+=s,e.dx-=s}}function u(r){var i=a||o(r),u=i[0];return u.x=0,u.y=0,u.dx=l[0],u.dy=l[1],a&&o.revalue(u),n([u],u.dx*u.dy/u.value),(a?e:t)(u),h&&(a=i),i}var a,o=ya.layout.hierarchy(),c=Math.round,l=[1,1],s=null,f=Vi,h=!1,g="squarify",p=.5*(1+Math.sqrt(5));return u.size=function(n){return arguments.length?(l=n,u):l},u.padding=function(n){function t(t){var e=n.call(u,t,t.depth);return null==e?Vi(t):Xi(t,"number"==typeof e?[e,e,e,e]:e)}function e(t){return Xi(t,n)}if(!arguments.length)return s;var r;return f=null==(s=n)?Vi:"function"==(r=typeof n)?t:"number"===r?(n=[n,n,n,n],e):e,u},u.round=function(n){return arguments.length?(c=n?Math.round:Number,u):c!=Number},u.sticky=function(n){return arguments.length?(h=n,a=null,u):h},u.ratio=function(n){return arguments.length?(p=n,u):p},u.mode=function(n){return arguments.length?(g=n+"",u):g},ii(u,o)},ya.random={normal:function(n,t){var e=arguments.length;return 2>e&&(t=1),1>e&&(n=0),function(){var e,r,i;do e=2*Math.random()-1,r=2*Math.random()-1,i=e*e+r*r;while(!i||i>1);return n+t*e*Math.sqrt(-2*Math.log(i)/i)}},logNormal:function(){var n=ya.random.normal.apply(ya,arguments);return function(){return Math.exp(n())}},irwinHall:function(n){return function(){for(var t=0,e=0;n>e;e++)t+=Math.random();return t/n}}},ya.scale={};var kc={floor:mt,ceil:mt};ya.scale.linear=function(){return Ki([0,1],[0,1],Sr,!1)},ya.scale.log=function(){return uu(ya.scale.linear().domain([0,1]),10,!0,[1,10])};var Ac=ya.format(".0e"),Nc={floor:function(n){return-Math.ceil(-n)},ceil:function(n){return-Math.floor(-n)}};ya.scale.pow=function(){return au(ya.scale.linear(),1,[0,1])},ya.scale.sqrt=function(){return ya.scale.pow().exponent(.5)},ya.scale.ordinal=function(){return cu([],{t:"range",a:[[]]})},ya.scale.category10=function(){return ya.scale.ordinal().range(qc)},ya.scale.category20=function(){return ya.scale.ordinal().range(Tc)},ya.scale.category20b=function(){return ya.scale.ordinal().range(Cc)},ya.scale.category20c=function(){return ya.scale.ordinal().range(zc)};var qc=[2062260,16744206,2924588,14034728,9725885,9197131,14907330,8355711,12369186,1556175].map(ut),Tc=[2062260,11454440,16744206,16759672,2924588,10018698,14034728,16750742,9725885,12955861,9197131,12885140,14907330,16234194,8355711,13092807,12369186,14408589,1556175,10410725].map(ut),Cc=[3750777,5395619,7040719,10264286,6519097,9216594,11915115,13556636,9202993,12426809,15186514,15190932,8666169,11356490,14049643,15177372,8077683,10834324,13528509,14589654].map(ut),zc=[3244733,7057110,10406625,13032431,15095053,16616764,16625259,16634018,3253076,7652470,10607003,13101504,7695281,10394312,12369372,14342891,6513507,9868950,12434877,14277081].map(ut);ya.scale.quantile=function(){return lu([],[])},ya.scale.quantize=function(){return su(0,1,[0,1])},ya.scale.threshold=function(){return fu([.5],[0,1])},ya.scale.identity=function(){return hu([0,1])},ya.svg.arc=function(){function n(){var n=t.apply(this,arguments),u=e.apply(this,arguments),a=r.apply(this,arguments)+Dc,o=i.apply(this,arguments)+Dc,c=(a>o&&(c=a,a=o,o=c),o-a),l=Ka>c?"0":"1",s=Math.cos(a),f=Math.sin(a),h=Math.cos(o),g=Math.sin(o);return c>=jc?n?"M0,"+u+"A"+u+","+u+" 0 1,1 0,"+-u+"A"+u+","+u+" 0 1,1 0,"+u+"M0,"+n+"A"+n+","+n+" 0 1,0 0,"+-n+"A"+n+","+n+" 0 1,0 0,"+n+"Z":"M0,"+u+"A"+u+","+u+" 0 1,1 0,"+-u+"A"+u+","+u+" 0 1,1 0,"+u+"Z":n?"M"+u*s+","+u*f+"A"+u+","+u+" 0 "+l+",1 "+u*h+","+u*g+"L"+n*h+","+n*g+"A"+n+","+n+" 0 "+l+",0 "+n*s+","+n*f+"Z":"M"+u*s+","+u*f+"A"+u+","+u+" 0 "+l+",1 "+u*h+","+u*g+"L0,0"+"Z"}var t=gu,e=pu,r=mu,i=du;return n.innerRadius=function(e){return arguments.length?(t=pt(e),n):t},n.outerRadius=function(t){return arguments.length?(e=pt(t),n):e},n.startAngle=function(t){return arguments.length?(r=pt(t),n):r},n.endAngle=function(t){return arguments.length?(i=pt(t),n):i},n.centroid=function(){var n=(t.apply(this,arguments)+e.apply(this,arguments))/2,u=(r.apply(this,arguments)+i.apply(this,arguments))/2+Dc;return[Math.cos(u)*n,Math.sin(u)*n]},n};var Dc=-Ka/2,jc=2*Ka-1e-6;ya.svg.line.radial=function(){var n=Ue(vu);return n.radius=n.x,delete n.x,n.angle=n.y,delete n.y,n},$e.reverse=We,We.reverse=$e,ya.svg.area=function(){return yu(mt)},ya.svg.area.radial=function(){var n=yu(vu);return n.radius=n.x,delete n.x,n.innerRadius=n.x0,delete n.x0,n.outerRadius=n.x1,delete n.x1,n.angle=n.y,delete n.y,n.startAngle=n.y0,delete n.y0,n.endAngle=n.y1,delete n.y1,n},ya.svg.chord=function(){function n(n,o){var c=t(this,u,n,o),l=t(this,a,n,o);return"M"+c.p0+r(c.r,c.p1,c.a1-c.a0)+(e(c,l)?i(c.r,c.p1,c.r,c.p0):i(c.r,c.p1,l.r,l.p0)+r(l.r,l.p1,l.a1-l.a0)+i(l.r,l.p1,c.r,c.p0))+"Z"}function t(n,t,e,r){var i=t.call(n,e,r),u=o.call(n,i,r),a=c.call(n,i,r)+Dc,s=l.call(n,i,r)+Dc;return{r:u,a0:a,a1:s,p0:[u*Math.cos(a),u*Math.sin(a)],p1:[u*Math.cos(s),u*Math.sin(s)]}}function e(n,t){return n.a0==t.a0&&n.a1==t.a1}function r(n,t,e){return"A"+n+","+n+" 0 "+ +(e>Ka)+",1 "+t}function i(n,t,e,r){return"Q 0,0 "+r}var u=ze,a=De,o=Mu,c=mu,l=du;return n.radius=function(t){return arguments.length?(o=pt(t),n):o},n.source=function(t){return arguments.length?(u=pt(t),n):u},n.target=function(t){return arguments.length?(a=pt(t),n):a},n.startAngle=function(t){return arguments.length?(c=pt(t),n):c},n.endAngle=function(t){return arguments.length?(l=pt(t),n):l},n},ya.svg.diagonal=function(){function n(n,i){var u=t.call(this,n,i),a=e.call(this,n,i),o=(u.y+a.y)/2,c=[u,{x:u.x,y:o},{x:a.x,y:o},a];return c=c.map(r),"M"+c[0]+"C"+c[1]+" "+c[2]+" "+c[3]}var t=ze,e=De,r=xu;return n.source=function(e){return arguments.length?(t=pt(e),n):t},n.target=function(t){return arguments.length?(e=pt(t),n):e},n.projection=function(t){return arguments.length?(r=t,n):r},n},ya.svg.diagonal.radial=function(){var n=ya.svg.diagonal(),t=xu,e=n.projection;return n.projection=function(n){return arguments.length?e(bu(t=n)):t},n},ya.svg.symbol=function(){function n(n,r){return(Lc.get(t.call(this,n,r))||Su)(e.call(this,n,r))}var t=wu,e=_u;return n.type=function(e){return arguments.length?(t=pt(e),n):t},n.size=function(t){return arguments.length?(e=pt(t),n):e},n};var Lc=ya.map({circle:Su,cross:function(n){var t=Math.sqrt(n/5)/2;return"M"+-3*t+","+-t+"H"+-t+"V"+-3*t+"H"+t+"V"+-t+"H"+3*t+"V"+t+"H"+t+"V"+3*t+"H"+-t+"V"+t+"H"+-3*t+"Z"},diamond:function(n){var t=Math.sqrt(n/(2*Oc)),e=t*Oc;return"M0,"+-t+"L"+e+",0"+" 0,"+t+" "+-e+",0"+"Z"},square:function(n){var t=Math.sqrt(n)/2;return"M"+-t+","+-t+"L"+t+","+-t+" "+t+","+t+" "+-t+","+t+"Z"},"triangle-down":function(n){var t=Math.sqrt(n/Pc),e=t*Pc/2;return"M0,"+e+"L"+t+","+-e+" "+-t+","+-e+"Z"},"triangle-up":function(n){var t=Math.sqrt(n/Pc),e=t*Pc/2;return"M0,"+-e+"L"+t+","+e+" "+-t+","+e+"Z"}});ya.svg.symbolTypes=Lc.keys();var Hc,Fc,Pc=Math.sqrt(3),Oc=Math.tan(30*to),Yc=[],Rc=0;Yc.call=Ya.call,Yc.empty=Ya.empty,Yc.node=Ya.node,Yc.size=Ya.size,ya.transition=function(n){return arguments.length?Hc?n.transition():n:Ia.transition()},ya.transition.prototype=Yc,Yc.select=function(n){var t,e,r,i=this.id,u=[];n=v(n);for(var a=-1,o=this.length;++a<o;){u.push(t=[]);for(var c=this[a],l=-1,s=c.length;++l<s;)(r=c[l])&&(e=n.call(r,r.__data__,l,a))?("__data__"in r&&(e.__data__=r.__data__),Nu(e,l,i,r.__transition__[i]),t.push(e)):t.push(null)}return Eu(u,i)},Yc.selectAll=function(n){var t,e,r,i,u,a=this.id,o=[];n=y(n);for(var c=-1,l=this.length;++c<l;)for(var s=this[c],f=-1,h=s.length;++f<h;)if(r=s[f]){u=r.__transition__[a],e=n.call(r,r.__data__,f,c),o.push(t=[]);for(var g=-1,p=e.length;++g<p;)(i=e[g])&&Nu(i,g,a,u),t.push(i)}return Eu(o,a)},Yc.filter=function(n){var t,e,r,i=[];"function"!=typeof n&&(n=N(n));for(var u=0,a=this.length;a>u;u++){i.push(t=[]);for(var e=this[u],o=0,c=e.length;c>o;o++)(r=e[o])&&n.call(r,r.__data__,o)&&t.push(r)}return Eu(i,this.id)},Yc.tween=function(n,t){var e=this.id;return arguments.length<2?this.node().__transition__[e].tween.get(n):T(this,null==t?function(t){t.__transition__[e].tween.remove(n)}:function(r){r.__transition__[e].tween.set(n,t)})},Yc.attr=function(n,t){function e(){this.removeAttribute(o)}function r(){this.removeAttributeNS(o.space,o.local)}function i(n){return null==n?e:(n+="",function(){var t,e=this.getAttribute(o);return e!==n&&(t=a(e,n),function(n){this.setAttribute(o,t(n))})})}function u(n){return null==n?r:(n+="",function(){var t,e=this.getAttributeNS(o.space,o.local);return e!==n&&(t=a(e,n),function(n){this.setAttributeNS(o.space,o.local,t(n))})})}if(arguments.length<2){for(t in n)this.attr(t,n[t]);return this}var a="transform"==n?Br:Sr,o=ya.ns.qualify(n);return ku(this,"attr."+n,t,o.local?u:i)},Yc.attrTween=function(n,t){function e(n,e){var r=t.call(this,n,e,this.getAttribute(i));return r&&function(n){this.setAttribute(i,r(n))}}function r(n,e){var r=t.call(this,n,e,this.getAttributeNS(i.space,i.local));return r&&function(n){this.setAttributeNS(i.space,i.local,r(n))}}var i=ya.ns.qualify(n);return this.tween("attr."+n,i.local?r:e)},Yc.style=function(n,t,e){function r(){this.style.removeProperty(n)}function i(t){return null==t?r:(t+="",function(){var r,i=ba.getComputedStyle(this,null).getPropertyValue(n);return i!==t&&(r=Sr(i,t),function(t){this.style.setProperty(n,r(t),e)})})}var u=arguments.length;if(3>u){if("string"!=typeof n){2>u&&(t="");for(e in n)this.style(e,n[e],t);return this}e=""}return ku(this,"style."+n,t,i)},Yc.styleTween=function(n,t,e){function r(r,i){var u=t.call(this,r,i,ba.getComputedStyle(this,null).getPropertyValue(n));return u&&function(t){this.style.setProperty(n,u(t),e)}}return arguments.length<3&&(e=""),this.tween("style."+n,r)},Yc.text=function(n){return ku(this,"text",n,Au)},Yc.remove=function(){return this.each("end.transition",function(){var n;!this.__transition__&&(n=this.parentNode)&&n.removeChild(this)})},Yc.ease=function(n){var t=this.id;return arguments.length<1?this.node().__transition__[t].ease:("function"!=typeof n&&(n=ya.ease.apply(ya,arguments)),T(this,function(e){e.__transition__[t].ease=n}))},Yc.delay=function(n){var t=this.id;return T(this,"function"==typeof n?function(e,r,i){e.__transition__[t].delay=0|n.call(e,e.__data__,r,i)}:(n|=0,function(e){e.__transition__[t].delay=n}))},Yc.duration=function(n){var t=this.id;return T(this,"function"==typeof n?function(e,r,i){e.__transition__[t].duration=Math.max(1,0|n.call(e,e.__data__,r,i))}:(n=Math.max(1,0|n),function(e){e.__transition__[t].duration=n}))},Yc.each=function(n,t){var e=this.id;if(arguments.length<2){var r=Fc,i=Hc;Hc=e,T(this,function(t,r,i){Fc=t.__transition__[e],n.call(t,t.__data__,r,i)}),Fc=r,Hc=i}else T(this,function(r){var i=r.__transition__[e];(i.event||(i.event=ya.dispatch("start","end"))).on(n,t)});return this},Yc.transition=function(){for(var n,t,e,r,i=this.id,u=++Rc,a=[],o=0,c=this.length;c>o;o++){a.push(n=[]);for(var t=this[o],l=0,s=t.length;s>l;l++)(e=t[l])&&(r=Object.create(e.__transition__[i]),r.delay+=r.duration,Nu(e,l,u,r)),n.push(e)}return Eu(a,u)},ya.svg.axis=function(){function n(n){n.each(function(){var n,f=ya.select(this),h=null==l?e.ticks?e.ticks.apply(e,c):e.domain():l,g=null==t?e.tickFormat?e.tickFormat.apply(e,c):String:t,p=Cu(e,h,s),m=f.selectAll(".tick.minor").data(p,String),d=m.enter().insert("line",".tick").attr("class","tick minor").style("opacity",1e-6),v=ya.transition(m.exit()).style("opacity",1e-6).remove(),y=ya.transition(m).style("opacity",1),M=f.selectAll(".tick.major").data(h,String),x=M.enter().insert("g",".domain").attr("class","tick major").style("opacity",1e-6),b=ya.transition(M.exit()).style("opacity",1e-6).remove(),_=ya.transition(M).style("opacity",1),w=Bi(e),S=f.selectAll(".domain").data([0]),E=(S.enter().append("path").attr("class","domain"),ya.transition(S)),k=e.copy(),A=this.__chart__||k;
5
+ this.__chart__=k,x.append("line"),x.append("text");var N=x.select("line"),q=_.select("line"),T=M.select("text").text(g),C=x.select("text"),z=_.select("text");switch(r){case"bottom":n=qu,d.attr("y2",u),y.attr("x2",0).attr("y2",u),N.attr("y2",i),C.attr("y",Math.max(i,0)+o),q.attr("x2",0).attr("y2",i),z.attr("x",0).attr("y",Math.max(i,0)+o),T.attr("dy",".71em").style("text-anchor","middle"),E.attr("d","M"+w[0]+","+a+"V0H"+w[1]+"V"+a);break;case"top":n=qu,d.attr("y2",-u),y.attr("x2",0).attr("y2",-u),N.attr("y2",-i),C.attr("y",-(Math.max(i,0)+o)),q.attr("x2",0).attr("y2",-i),z.attr("x",0).attr("y",-(Math.max(i,0)+o)),T.attr("dy","0em").style("text-anchor","middle"),E.attr("d","M"+w[0]+","+-a+"V0H"+w[1]+"V"+-a);break;case"left":n=Tu,d.attr("x2",-u),y.attr("x2",-u).attr("y2",0),N.attr("x2",-i),C.attr("x",-(Math.max(i,0)+o)),q.attr("x2",-i).attr("y2",0),z.attr("x",-(Math.max(i,0)+o)).attr("y",0),T.attr("dy",".32em").style("text-anchor","end"),E.attr("d","M"+-a+","+w[0]+"H0V"+w[1]+"H"+-a);break;case"right":n=Tu,d.attr("x2",u),y.attr("x2",u).attr("y2",0),N.attr("x2",i),C.attr("x",Math.max(i,0)+o),q.attr("x2",i).attr("y2",0),z.attr("x",Math.max(i,0)+o).attr("y",0),T.attr("dy",".32em").style("text-anchor","start"),E.attr("d","M"+a+","+w[0]+"H0V"+w[1]+"H"+a)}if(e.rangeBand){var D=k.rangeBand()/2,j=function(n){return k(n)+D};x.call(n,j),_.call(n,j)}else x.call(n,A),_.call(n,k),b.call(n,k),d.call(n,A),y.call(n,k),v.call(n,k)})}var t,e=ya.scale.linear(),r=Uc,i=6,u=6,a=6,o=3,c=[10],l=null,s=0;return n.scale=function(t){return arguments.length?(e=t,n):e},n.orient=function(t){return arguments.length?(r=t in Ic?t+"":Uc,n):r},n.ticks=function(){return arguments.length?(c=arguments,n):c},n.tickValues=function(t){return arguments.length?(l=t,n):l},n.tickFormat=function(e){return arguments.length?(t=e,n):t},n.tickSize=function(t,e){if(!arguments.length)return i;var r=arguments.length-1;return i=+t,u=r>1?+e:i,a=r>0?+arguments[r]:i,n},n.tickPadding=function(t){return arguments.length?(o=+t,n):o},n.tickSubdivide=function(t){return arguments.length?(s=+t,n):s},n};var Uc="bottom",Ic={top:1,right:1,bottom:1,left:1};ya.svg.brush=function(){function n(u){u.each(function(){var u,a=ya.select(this),s=a.selectAll(".background").data([0]),f=a.selectAll(".extent").data([0]),h=a.selectAll(".resize").data(l,String);a.style("pointer-events","all").on("mousedown.brush",i).on("touchstart.brush",i),s.enter().append("rect").attr("class","background").style("visibility","hidden").style("cursor","crosshair"),f.enter().append("rect").attr("class","extent").style("cursor","move"),h.enter().append("g").attr("class",function(n){return"resize "+n}).style("cursor",function(n){return Vc[n]}).append("rect").attr("x",function(n){return/[ew]$/.test(n)?-3:null}).attr("y",function(n){return/^[ns]/.test(n)?-3:null}).attr("width",6).attr("height",6).style("visibility","hidden"),h.style("display",n.empty()?"none":null),h.exit().remove(),o&&(u=Bi(o),s.attr("x",u[0]).attr("width",u[1]-u[0]),e(a)),c&&(u=Bi(c),s.attr("y",u[0]).attr("height",u[1]-u[0]),r(a)),t(a)})}function t(n){n.selectAll(".resize").attr("transform",function(n){return"translate("+s[+/e$/.test(n)][0]+","+s[+/^s/.test(n)][1]+")"})}function e(n){n.select(".extent").attr("x",s[0][0]),n.selectAll(".extent,.n>rect,.s>rect").attr("width",s[1][0]-s[0][0])}function r(n){n.select(".extent").attr("y",s[0][1]),n.selectAll(".extent,.e>rect,.w>rect").attr("height",s[1][1]-s[0][1])}function i(){function i(){var n=ya.event.changedTouches;return n?ya.touches(M,n)[0]:ya.mouse(M)}function l(){32==ya.event.keyCode&&(k||(v=null,N[0]-=s[1][0],N[1]-=s[1][1],k=2),g())}function h(){32==ya.event.keyCode&&2==k&&(N[0]+=s[1][0],N[1]+=s[1][1],k=0,g())}function p(){var n=i(),u=!1;y&&(n[0]+=y[0],n[1]+=y[1]),k||(ya.event.altKey?(v||(v=[(s[0][0]+s[1][0])/2,(s[0][1]+s[1][1])/2]),N[0]=s[+(n[0]<v[0])][0],N[1]=s[+(n[1]<v[1])][1]):v=null),S&&m(n,o,0)&&(e(_),u=!0),E&&m(n,c,1)&&(r(_),u=!0),u&&(t(_),b({type:"brush",mode:k?"move":"resize"}))}function m(n,t,e){var r,i,a=Bi(t),o=a[0],c=a[1],l=N[e],h=s[1][e]-s[0][e];return k&&(o-=l,c-=h+l),r=f[e]?Math.max(o,Math.min(c,n[e])):n[e],k?i=(r+=l)+h:(v&&(l=Math.max(o,Math.min(c,2*v[e]-r))),r>l?(i=r,r=l):i=l),s[0][e]!==r||s[1][e]!==i?(u=null,s[0][e]=r,s[1][e]=i,!0):void 0}function d(){p(),_.style("pointer-events","all").selectAll(".resize").style("display",n.empty()?"none":null),ya.select("body").style("cursor",null),q.on("mousemove.brush",null).on("mouseup.brush",null).on("touchmove.brush",null).on("touchend.brush",null).on("keydown.brush",null).on("keyup.brush",null),A(),b({type:"brushend"})}var v,y,M=this,x=ya.select(ya.event.target),b=a.of(M,arguments),_=ya.select(M),w=x.datum(),S=!/^(n|s)$/.test(w)&&o,E=!/^(e|w)$/.test(w)&&c,k=x.classed("extent"),A=H(),N=i(),q=ya.select(ba).on("keydown.brush",l).on("keyup.brush",h);if(ya.event.changedTouches?q.on("touchmove.brush",p).on("touchend.brush",d):q.on("mousemove.brush",p).on("mouseup.brush",d),k)N[0]=s[0][0]-N[0],N[1]=s[0][1]-N[1];else if(w){var T=+/w$/.test(w),C=+/^n/.test(w);y=[s[1-T][0]-N[0],s[1-C][1]-N[1]],N[0]=s[T][0],N[1]=s[C][1]}else ya.event.altKey&&(v=N.slice());_.style("pointer-events","none").selectAll(".resize").style("display",null),ya.select("body").style("cursor",x.style("cursor")),b({type:"brushstart"}),p()}var u,a=m(n,"brushstart","brush","brushend"),o=null,c=null,l=Xc[0],s=[[0,0],[0,0]],f=[!0,!0];return n.x=function(t){return arguments.length?(o=t,l=Xc[!o<<1|!c],n):o},n.y=function(t){return arguments.length?(c=t,l=Xc[!o<<1|!c],n):c},n.clamp=function(t){return arguments.length?(o&&c?f=[!!t[0],!!t[1]]:(o||c)&&(f[+!o]=!!t),n):o&&c?f:o||c?f[+!o]:null},n.extent=function(t){var e,r,i,a,l;return arguments.length?(u=[[0,0],[0,0]],o&&(e=t[0],r=t[1],c&&(e=e[0],r=r[0]),u[0][0]=e,u[1][0]=r,o.invert&&(e=o(e),r=o(r)),e>r&&(l=e,e=r,r=l),s[0][0]=0|e,s[1][0]=0|r),c&&(i=t[0],a=t[1],o&&(i=i[1],a=a[1]),u[0][1]=i,u[1][1]=a,c.invert&&(i=c(i),a=c(a)),i>a&&(l=i,i=a,a=l),s[0][1]=0|i,s[1][1]=0|a),n):(t=u||s,o&&(e=t[0][0],r=t[1][0],u||(e=s[0][0],r=s[1][0],o.invert&&(e=o.invert(e),r=o.invert(r)),e>r&&(l=e,e=r,r=l))),c&&(i=t[0][1],a=t[1][1],u||(i=s[0][1],a=s[1][1],c.invert&&(i=c.invert(i),a=c.invert(a)),i>a&&(l=i,i=a,a=l))),o&&c?[[e,i],[r,a]]:o?[e,r]:c&&[i,a])},n.clear=function(){return u=null,s[0][0]=s[0][1]=s[1][0]=s[1][1]=0,n},n.empty=function(){return o&&s[0][0]===s[1][0]||c&&s[0][1]===s[1][1]},ya.rebind(n,a,"on")};var Vc={n:"ns-resize",e:"ew-resize",s:"ns-resize",w:"ew-resize",nw:"nwse-resize",ne:"nesw-resize",se:"nwse-resize",sw:"nesw-resize"},Xc=[["n","e","s","w","nw","ne","se","sw"],["e","w"],["n","s"],[]];ya.time={};var Zc=Date,Bc=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];zu.prototype={getDate:function(){return this._.getUTCDate()},getDay:function(){return this._.getUTCDay()},getFullYear:function(){return this._.getUTCFullYear()},getHours:function(){return this._.getUTCHours()},getMilliseconds:function(){return this._.getUTCMilliseconds()},getMinutes:function(){return this._.getUTCMinutes()},getMonth:function(){return this._.getUTCMonth()},getSeconds:function(){return this._.getUTCSeconds()},getTime:function(){return this._.getTime()},getTimezoneOffset:function(){return 0},valueOf:function(){return this._.valueOf()},setDate:function(){$c.setUTCDate.apply(this._,arguments)},setDay:function(){$c.setUTCDay.apply(this._,arguments)},setFullYear:function(){$c.setUTCFullYear.apply(this._,arguments)},setHours:function(){$c.setUTCHours.apply(this._,arguments)},setMilliseconds:function(){$c.setUTCMilliseconds.apply(this._,arguments)},setMinutes:function(){$c.setUTCMinutes.apply(this._,arguments)},setMonth:function(){$c.setUTCMonth.apply(this._,arguments)},setSeconds:function(){$c.setUTCSeconds.apply(this._,arguments)},setTime:function(){$c.setTime.apply(this._,arguments)}};var $c=Date.prototype,Wc="%a %b %e %X %Y",Jc="%m/%d/%Y",Gc="%H:%M:%S",Kc=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],Qc=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],nl=["January","February","March","April","May","June","July","August","September","October","November","December"],tl=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];ya.time.year=Du(function(n){return n=ya.time.day(n),n.setMonth(0,1),n},function(n,t){n.setFullYear(n.getFullYear()+t)},function(n){return n.getFullYear()}),ya.time.years=ya.time.year.range,ya.time.years.utc=ya.time.year.utc.range,ya.time.day=Du(function(n){var t=new Zc(2e3,0);return t.setFullYear(n.getFullYear(),n.getMonth(),n.getDate()),t},function(n,t){n.setDate(n.getDate()+t)},function(n){return n.getDate()-1}),ya.time.days=ya.time.day.range,ya.time.days.utc=ya.time.day.utc.range,ya.time.dayOfYear=function(n){var t=ya.time.year(n);return Math.floor((n-t-6e4*(n.getTimezoneOffset()-t.getTimezoneOffset()))/864e5)},Bc.forEach(function(n,t){n=n.toLowerCase(),t=7-t;var e=ya.time[n]=Du(function(n){return(n=ya.time.day(n)).setDate(n.getDate()-(n.getDay()+t)%7),n},function(n,t){n.setDate(n.getDate()+7*Math.floor(t))},function(n){var e=ya.time.year(n).getDay();return Math.floor((ya.time.dayOfYear(n)+(e+t)%7)/7)-(e!==t)});ya.time[n+"s"]=e.range,ya.time[n+"s"].utc=e.utc.range,ya.time[n+"OfYear"]=function(n){var e=ya.time.year(n).getDay();return Math.floor((ya.time.dayOfYear(n)+(e+t)%7)/7)}}),ya.time.week=ya.time.sunday,ya.time.weeks=ya.time.sunday.range,ya.time.weeks.utc=ya.time.sunday.utc.range,ya.time.weekOfYear=ya.time.sundayOfYear,ya.time.format=function(n){function t(t){for(var r,i,u,a=[],o=-1,c=0;++o<e;)37===n.charCodeAt(o)&&(a.push(n.substring(c,o)),null!=(i=fl[r=n.charAt(++o)])&&(r=n.charAt(++o)),(u=hl[r])&&(r=u(t,null==i?"e"===r?" ":"0":i)),a.push(r),c=o+1);return a.push(n.substring(c,o)),a.join("")}var e=n.length;return t.parse=function(t){var e={y:1900,m:0,d:1,H:0,M:0,S:0,L:0},r=Lu(e,n,t,0);if(r!=t.length)return null;"p"in e&&(e.H=e.H%12+12*e.p);var i=new Zc;return"j"in e?i.setFullYear(e.y,0,e.j):"w"in e&&("W"in e||"U"in e)?(i.setFullYear(e.y,0,1),i.setFullYear(e.y,0,"W"in e?(e.w+6)%7+7*e.W-(i.getDay()+5)%7:e.w+7*e.U-(i.getDay()+6)%7)):i.setFullYear(e.y,e.m,e.d),i.setHours(e.H,e.M,e.S,e.L),i},t.toString=function(){return n},t};var el=Hu(Kc),rl=Fu(Kc),il=Hu(Qc),ul=Fu(Qc),al=Hu(nl),ol=Fu(nl),cl=Hu(tl),ll=Fu(tl),sl=/^%/,fl={"-":"",_:" ",0:"0"},hl={a:function(n){return Qc[n.getDay()]},A:function(n){return Kc[n.getDay()]},b:function(n){return tl[n.getMonth()]},B:function(n){return nl[n.getMonth()]},c:ya.time.format(Wc),d:function(n,t){return Pu(n.getDate(),t,2)},e:function(n,t){return Pu(n.getDate(),t,2)},H:function(n,t){return Pu(n.getHours(),t,2)},I:function(n,t){return Pu(n.getHours()%12||12,t,2)},j:function(n,t){return Pu(1+ya.time.dayOfYear(n),t,3)},L:function(n,t){return Pu(n.getMilliseconds(),t,3)},m:function(n,t){return Pu(n.getMonth()+1,t,2)},M:function(n,t){return Pu(n.getMinutes(),t,2)},p:function(n){return n.getHours()>=12?"PM":"AM"},S:function(n,t){return Pu(n.getSeconds(),t,2)},U:function(n,t){return Pu(ya.time.sundayOfYear(n),t,2)},w:function(n){return n.getDay()},W:function(n,t){return Pu(ya.time.mondayOfYear(n),t,2)},x:ya.time.format(Jc),X:ya.time.format(Gc),y:function(n,t){return Pu(n.getFullYear()%100,t,2)},Y:function(n,t){return Pu(n.getFullYear()%1e4,t,4)},Z:aa,"%":function(){return"%"}},gl={a:Ou,A:Yu,b:Vu,B:Xu,c:Zu,d:Qu,e:Qu,H:ta,I:ta,j:na,L:ia,m:Ku,M:ea,p:ua,S:ra,U:Uu,w:Ru,W:Iu,x:Bu,X:$u,y:Ju,Y:Wu,"%":oa},pl=/^\s*\d+/,ml=ya.map({am:0,pm:1});ya.time.format.utc=function(n){function t(n){try{Zc=zu;var t=new Zc;return t._=n,e(t)}finally{Zc=Date}}var e=ya.time.format(n);return t.parse=function(n){try{Zc=zu;var t=e.parse(n);return t&&t._}finally{Zc=Date}},t.toString=e.toString,t};var dl=ya.time.format.utc("%Y-%m-%dT%H:%M:%S.%LZ");ya.time.format.iso=Date.prototype.toISOString&&+new Date("2000-01-01T00:00:00.000Z")?ca:dl,ca.parse=function(n){var t=new Date(n);return isNaN(t)?null:t},ca.toString=dl.toString,ya.time.second=Du(function(n){return new Zc(1e3*Math.floor(n/1e3))},function(n,t){n.setTime(n.getTime()+1e3*Math.floor(t))},function(n){return n.getSeconds()}),ya.time.seconds=ya.time.second.range,ya.time.seconds.utc=ya.time.second.utc.range,ya.time.minute=Du(function(n){return new Zc(6e4*Math.floor(n/6e4))},function(n,t){n.setTime(n.getTime()+6e4*Math.floor(t))},function(n){return n.getMinutes()}),ya.time.minutes=ya.time.minute.range,ya.time.minutes.utc=ya.time.minute.utc.range,ya.time.hour=Du(function(n){var t=n.getTimezoneOffset()/60;return new Zc(36e5*(Math.floor(n/36e5-t)+t))},function(n,t){n.setTime(n.getTime()+36e5*Math.floor(t))},function(n){return n.getHours()}),ya.time.hours=ya.time.hour.range,ya.time.hours.utc=ya.time.hour.utc.range,ya.time.month=Du(function(n){return n=ya.time.day(n),n.setDate(1),n},function(n,t){n.setMonth(n.getMonth()+t)},function(n){return n.getMonth()}),ya.time.months=ya.time.month.range,ya.time.months.utc=ya.time.month.utc.range;var vl=[1e3,5e3,15e3,3e4,6e4,3e5,9e5,18e5,36e5,108e5,216e5,432e5,864e5,1728e5,6048e5,2592e6,7776e6,31536e6],yl=[[ya.time.second,1],[ya.time.second,5],[ya.time.second,15],[ya.time.second,30],[ya.time.minute,1],[ya.time.minute,5],[ya.time.minute,15],[ya.time.minute,30],[ya.time.hour,1],[ya.time.hour,3],[ya.time.hour,6],[ya.time.hour,12],[ya.time.day,1],[ya.time.day,2],[ya.time.week,1],[ya.time.month,1],[ya.time.month,3],[ya.time.year,1]],Ml=[[ya.time.format("%Y"),Xt],[ya.time.format("%B"),function(n){return n.getMonth()}],[ya.time.format("%b %d"),function(n){return 1!=n.getDate()}],[ya.time.format("%a %d"),function(n){return n.getDay()&&1!=n.getDate()}],[ya.time.format("%I %p"),function(n){return n.getHours()}],[ya.time.format("%I:%M"),function(n){return n.getMinutes()}],[ya.time.format(":%S"),function(n){return n.getSeconds()}],[ya.time.format(".%L"),function(n){return n.getMilliseconds()}]],xl=ya.scale.linear(),bl=fa(Ml);yl.year=function(n,t){return xl.domain(n.map(ga)).ticks(t).map(ha)},ya.time.scale=function(){return la(ya.scale.linear(),yl,bl)};var _l=yl.map(function(n){return[n[0].utc,n[1]]}),wl=[[ya.time.format.utc("%Y"),Xt],[ya.time.format.utc("%B"),function(n){return n.getUTCMonth()}],[ya.time.format.utc("%b %d"),function(n){return 1!=n.getUTCDate()}],[ya.time.format.utc("%a %d"),function(n){return n.getUTCDay()&&1!=n.getUTCDate()}],[ya.time.format.utc("%I %p"),function(n){return n.getUTCHours()}],[ya.time.format.utc("%I:%M"),function(n){return n.getUTCMinutes()}],[ya.time.format.utc(":%S"),function(n){return n.getUTCSeconds()}],[ya.time.format.utc(".%L"),function(n){return n.getUTCMilliseconds()}]],Sl=fa(wl);return _l.year=function(n,t){return xl.domain(n.map(ma)).ticks(t).map(pa)},ya.time.scale.utc=function(){return la(ya.scale.linear(),_l,Sl)},ya.text=dt(function(n){return n.responseText}),ya.json=function(n,t){return vt(n,"application/json",da,t)},ya.html=function(n,t){return vt(n,"text/html",va,t)},ya.xml=dt(function(n){return n.responseXML}),ya}();
@@ -0,0 +1,37 @@
1
+ #= require_directory ./gridster
2
+
3
+ # This file enables gridster integration (http://gridster.net/)
4
+ # Delete it if you'd rather handle the layout yourself.
5
+ # You'll miss out on a lot if you do, but we won't hold it against you.
6
+
7
+ Dashing.gridsterLayout = (positions) ->
8
+ Dashing.customGridsterLayout = true
9
+ positions = positions.replace(/^"|"$/g, '')
10
+ positions = $.parseJSON(positions)
11
+ widgets = $("[data-row^=]")
12
+ for widget, index in widgets
13
+ $(widget).attr('data-row', positions[index].row)
14
+ $(widget).attr('data-col', positions[index].col)
15
+
16
+ Dashing.getWidgetPositions = ->
17
+ $(".gridster ul:first").gridster().data('gridster').serialize()
18
+
19
+ Dashing.showGridsterInstructions = ->
20
+ newWidgetPositions = Dashing.getWidgetPositions()
21
+
22
+ unless JSON.stringify(newWidgetPositions) == JSON.stringify(Dashing.currentWidgetPositions)
23
+ Dashing.currentWidgetPositions = newWidgetPositions
24
+ $('#save-gridster').slideDown()
25
+ $('#gridster-code').text("
26
+ <script type='text/javascript'>\n
27
+ $(function() {\n
28
+ \ \ Dashing.gridsterLayout('#{JSON.stringify(Dashing.currentWidgetPositions)}')\n
29
+ });\n
30
+ </script>
31
+ ")
32
+
33
+ $ ->
34
+ $('#save-gridster').leanModal()
35
+
36
+ $('#save-gridster').click ->
37
+ $('#save-gridster').slideUp()
@@ -0,0 +1,3263 @@
1
+ /*! gridster.js - v0.1.0 - 2013-02-15
2
+ * http://gridster.net/
3
+ * Copyright (c) 2013 ducksboard; Licensed MIT */
4
+
5
+ ;(function($, window, document, undefined){
6
+ /**
7
+ * Creates objects with coordinates (x1, y1, x2, y2, cx, cy, width, height)
8
+ * to simulate DOM elements on the screen.
9
+ * Coords is used by Gridster to create a faux grid with any DOM element can
10
+ * collide.
11
+ *
12
+ * @class Coords
13
+ * @param {HTMLElement|Object} obj The jQuery HTMLElement or a object with: left,
14
+ * top, width and height properties.
15
+ * @return {Object} Coords instance.
16
+ * @constructor
17
+ */
18
+ function Coords(obj) {
19
+ if (obj[0] && $.isPlainObject(obj[0])) {
20
+ this.data = obj[0];
21
+ }else {
22
+ this.el = obj;
23
+ }
24
+
25
+ this.isCoords = true;
26
+ this.coords = {};
27
+ this.init();
28
+ return this;
29
+ }
30
+
31
+
32
+ var fn = Coords.prototype;
33
+
34
+
35
+ fn.init = function(){
36
+ this.set();
37
+ this.original_coords = this.get();
38
+ };
39
+
40
+
41
+ fn.set = function(update, not_update_offsets) {
42
+ var el = this.el;
43
+
44
+ if (el && !update) {
45
+ this.data = el.offset();
46
+ this.data.width = el.width();
47
+ this.data.height = el.height();
48
+ }
49
+
50
+ if (el && update && !not_update_offsets) {
51
+ var offset = el.offset();
52
+ this.data.top = offset.top;
53
+ this.data.left = offset.left;
54
+ }
55
+
56
+ var d = this.data;
57
+
58
+ this.coords.x1 = d.left;
59
+ this.coords.y1 = d.top;
60
+ this.coords.x2 = d.left + d.width;
61
+ this.coords.y2 = d.top + d.height;
62
+ this.coords.cx = d.left + (d.width / 2);
63
+ this.coords.cy = d.top + (d.height / 2);
64
+ this.coords.width = d.width;
65
+ this.coords.height = d.height;
66
+ this.coords.el = el || false ;
67
+
68
+ return this;
69
+ };
70
+
71
+
72
+ fn.update = function(data){
73
+ if (!data && !this.el) {
74
+ return this;
75
+ }
76
+
77
+ if (data) {
78
+ var new_data = $.extend({}, this.data, data);
79
+ this.data = new_data;
80
+ return this.set(true, true);
81
+ }
82
+
83
+ this.set(true);
84
+ return this;
85
+ };
86
+
87
+
88
+ fn.get = function(){
89
+ return this.coords;
90
+ };
91
+
92
+
93
+ //jQuery adapter
94
+ $.fn.coords = function() {
95
+ if (this.data('coords') ) {
96
+ return this.data('coords');
97
+ }
98
+
99
+ var ins = new Coords(this, arguments[0]);
100
+ this.data('coords', ins);
101
+ return ins;
102
+ };
103
+
104
+ }(jQuery, window, document));
105
+
106
+ ;(function($, window, document, undefined){
107
+
108
+ var defaults = {
109
+ colliders_context: document.body
110
+ // ,on_overlap: function(collider_data){},
111
+ // on_overlap_start : function(collider_data){},
112
+ // on_overlap_stop : function(collider_data){}
113
+ };
114
+
115
+
116
+ /**
117
+ * Detects collisions between a DOM element against other DOM elements or
118
+ * Coords objects.
119
+ *
120
+ * @class Collision
121
+ * @uses Coords
122
+ * @param {HTMLElement} el The jQuery wrapped HTMLElement.
123
+ * @param {HTMLElement|Array} colliders Can be a jQuery collection
124
+ * of HTMLElements or an Array of Coords instances.
125
+ * @param {Object} [options] An Object with all options you want to
126
+ * overwrite:
127
+ * @param {Function} [options.on_overlap_start] Executes a function the first
128
+ * time each `collider ` is overlapped.
129
+ * @param {Function} [options.on_overlap_stop] Executes a function when a
130
+ * `collider` is no longer collided.
131
+ * @param {Function} [options.on_overlap] Executes a function when the
132
+ * mouse is moved during the collision.
133
+ * @return {Object} Collision instance.
134
+ * @constructor
135
+ */
136
+ function Collision(el, colliders, options) {
137
+ this.options = $.extend(defaults, options);
138
+ this.$element = el;
139
+ this.last_colliders = [];
140
+ this.last_colliders_coords = [];
141
+ if (typeof colliders === 'string' || colliders instanceof jQuery) {
142
+ this.$colliders = $(colliders,
143
+ this.options.colliders_context).not(this.$element);
144
+ }else{
145
+ this.colliders = $(colliders);
146
+ }
147
+
148
+ this.init();
149
+ }
150
+
151
+
152
+ var fn = Collision.prototype;
153
+
154
+
155
+ fn.init = function() {
156
+ this.find_collisions();
157
+ };
158
+
159
+
160
+ fn.overlaps = function(a, b) {
161
+ var x = false;
162
+ var y = false;
163
+
164
+ if ((b.x1 >= a.x1 && b.x1 <= a.x2) ||
165
+ (b.x2 >= a.x1 && b.x2 <= a.x2) ||
166
+ (a.x1 >= b.x1 && a.x2 <= b.x2)
167
+ ) { x = true; }
168
+
169
+ if ((b.y1 >= a.y1 && b.y1 <= a.y2) ||
170
+ (b.y2 >= a.y1 && b.y2 <= a.y2) ||
171
+ (a.y1 >= b.y1 && a.y2 <= b.y2)
172
+ ) { y = true; }
173
+
174
+ return (x && y);
175
+ };
176
+
177
+
178
+ fn.detect_overlapping_region = function(a, b){
179
+ var regionX = '';
180
+ var regionY = '';
181
+
182
+ if (a.y1 > b.cy && a.y1 < b.y2) { regionX = 'N'; }
183
+ if (a.y2 > b.y1 && a.y2 < b.cy) { regionX = 'S'; }
184
+ if (a.x1 > b.cx && a.x1 < b.x2) { regionY = 'W'; }
185
+ if (a.x2 > b.x1 && a.x2 < b.cx) { regionY = 'E'; }
186
+
187
+ return (regionX + regionY) || 'C';
188
+ };
189
+
190
+
191
+ fn.calculate_overlapped_area_coords = function(a, b){
192
+ var x1 = Math.max(a.x1, b.x1);
193
+ var y1 = Math.max(a.y1, b.y1);
194
+ var x2 = Math.min(a.x2, b.x2);
195
+ var y2 = Math.min(a.y2, b.y2);
196
+
197
+ return $({
198
+ left: x1,
199
+ top: y1,
200
+ width : (x2 - x1),
201
+ height: (y2 - y1)
202
+ }).coords().get();
203
+ };
204
+
205
+
206
+ fn.calculate_overlapped_area = function(coords){
207
+ return (coords.width * coords.height);
208
+ };
209
+
210
+
211
+ fn.manage_colliders_start_stop = function(new_colliders_coords, start_callback, stop_callback){
212
+ var last = this.last_colliders_coords;
213
+
214
+ for (var i = 0, il = last.length; i < il; i++) {
215
+ if ($.inArray(last[i], new_colliders_coords) === -1) {
216
+ start_callback.call(this, last[i]);
217
+ }
218
+ }
219
+
220
+ for (var j = 0, jl = new_colliders_coords.length; j < jl; j++) {
221
+ if ($.inArray(new_colliders_coords[j], last) === -1) {
222
+ stop_callback.call(this, new_colliders_coords[j]);
223
+ }
224
+
225
+ }
226
+ };
227
+
228
+
229
+ fn.find_collisions = function(player_data_coords){
230
+ var self = this;
231
+ var colliders_coords = [];
232
+ var colliders_data = [];
233
+ var $colliders = (this.colliders || this.$colliders);
234
+ var count = $colliders.length;
235
+ var player_coords = self.$element.coords()
236
+ .update(player_data_coords || false).get();
237
+
238
+ while(count--){
239
+ var $collider = self.$colliders ?
240
+ $($colliders[count]) : $colliders[count];
241
+ var $collider_coords_ins = ($collider.isCoords) ?
242
+ $collider : $collider.coords();
243
+ var collider_coords = $collider_coords_ins.get();
244
+ var overlaps = self.overlaps(player_coords, collider_coords);
245
+
246
+ if (!overlaps) {
247
+ continue;
248
+ }
249
+
250
+ var region = self.detect_overlapping_region(
251
+ player_coords, collider_coords);
252
+
253
+ //todo: make this an option
254
+ if (region === 'C'){
255
+ var area_coords = self.calculate_overlapped_area_coords(
256
+ player_coords, collider_coords);
257
+ var area = self.calculate_overlapped_area(area_coords);
258
+ var collider_data = {
259
+ area: area,
260
+ area_coords : area_coords,
261
+ region: region,
262
+ coords: collider_coords,
263
+ player_coords: player_coords,
264
+ el: $collider
265
+ };
266
+
267
+ if (self.options.on_overlap) {
268
+ self.options.on_overlap.call(this, collider_data);
269
+ }
270
+ colliders_coords.push($collider_coords_ins);
271
+ colliders_data.push(collider_data);
272
+ }
273
+ }
274
+
275
+ if (self.options.on_overlap_stop || self.options.on_overlap_start) {
276
+ this.manage_colliders_start_stop(colliders_coords,
277
+ self.options.on_overlap_stop, self.options.on_overlap_start);
278
+ }
279
+
280
+ this.last_colliders_coords = colliders_coords;
281
+
282
+ return colliders_data;
283
+ };
284
+
285
+
286
+ fn.get_closest_colliders = function(player_data_coords){
287
+ var colliders = this.find_collisions(player_data_coords);
288
+
289
+ colliders.sort(function(a, b) {
290
+ /* if colliders are being overlapped by the "C" (center) region,
291
+ * we have to set a lower index in the array to which they are placed
292
+ * above in the grid. */
293
+ if (a.region === 'C' && b.region === 'C') {
294
+ if (a.coords.y1 < b.coords.y1 || a.coords.x1 < b.coords.x1) {
295
+ return - 1;
296
+ }else{
297
+ return 1;
298
+ }
299
+ }
300
+
301
+ if (a.area < b.area) {
302
+ return 1;
303
+ }
304
+
305
+ return 1;
306
+ });
307
+ return colliders;
308
+ };
309
+
310
+
311
+ //jQuery adapter
312
+ $.fn.collision = function(collider, options) {
313
+ return new Collision( this, collider, options );
314
+ };
315
+
316
+
317
+ }(jQuery, window, document));
318
+
319
+ ;(function(window, undefined) {
320
+ /* Debounce and throttle functions taken from underscore.js */
321
+ window.debounce = function(func, wait, immediate) {
322
+ var timeout;
323
+ return function() {
324
+ var context = this, args = arguments;
325
+ var later = function() {
326
+ timeout = null;
327
+ if (!immediate) func.apply(context, args);
328
+ };
329
+ if (immediate && !timeout) func.apply(context, args);
330
+ clearTimeout(timeout);
331
+ timeout = setTimeout(later, wait);
332
+ };
333
+ };
334
+
335
+
336
+ window.throttle = function(func, wait) {
337
+ var context, args, timeout, throttling, more, result;
338
+ var whenDone = debounce(
339
+ function(){ more = throttling = false; }, wait);
340
+ return function() {
341
+ context = this; args = arguments;
342
+ var later = function() {
343
+ timeout = null;
344
+ if (more) func.apply(context, args);
345
+ whenDone();
346
+ };
347
+ if (!timeout) timeout = setTimeout(later, wait);
348
+ if (throttling) {
349
+ more = true;
350
+ } else {
351
+ result = func.apply(context, args);
352
+ }
353
+ whenDone();
354
+ throttling = true;
355
+ return result;
356
+ };
357
+ };
358
+
359
+ })(window);
360
+
361
+ ;(function($, window, document, undefined){
362
+
363
+ var defaults = {
364
+ items: '.gs_w',
365
+ distance: 1,
366
+ limit: true,
367
+ offset_left: 0,
368
+ autoscroll: true,
369
+ ignore_dragging: ['INPUT', 'TEXTAREA', 'SELECT', 'BUTTON'],
370
+ handle: null
371
+ // drag: function(e){},
372
+ // start : function(e, ui){},
373
+ // stop : function(e){}
374
+ };
375
+
376
+ var $window = $(window);
377
+ var isTouch = !!('ontouchstart' in window);
378
+ var pointer_events = {
379
+ start: isTouch ? 'touchstart' : 'mousedown.draggable',
380
+ move: isTouch ? 'touchmove' : 'mousemove.draggable',
381
+ end: isTouch ? 'touchend' : 'mouseup.draggable'
382
+ };
383
+
384
+ /**
385
+ * Basic drag implementation for DOM elements inside a container.
386
+ * Provide start/stop/drag callbacks.
387
+ *
388
+ * @class Draggable
389
+ * @param {HTMLElement} el The HTMLelement that contains all the widgets
390
+ * to be dragged.
391
+ * @param {Object} [options] An Object with all options you want to
392
+ * overwrite:
393
+ * @param {HTMLElement|String} [options.items] Define who will
394
+ * be the draggable items. Can be a CSS Selector String or a
395
+ * collection of HTMLElements.
396
+ * @param {Number} [options.distance] Distance in pixels after mousedown
397
+ * the mouse must move before dragging should start.
398
+ * @param {Boolean} [options.limit] Constrains dragging to the width of
399
+ * the container
400
+ * @param {offset_left} [options.offset_left] Offset added to the item
401
+ * that is being dragged.
402
+ * @param {Number} [options.drag] Executes a callback when the mouse is
403
+ * moved during the dragging.
404
+ * @param {Number} [options.start] Executes a callback when the drag
405
+ * starts.
406
+ * @param {Number} [options.stop] Executes a callback when the drag stops.
407
+ * @return {Object} Returns `el`.
408
+ * @constructor
409
+ */
410
+ function Draggable(el, options) {
411
+ this.options = $.extend({}, defaults, options);
412
+ this.$body = $(document.body);
413
+ this.$container = $(el);
414
+ this.$dragitems = $(this.options.items, this.$container);
415
+ this.is_dragging = false;
416
+ this.player_min_left = 0 + this.options.offset_left;
417
+ this.init();
418
+ }
419
+
420
+ var fn = Draggable.prototype;
421
+
422
+ fn.init = function() {
423
+ this.calculate_positions();
424
+ this.$container.css('position', 'relative');
425
+ this.disabled = false;
426
+ this.events();
427
+
428
+ this.on_window_resize = throttle($.proxy(this.calculate_positions, this), 200);
429
+ $(window).bind('resize', this.on_window_resize);
430
+ };
431
+
432
+ fn.events = function() {
433
+ this.proxied_on_select_start = $.proxy(this.on_select_start, this);
434
+ this.$container.on('selectstart', this.proxied_on_select_start);
435
+
436
+ this.proxied_drag_handler = $.proxy(this.drag_handler, this);
437
+ this.$container.on(pointer_events.start, this.options.items, this.proxied_drag_handler);
438
+
439
+ this.proxied_pointer_events_end = $.proxy(function(e) {
440
+ this.is_dragging = false;
441
+ if (this.disabled) { return; }
442
+ this.$body.off(pointer_events.move);
443
+ if (this.drag_start) {
444
+ this.on_dragstop(e);
445
+ }
446
+ }, this);
447
+ this.$body.on(pointer_events.end, this.proxied_pointer_events_end);
448
+ };
449
+
450
+ fn.get_actual_pos = function($el) {
451
+ var pos = $el.position();
452
+ return pos;
453
+ };
454
+
455
+
456
+ fn.get_mouse_pos = function(e) {
457
+ if (isTouch) {
458
+ var oe = e.originalEvent;
459
+ e = oe.touches.length ? oe.touches[0] : oe.changedTouches[0];
460
+ }
461
+
462
+ return {
463
+ left: e.clientX,
464
+ top: e.clientY
465
+ };
466
+ };
467
+
468
+
469
+ fn.get_offset = function(e) {
470
+ e.preventDefault();
471
+ var mouse_actual_pos = this.get_mouse_pos(e);
472
+ var diff_x = Math.round(
473
+ mouse_actual_pos.left - this.mouse_init_pos.left);
474
+ var diff_y = Math.round(mouse_actual_pos.top - this.mouse_init_pos.top);
475
+
476
+ var left = Math.round(this.el_init_offset.left + diff_x - this.baseX);
477
+ var top = Math.round(
478
+ this.el_init_offset.top + diff_y - this.baseY + this.scrollOffset);
479
+
480
+ if (this.options.limit) {
481
+ if (left > this.player_max_left) {
482
+ left = this.player_max_left;
483
+ }else if(left < this.player_min_left) {
484
+ left = this.player_min_left;
485
+ }
486
+ }
487
+
488
+ return {
489
+ left: left,
490
+ top: top,
491
+ mouse_left: mouse_actual_pos.left,
492
+ mouse_top: mouse_actual_pos.top
493
+ };
494
+ };
495
+
496
+
497
+ fn.manage_scroll = function(offset) {
498
+ /* scroll document */
499
+ var nextScrollTop;
500
+ var scrollTop = $window.scrollTop();
501
+ var min_window_y = scrollTop;
502
+ var max_window_y = min_window_y + this.window_height;
503
+
504
+ var mouse_down_zone = max_window_y - 50;
505
+ var mouse_up_zone = min_window_y + 50;
506
+
507
+ var abs_mouse_left = offset.mouse_left;
508
+ var abs_mouse_top = min_window_y + offset.mouse_top;
509
+
510
+ var max_player_y = (this.doc_height - this.window_height +
511
+ this.player_height);
512
+
513
+ if (abs_mouse_top >= mouse_down_zone) {
514
+ nextScrollTop = scrollTop + 30;
515
+ if (nextScrollTop < max_player_y) {
516
+ $window.scrollTop(nextScrollTop);
517
+ this.scrollOffset = this.scrollOffset + 30;
518
+ }
519
+ }
520
+
521
+ if (abs_mouse_top <= mouse_up_zone) {
522
+ nextScrollTop = scrollTop - 30;
523
+ if (nextScrollTop > 0) {
524
+ $window.scrollTop(nextScrollTop);
525
+ this.scrollOffset = this.scrollOffset - 30;
526
+ }
527
+ }
528
+ };
529
+
530
+
531
+ fn.calculate_positions = function(e) {
532
+ this.window_height = $window.height();
533
+ };
534
+
535
+
536
+ fn.drag_handler = function(e) {
537
+ var node = e.target.nodeName;
538
+ if (this.disabled || e.which !== 1 && !isTouch) {
539
+ return;
540
+ }
541
+
542
+ if (this.ignore_drag(e)) {
543
+ return;
544
+ }
545
+
546
+ var self = this;
547
+ var first = true;
548
+ this.$player = $(e.currentTarget);
549
+
550
+ this.el_init_pos = this.get_actual_pos(this.$player);
551
+ this.mouse_init_pos = this.get_mouse_pos(e);
552
+ this.offsetY = this.mouse_init_pos.top - this.el_init_pos.top;
553
+
554
+ this.on_pointer_events_move = function(mme){
555
+ var mouse_actual_pos = self.get_mouse_pos(mme);
556
+ var diff_x = Math.abs(
557
+ mouse_actual_pos.left - self.mouse_init_pos.left);
558
+ var diff_y = Math.abs(
559
+ mouse_actual_pos.top - self.mouse_init_pos.top);
560
+ if (!(diff_x > self.options.distance ||
561
+ diff_y > self.options.distance)
562
+ ) {
563
+ return false;
564
+ }
565
+
566
+ if (first) {
567
+ first = false;
568
+ self.on_dragstart.call(self, mme);
569
+ return false;
570
+ }
571
+
572
+ if (self.is_dragging === true) {
573
+ self.on_dragmove.call(self, mme);
574
+ }
575
+
576
+ return false;
577
+ };
578
+
579
+ this.$body.on(pointer_events.move, this.on_pointer_events_move);
580
+
581
+ return false;
582
+ };
583
+
584
+
585
+ fn.on_dragstart = function(e) {
586
+ e.preventDefault();
587
+ this.drag_start = true;
588
+ this.is_dragging = true;
589
+ var offset = this.$container.offset();
590
+ this.baseX = Math.round(offset.left);
591
+ this.baseY = Math.round(offset.top);
592
+ this.doc_height = $(document).height();
593
+
594
+ if (this.options.helper === 'clone') {
595
+ this.$helper = this.$player.clone()
596
+ .appendTo(this.$container).addClass('helper');
597
+ this.helper = true;
598
+ }else{
599
+ this.helper = false;
600
+ }
601
+ this.scrollOffset = 0;
602
+ this.el_init_offset = this.$player.offset();
603
+ this.player_width = this.$player.width();
604
+ this.player_height = this.$player.height();
605
+ this.player_max_left = (this.$container.width() - this.player_width +
606
+ this.options.offset_left);
607
+
608
+ if (this.options.start) {
609
+ this.options.start.call(this.$player, e, {
610
+ helper: this.helper ? this.$helper : this.$player
611
+ });
612
+ }
613
+ return false;
614
+ };
615
+
616
+
617
+ fn.on_dragmove = function(e) {
618
+ var offset = this.get_offset(e);
619
+
620
+ this.options.autoscroll && this.manage_scroll(offset);
621
+
622
+ (this.helper ? this.$helper : this.$player).css({
623
+ 'position': 'absolute',
624
+ 'left' : offset.left,
625
+ 'top' : offset.top
626
+ });
627
+
628
+ var ui = {
629
+ 'position': {
630
+ 'left': offset.left,
631
+ 'top': offset.top
632
+ }
633
+ };
634
+
635
+ if (this.options.drag) {
636
+ this.options.drag.call(this.$player, e, ui);
637
+ }
638
+ return false;
639
+ };
640
+
641
+
642
+ fn.on_dragstop = function(e) {
643
+ var offset = this.get_offset(e);
644
+ this.drag_start = false;
645
+
646
+ var ui = {
647
+ 'position': {
648
+ 'left': offset.left,
649
+ 'top': offset.top
650
+ }
651
+ };
652
+
653
+ if (this.options.stop) {
654
+ this.options.stop.call(this.$player, e, ui);
655
+ }
656
+
657
+ if (this.helper) {
658
+ this.$helper.remove();
659
+ }
660
+
661
+ return false;
662
+ };
663
+
664
+ fn.on_select_start = function(e) {
665
+ if (this.disabled) { return; }
666
+
667
+ if (this.ignore_drag(e)) {
668
+ return;
669
+ }
670
+
671
+ return false;
672
+ };
673
+
674
+ fn.enable = function() {
675
+ this.disabled = false;
676
+ };
677
+
678
+ fn.disable = function() {
679
+ this.disabled = true;
680
+ };
681
+
682
+
683
+ fn.destroy = function(){
684
+ this.disable();
685
+
686
+ this.$container.off('selectstart', this.proxied_on_select_start);
687
+ this.$container.off(pointer_events.start, this.proxied_drag_handler);
688
+ this.$body.off(pointer_events.end, this.proxied_pointer_events_end);
689
+ this.$body.off(pointer_events.move, this.on_pointer_events_move);
690
+ $(window).unbind('resize', this.on_window_resize);
691
+
692
+ $.removeData(this.$container, 'drag');
693
+ };
694
+
695
+ fn.ignore_drag = function(event) {
696
+ if (this.options.handle) {
697
+ return !$(event.target).is(this.options.handle);
698
+ }
699
+
700
+ return $.inArray(event.target.nodeName, this.options.ignore_dragging) >= 0;
701
+ };
702
+
703
+ //jQuery adapter
704
+ $.fn.drag = function ( options ) {
705
+ return this.each(function () {
706
+ if (!$.data(this, 'drag')) {
707
+ $.data(this, 'drag', new Draggable( this, options ));
708
+ }
709
+ });
710
+ };
711
+
712
+
713
+ }(jQuery, window, document));
714
+
715
+ ;(function($, window, document, undefined) {
716
+
717
+ var defaults = {
718
+ namespace: '',
719
+ widget_selector: 'li',
720
+ widget_margins: [10, 10],
721
+ widget_base_dimensions: [400, 225],
722
+ extra_rows: 0,
723
+ extra_cols: 0,
724
+ min_cols: 1,
725
+ min_rows: 15,
726
+ max_size_x: 6,
727
+ autogenerate_stylesheet: true,
728
+ avoid_overlapped_widgets: true,
729
+ serialize_params: function($w, wgd) {
730
+ return {
731
+ col: wgd.col,
732
+ row: wgd.row,
733
+ size_x: wgd.size_x,
734
+ size_y: wgd.size_y
735
+ };
736
+ },
737
+ collision: {},
738
+ draggable: {
739
+ distance: 4
740
+ }
741
+ };
742
+
743
+
744
+ /**
745
+ * @class Gridster
746
+ * @uses Draggable
747
+ * @uses Collision
748
+ * @param {HTMLElement} el The HTMLelement that contains all the widgets.
749
+ * @param {Object} [options] An Object with all options you want to
750
+ * overwrite:
751
+ * @param {HTMLElement|String} [options.widget_selector] Define who will
752
+ * be the draggable widgets. Can be a CSS Selector String or a
753
+ * collection of HTMLElements
754
+ * @param {Array} [options.widget_margins] Margin between widgets.
755
+ * The first index for the horizontal margin (left, right) and
756
+ * the second for the vertical margin (top, bottom).
757
+ * @param {Array} [options.widget_base_dimensions] Base widget dimensions
758
+ * in pixels. The first index for the width and the second for the
759
+ * height.
760
+ * @param {Number} [options.extra_cols] Add more columns in addition to
761
+ * those that have been calculated.
762
+ * @param {Number} [options.extra_rows] Add more rows in addition to
763
+ * those that have been calculated.
764
+ * @param {Number} [options.min_cols] The minimum required columns.
765
+ * @param {Number} [options.min_rows] The minimum required rows.
766
+ * @param {Number} [options.max_size_x] The maximum number of columns
767
+ * that a widget can span.
768
+ * @param {Boolean} [options.autogenerate_stylesheet] If true, all the
769
+ * CSS required to position all widgets in their respective columns
770
+ * and rows will be generated automatically and injected to the
771
+ * `<head>` of the document. You can set this to false, and write
772
+ * your own CSS targeting rows and cols via data-attributes like so:
773
+ * `[data-col="1"] { left: 10px; }`
774
+ * @param {Boolean} [options.avoid_overlapped_widgets] Avoid that widgets loaded
775
+ * from the DOM can be overlapped. It is helpful if the positions were
776
+ * bad stored in the database or if there was any conflict.
777
+ * @param {Function} [options.serialize_params] Return the data you want
778
+ * for each widget in the serialization. Two arguments are passed:
779
+ * `$w`: the jQuery wrapped HTMLElement, and `wgd`: the grid
780
+ * coords object (`col`, `row`, `size_x`, `size_y`).
781
+ * @param {Object} [options.collision] An Object with all options for
782
+ * Collision class you want to overwrite. See Collision docs for
783
+ * more info.
784
+ * @param {Object} [options.draggable] An Object with all options for
785
+ * Draggable class you want to overwrite. See Draggable docs for more
786
+ * info.
787
+ *
788
+ * @constructor
789
+ */
790
+ function Gridster(el, options) {
791
+ this.options = $.extend(true, defaults, options);
792
+ this.$el = $(el);
793
+ this.$wrapper = this.$el.parent();
794
+ this.$widgets = this.$el.children(this.options.widget_selector).addClass('gs_w');
795
+ this.widgets = [];
796
+ this.$changed = $([]);
797
+ this.wrapper_width = this.$wrapper.width();
798
+ this.min_widget_width = (this.options.widget_margins[0] * 2) +
799
+ this.options.widget_base_dimensions[0];
800
+ this.min_widget_height = (this.options.widget_margins[1] * 2) +
801
+ this.options.widget_base_dimensions[1];
802
+ this.init();
803
+ }
804
+
805
+ Gridster.generated_stylesheets = [];
806
+
807
+ var fn = Gridster.prototype;
808
+
809
+ fn.init = function() {
810
+ this.generate_grid_and_stylesheet();
811
+ this.get_widgets_from_DOM();
812
+ this.set_dom_grid_height();
813
+ this.$wrapper.addClass('ready');
814
+ this.draggable();
815
+
816
+ this.on_window_resize = throttle($.proxy(this.recalculate_faux_grid, this), 200);
817
+
818
+ $(window).bind('resize', this.on_window_resize);
819
+ };
820
+
821
+
822
+ /**
823
+ * Disables dragging.
824
+ *
825
+ * @method disable
826
+ * @return {Class} Returns the instance of the Gridster Class.
827
+ */
828
+ fn.disable = function() {
829
+ this.$wrapper.find('.player-revert').removeClass('player-revert');
830
+ this.drag_api.disable();
831
+ return this;
832
+ };
833
+
834
+
835
+ /**
836
+ * Enables dragging.
837
+ *
838
+ * @method enable
839
+ * @return {Class} Returns the instance of the Gridster Class.
840
+ */
841
+ fn.enable = function() {
842
+ this.drag_api.enable();
843
+ return this;
844
+ };
845
+
846
+
847
+ /**
848
+ * Add a new widget to the grid.
849
+ *
850
+ * @method add_widget
851
+ * @param {String|HTMLElement} html The string representing the HTML of the widget
852
+ * or the HTMLElement.
853
+ * @param {Number} [size_x] The nº of rows the widget occupies horizontally.
854
+ * @param {Number} [size_y] The nº of columns the widget occupies vertically.
855
+ * @param {Number} [col] The column the widget should start in.
856
+ * @param {Number} [row] The row the widget should start in.
857
+ * @return {HTMLElement} Returns the jQuery wrapped HTMLElement representing.
858
+ * the widget that was just created.
859
+ */
860
+ fn.add_widget = function(html, size_x, size_y, col, row) {
861
+ var pos;
862
+ size_x || (size_x = 1);
863
+ size_y || (size_y = 1);
864
+
865
+ if (!col & !row) {
866
+ pos = this.next_position(size_x, size_y);
867
+ }else{
868
+ pos = {
869
+ col: col,
870
+ row: row
871
+ };
872
+
873
+ this.empty_cells(col, row, size_x, size_y);
874
+ }
875
+
876
+ var $w = $(html).attr({
877
+ 'data-col': pos.col,
878
+ 'data-row': pos.row,
879
+ 'data-sizex' : size_x,
880
+ 'data-sizey' : size_y
881
+ }).addClass('gs_w').appendTo(this.$el).hide();
882
+
883
+ this.$widgets = this.$widgets.add($w);
884
+
885
+ this.register_widget($w);
886
+
887
+ this.add_faux_rows(pos.size_y);
888
+ //this.add_faux_cols(pos.size_x);
889
+
890
+ this.set_dom_grid_height();
891
+
892
+ return $w.fadeIn();
893
+ };
894
+
895
+
896
+
897
+ /**
898
+ * Change the size of a widget.
899
+ *
900
+ * @method resize_widget
901
+ * @param {HTMLElement} $widget The jQuery wrapped HTMLElement
902
+ * representing the widget.
903
+ * @param {Number} size_x The number of columns that will occupy the widget.
904
+ * @param {Number} size_y The number of rows that will occupy the widget.
905
+ * @return {HTMLElement} Returns $widget.
906
+ */
907
+ fn.resize_widget = function($widget, size_x, size_y) {
908
+ var wgd = $widget.coords().grid;
909
+ size_x || (size_x = wgd.size_x);
910
+ size_y || (size_y = wgd.size_y);
911
+
912
+ if (size_x > this.cols) {
913
+ size_x = this.cols;
914
+ }
915
+
916
+ var old_cells_occupied = this.get_cells_occupied(wgd);
917
+ var old_size_x = wgd.size_x;
918
+ var old_size_y = wgd.size_y;
919
+ var old_col = wgd.col;
920
+ var new_col = old_col;
921
+ var wider = size_x > old_size_x;
922
+ var taller = size_y > old_size_y;
923
+
924
+ if (old_col + size_x - 1 > this.cols) {
925
+ var diff = old_col + (size_x - 1) - this.cols;
926
+ var c = old_col - diff;
927
+ new_col = Math.max(1, c);
928
+ }
929
+
930
+ var new_grid_data = {
931
+ col: new_col,
932
+ row: wgd.row,
933
+ size_x: size_x,
934
+ size_y: size_y
935
+ };
936
+
937
+ var new_cells_occupied = this.get_cells_occupied(new_grid_data);
938
+
939
+ var empty_cols = [];
940
+ $.each(old_cells_occupied.cols, function(i, col) {
941
+ if ($.inArray(col, new_cells_occupied.cols) === -1) {
942
+ empty_cols.push(col);
943
+ }
944
+ });
945
+
946
+ var occupied_cols = [];
947
+ $.each(new_cells_occupied.cols, function(i, col) {
948
+ if ($.inArray(col, old_cells_occupied.cols) === -1) {
949
+ occupied_cols.push(col);
950
+ }
951
+ });
952
+
953
+ var empty_rows = [];
954
+ $.each(old_cells_occupied.rows, function(i, row) {
955
+ if ($.inArray(row, new_cells_occupied.rows) === -1) {
956
+ empty_rows.push(row);
957
+ }
958
+ });
959
+
960
+ var occupied_rows = [];
961
+ $.each(new_cells_occupied.rows, function(i, row) {
962
+ if ($.inArray(row, old_cells_occupied.rows) === -1) {
963
+ occupied_rows.push(row);
964
+ }
965
+ });
966
+
967
+ this.remove_from_gridmap(wgd);
968
+
969
+ if (occupied_cols.length) {
970
+ var cols_to_empty = [
971
+ new_col, wgd.row, size_x, Math.min(old_size_y, size_y), $widget
972
+ ];
973
+ this.empty_cells.apply(this, cols_to_empty);
974
+ }
975
+
976
+ if (occupied_rows.length) {
977
+ var rows_to_empty = [new_col, wgd.row, size_x, size_y, $widget];
978
+ this.empty_cells.apply(this, rows_to_empty);
979
+ }
980
+
981
+ wgd.col = new_col;
982
+ wgd.size_x = size_x;
983
+ wgd.size_y = size_y;
984
+ this.add_to_gridmap(new_grid_data, $widget);
985
+
986
+ //update coords instance attributes
987
+ $widget.data('coords').update({
988
+ width: (size_x * this.options.widget_base_dimensions[0] +
989
+ ((size_x - 1) * this.options.widget_margins[0]) * 2),
990
+ height: (size_y * this.options.widget_base_dimensions[1] +
991
+ ((size_y - 1) * this.options.widget_margins[1]) * 2)
992
+ });
993
+
994
+ if (size_y > old_size_y) {
995
+ this.add_faux_rows(size_y - old_size_y);
996
+ }
997
+
998
+ if (size_x > old_size_x) {
999
+ this.add_faux_cols(size_x - old_size_x);
1000
+ }
1001
+
1002
+ $widget.attr({
1003
+ 'data-col': new_col,
1004
+ 'data-sizex': size_x,
1005
+ 'data-sizey': size_y
1006
+ });
1007
+
1008
+ if (empty_cols.length) {
1009
+ var cols_to_remove_holes = [
1010
+ empty_cols[0], wgd.row,
1011
+ empty_cols.length,
1012
+ Math.min(old_size_y, size_y),
1013
+ $widget
1014
+ ];
1015
+
1016
+ this.remove_empty_cells.apply(this, cols_to_remove_holes);
1017
+ }
1018
+
1019
+ if (empty_rows.length) {
1020
+ var rows_to_remove_holes = [
1021
+ new_col, wgd.row, size_x, size_y, $widget
1022
+ ];
1023
+ this.remove_empty_cells.apply(this, rows_to_remove_holes);
1024
+ }
1025
+
1026
+ return $widget;
1027
+ };
1028
+
1029
+ /**
1030
+ * Move down widgets in cells represented by the arguments col, row, size_x,
1031
+ * size_y
1032
+ *
1033
+ * @method empty_cells
1034
+ * @param {Number} col The column where the group of cells begin.
1035
+ * @param {Number} row The row where the group of cells begin.
1036
+ * @param {Number} size_x The number of columns that the group of cells
1037
+ * occupy.
1038
+ * @param {Number} size_y The number of rows that the group of cells
1039
+ * occupy.
1040
+ * @param {HTMLElement} $exclude Exclude widgets from being moved.
1041
+ * @return {Class} Returns the instance of the Gridster Class.
1042
+ */
1043
+ fn.empty_cells = function(col, row, size_x, size_y, $exclude) {
1044
+ var $nexts = this.widgets_below({
1045
+ col: col,
1046
+ row: row - size_y,
1047
+ size_x: size_x,
1048
+ size_y: size_y
1049
+ });
1050
+
1051
+ $nexts.not($exclude).each($.proxy(function(i, w) {
1052
+ var wgd = $(w).coords().grid;
1053
+ if (!(wgd.row <= (row + size_y - 1))) { return; }
1054
+ var diff = (row + size_y) - wgd.row;
1055
+ this.move_widget_down($(w), diff);
1056
+ }, this));
1057
+
1058
+ this.set_dom_grid_height();
1059
+
1060
+ return this;
1061
+ };
1062
+
1063
+
1064
+ /**
1065
+ * Move up widgets below cells represented by the arguments col, row, size_x,
1066
+ * size_y.
1067
+ *
1068
+ * @method remove_empty_cells
1069
+ * @param {Number} col The column where the group of cells begin.
1070
+ * @param {Number} row The row where the group of cells begin.
1071
+ * @param {Number} size_x The number of columns that the group of cells
1072
+ * occupy.
1073
+ * @param {Number} size_y The number of rows that the group of cells
1074
+ * occupy.
1075
+ * @param {HTMLElement} exclude Exclude widgets from being moved.
1076
+ * @return {Class} Returns the instance of the Gridster Class.
1077
+ */
1078
+ fn.remove_empty_cells = function(col, row, size_x, size_y, exclude) {
1079
+ var $nexts = this.widgets_below({
1080
+ col: col,
1081
+ row: row,
1082
+ size_x: size_x,
1083
+ size_y: size_y
1084
+ });
1085
+
1086
+ $nexts.not(exclude).each($.proxy(function(i, widget) {
1087
+ this.move_widget_up( $(widget), size_y );
1088
+ }, this));
1089
+
1090
+ this.set_dom_grid_height();
1091
+
1092
+ return this;
1093
+ };
1094
+
1095
+
1096
+ /**
1097
+ * Get the most left column below to add a new widget.
1098
+ *
1099
+ * @method next_position
1100
+ * @param {Number} size_x The nº of rows the widget occupies horizontally.
1101
+ * @param {Number} size_y The nº of columns the widget occupies vertically.
1102
+ * @return {Object} Returns a grid coords object representing the future
1103
+ * widget coords.
1104
+ */
1105
+ fn.next_position = function(size_x, size_y) {
1106
+ size_x || (size_x = 1);
1107
+ size_y || (size_y = 1);
1108
+ var ga = this.gridmap;
1109
+ var cols_l = ga.length;
1110
+ var valid_pos = [];
1111
+ var rows_l;
1112
+
1113
+ for (var c = 1; c < cols_l; c++) {
1114
+ rows_l = ga[c].length;
1115
+ for (var r = 1; r <= rows_l; r++) {
1116
+ var can_move_to = this.can_move_to({
1117
+ size_x: size_x,
1118
+ size_y: size_y
1119
+ }, c, r);
1120
+
1121
+ if (can_move_to) {
1122
+ valid_pos.push({
1123
+ col: c,
1124
+ row: r,
1125
+ size_y: size_y,
1126
+ size_x: size_x
1127
+ });
1128
+ }
1129
+ }
1130
+ }
1131
+
1132
+ if (valid_pos.length) {
1133
+ return this.sort_by_row_and_col_asc(valid_pos)[0];
1134
+ }
1135
+ return false;
1136
+ };
1137
+
1138
+
1139
+ /**
1140
+ * Remove a widget from the grid.
1141
+ *
1142
+ * @method remove_widget
1143
+ * @param {HTMLElement} el The jQuery wrapped HTMLElement you want to remove.
1144
+ * @param {Boolean|Function} silent If true, widgets below the removed one
1145
+ * will not move up. If a Function is passed it will be used as callback.
1146
+ * @param {Function} callback Function executed when the widget is removed.
1147
+ * @return {Class} Returns the instance of the Gridster Class.
1148
+ */
1149
+ fn.remove_widget = function(el, silent, callback) {
1150
+ var $el = el instanceof jQuery ? el : $(el);
1151
+ var wgd = $el.coords().grid;
1152
+
1153
+ // if silent is a function assume it's a callback
1154
+ if ($.isFunction(silent)) {
1155
+ callback = silent;
1156
+ silent = false;
1157
+ }
1158
+
1159
+ this.cells_occupied_by_placeholder = {};
1160
+ this.$widgets = this.$widgets.not($el);
1161
+
1162
+ var $nexts = this.widgets_below($el);
1163
+
1164
+ this.remove_from_gridmap(wgd);
1165
+
1166
+ $el.fadeOut($.proxy(function() {
1167
+ $el.remove();
1168
+
1169
+ if (!silent) {
1170
+ $nexts.each($.proxy(function(i, widget) {
1171
+ this.move_widget_up( $(widget), wgd.size_y );
1172
+ }, this));
1173
+ }
1174
+
1175
+ this.set_dom_grid_height();
1176
+
1177
+ if (callback) {
1178
+ callback.call(this, el);
1179
+ }
1180
+ }, this));
1181
+ };
1182
+
1183
+
1184
+ /**
1185
+ * Remove all widgets from the grid.
1186
+ *
1187
+ * @method remove_all_widgets
1188
+ * @param {Function} callback Function executed for each widget removed.
1189
+ * @return {Class} Returns the instance of the Gridster Class.
1190
+ */
1191
+ fn.remove_all_widgets = function(callback) {
1192
+ this.$widgets.each($.proxy(function(i, el){
1193
+ this.remove_widget(el, true, callback);
1194
+ }, this));
1195
+
1196
+ return this;
1197
+ };
1198
+
1199
+
1200
+ /**
1201
+ * Returns a serialized array of the widgets in the grid.
1202
+ *
1203
+ * @method serialize
1204
+ * @param {HTMLElement} [$widgets] The collection of jQuery wrapped
1205
+ * HTMLElements you want to serialize. If no argument is passed all widgets
1206
+ * will be serialized.
1207
+ * @return {Array} Returns an Array of Objects with the data specified in
1208
+ * the serialize_params option.
1209
+ */
1210
+ fn.serialize = function($widgets) {
1211
+ $widgets || ($widgets = this.$widgets);
1212
+ var result = [];
1213
+ $widgets.each($.proxy(function(i, widget) {
1214
+ result.push(this.options.serialize_params(
1215
+ $(widget), $(widget).coords().grid ) );
1216
+ }, this));
1217
+
1218
+ return result;
1219
+ };
1220
+
1221
+
1222
+ /**
1223
+ * Returns a serialized array of the widgets that have changed their
1224
+ * position.
1225
+ *
1226
+ * @method serialize_changed
1227
+ * @return {Array} Returns an Array of Objects with the data specified in
1228
+ * the serialize_params option.
1229
+ */
1230
+ fn.serialize_changed = function() {
1231
+ return this.serialize(this.$changed);
1232
+ };
1233
+
1234
+
1235
+ /**
1236
+ * Creates the grid coords object representing the widget a add it to the
1237
+ * mapped array of positions.
1238
+ *
1239
+ * @method register_widget
1240
+ * @return {Array} Returns the instance of the Gridster class.
1241
+ */
1242
+ fn.register_widget = function($el) {
1243
+
1244
+ var wgd = {
1245
+ 'col': parseInt($el.attr('data-col'), 10),
1246
+ 'row': parseInt($el.attr('data-row'), 10),
1247
+ 'size_x': parseInt($el.attr('data-sizex'), 10),
1248
+ 'size_y': parseInt($el.attr('data-sizey'), 10),
1249
+ 'el': $el
1250
+ };
1251
+
1252
+ if (this.options.avoid_overlapped_widgets &&
1253
+ !this.can_move_to(
1254
+ {size_x: wgd.size_x, size_y: wgd.size_y}, wgd.col, wgd.row)
1255
+ ) {
1256
+ wgd = this.next_position(wgd.size_x, wgd.size_y);
1257
+ wgd.el = $el;
1258
+ $el.attr({
1259
+ 'data-col': wgd.col,
1260
+ 'data-row': wgd.row,
1261
+ 'data-sizex': wgd.size_x,
1262
+ 'data-sizey': wgd.size_y
1263
+ });
1264
+ }
1265
+
1266
+ // attach Coord object to player data-coord attribute
1267
+ $el.data('coords', $el.coords());
1268
+
1269
+ // Extend Coord object with grid position info
1270
+ $el.data('coords').grid = wgd;
1271
+
1272
+ this.add_to_gridmap(wgd, $el);
1273
+
1274
+ return this;
1275
+ };
1276
+
1277
+
1278
+ /**
1279
+ * Update in the mapped array of positions the value of cells represented by
1280
+ * the grid coords object passed in the `grid_data` param.
1281
+ *
1282
+ * @param {Object} grid_data The grid coords object representing the cells
1283
+ * to update in the mapped array.
1284
+ * @param {HTMLElement|Boolean} value Pass `false` or the jQuery wrapped
1285
+ * HTMLElement, depends if you want to delete an existing position or add
1286
+ * a new one.
1287
+ * @method update_widget_position
1288
+ * @return {Class} Returns the instance of the Gridster Class.
1289
+ */
1290
+ fn.update_widget_position = function(grid_data, value) {
1291
+ this.for_each_cell_occupied(grid_data, function(col, row) {
1292
+ if (!this.gridmap[col]) { return this; }
1293
+ this.gridmap[col][row] = value;
1294
+ });
1295
+ return this;
1296
+ };
1297
+
1298
+
1299
+ /**
1300
+ * Remove a widget from the mapped array of positions.
1301
+ *
1302
+ * @method remove_from_gridmap
1303
+ * @param {Object} grid_data The grid coords object representing the cells
1304
+ * to update in the mapped array.
1305
+ * @return {Class} Returns the instance of the Gridster Class.
1306
+ */
1307
+ fn.remove_from_gridmap = function(grid_data) {
1308
+ return this.update_widget_position(grid_data, false);
1309
+ };
1310
+
1311
+
1312
+ /**
1313
+ * Add a widget to the mapped array of positions.
1314
+ *
1315
+ * @method add_to_gridmap
1316
+ * @param {Object} grid_data The grid coords object representing the cells
1317
+ * to update in the mapped array.
1318
+ * @param {HTMLElement|Boolean} value The value to set in the specified
1319
+ * position .
1320
+ * @return {Class} Returns the instance of the Gridster Class.
1321
+ */
1322
+ fn.add_to_gridmap = function(grid_data, value) {
1323
+ this.update_widget_position(grid_data, value || grid_data.el);
1324
+
1325
+ if (grid_data.el) {
1326
+ var $widgets = this.widgets_below(grid_data.el);
1327
+ $widgets.each($.proxy(function(i, widget) {
1328
+ this.move_widget_up( $(widget));
1329
+ }, this));
1330
+ }
1331
+ };
1332
+
1333
+
1334
+ /**
1335
+ * Make widgets draggable.
1336
+ *
1337
+ * @uses Draggable
1338
+ * @method draggable
1339
+ * @return {Class} Returns the instance of the Gridster Class.
1340
+ */
1341
+ fn.draggable = function() {
1342
+ var self = this;
1343
+ var draggable_options = $.extend(true, {}, this.options.draggable, {
1344
+ offset_left: this.options.widget_margins[0],
1345
+ start: function(event, ui) {
1346
+ self.$widgets.filter('.player-revert')
1347
+ .removeClass('player-revert');
1348
+
1349
+ self.$player = $(this);
1350
+ self.$helper = self.options.draggable.helper === 'clone' ?
1351
+ $(ui.helper) : self.$player;
1352
+ self.helper = !self.$helper.is(self.$player);
1353
+
1354
+ self.on_start_drag.call(self, event, ui);
1355
+ self.$el.trigger('gridster:dragstart');
1356
+ },
1357
+ stop: function(event, ui) {
1358
+ self.on_stop_drag.call(self, event, ui);
1359
+ self.$el.trigger('gridster:dragstop');
1360
+ },
1361
+ drag: throttle(function(event, ui) {
1362
+ self.on_drag.call(self, event, ui);
1363
+ self.$el.trigger('gridster:drag');
1364
+ }, 60)
1365
+ });
1366
+
1367
+ this.drag_api = this.$el.drag(draggable_options).data('drag');
1368
+ return this;
1369
+ };
1370
+
1371
+
1372
+ /**
1373
+ * This function is executed when the player begins to be dragged.
1374
+ *
1375
+ * @method on_start_drag
1376
+ * @param {Event} event The original browser event
1377
+ * @param {Object} ui A prepared ui object.
1378
+ */
1379
+ fn.on_start_drag = function(event, ui) {
1380
+
1381
+ this.$helper.add(this.$player).add(this.$wrapper).addClass('dragging');
1382
+
1383
+ this.$player.addClass('player');
1384
+ this.player_grid_data = this.$player.coords().grid;
1385
+ this.placeholder_grid_data = $.extend({}, this.player_grid_data);
1386
+
1387
+ //set new grid height along the dragging period
1388
+ this.$el.css('height', this.$el.height() +
1389
+ (this.player_grid_data.size_y * this.min_widget_height));
1390
+
1391
+ var colliders = this.faux_grid;
1392
+ var coords = this.$player.data('coords').coords;
1393
+
1394
+ this.cells_occupied_by_player = this.get_cells_occupied(
1395
+ this.player_grid_data);
1396
+ this.cells_occupied_by_placeholder = this.get_cells_occupied(
1397
+ this.placeholder_grid_data);
1398
+
1399
+ this.last_cols = [];
1400
+ this.last_rows = [];
1401
+
1402
+
1403
+ // see jquery.collision.js
1404
+ this.collision_api = this.$helper.collision(
1405
+ colliders, this.options.collision);
1406
+
1407
+ this.$preview_holder = $('<li />', {
1408
+ 'class': 'preview-holder',
1409
+ 'data-row': this.$player.attr('data-row'),
1410
+ 'data-col': this.$player.attr('data-col'),
1411
+ css: {
1412
+ width: coords.width,
1413
+ height: coords.height
1414
+ }
1415
+ }).appendTo(this.$el);
1416
+
1417
+ if (this.options.draggable.start) {
1418
+ this.options.draggable.start.call(this, event, ui);
1419
+ }
1420
+ };
1421
+
1422
+
1423
+ /**
1424
+ * This function is executed when the player is being dragged.
1425
+ *
1426
+ * @method on_drag
1427
+ * @param {Event} event The original browser event
1428
+ * @param {Object} ui A prepared ui object.
1429
+ */
1430
+ fn.on_drag = function(event, ui) {
1431
+ //break if dragstop has been fired
1432
+ if (this.$player === null) {
1433
+ return false;
1434
+ }
1435
+
1436
+ var abs_offset = {
1437
+ left: ui.position.left + this.baseX,
1438
+ top: ui.position.top + this.baseY
1439
+ };
1440
+
1441
+ this.colliders_data = this.collision_api.get_closest_colliders(
1442
+ abs_offset);
1443
+
1444
+ this.on_overlapped_column_change(
1445
+ this.on_start_overlapping_column,
1446
+ this.on_stop_overlapping_column
1447
+ );
1448
+
1449
+ this.on_overlapped_row_change(
1450
+ this.on_start_overlapping_row,
1451
+ this.on_stop_overlapping_row
1452
+ );
1453
+
1454
+ if (this.helper && this.$player) {
1455
+ this.$player.css({
1456
+ 'left': ui.position.left,
1457
+ 'top': ui.position.top
1458
+ });
1459
+ }
1460
+
1461
+ if (this.options.draggable.drag) {
1462
+ this.options.draggable.drag.call(this, event, ui);
1463
+ }
1464
+ };
1465
+
1466
+ /**
1467
+ * This function is executed when the player stops being dragged.
1468
+ *
1469
+ * @method on_stop_drag
1470
+ * @param {Event} event The original browser event
1471
+ * @param {Object} ui A prepared ui object.
1472
+ */
1473
+ fn.on_stop_drag = function(event, ui) {
1474
+ this.$helper.add(this.$player).add(this.$wrapper)
1475
+ .removeClass('dragging');
1476
+
1477
+ ui.position.left = ui.position.left + this.baseX;
1478
+ ui.position.top = ui.position.top + this.baseY;
1479
+ this.colliders_data = this.collision_api.get_closest_colliders(ui.position);
1480
+
1481
+ this.on_overlapped_column_change(
1482
+ this.on_start_overlapping_column,
1483
+ this.on_stop_overlapping_column
1484
+ );
1485
+
1486
+ this.on_overlapped_row_change(
1487
+ this.on_start_overlapping_row,
1488
+ this.on_stop_overlapping_row
1489
+ );
1490
+
1491
+ this.$player.addClass('player-revert').removeClass('player')
1492
+ .attr({
1493
+ 'data-col': this.placeholder_grid_data.col,
1494
+ 'data-row': this.placeholder_grid_data.row
1495
+ }).css({
1496
+ 'left': '',
1497
+ 'top': ''
1498
+ });
1499
+
1500
+ this.$changed = this.$changed.add(this.$player);
1501
+
1502
+ this.cells_occupied_by_player = this.get_cells_occupied(
1503
+ this.placeholder_grid_data);
1504
+ this.set_cells_player_occupies(
1505
+ this.placeholder_grid_data.col, this.placeholder_grid_data.row);
1506
+
1507
+ this.$player.coords().grid.row = this.placeholder_grid_data.row;
1508
+ this.$player.coords().grid.col = this.placeholder_grid_data.col;
1509
+
1510
+ if (this.options.draggable.stop) {
1511
+ this.options.draggable.stop.call(this, event, ui);
1512
+ }
1513
+
1514
+ this.$preview_holder.remove();
1515
+
1516
+ this.$player = null;
1517
+ this.$helper = null;
1518
+ this.placeholder_grid_data = {};
1519
+ this.player_grid_data = {};
1520
+ this.cells_occupied_by_placeholder = {};
1521
+ this.cells_occupied_by_player = {};
1522
+
1523
+ this.set_dom_grid_height();
1524
+ };
1525
+
1526
+
1527
+ /**
1528
+ * Executes the callbacks passed as arguments when a column begins to be
1529
+ * overlapped or stops being overlapped.
1530
+ *
1531
+ * @param {Function} start_callback Function executed when a new column
1532
+ * begins to be overlapped. The column is passed as first argument.
1533
+ * @param {Function} stop_callback Function executed when a column stops
1534
+ * being overlapped. The column is passed as first argument.
1535
+ * @method on_overlapped_column_change
1536
+ * @return {Class} Returns the instance of the Gridster Class.
1537
+ */
1538
+ fn.on_overlapped_column_change = function(start_callback, stop_callback) {
1539
+ if (!this.colliders_data.length) {
1540
+ return this;
1541
+ }
1542
+ var cols = this.get_targeted_columns(
1543
+ this.colliders_data[0].el.data.col);
1544
+
1545
+ var last_n_cols = this.last_cols.length;
1546
+ var n_cols = cols.length;
1547
+ var i;
1548
+
1549
+ for (i = 0; i < n_cols; i++) {
1550
+ if ($.inArray(cols[i], this.last_cols) === -1) {
1551
+ (start_callback || $.noop).call(this, cols[i]);
1552
+ }
1553
+ }
1554
+
1555
+ for (i = 0; i< last_n_cols; i++) {
1556
+ if ($.inArray(this.last_cols[i], cols) === -1) {
1557
+ (stop_callback || $.noop).call(this, this.last_cols[i]);
1558
+ }
1559
+ }
1560
+
1561
+ this.last_cols = cols;
1562
+
1563
+ return this;
1564
+ };
1565
+
1566
+
1567
+ /**
1568
+ * Executes the callbacks passed as arguments when a row starts to be
1569
+ * overlapped or stops being overlapped.
1570
+ *
1571
+ * @param {Function} start_callback Function executed when a new row begins
1572
+ * to be overlapped. The row is passed as first argument.
1573
+ * @param {Function} end_callback Function executed when a row stops being
1574
+ * overlapped. The row is passed as first argument.
1575
+ * @method on_overlapped_row_change
1576
+ * @return {Class} Returns the instance of the Gridster Class.
1577
+ */
1578
+ fn.on_overlapped_row_change = function(start_callback, end_callback) {
1579
+ if (!this.colliders_data.length) {
1580
+ return this;
1581
+ }
1582
+ var rows = this.get_targeted_rows(this.colliders_data[0].el.data.row);
1583
+ var last_n_rows = this.last_rows.length;
1584
+ var n_rows = rows.length;
1585
+ var i;
1586
+
1587
+ for (i = 0; i < n_rows; i++) {
1588
+ if ($.inArray(rows[i], this.last_rows) === -1) {
1589
+ (start_callback || $.noop).call(this, rows[i]);
1590
+ }
1591
+ }
1592
+
1593
+ for (i = 0; i < last_n_rows; i++) {
1594
+ if ($.inArray(this.last_rows[i], rows) === -1) {
1595
+ (end_callback || $.noop).call(this, this.last_rows[i]);
1596
+ }
1597
+ }
1598
+
1599
+ this.last_rows = rows;
1600
+ };
1601
+
1602
+
1603
+ /**
1604
+ * Sets the current position of the player
1605
+ *
1606
+ * @param {Number} col
1607
+ * @param {Number} row
1608
+ * @param {Boolean} no_player
1609
+ * @method set_player
1610
+ * @return {object}
1611
+ */
1612
+ fn.set_player = function(col, row, no_player) {
1613
+ var self = this;
1614
+ if (!no_player) {
1615
+ this.empty_cells_player_occupies();
1616
+ }
1617
+ var cell = !no_player ? self.colliders_data[0].el.data : {col: col};
1618
+ var to_col = cell.col;
1619
+ var to_row = row || cell.row;
1620
+
1621
+ this.player_grid_data = {
1622
+ col: to_col,
1623
+ row: to_row,
1624
+ size_y : this.player_grid_data.size_y,
1625
+ size_x : this.player_grid_data.size_x
1626
+ };
1627
+
1628
+ this.cells_occupied_by_player = this.get_cells_occupied(
1629
+ this.player_grid_data);
1630
+
1631
+ var $overlapped_widgets = this.get_widgets_overlapped(
1632
+ this.player_grid_data);
1633
+
1634
+ var constraints = this.widgets_constraints($overlapped_widgets);
1635
+
1636
+ this.manage_movements(constraints.can_go_up, to_col, to_row);
1637
+ this.manage_movements(constraints.can_not_go_up, to_col, to_row);
1638
+
1639
+ /* if there is not widgets overlapping in the new player position,
1640
+ * update the new placeholder position. */
1641
+ if (!$overlapped_widgets.length) {
1642
+ var pp = this.can_go_player_up(this.player_grid_data);
1643
+ if (pp !== false) {
1644
+ to_row = pp;
1645
+ }
1646
+ this.set_placeholder(to_col, to_row);
1647
+ }
1648
+
1649
+ return {
1650
+ col: to_col,
1651
+ row: to_row
1652
+ };
1653
+ };
1654
+
1655
+
1656
+ /**
1657
+ * See which of the widgets in the $widgets param collection can go to
1658
+ * a upper row and which not.
1659
+ *
1660
+ * @method widgets_contraints
1661
+ * @param {jQuery} $widgets A jQuery wrapped collection of
1662
+ * HTMLElements.
1663
+ * @return {object} Returns a literal Object with two keys: `can_go_up` &
1664
+ * `can_not_go_up`. Each contains a set of HTMLElements.
1665
+ */
1666
+ fn.widgets_constraints = function($widgets) {
1667
+ var $widgets_can_go_up = $([]);
1668
+ var $widgets_can_not_go_up;
1669
+ var wgd_can_go_up = [];
1670
+ var wgd_can_not_go_up = [];
1671
+
1672
+ $widgets.each($.proxy(function(i, w) {
1673
+ var $w = $(w);
1674
+ var wgd = $w.coords().grid;
1675
+ if (this.can_go_widget_up(wgd)) {
1676
+ $widgets_can_go_up = $widgets_can_go_up.add($w);
1677
+ wgd_can_go_up.push(wgd);
1678
+ }else{
1679
+ wgd_can_not_go_up.push(wgd);
1680
+ }
1681
+ }, this));
1682
+
1683
+ $widgets_can_not_go_up = $widgets.not($widgets_can_go_up);
1684
+
1685
+ return {
1686
+ can_go_up: this.sort_by_row_asc(wgd_can_go_up),
1687
+ can_not_go_up: this.sort_by_row_desc(wgd_can_not_go_up)
1688
+ };
1689
+ };
1690
+
1691
+
1692
+ /**
1693
+ * Sorts an Array of grid coords objects (representing the grid coords of
1694
+ * each widget) in ascending way.
1695
+ *
1696
+ * @method sort_by_row_asc
1697
+ * @param {Array} widgets Array of grid coords objects
1698
+ * @return {Array} Returns the array sorted.
1699
+ */
1700
+ fn.sort_by_row_asc = function(widgets) {
1701
+ widgets = widgets.sort(function(a, b) {
1702
+ if (!a.row) {
1703
+ a = $(a).coords().grid;
1704
+ b = $(b).coords().grid;
1705
+ }
1706
+
1707
+ if (a.row > b.row) {
1708
+ return 1;
1709
+ }
1710
+ return -1;
1711
+ });
1712
+
1713
+ return widgets;
1714
+ };
1715
+
1716
+
1717
+ /**
1718
+ * Sorts an Array of grid coords objects (representing the grid coords of
1719
+ * each widget) placing first the empty cells upper left.
1720
+ *
1721
+ * @method sort_by_row_and_col_asc
1722
+ * @param {Array} widgets Array of grid coords objects
1723
+ * @return {Array} Returns the array sorted.
1724
+ */
1725
+ fn.sort_by_row_and_col_asc = function(widgets) {
1726
+ widgets = widgets.sort(function(a, b) {
1727
+ if (a.row > b.row || a.row === b.row && a.col > b.col) {
1728
+ return 1;
1729
+ }
1730
+ return -1;
1731
+ });
1732
+
1733
+ return widgets;
1734
+ };
1735
+
1736
+
1737
+ /**
1738
+ * Sorts an Array of grid coords objects by column (representing the grid
1739
+ * coords of each widget) in ascending way.
1740
+ *
1741
+ * @method sort_by_col_asc
1742
+ * @param {Array} widgets Array of grid coords objects
1743
+ * @return {Array} Returns the array sorted.
1744
+ */
1745
+ fn.sort_by_col_asc = function(widgets) {
1746
+ widgets = widgets.sort(function(a, b) {
1747
+ if (a.col > b.col) {
1748
+ return 1;
1749
+ }
1750
+ return -1;
1751
+ });
1752
+
1753
+ return widgets;
1754
+ };
1755
+
1756
+
1757
+ /**
1758
+ * Sorts an Array of grid coords objects (representing the grid coords of
1759
+ * each widget) in descending way.
1760
+ *
1761
+ * @method sort_by_row_desc
1762
+ * @param {Array} widgets Array of grid coords objects
1763
+ * @return {Array} Returns the array sorted.
1764
+ */
1765
+ fn.sort_by_row_desc = function(widgets) {
1766
+ widgets = widgets.sort(function(a, b) {
1767
+ if (a.row + a.size_y < b.row + b.size_y) {
1768
+ return 1;
1769
+ }
1770
+ return -1;
1771
+ });
1772
+ return widgets;
1773
+ };
1774
+
1775
+
1776
+ /**
1777
+ * Sorts an Array of grid coords objects (representing the grid coords of
1778
+ * each widget) in descending way.
1779
+ *
1780
+ * @method manage_movements
1781
+ * @param {jQuery} $widgets A jQuery collection of HTMLElements
1782
+ * representing the widgets you want to move.
1783
+ * @param {Number} to_col The column to which we want to move the widgets.
1784
+ * @param {Number} to_row The row to which we want to move the widgets.
1785
+ * @return {Class} Returns the instance of the Gridster Class.
1786
+ */
1787
+ fn.manage_movements = function($widgets, to_col, to_row) {
1788
+ $.each($widgets, $.proxy(function(i, w) {
1789
+ var wgd = w;
1790
+ var $w = wgd.el;
1791
+
1792
+ var can_go_widget_up = this.can_go_widget_up(wgd);
1793
+
1794
+ if (can_go_widget_up) {
1795
+ //target CAN go up
1796
+ //so move widget up
1797
+ this.move_widget_to($w, can_go_widget_up);
1798
+ this.set_placeholder(to_col, can_go_widget_up + wgd.size_y);
1799
+
1800
+ } else {
1801
+ //target can't go up
1802
+ var can_go_player_up = this.can_go_player_up(
1803
+ this.player_grid_data);
1804
+
1805
+ if (!can_go_player_up) {
1806
+ // target can't go up
1807
+ // player cant't go up
1808
+ // so we need to move widget down to a position that dont
1809
+ // overlaps player
1810
+ var y = (to_row + this.player_grid_data.size_y) - wgd.row;
1811
+
1812
+ this.move_widget_down($w, y);
1813
+ this.set_placeholder(to_col, to_row);
1814
+ }
1815
+ }
1816
+ }, this));
1817
+
1818
+ return this;
1819
+ };
1820
+
1821
+ /**
1822
+ * Determines if there is a widget in the row and col given. Or if the
1823
+ * HTMLElement passed as first argument is the player.
1824
+ *
1825
+ * @method is_player
1826
+ * @param {Number|HTMLElement} col_or_el A jQuery wrapped collection of
1827
+ * HTMLElements.
1828
+ * @param {Number} [row] The column to which we want to move the widgets.
1829
+ * @return {Boolean} Returns true or false.
1830
+ */
1831
+ fn.is_player = function(col_or_el, row) {
1832
+ if (row && !this.gridmap[col_or_el]) { return false; }
1833
+ var $w = row ? this.gridmap[col_or_el][row] : col_or_el;
1834
+ return $w && ($w.is(this.$player) || $w.is(this.$helper));
1835
+ };
1836
+
1837
+
1838
+ /**
1839
+ * Determines if the widget that is being dragged is currently over the row
1840
+ * and col given.
1841
+ *
1842
+ * @method is_player_in
1843
+ * @param {Number} col The column to check.
1844
+ * @param {Number} row The row to check.
1845
+ * @return {Boolean} Returns true or false.
1846
+ */
1847
+ fn.is_player_in = function(col, row) {
1848
+ var c = this.cells_occupied_by_player || {};
1849
+ return $.inArray(col, c.cols) >= 0 && $.inArray(row, c.rows) >= 0;
1850
+ };
1851
+
1852
+
1853
+ /**
1854
+ * Determines if the placeholder is currently over the row and col given.
1855
+ *
1856
+ * @method is_placeholder_in
1857
+ * @param {Number} col The column to check.
1858
+ * @param {Number} row The row to check.
1859
+ * @return {Boolean} Returns true or false.
1860
+ */
1861
+ fn.is_placeholder_in = function(col, row) {
1862
+ var c = this.cells_occupied_by_placeholder || {};
1863
+ return this.is_placeholder_in_col(col) && $.inArray(row, c.rows) >= 0;
1864
+ };
1865
+
1866
+
1867
+ /**
1868
+ * Determines if the placeholder is currently over the column given.
1869
+ *
1870
+ * @method is_placeholder_in_col
1871
+ * @param {Number} col The column to check.
1872
+ * @return {Boolean} Returns true or false.
1873
+ */
1874
+ fn.is_placeholder_in_col = function(col) {
1875
+ var c = this.cells_occupied_by_placeholder || [];
1876
+ return $.inArray(col, c.cols) >= 0;
1877
+ };
1878
+
1879
+
1880
+ /**
1881
+ * Determines if the cell represented by col and row params is empty.
1882
+ *
1883
+ * @method is_empty
1884
+ * @param {Number} col The column to check.
1885
+ * @param {Number} row The row to check.
1886
+ * @return {Boolean} Returns true or false.
1887
+ */
1888
+ fn.is_empty = function(col, row) {
1889
+ if (typeof this.gridmap[col] !== 'undefined' &&
1890
+ typeof this.gridmap[col][row] !== 'undefined' &&
1891
+ this.gridmap[col][row] === false
1892
+ ) {
1893
+ return true;
1894
+ }
1895
+ return false;
1896
+ };
1897
+
1898
+
1899
+ /**
1900
+ * Determines if the cell represented by col and row params is occupied.
1901
+ *
1902
+ * @method is_occupied
1903
+ * @param {Number} col The column to check.
1904
+ * @param {Number} row The row to check.
1905
+ * @return {Boolean} Returns true or false.
1906
+ */
1907
+ fn.is_occupied = function(col, row) {
1908
+ if (!this.gridmap[col]) {
1909
+ return false;
1910
+ }
1911
+
1912
+ if (this.gridmap[col][row]) {
1913
+ return true;
1914
+ }
1915
+ return false;
1916
+ };
1917
+
1918
+
1919
+ /**
1920
+ * Determines if there is a widget in the cell represented by col/row params.
1921
+ *
1922
+ * @method is_widget
1923
+ * @param {Number} col The column to check.
1924
+ * @param {Number} row The row to check.
1925
+ * @return {Boolean|HTMLElement} Returns false if there is no widget,
1926
+ * else returns the jQuery HTMLElement
1927
+ */
1928
+ fn.is_widget = function(col, row) {
1929
+ var cell = this.gridmap[col];
1930
+ if (!cell) {
1931
+ return false;
1932
+ }
1933
+
1934
+ cell = cell[row];
1935
+
1936
+ if (cell) {
1937
+ return cell;
1938
+ }
1939
+
1940
+ return false;
1941
+ };
1942
+
1943
+
1944
+ /**
1945
+ * Determines if there is a widget in the cell represented by col/row
1946
+ * params and if this is under the widget that is being dragged.
1947
+ *
1948
+ * @method is_widget_under_player
1949
+ * @param {Number} col The column to check.
1950
+ * @param {Number} row The row to check.
1951
+ * @return {Boolean} Returns true or false.
1952
+ */
1953
+ fn.is_widget_under_player = function(col, row) {
1954
+ if (this.is_widget(col, row)) {
1955
+ return this.is_player_in(col, row);
1956
+ }
1957
+ return false;
1958
+ };
1959
+
1960
+
1961
+ /**
1962
+ * Get widgets overlapping with the player or with the object passed
1963
+ * representing the grid cells.
1964
+ *
1965
+ * @method get_widgets_under_player
1966
+ * @return {HTMLElement} Returns a jQuery collection of HTMLElements
1967
+ */
1968
+ fn.get_widgets_under_player = function(cells) {
1969
+ cells || (cells = this.cells_occupied_by_player || {cols: [], rows: []});
1970
+ var $widgets = $([]);
1971
+
1972
+ $.each(cells.cols, $.proxy(function(i, col) {
1973
+ $.each(cells.rows, $.proxy(function(i, row) {
1974
+ if(this.is_widget(col, row)) {
1975
+ $widgets = $widgets.add(this.gridmap[col][row]);
1976
+ }
1977
+ }, this));
1978
+ }, this));
1979
+
1980
+ return $widgets;
1981
+ };
1982
+
1983
+
1984
+ /**
1985
+ * Put placeholder at the row and column specified.
1986
+ *
1987
+ * @method set_placeholder
1988
+ * @param {Number} col The column to which we want to move the
1989
+ * placeholder.
1990
+ * @param {Number} row The row to which we want to move the
1991
+ * placeholder.
1992
+ * @return {Class} Returns the instance of the Gridster Class.
1993
+ */
1994
+ fn.set_placeholder = function(col, row) {
1995
+ var phgd = $.extend({}, this.placeholder_grid_data);
1996
+ var $nexts = this.widgets_below({
1997
+ col: phgd.col,
1998
+ row: phgd.row,
1999
+ size_y: phgd.size_y,
2000
+ size_x: phgd.size_x
2001
+ });
2002
+
2003
+ // Prevents widgets go out of the grid
2004
+ var right_col = (col + phgd.size_x - 1);
2005
+ if (right_col > this.cols) {
2006
+ col = col - (right_col - col);
2007
+ }
2008
+
2009
+ var moved_down = this.placeholder_grid_data.row < row;
2010
+ var changed_column = this.placeholder_grid_data.col !== col;
2011
+
2012
+ this.placeholder_grid_data.col = col;
2013
+ this.placeholder_grid_data.row = row;
2014
+
2015
+ this.cells_occupied_by_placeholder = this.get_cells_occupied(
2016
+ this.placeholder_grid_data);
2017
+
2018
+ this.$preview_holder.attr({
2019
+ 'data-row' : row,
2020
+ 'data-col' : col
2021
+ });
2022
+
2023
+ if (moved_down || changed_column) {
2024
+ $nexts.each($.proxy(function(i, widget) {
2025
+ this.move_widget_up(
2026
+ $(widget), this.placeholder_grid_data.col - col + phgd.size_y);
2027
+ }, this));
2028
+ }
2029
+
2030
+
2031
+ var $widgets_under_ph = this.get_widgets_under_player(this.cells_occupied_by_placeholder);
2032
+ if ($widgets_under_ph.length) {
2033
+ $widgets_under_ph.each($.proxy(function(i, widget) {
2034
+ var $w = $(widget);
2035
+ this.move_widget_down(
2036
+ $w, row + phgd.size_y - $w.data('coords').grid.row);
2037
+ }, this));
2038
+ }
2039
+
2040
+ };
2041
+
2042
+
2043
+ /**
2044
+ * Determines whether the player can move to a position above.
2045
+ *
2046
+ * @method can_go_player_up
2047
+ * @param {Object} widget_grid_data The actual grid coords object of the
2048
+ * player.
2049
+ * @return {Number|Boolean} If the player can be moved to an upper row
2050
+ * returns the row number, else returns false.
2051
+ */
2052
+ fn.can_go_player_up = function(widget_grid_data) {
2053
+ var p_bottom_row = widget_grid_data.row + widget_grid_data.size_y - 1;
2054
+ var result = true;
2055
+ var upper_rows = [];
2056
+ var min_row = 10000;
2057
+ var $widgets_under_player = this.get_widgets_under_player();
2058
+
2059
+ /* generate an array with columns as index and array with upper rows
2060
+ * empty as value */
2061
+ this.for_each_column_occupied(widget_grid_data, function(tcol) {
2062
+ var grid_col = this.gridmap[tcol];
2063
+ var r = p_bottom_row + 1;
2064
+ upper_rows[tcol] = [];
2065
+
2066
+ while (--r > 0) {
2067
+ if (this.is_empty(tcol, r) || this.is_player(tcol, r) ||
2068
+ this.is_widget(tcol, r) &&
2069
+ grid_col[r].is($widgets_under_player)
2070
+ ) {
2071
+ upper_rows[tcol].push(r);
2072
+ min_row = r < min_row ? r : min_row;
2073
+ }else{
2074
+ break;
2075
+ }
2076
+ }
2077
+
2078
+ if (upper_rows[tcol].length === 0) {
2079
+ result = false;
2080
+ return true; //break
2081
+ }
2082
+
2083
+ upper_rows[tcol].sort();
2084
+ });
2085
+
2086
+ if (!result) { return false; }
2087
+
2088
+ return this.get_valid_rows(widget_grid_data, upper_rows, min_row);
2089
+ };
2090
+
2091
+
2092
+ /**
2093
+ * Determines whether a widget can move to a position above.
2094
+ *
2095
+ * @method can_go_widget_up
2096
+ * @param {Object} widget_grid_data The actual grid coords object of the
2097
+ * widget we want to check.
2098
+ * @return {Number|Boolean} If the widget can be moved to an upper row
2099
+ * returns the row number, else returns false.
2100
+ */
2101
+ fn.can_go_widget_up = function(widget_grid_data) {
2102
+ var p_bottom_row = widget_grid_data.row + widget_grid_data.size_y - 1;
2103
+ var result = true;
2104
+ var upper_rows = [];
2105
+ var min_row = 10000;
2106
+
2107
+ /* generate an array with columns as index and array with topmost rows
2108
+ * empty as value */
2109
+ this.for_each_column_occupied(widget_grid_data, function(tcol) {
2110
+ var grid_col = this.gridmap[tcol];
2111
+ upper_rows[tcol] = [];
2112
+
2113
+ var r = p_bottom_row + 1;
2114
+ // iterate over each row
2115
+ while (--r > 0) {
2116
+ if (this.is_widget(tcol, r) && !this.is_player_in(tcol, r)) {
2117
+ if (!grid_col[r].is(widget_grid_data.el)) {
2118
+ break;
2119
+ }
2120
+ }
2121
+
2122
+ if (!this.is_player(tcol, r) &&
2123
+ !this.is_placeholder_in(tcol, r) &&
2124
+ !this.is_player_in(tcol, r)) {
2125
+ upper_rows[tcol].push(r);
2126
+ }
2127
+
2128
+ if (r < min_row) {
2129
+ min_row = r;
2130
+ }
2131
+ }
2132
+
2133
+ if (upper_rows[tcol].length === 0) {
2134
+ result = false;
2135
+ return true; //break
2136
+ }
2137
+
2138
+ upper_rows[tcol].sort();
2139
+ });
2140
+
2141
+ if (!result) { return false; }
2142
+
2143
+ return this.get_valid_rows(widget_grid_data, upper_rows, min_row);
2144
+ };
2145
+
2146
+
2147
+ /**
2148
+ * Search a valid row for the widget represented by `widget_grid_data' in
2149
+ * the `upper_rows` array. Iteration starts from row specified in `min_row`.
2150
+ *
2151
+ * @method get_valid_rows
2152
+ * @param {Object} widget_grid_data The actual grid coords object of the
2153
+ * player.
2154
+ * @param {Array} upper_rows An array with columns as index and arrays
2155
+ * of valid rows as values.
2156
+ * @param {Number} min_row The upper row from which the iteration will start.
2157
+ * @return {Number|Boolean} Returns the upper row valid from the `upper_rows`
2158
+ * for the widget in question.
2159
+ */
2160
+ fn.get_valid_rows = function(widget_grid_data, upper_rows, min_row) {
2161
+ var p_top_row = widget_grid_data.row;
2162
+ var p_bottom_row = widget_grid_data.row + widget_grid_data.size_y - 1;
2163
+ var size_y = widget_grid_data.size_y;
2164
+ var r = min_row - 1;
2165
+ var valid_rows = [];
2166
+
2167
+ while (++r <= p_bottom_row ) {
2168
+ var common = true;
2169
+ $.each(upper_rows, function(col, rows) {
2170
+ if ($.isArray(rows) && $.inArray(r, rows) === -1) {
2171
+ common = false;
2172
+ }
2173
+ });
2174
+
2175
+ if (common === true) {
2176
+ valid_rows.push(r);
2177
+ if (valid_rows.length === size_y) {
2178
+ break;
2179
+ }
2180
+ }
2181
+ }
2182
+
2183
+ var new_row = false;
2184
+ if (size_y === 1) {
2185
+ if (valid_rows[0] !== p_top_row) {
2186
+ new_row = valid_rows[0] || false;
2187
+ }
2188
+ }else{
2189
+ if (valid_rows[0] !== p_top_row) {
2190
+ new_row = this.get_consecutive_numbers_index(
2191
+ valid_rows, size_y);
2192
+ }
2193
+ }
2194
+
2195
+ return new_row;
2196
+ };
2197
+
2198
+
2199
+ fn.get_consecutive_numbers_index = function(arr, size_y) {
2200
+ var max = arr.length;
2201
+ var result = [];
2202
+ var first = true;
2203
+ var prev = -1; // or null?
2204
+
2205
+ for (var i=0; i < max; i++) {
2206
+ if (first || arr[i] === prev + 1) {
2207
+ result.push(i);
2208
+ if (result.length === size_y) {
2209
+ break;
2210
+ }
2211
+ first = false;
2212
+ }else{
2213
+ result = [];
2214
+ first = true;
2215
+ }
2216
+
2217
+ prev = arr[i];
2218
+ }
2219
+
2220
+ return result.length >= size_y ? arr[result[0]] : false;
2221
+ };
2222
+
2223
+
2224
+ /**
2225
+ * Get widgets overlapping with the player.
2226
+ *
2227
+ * @method get_widgets_overlapped
2228
+ * @return {jQuery} Returns a jQuery collection of HTMLElements.
2229
+ */
2230
+ fn.get_widgets_overlapped = function() {
2231
+ var $w;
2232
+ var $widgets = $([]);
2233
+ var used = [];
2234
+ var rows_from_bottom = this.cells_occupied_by_player.rows.slice(0);
2235
+ rows_from_bottom.reverse();
2236
+
2237
+ $.each(this.cells_occupied_by_player.cols, $.proxy(function(i, col) {
2238
+ $.each(rows_from_bottom, $.proxy(function(i, row) {
2239
+ // if there is a widget in the player position
2240
+ if (!this.gridmap[col]) { return true; } //next iteration
2241
+ var $w = this.gridmap[col][row];
2242
+ if (this.is_occupied(col, row) && !this.is_player($w) &&
2243
+ $.inArray($w, used) === -1
2244
+ ) {
2245
+ $widgets = $widgets.add($w);
2246
+ used.push($w);
2247
+ }
2248
+
2249
+ }, this));
2250
+ }, this));
2251
+
2252
+ return $widgets;
2253
+ };
2254
+
2255
+
2256
+ /**
2257
+ * This callback is executed when the player begins to collide with a column.
2258
+ *
2259
+ * @method on_start_overlapping_column
2260
+ * @param {Number} col The collided column.
2261
+ * @return {jQuery} Returns a jQuery collection of HTMLElements.
2262
+ */
2263
+ fn.on_start_overlapping_column = function(col) {
2264
+ this.set_player(col, false);
2265
+ };
2266
+
2267
+
2268
+ /**
2269
+ * A callback executed when the player begins to collide with a row.
2270
+ *
2271
+ * @method on_start_overlapping_row
2272
+ * @param {Number} row The collided row.
2273
+ * @return {jQuery} Returns a jQuery collection of HTMLElements.
2274
+ */
2275
+ fn.on_start_overlapping_row = function(row) {
2276
+ this.set_player(false, row);
2277
+ };
2278
+
2279
+
2280
+ /**
2281
+ * A callback executed when the the player ends to collide with a column.
2282
+ *
2283
+ * @method on_stop_overlapping_column
2284
+ * @param {Number} col The collided row.
2285
+ * @return {jQuery} Returns a jQuery collection of HTMLElements.
2286
+ */
2287
+ fn.on_stop_overlapping_column = function(col) {
2288
+ this.set_player(col, false);
2289
+
2290
+ var self = this;
2291
+ this.for_each_widget_below(col, this.cells_occupied_by_player.rows[0],
2292
+ function(tcol, trow) {
2293
+ self.move_widget_up(this, self.player_grid_data.size_y);
2294
+ });
2295
+ };
2296
+
2297
+
2298
+ /**
2299
+ * This callback is executed when the player ends to collide with a row.
2300
+ *
2301
+ * @method on_stop_overlapping_row
2302
+ * @param {Number} row The collided row.
2303
+ * @return {jQuery} Returns a jQuery collection of HTMLElements.
2304
+ */
2305
+ fn.on_stop_overlapping_row = function(row) {
2306
+ this.set_player(false, row);
2307
+
2308
+ var self = this;
2309
+ var cols = this.cells_occupied_by_player.cols;
2310
+ for (var c = 0, cl = cols.length; c < cl; c++) {
2311
+ this.for_each_widget_below(cols[c], row, function(tcol, trow) {
2312
+ self.move_widget_up(this, self.player_grid_data.size_y);
2313
+ });
2314
+ }
2315
+ };
2316
+
2317
+
2318
+ /**
2319
+ * Move a widget to a specific row. The cell or cells must be empty.
2320
+ * If the widget has widgets below, all of these widgets will be moved also
2321
+ * if they can.
2322
+ *
2323
+ * @method move_widget_to
2324
+ * @param {HTMLElement} $widget The jQuery wrapped HTMLElement of the
2325
+ * widget is going to be moved.
2326
+ * @return {Class} Returns the instance of the Gridster Class.
2327
+ */
2328
+ fn.move_widget_to = function($widget, row) {
2329
+ var self = this;
2330
+ var widget_grid_data = $widget.coords().grid;
2331
+ var diff = row - widget_grid_data.row;
2332
+ var $next_widgets = this.widgets_below($widget);
2333
+
2334
+ var can_move_to_new_cell = this.can_move_to(
2335
+ widget_grid_data, widget_grid_data.col, row, $widget);
2336
+
2337
+ if (can_move_to_new_cell === false) {
2338
+ return false;
2339
+ }
2340
+
2341
+ this.remove_from_gridmap(widget_grid_data);
2342
+ widget_grid_data.row = row;
2343
+ this.add_to_gridmap(widget_grid_data);
2344
+ $widget.attr('data-row', row);
2345
+ this.$changed = this.$changed.add($widget);
2346
+
2347
+
2348
+ $next_widgets.each(function(i, widget) {
2349
+ var $w = $(widget);
2350
+ var wgd = $w.coords().grid;
2351
+ var can_go_up = self.can_go_widget_up(wgd);
2352
+ if (can_go_up && can_go_up !== wgd.row) {
2353
+ self.move_widget_to($w, can_go_up);
2354
+ }
2355
+ });
2356
+
2357
+ return this;
2358
+ };
2359
+
2360
+
2361
+ /**
2362
+ * Move up the specified widget and all below it.
2363
+ *
2364
+ * @method move_widget_up
2365
+ * @param {HTMLElement} $widget The widget you want to move.
2366
+ * @param {Number} [y_units] The number of cells that the widget has to move.
2367
+ * @return {Class} Returns the instance of the Gridster Class.
2368
+ */
2369
+ fn.move_widget_up = function($widget, y_units) {
2370
+ var el_grid_data = $widget.coords().grid;
2371
+ var actual_row = el_grid_data.row;
2372
+ var moved = [];
2373
+ var can_go_up = true;
2374
+ y_units || (y_units = 1);
2375
+
2376
+ if (!this.can_go_up($widget)) { return false; } //break;
2377
+
2378
+ this.for_each_column_occupied(el_grid_data, function(col) {
2379
+ // can_go_up
2380
+ if ($.inArray($widget, moved) === -1) {
2381
+ var widget_grid_data = $widget.coords().grid;
2382
+ var next_row = actual_row - y_units;
2383
+ next_row = this.can_go_up_to_row(
2384
+ widget_grid_data, col, next_row);
2385
+
2386
+ if (!next_row) {
2387
+ return true;
2388
+ }
2389
+
2390
+ var $next_widgets = this.widgets_below($widget);
2391
+
2392
+ this.remove_from_gridmap(widget_grid_data);
2393
+ widget_grid_data.row = next_row;
2394
+ this.add_to_gridmap(widget_grid_data);
2395
+ $widget.attr('data-row', widget_grid_data.row);
2396
+ this.$changed = this.$changed.add($widget);
2397
+
2398
+ moved.push($widget);
2399
+
2400
+ $next_widgets.each($.proxy(function(i, widget) {
2401
+ this.move_widget_up($(widget), y_units);
2402
+ }, this));
2403
+ }
2404
+ });
2405
+
2406
+ };
2407
+
2408
+
2409
+ /**
2410
+ * Move down the specified widget and all below it.
2411
+ *
2412
+ * @method move_widget_down
2413
+ * @param {jQuery} $widget The jQuery object representing the widget
2414
+ * you want to move.
2415
+ * @param {Number} y_units The number of cells that the widget has to move.
2416
+ * @return {Class} Returns the instance of the Gridster Class.
2417
+ */
2418
+ fn.move_widget_down = function($widget, y_units) {
2419
+ var el_grid_data = $widget.coords().grid;
2420
+ var actual_row = el_grid_data.row;
2421
+ var moved = [];
2422
+ var y_diff = y_units;
2423
+
2424
+ if (!$widget) { return false; }
2425
+
2426
+ if ($.inArray($widget, moved) === -1) {
2427
+
2428
+ var widget_grid_data = $widget.coords().grid;
2429
+ var next_row = actual_row + y_units;
2430
+ var $next_widgets = this.widgets_below($widget);
2431
+
2432
+ this.remove_from_gridmap(widget_grid_data);
2433
+
2434
+ $next_widgets.each($.proxy(function(i, widget) {
2435
+ var $w = $(widget);
2436
+ var wd = $w.coords().grid;
2437
+ var tmp_y = this.displacement_diff(
2438
+ wd, widget_grid_data, y_diff);
2439
+
2440
+ if (tmp_y > 0) {
2441
+ this.move_widget_down($w, tmp_y);
2442
+ }
2443
+ }, this));
2444
+
2445
+ widget_grid_data.row = next_row;
2446
+ this.update_widget_position(widget_grid_data, $widget);
2447
+ $widget.attr('data-row', widget_grid_data.row);
2448
+ this.$changed = this.$changed.add($widget);
2449
+
2450
+ moved.push($widget);
2451
+ }
2452
+ };
2453
+
2454
+
2455
+ /**
2456
+ * Check if the widget can move to the specified row, else returns the
2457
+ * upper row possible.
2458
+ *
2459
+ * @method can_go_up_to_row
2460
+ * @param {Number} widget_grid_data The current grid coords object of the
2461
+ * widget.
2462
+ * @param {Number} col The target column.
2463
+ * @param {Number} row The target row.
2464
+ * @return {Boolean|Number} Returns the row number if the widget can move
2465
+ * to the target position, else returns false.
2466
+ */
2467
+ fn.can_go_up_to_row = function(widget_grid_data, col, row) {
2468
+ var ga = this.gridmap;
2469
+ var result = true;
2470
+ var urc = []; // upper_rows_in_columns
2471
+ var actual_row = widget_grid_data.row;
2472
+ var r;
2473
+
2474
+ /* generate an array with columns as index and array with
2475
+ * upper rows empty in the column */
2476
+ this.for_each_column_occupied(widget_grid_data, function(tcol) {
2477
+ var grid_col = ga[tcol];
2478
+ urc[tcol] = [];
2479
+
2480
+ r = actual_row;
2481
+ while (r--) {
2482
+ if (this.is_empty(tcol, r) &&
2483
+ !this.is_placeholder_in(tcol, r)
2484
+ ) {
2485
+ urc[tcol].push(r);
2486
+ }else{
2487
+ break;
2488
+ }
2489
+ }
2490
+
2491
+ if (!urc[tcol].length) {
2492
+ result = false;
2493
+ return true;
2494
+ }
2495
+
2496
+ });
2497
+
2498
+ if (!result) { return false; }
2499
+
2500
+ /* get common rows starting from upper position in all the columns
2501
+ * that widget occupies */
2502
+ r = row;
2503
+ for (r = 1; r < actual_row; r++) {
2504
+ var common = true;
2505
+
2506
+ for (var uc = 0, ucl = urc.length; uc < ucl; uc++) {
2507
+ if (urc[uc] && $.inArray(r, urc[uc]) === -1) {
2508
+ common = false;
2509
+ }
2510
+ }
2511
+
2512
+ if (common === true) {
2513
+ result = r;
2514
+ break;
2515
+ }
2516
+ }
2517
+
2518
+ return result;
2519
+ };
2520
+
2521
+
2522
+ fn.displacement_diff = function(widget_grid_data, parent_bgd, y_units) {
2523
+ var actual_row = widget_grid_data.row;
2524
+ var diffs = [];
2525
+ var parent_max_y = parent_bgd.row + parent_bgd.size_y;
2526
+
2527
+ this.for_each_column_occupied(widget_grid_data, function(col) {
2528
+ var temp_y_units = 0;
2529
+
2530
+ for (var r = parent_max_y; r < actual_row; r++) {
2531
+ if (this.is_empty(col, r)) {
2532
+ temp_y_units = temp_y_units + 1;
2533
+ }
2534
+ }
2535
+
2536
+ diffs.push(temp_y_units);
2537
+ });
2538
+
2539
+ var max_diff = Math.max.apply(Math, diffs);
2540
+ y_units = (y_units - max_diff);
2541
+
2542
+ return y_units > 0 ? y_units : 0;
2543
+ };
2544
+
2545
+
2546
+ /**
2547
+ * Get widgets below a widget.
2548
+ *
2549
+ * @method widgets_below
2550
+ * @param {HTMLElement} $el The jQuery wrapped HTMLElement.
2551
+ * @return {jQuery} A jQuery collection of HTMLElements.
2552
+ */
2553
+ fn.widgets_below = function($el) {
2554
+ var el_grid_data = $.isPlainObject($el) ? $el : $el.coords().grid;
2555
+ var self = this;
2556
+ var ga = this.gridmap;
2557
+ var next_row = el_grid_data.row + el_grid_data.size_y - 1;
2558
+ var $nexts = $([]);
2559
+
2560
+ this.for_each_column_occupied(el_grid_data, function(col) {
2561
+ self.for_each_widget_below(col, next_row, function(tcol, trow) {
2562
+ if (!self.is_player(this) && $.inArray(this, $nexts) === -1) {
2563
+ $nexts = $nexts.add(this);
2564
+ return true; // break
2565
+ }
2566
+ });
2567
+ });
2568
+
2569
+ return this.sort_by_row_asc($nexts);
2570
+ };
2571
+
2572
+
2573
+ /**
2574
+ * Update the array of mapped positions with the new player position.
2575
+ *
2576
+ * @method set_cells_player_occupies
2577
+ * @param {Number} col The new player col.
2578
+ * @param {Number} col The new player row.
2579
+ * @return {Class} Returns the instance of the Gridster Class.
2580
+ */
2581
+ fn.set_cells_player_occupies = function(col, row) {
2582
+ this.remove_from_gridmap(this.placeholder_grid_data);
2583
+ this.placeholder_grid_data.col = col;
2584
+ this.placeholder_grid_data.row = row;
2585
+ this.add_to_gridmap(this.placeholder_grid_data, this.$player);
2586
+ return this;
2587
+ };
2588
+
2589
+
2590
+ /**
2591
+ * Remove from the array of mapped positions the reference to the player.
2592
+ *
2593
+ * @method empty_cells_player_occupies
2594
+ * @return {Class} Returns the instance of the Gridster Class.
2595
+ */
2596
+ fn.empty_cells_player_occupies = function() {
2597
+ this.remove_from_gridmap(this.placeholder_grid_data);
2598
+ return this;
2599
+ };
2600
+
2601
+
2602
+ fn.can_go_up = function($el) {
2603
+ var el_grid_data = $el.coords().grid;
2604
+ var initial_row = el_grid_data.row;
2605
+ var prev_row = initial_row - 1;
2606
+ var ga = this.gridmap;
2607
+ var upper_rows_by_column = [];
2608
+
2609
+ var result = true;
2610
+ if (initial_row === 1) { return false; }
2611
+
2612
+ this.for_each_column_occupied(el_grid_data, function(col) {
2613
+ var $w = this.is_widget(col, prev_row);
2614
+
2615
+ if (this.is_occupied(col, prev_row) ||
2616
+ this.is_player(col, prev_row) ||
2617
+ this.is_placeholder_in(col, prev_row) ||
2618
+ this.is_player_in(col, prev_row)
2619
+ ) {
2620
+ result = false;
2621
+ return true; //break
2622
+ }
2623
+ });
2624
+
2625
+ return result;
2626
+ };
2627
+
2628
+
2629
+
2630
+ /**
2631
+ * Check if it's possible to move a widget to a specific col/row. It takes
2632
+ * into account the dimensions (`size_y` and `size_x` attrs. of the grid
2633
+ * coords object) the widget occupies.
2634
+ *
2635
+ * @method can_move_to
2636
+ * @param {Object} widget_grid_data The grid coords object that represents
2637
+ * the widget.
2638
+ * @param {Object} col The col to check.
2639
+ * @param {Object} row The row to check.
2640
+ * @param {Number} [max_row] The max row allowed.
2641
+ * @return {Boolean} Returns true if all cells are empty, else return false.
2642
+ */
2643
+ fn.can_move_to = function(widget_grid_data, col, row, max_row) {
2644
+ var ga = this.gridmap;
2645
+ var $w = widget_grid_data.el;
2646
+ var future_wd = {
2647
+ size_y: widget_grid_data.size_y,
2648
+ size_x: widget_grid_data.size_x,
2649
+ col: col,
2650
+ row: row
2651
+ };
2652
+ var result = true;
2653
+
2654
+ //Prevents widgets go out of the grid
2655
+ var right_col = col + widget_grid_data.size_x - 1;
2656
+ if (right_col > this.cols) {
2657
+ return false;
2658
+ }
2659
+
2660
+ if (max_row && max_row < row + widget_grid_data.size_y - 1) {
2661
+ return false;
2662
+ }
2663
+
2664
+ this.for_each_cell_occupied(future_wd, function(tcol, trow) {
2665
+ var $tw = this.is_widget(tcol, trow);
2666
+ if ($tw && (!widget_grid_data.el || $tw.is($w))) {
2667
+ result = false;
2668
+ }
2669
+ });
2670
+
2671
+ return result;
2672
+ };
2673
+
2674
+
2675
+ /**
2676
+ * Given the leftmost column returns all columns that are overlapping
2677
+ * with the player.
2678
+ *
2679
+ * @method get_targeted_columns
2680
+ * @param {Number} [from_col] The leftmost column.
2681
+ * @return {Array} Returns an array with column numbers.
2682
+ */
2683
+ fn.get_targeted_columns = function(from_col) {
2684
+ var max = (from_col || this.player_grid_data.col) +
2685
+ (this.player_grid_data.size_x - 1);
2686
+ var cols = [];
2687
+ for (var col = from_col; col <= max; col++) {
2688
+ cols.push(col);
2689
+ }
2690
+ return cols;
2691
+ };
2692
+
2693
+
2694
+ /**
2695
+ * Given the upper row returns all rows that are overlapping with the player.
2696
+ *
2697
+ * @method get_targeted_rows
2698
+ * @param {Number} [from_row] The upper row.
2699
+ * @return {Array} Returns an array with row numbers.
2700
+ */
2701
+ fn.get_targeted_rows = function(from_row) {
2702
+ var max = (from_row || this.player_grid_data.row) +
2703
+ (this.player_grid_data.size_y - 1);
2704
+ var rows = [];
2705
+ for (var row = from_row; row <= max; row++) {
2706
+ rows.push(row);
2707
+ }
2708
+ return rows;
2709
+ };
2710
+
2711
+ /**
2712
+ * Get all columns and rows that a widget occupies.
2713
+ *
2714
+ * @method get_cells_occupied
2715
+ * @param {Object} el_grid_data The grid coords object of the widget.
2716
+ * @return {Object} Returns an object like `{ cols: [], rows: []}`.
2717
+ */
2718
+ fn.get_cells_occupied = function(el_grid_data) {
2719
+ var cells = { cols: [], rows: []};
2720
+ var i;
2721
+ if (arguments[1] instanceof jQuery) {
2722
+ el_grid_data = arguments[1].coords().grid;
2723
+ }
2724
+
2725
+ for (i = 0; i < el_grid_data.size_x; i++) {
2726
+ var col = el_grid_data.col + i;
2727
+ cells.cols.push(col);
2728
+ }
2729
+
2730
+ for (i = 0; i < el_grid_data.size_y; i++) {
2731
+ var row = el_grid_data.row + i;
2732
+ cells.rows.push(row);
2733
+ }
2734
+
2735
+ return cells;
2736
+ };
2737
+
2738
+
2739
+ /**
2740
+ * Iterate over the cells occupied by a widget executing a function for
2741
+ * each one.
2742
+ *
2743
+ * @method for_each_cell_occupied
2744
+ * @param {Object} el_grid_data The grid coords object that represents the
2745
+ * widget.
2746
+ * @param {Function} callback The function to execute on each column
2747
+ * iteration. Column and row are passed as arguments.
2748
+ * @return {Class} Returns the instance of the Gridster Class.
2749
+ */
2750
+ fn.for_each_cell_occupied = function(grid_data, callback) {
2751
+ this.for_each_column_occupied(grid_data, function(col) {
2752
+ this.for_each_row_occupied(grid_data, function(row) {
2753
+ callback.call(this, col, row);
2754
+ });
2755
+ });
2756
+ return this;
2757
+ };
2758
+
2759
+
2760
+ /**
2761
+ * Iterate over the columns occupied by a widget executing a function for
2762
+ * each one.
2763
+ *
2764
+ * @method for_each_column_occupied
2765
+ * @param {Object} el_grid_data The grid coords object that represents
2766
+ * the widget.
2767
+ * @param {Function} callback The function to execute on each column
2768
+ * iteration. The column number is passed as first argument.
2769
+ * @return {Class} Returns the instance of the Gridster Class.
2770
+ */
2771
+ fn.for_each_column_occupied = function(el_grid_data, callback) {
2772
+ for (var i = 0; i < el_grid_data.size_x; i++) {
2773
+ var col = el_grid_data.col + i;
2774
+ callback.call(this, col, el_grid_data);
2775
+ }
2776
+ };
2777
+
2778
+
2779
+ /**
2780
+ * Iterate over the rows occupied by a widget executing a function for
2781
+ * each one.
2782
+ *
2783
+ * @method for_each_row_occupied
2784
+ * @param {Object} el_grid_data The grid coords object that represents
2785
+ * the widget.
2786
+ * @param {Function} callback The function to execute on each column
2787
+ * iteration. The row number is passed as first argument.
2788
+ * @return {Class} Returns the instance of the Gridster Class.
2789
+ */
2790
+ fn.for_each_row_occupied = function(el_grid_data, callback) {
2791
+ for (var i = 0; i < el_grid_data.size_y; i++) {
2792
+ var row = el_grid_data.row + i;
2793
+ callback.call(this, row, el_grid_data);
2794
+ }
2795
+ };
2796
+
2797
+
2798
+
2799
+ fn._traversing_widgets = function(type, direction, col, row, callback) {
2800
+ var ga = this.gridmap;
2801
+ if (!ga[col]) { return; }
2802
+
2803
+ var cr, max;
2804
+ var action = type + '/' + direction;
2805
+ if (arguments[2] instanceof jQuery) {
2806
+ var el_grid_data = arguments[2].coords().grid;
2807
+ col = el_grid_data.col;
2808
+ row = el_grid_data.row;
2809
+ callback = arguments[3];
2810
+ }
2811
+ var matched = [];
2812
+ var trow = row;
2813
+
2814
+
2815
+ var methods = {
2816
+ 'for_each/above': function() {
2817
+ while (trow--) {
2818
+ if (trow > 0 && this.is_widget(col, trow) &&
2819
+ $.inArray(ga[col][trow], matched) === -1
2820
+ ) {
2821
+ cr = callback.call(ga[col][trow], col, trow);
2822
+ matched.push(ga[col][trow]);
2823
+ if (cr) { break; }
2824
+ }
2825
+ }
2826
+ },
2827
+ 'for_each/below': function() {
2828
+ for (trow = row + 1, max = ga[col].length; trow < max; trow++) {
2829
+ if (this.is_widget(col, trow) &&
2830
+ $.inArray(ga[col][trow], matched) === -1
2831
+ ) {
2832
+ cr = callback.call(ga[col][trow], col, trow);
2833
+ matched.push(ga[col][trow]);
2834
+ if (cr) { break; }
2835
+ }
2836
+ }
2837
+ }
2838
+ };
2839
+
2840
+ if (methods[action]) {
2841
+ methods[action].call(this);
2842
+ }
2843
+ };
2844
+
2845
+
2846
+ /**
2847
+ * Iterate over each widget above the column and row specified.
2848
+ *
2849
+ * @method for_each_widget_above
2850
+ * @param {Number} col The column to start iterating.
2851
+ * @param {Number} row The row to start iterating.
2852
+ * @param {Function} callback The function to execute on each widget
2853
+ * iteration. The value of `this` inside the function is the jQuery
2854
+ * wrapped HTMLElement.
2855
+ * @return {Class} Returns the instance of the Gridster Class.
2856
+ */
2857
+ fn.for_each_widget_above = function(col, row, callback) {
2858
+ this._traversing_widgets('for_each', 'above', col, row, callback);
2859
+ return this;
2860
+ };
2861
+
2862
+
2863
+ /**
2864
+ * Iterate over each widget below the column and row specified.
2865
+ *
2866
+ * @method for_each_widget_below
2867
+ * @param {Number} col The column to start iterating.
2868
+ * @param {Number} row The row to start iterating.
2869
+ * @param {Function} callback The function to execute on each widget
2870
+ * iteration. The value of `this` inside the function is the jQuery wrapped
2871
+ * HTMLElement.
2872
+ * @return {Class} Returns the instance of the Gridster Class.
2873
+ */
2874
+ fn.for_each_widget_below = function(col, row, callback) {
2875
+ this._traversing_widgets('for_each', 'below', col, row, callback);
2876
+ return this;
2877
+ };
2878
+
2879
+
2880
+ /**
2881
+ * Returns the highest occupied cell in the grid.
2882
+ *
2883
+ * @method get_highest_occupied_cell
2884
+ * @return {Object} Returns an object with `col` and `row` numbers.
2885
+ */
2886
+ fn.get_highest_occupied_cell = function() {
2887
+ var r;
2888
+ var gm = this.gridmap;
2889
+ var rows = [];
2890
+ var row_in_col = [];
2891
+ for (var c = gm.length - 1; c >= 1; c--) {
2892
+ for (r = gm[c].length - 1; r >= 1; r--) {
2893
+ if (this.is_widget(c, r)) {
2894
+ rows.push(r);
2895
+ row_in_col[r] = c;
2896
+ break;
2897
+ }
2898
+ }
2899
+ }
2900
+
2901
+ var highest_row = Math.max.apply(Math, rows);
2902
+
2903
+ this.highest_occupied_cell = {
2904
+ col: row_in_col[highest_row],
2905
+ row: highest_row
2906
+ };
2907
+
2908
+ return this.highest_occupied_cell;
2909
+ };
2910
+
2911
+
2912
+ fn.get_widgets_from = function(col, row) {
2913
+ var ga = this.gridmap;
2914
+ var $widgets = $();
2915
+
2916
+ if (col) {
2917
+ $widgets = $widgets.add(
2918
+ this.$widgets.filter(function() {
2919
+ var tcol = $(this).attr('data-col');
2920
+ return (tcol === col || tcol > col);
2921
+ })
2922
+ );
2923
+ }
2924
+
2925
+ if (row) {
2926
+ $widgets = $widgets.add(
2927
+ this.$widgets.filter(function() {
2928
+ var trow = $(this).attr('data-row');
2929
+ return (trow === row || trow > row);
2930
+ })
2931
+ );
2932
+ }
2933
+
2934
+ return $widgets;
2935
+ };
2936
+
2937
+
2938
+ /**
2939
+ * Set the current height of the parent grid.
2940
+ *
2941
+ * @method set_dom_grid_height
2942
+ * @return {Object} Returns the instance of the Gridster class.
2943
+ */
2944
+ fn.set_dom_grid_height = function() {
2945
+ var r = this.get_highest_occupied_cell().row;
2946
+ this.$el.css('height', r * this.min_widget_height);
2947
+ return this;
2948
+ };
2949
+
2950
+
2951
+ /**
2952
+ * It generates the neccessary styles to position the widgets.
2953
+ *
2954
+ * @method generate_stylesheet
2955
+ * @param {Number} rows Number of columns.
2956
+ * @param {Number} cols Number of rows.
2957
+ * @return {Object} Returns the instance of the Gridster class.
2958
+ */
2959
+ fn.generate_stylesheet = function(opts) {
2960
+ var styles = '';
2961
+ var max_size_x = this.options.max_size_x;
2962
+ var max_rows = 0;
2963
+ var max_cols = 0;
2964
+ var i;
2965
+ var rules;
2966
+
2967
+ opts || (opts = {});
2968
+ opts.cols || (opts.cols = this.cols);
2969
+ opts.rows || (opts.rows = this.rows);
2970
+ opts.namespace || (opts.namespace = this.options.namespace);
2971
+ opts.widget_base_dimensions ||
2972
+ (opts.widget_base_dimensions = this.options.widget_base_dimensions);
2973
+ opts.widget_margins ||
2974
+ (opts.widget_margins = this.options.widget_margins);
2975
+ opts.min_widget_width = (opts.widget_margins[0] * 2) +
2976
+ opts.widget_base_dimensions[0];
2977
+ opts.min_widget_height = (opts.widget_margins[1] * 2) +
2978
+ opts.widget_base_dimensions[1];
2979
+
2980
+ // don't duplicate stylesheets for the same configuration
2981
+ var serialized_opts = $.param(opts);
2982
+ if ($.inArray(serialized_opts, Gridster.generated_stylesheets) >= 0) {
2983
+ return false;
2984
+ }
2985
+
2986
+ Gridster.generated_stylesheets.push(serialized_opts);
2987
+
2988
+ /* generate CSS styles for cols */
2989
+ for (i = opts.cols; i >= 0; i--) {
2990
+ styles += (opts.namespace + ' [data-col="'+ (i + 1) + '"] { left:' +
2991
+ ((i * opts.widget_base_dimensions[0]) +
2992
+ (i * opts.widget_margins[0]) +
2993
+ ((i + 1) * opts.widget_margins[0])) + 'px;} ');
2994
+ }
2995
+
2996
+ /* generate CSS styles for rows */
2997
+ for (i = opts.rows; i >= 0; i--) {
2998
+ styles += (opts.namespace + ' [data-row="' + (i + 1) + '"] { top:' +
2999
+ ((i * opts.widget_base_dimensions[1]) +
3000
+ (i * opts.widget_margins[1]) +
3001
+ ((i + 1) * opts.widget_margins[1]) ) + 'px;} ');
3002
+ }
3003
+
3004
+ for (var y = 1; y <= opts.rows; y++) {
3005
+ styles += (opts.namespace + ' [data-sizey="' + y + '"] { height:' +
3006
+ (y * opts.widget_base_dimensions[1] +
3007
+ (y - 1) * (opts.widget_margins[1] * 2)) + 'px;}');
3008
+ }
3009
+
3010
+ for (var x = 1; x <= max_size_x; x++) {
3011
+ styles += (opts.namespace + ' [data-sizex="' + x + '"] { width:' +
3012
+ (x * opts.widget_base_dimensions[0] +
3013
+ (x - 1) * (opts.widget_margins[0] * 2)) + 'px;}');
3014
+ }
3015
+
3016
+ return this.add_style_tag(styles);
3017
+ };
3018
+
3019
+
3020
+ /**
3021
+ * Injects the given CSS as string to the head of the document.
3022
+ *
3023
+ * @method add_style_tag
3024
+ * @param {String} css The styles to apply.
3025
+ * @return {Object} Returns the instance of the Gridster class.
3026
+ */
3027
+ fn.add_style_tag = function(css) {
3028
+ var d = document;
3029
+ var tag = d.createElement('style');
3030
+
3031
+ d.getElementsByTagName('head')[0].appendChild(tag);
3032
+ tag.setAttribute('type', 'text/css');
3033
+
3034
+ if (tag.styleSheet) {
3035
+ tag.styleSheet.cssText = css;
3036
+ }else{
3037
+ tag.appendChild(document.createTextNode(css));
3038
+ }
3039
+ return this;
3040
+ };
3041
+
3042
+
3043
+ /**
3044
+ * Generates a faux grid to collide with it when a widget is dragged and
3045
+ * detect row or column that we want to go.
3046
+ *
3047
+ * @method generate_faux_grid
3048
+ * @param {Number} rows Number of columns.
3049
+ * @param {Number} cols Number of rows.
3050
+ * @return {Object} Returns the instance of the Gridster class.
3051
+ */
3052
+ fn.generate_faux_grid = function(rows, cols) {
3053
+ this.faux_grid = [];
3054
+ this.gridmap = [];
3055
+ var col;
3056
+ var row;
3057
+ for (col = cols; col > 0; col--) {
3058
+ this.gridmap[col] = [];
3059
+ for (row = rows; row > 0; row--) {
3060
+ this.add_faux_cell(row, col);
3061
+ }
3062
+ }
3063
+ return this;
3064
+ };
3065
+
3066
+
3067
+ /**
3068
+ * Add cell to the faux grid.
3069
+ *
3070
+ * @method add_faux_cell
3071
+ * @param {Number} row The row for the new faux cell.
3072
+ * @param {Number} col The col for the new faux cell.
3073
+ * @return {Object} Returns the instance of the Gridster class.
3074
+ */
3075
+ fn.add_faux_cell = function(row, col) {
3076
+ var coords = $({
3077
+ left: this.baseX + ((col - 1) * this.min_widget_width),
3078
+ top: this.baseY + (row -1) * this.min_widget_height,
3079
+ width: this.min_widget_width,
3080
+ height: this.min_widget_height,
3081
+ col: col,
3082
+ row: row,
3083
+ original_col: col,
3084
+ original_row: row
3085
+ }).coords();
3086
+
3087
+ if (!$.isArray(this.gridmap[col])) {
3088
+ this.gridmap[col] = [];
3089
+ }
3090
+
3091
+ this.gridmap[col][row] = false;
3092
+ this.faux_grid.push(coords);
3093
+
3094
+ return this;
3095
+ };
3096
+
3097
+
3098
+ /**
3099
+ * Add rows to the faux grid.
3100
+ *
3101
+ * @method add_faux_rows
3102
+ * @param {Number} rows The number of rows you want to add to the faux grid.
3103
+ * @return {Object} Returns the instance of the Gridster class.
3104
+ */
3105
+ fn.add_faux_rows = function(rows) {
3106
+ var actual_rows = this.rows;
3107
+ var max_rows = actual_rows + (rows || 1);
3108
+
3109
+ for (var r = max_rows; r > actual_rows; r--) {
3110
+ for (var c = this.cols; c >= 1; c--) {
3111
+ this.add_faux_cell(r, c);
3112
+ }
3113
+ }
3114
+
3115
+ this.rows = max_rows;
3116
+
3117
+ if (this.options.autogenerate_stylesheet) {
3118
+ this.generate_stylesheet();
3119
+ }
3120
+
3121
+ return this;
3122
+ };
3123
+
3124
+ /**
3125
+ * Add cols to the faux grid.
3126
+ *
3127
+ * @method add_faux_cols
3128
+ * @param {Number} cols The number of cols you want to add to the faux grid.
3129
+ * @return {Object} Returns the instance of the Gridster class.
3130
+ */
3131
+ fn.add_faux_cols = function(cols) {
3132
+ var actual_cols = this.cols;
3133
+ var max_cols = actual_cols + (cols || 1);
3134
+
3135
+ for (var c = actual_cols; c < max_cols; c++) {
3136
+ for (var r = this.rows; r >= 1; r--) {
3137
+ this.add_faux_cell(r, c);
3138
+ }
3139
+ }
3140
+
3141
+ this.cols = max_cols;
3142
+
3143
+ if (this.options.autogenerate_stylesheet) {
3144
+ this.generate_stylesheet();
3145
+ }
3146
+
3147
+ return this;
3148
+ };
3149
+
3150
+
3151
+ /**
3152
+ * Recalculates the offsets for the faux grid. You need to use it when
3153
+ * the browser is resized.
3154
+ *
3155
+ * @method recalculate_faux_grid
3156
+ * @return {Object} Returns the instance of the Gridster class.
3157
+ */
3158
+ fn.recalculate_faux_grid = function() {
3159
+ var aw = this.$wrapper.width();
3160
+ this.baseX = ($(window).width() - aw) / 2;
3161
+ this.baseY = this.$wrapper.offset().top;
3162
+
3163
+ $.each(this.faux_grid, $.proxy(function(i, coords) {
3164
+ this.faux_grid[i] = coords.update({
3165
+ left: this.baseX + (coords.data.col -1) * this.min_widget_width,
3166
+ top: this.baseY + (coords.data.row -1) * this.min_widget_height
3167
+ });
3168
+
3169
+ }, this));
3170
+
3171
+ return this;
3172
+ };
3173
+
3174
+
3175
+ /**
3176
+ * Get all widgets in the DOM and register them.
3177
+ *
3178
+ * @method get_widgets_from_DOM
3179
+ * @return {Object} Returns the instance of the Gridster class.
3180
+ */
3181
+ fn.get_widgets_from_DOM = function() {
3182
+ this.$widgets.each($.proxy(function(i, widget) {
3183
+ this.register_widget($(widget));
3184
+ }, this));
3185
+ return this;
3186
+ };
3187
+
3188
+
3189
+ /**
3190
+ * Calculate columns and rows to be set based on the configuration
3191
+ * parameters, grid dimensions, etc ...
3192
+ *
3193
+ * @method generate_grid_and_stylesheet
3194
+ * @return {Object} Returns the instance of the Gridster class.
3195
+ */
3196
+ fn.generate_grid_and_stylesheet = function() {
3197
+ var aw = this.$wrapper.width();
3198
+ var ah = this.$wrapper.height();
3199
+
3200
+ var cols = Math.floor(aw / this.min_widget_width) +
3201
+ this.options.extra_cols;
3202
+
3203
+ var actual_cols = this.$widgets.map(function() {
3204
+ return $(this).attr('data-col');
3205
+ });
3206
+ actual_cols = Array.prototype.slice.call(actual_cols, 0);
3207
+ //needed to pass tests with phantomjs
3208
+ actual_cols.length || (actual_cols = [0]);
3209
+
3210
+ var min_cols = Math.max.apply(Math, actual_cols);
3211
+
3212
+ // get all rows that could be occupied by the current widgets
3213
+ var max_rows = this.options.extra_rows;
3214
+ this.$widgets.each(function(i, w) {
3215
+ max_rows += (+$(w).attr('data-sizey'));
3216
+ });
3217
+
3218
+ this.cols = Math.max(min_cols, cols, this.options.min_cols);
3219
+ this.rows = Math.max(max_rows, this.options.min_rows);
3220
+
3221
+ this.baseX = ($(window).width() - aw) / 2;
3222
+ this.baseY = this.$wrapper.offset().top;
3223
+
3224
+ if (this.options.autogenerate_stylesheet) {
3225
+ this.generate_stylesheet();
3226
+ }
3227
+
3228
+ return this.generate_faux_grid(this.rows, this.cols);
3229
+ };
3230
+
3231
+ /**
3232
+ * Destroy this gridster by removing any sign of its presence, making it easy to avoid memory leaks
3233
+ *
3234
+ * @method destroy
3235
+ * @return {undefined}
3236
+ */
3237
+ fn.destroy = function(){
3238
+ // remove bound callback on window resize
3239
+ $(window).unbind('resize', this.on_window_resize);
3240
+
3241
+ if(this.drag_api){
3242
+ this.drag_api.destroy();
3243
+ }
3244
+
3245
+ // lastly, remove gridster element
3246
+ // this will additionally cause any data associated to this element to be removed, including this
3247
+ // very gridster instance
3248
+ this.$el.remove();
3249
+ };
3250
+
3251
+
3252
+ //jQuery adapter
3253
+ $.fn.gridster = function(options) {
3254
+ return this.each(function() {
3255
+ if (!$(this).data('gridster')) {
3256
+ $(this).data('gridster', new Gridster( this, options ));
3257
+ }
3258
+ });
3259
+ };
3260
+
3261
+ $.Gridster = fn;
3262
+
3263
+ }(jQuery, window, document));