forked from krig/eventcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventcontrol.min.js
1 lines (1 loc) · 7.15 KB
/
eventcontrol.min.js
1
!function(a){function b(a,b,c){var d=b-b%a,e=b+c;a>9e5&&(d-=36e5);for(var f=[d];e>d;)d+=a,d>=b&&e>=d&&f.push(d);return f.length>20&&(f=f.slice(0,20)),f}var c=1e4,d=31536e8,e=[126144e6,31536e6,10368e6,36288e5,24192e5,18144e5,12096e5,864e6],f=[31536e6,10368e6,26784e5,18144e5,12096e5,6048e5,3456e5,1728e5],g=[2592e5,1728e5,864e5,432e5,216e5,108e5,36e5,27e5,18e5,12e5,6e5,3e5,18e4,6e4,45e3,2e4,12e3,0],h=[432e5,216e5,144e5,108e5,36e5,18e5,9e5,3e5,24e4,18e4,12e4,6e4,3e4,15e3,1e4,5e3,2e3,1e3],i=function(b,c){return this.settings=a.extend({onhover:function(a,b,c,d){},onclick:function(a,b,c){},oncreate:function(a,b){},data:[],hammertime:!1,items_height:101,markers_height:31},c),this.element=b,this.width=b.width(),this.items_h=this.settings.items_height,this.markers_h=this.settings.markers_height,this._dragging=null,this._drag_x=0,b.addClass("eventcontrol"),b.append(['<div class="ec-items ec-draggable" style="top:0px;height:',this.items_h,'px;"></div>','<div class="ec-markers ec-draggable" style="top:',this.items_h+1,"px;height:",this.markers_h,'px;">','<div class="ec-ticks"></div>','<div class="ec-labels"></div>',"</div>"].join("")),this.items=b.children(".ec-items"),this.markers=b.children(".ec-markers"),this.ticks=this.markers.children(".ec-ticks"),this.labels=this.markers.children(".ec-labels"),this.min_time=moment("2070-01-01"),this.max_time=moment("1970-01-01"),this.timespan=d,this.max_timespan=d,this.center_time=this.min_time.valueOf()+.5*d,this.init(),this};i.prototype.init=function(){function d(){c.children(".ec-draggable").removeClass("ec-dragging"),b._dragging=null}function e(a,c,d){a>.9?a=.9:-.9>a&&(a=-.9);var e=a*b.timespan,f=moment(c+e),g=moment(d+e);(!f.isSame(b.min_time)||g.isSame(b.max_time))&&b.update_timespan(f,g)}var b=this,c=this.element;b.settings.hammertime?(b.mc=new Hammer.Manager(b.element.get()[0]),b.mc.add(new Hammer.Pan),b.mc.add(new Hammer.Tap({event:"doubletap",taps:2})),b.mc.add(new Hammer.Tap({event:"singletap"})),b.mc.get("doubletap").recognizeWith("singletap"),b.mc.get("singletap").requireFailure("doubletap"),b.mc.on("panstart panleft panright singletap doubletap tap",function(d){if("singletap"==d.type){var f=a(d.target);f.hasClass("ec-dot")&&b.settings.onclick.call(b,f.data("event"),f,d)}else if("panstart"==d.type)b._pan_min_time=b.min_time.valueOf(),b._pan_max_time=b.max_time.valueOf();else if("panleft"==d.type||"panright"==d.type){var g=-d.deltaX,h=g/b.width;e(h,b._pan_min_time,b._pan_max_time)}else if("doubletap"==d.type){var i=c.offset(),j=1,k=(d.center.x-i.left)/b.width;b.zoom(j,k)}else console.log("Unexpected hammer event",d.type)})):(c.on("click",function(c){var d=a(c.target);d.hasClass("ec-dot")&&b.settings.onclick.call(b,d.data("event"),d,c)}),c.mousedown(function(a){return 1==a.which?(c.children(".ec-draggable").addClass("ec-dragging"),b._dragging=!0,b._drag_x=a.pageX,b._drag_min_time=b.min_time.valueOf(),b._drag_max_time=b.max_time.valueOf(),!1):void 0}),a("body").mouseup(function(a){1==a.which&&d()}),a("body").on("dragend",function(){d()}),a("body").mousemove(function(a){if(1==a.which&&b._dragging){var c=-(a.pageX-b._drag_x),d=c/b.width;e(d,b._drag_min_time,b._drag_max_time)}})),a(window).resize(function(){b._dirty||b.min_time&&b.max_time&&(b._dirty=!0,window.setTimeout(function(){var a=b.min_time.clone(),c=b.max_time.clone();b.update_timespan(a,c)},400))}),c.on("mousewheel",function(a){a.preventDefault();var d=a.deltaY,e=c.offset(),f=(a.pageX-e.left)/b.width;b.zoom(d,f)}),a.each(b.settings.data,function(a,c){b.items.append('<div class="ec-dot" style="left:0px;top:0px;"></div>');var d=b.items.children(".ec-dot:last-child");d.data("event",c),c._starttime=moment(c.timestamp).valueOf(),b.settings.oncreate.call(b,c,d),d.hover(function(a){b.settings.onhover.call(b,c,d,a,"in")},function(a){b.settings.onhover.call(b,c,d,a,"out")});var e=moment(c.timestamp);e<b.min_time&&(b.min_time=e.clone()),e>b.max_time&&(b.max_time=e)}),b.min_time.subtract(5,"s"),b.max_time.add(5,"s"),b.center_time=b.min_time.valueOf()+.5*(b.max_time.valueOf()-b.min_time.valueOf()),b.update_timespan(b.min_time.clone(),b.max_time.clone())},i.prototype.save_state=function(){return{min_time:this.min_time.valueOf(),max_time:this.max_time.valueOf()}},i.prototype.load_state=function(a){this.update_timespan(a.min_time,a.max_time)},i.prototype.zoom=function(a,b){void 0===b&&(b=.5);var e,c=this.min_time.clone(),d=this.max_time.clone();return 0>a?(e=.5*this.timespan,c.subtract(e*b,"ms"),d.add(e*(1-b),"ms")):(e=.25*this.timespan,c.add(e*b,"ms"),d.subtract(e*(1-b),"ms")),this.update_timespan(c,d)},i.prototype.update_timespan=function(i,j){function A(b,c,d,e){if(c>v)if(z<x.length){var f=a(x[z]);f.css("left",c).css("top",d).text(e).addClass(b).removeClass("ec-label"==b?"ec-region-label":"ec-label"),v=c+f.width(),z+=1}else k.labels.append(['<div class="',b,'" style="left:',c,"px;top:",d,'px;">',e,"</div>"].join("")),v=c+k.labels.children("."+b+":last-child").width()}function B(b,c,d){if(y<w.length){var e=a(w[y]);e.css("left",b).css("top",c).css("height",d),y+=1}else k.ticks.append(['<div class="ec-tick" style="left:',b,"px;top:",c,"px;height:",d,'px;"></div>'].join(""))}var k=this,l=this.element,m=0;if(k._dirty=!1,k.width=l.width(),moment.isMoment(i)||(i=moment(i)),moment.isMoment(j)||(j=moment(j)),k.timespan=j.valueOf()-i.valueOf(),k.timespan<c){var n=k.min_time.valueOf()+.5*(k.max_time.valueOf()-k.min_time.valueOf());i=moment(n-.5*c),j=moment(n+.5*c),k.timespan=j.valueOf()-i.valueOf()}k.max_timespan==d&&(k.max_timespan=2*k.timespan),k.timespan>k.max_timespan&&(i=moment(k.center_time-.5*k.max_timespan),j=moment(k.center_time+.5*k.max_timespan),k.timespan=k.max_time.valueOf()-k.min_time.valueOf()),k.min_time=i,k.max_time=j;var p,q,o=k.min_time.valueOf(),r="YYYY-MM-DD",s="HH:mm",t=864e5,u=null;if(k.timespan>=5184e5){for(u=null,k.timespan>126144e6?r="YYYY":k.timespan>10368e6&&(r="YYYY-MM"),m=0;m<e.length;m++)if(k.timespan>e[m]){t=f[m];break}}else{for(m=0;m<g.length;m++)if(k.timespan>g[m]){u=h[m];break}6e4>u&&(s="HH:mm:ss")}p=b(t,o,k.timespan);var D,E,v=-1,w=k.ticks.children(".ec-tick"),x=k.labels.children(".ec-label,.ec-region-label"),y=0,z=0,C=k.width/k.timespan;if(null!==u)for(q=b(u,o,k.timespan),m=0;m<q.length;m++)D=q[m],E=C*(D-o),B(E,1,k.items_h+1+k.markers_h),A("ec-label",E+1,k.items_h+1,moment(D).format(s));else for(m=0;m<p.length;m++)B(C*(p[m]-o),1,.5*k.items_h);for(v=-1,m=0;m<p.length;m++){D=p[m];var F=C*(D-o);if(2>F)if(m+1<p.length){var G=C*(p[m+1]-o);G>60&&(F=2)}else F=2;A("ec-region-label",F+1,k.items_h+k.markers_h-14,moment(D).format(r))}for(m=y;m<w.length;m++)a(w[m]).remove();for(m=z;m<x.length;m++)a(x[m]).remove();var H=2,I=-100,J=H,K=8,L=K+H,M=k.items.children(".ec-dot");for(C=(k.width-2*H)/k.timespan,m=0;m<M.length;m++){var N=a(M[m]),O=N.data("event"),P=O._starttime,Q=Math.floor(H+C*(P-o)),R=Q%L;Q-=R;var S=H,T=!1;E=I,K>=Q+R-I?(T=!0,Q=E,S=J+L,S>k.items_h-H&&(E+=L,Q=E,S=H)):J=H,T||(Q+=R),I=Q,J=S,N.css("left",Q).css("top",S)}},a.fn.EventControl=function(b){var c=Array.prototype.slice(arguments,1);return this.each(function(){var d=a(this),e=d.data("eventcontrol");if(e){if(void 0===b)return e.save_state();if("zoom-in"==b)return e.zoom.apply(e,[1].concat(c));if("zoom-out"==b)return e.zoom.apply(e,[-1].concat(c));e.load_state(b)}else e=new i(d,b),d.data("eventcontrol",e);return e})}}(jQuery);