forked from Stigmatoz/web-activity-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclockpicker.min.js
More file actions
1 lines (1 loc) · 10.6 KB
/
clockpicker.min.js
File metadata and controls
1 lines (1 loc) · 10.6 KB
1
!function(){var P,t,i,e,x=window.jQuery,o=x(window),S=x(document),s="http://www.w3.org/2000/svg",E="SVGAngle"in window&&((i=document.createElement("div")).innerHTML="<svg/>",t=(i.firstChild&&i.firstChild.namespaceURI)==s,i.innerHTML="",t),c="transition"in(e=document.createElement("div").style)||"WebkitTransition"in e||"MozTransition"in e||"msTransition"in e||"OTransition"in e,n="ontouchstart"in window,D="mousedown"+(n?" touchstart":""),I="mousemove.clockpicker"+(n?" touchmove.clockpicker":""),B="mouseup.clockpicker"+(n?" touchend.clockpicker":""),d=navigator.vibrate?"vibrate":navigator.webkitVibrate?"webkitVibrate":null;function z(t){return document.createElementNS(s,t)}function O(t){return(t<10?"0":"")+t}var j=0;var L=100,U=80,W=13,N=c?350:1,X=['<div class="popover clockpicker-popover">','<div class="arrow"></div>','<div class="popover-title">','<span class="clockpicker-span-hours text-primary"></span>'," : ",'<span class="clockpicker-span-minutes"></span>','<span class="clockpicker-span-am-pm"></span>',"</div>",'<div class="popover-content">','<div class="clockpicker-plate">','<div class="clockpicker-canvas"></div>','<div class="clockpicker-dial clockpicker-hours"></div>','<div class="clockpicker-dial clockpicker-minutes clockpicker-dial-out"></div>',"</div>",'<span class="clockpicker-am-pm-block">',"</span>","</div>","</div>"].join("");function a(t,h){var i,e,s=x(X),u=s.find(".clockpicker-plate"),o=s.find(".clockpicker-hours"),c=s.find(".clockpicker-minutes"),n=s.find(".clockpicker-am-pm-block"),a="INPUT"===t.prop("tagName"),r=a?t:t.find("input"),p=t.find(".input-group-addon"),k=this;if(this.id=(e=++j+"",(i="cp")?i+e:e),this.element=t,this.options=h,this.isAppended=!1,this.isShown=!1,this.currentView="hours",this.isInput=a,this.input=r,this.addon=p,this.popover=s,this.plate=u,this.hoursView=o,this.minutesView=c,this.amPmBlock=n,this.spanHours=s.find(".clockpicker-span-hours"),this.spanMinutes=s.find(".clockpicker-span-minutes"),this.spanAmPm=s.find(".clockpicker-span-am-pm"),this.amOrPm="PM",h.twelvehour){var l=['<div class="clockpicker-am-pm-block">','<button type="button" class="btn btn-sm btn-default clockpicker-button clockpicker-am-button">',"AM</button>",'<button type="button" class="btn btn-sm btn-default clockpicker-button clockpicker-pm-button">',"PM</button>","</div>"].join("");x(l);x('<button type="button" class="btn btn-sm btn-default clockpicker-button am-button">AM</button>').on("click",function(){k.amOrPm="AM",x(".clockpicker-span-am-pm").empty().append("AM")}).appendTo(this.amPmBlock),x('<button type="button" class="btn btn-sm btn-default clockpicker-button pm-button">PM</button>').on("click",function(){k.amOrPm="PM",x(".clockpicker-span-am-pm").empty().append("PM")}).appendTo(this.amPmBlock)}h.autoclose||x('<button type="button" class="btn btn-sm btn-default btn-block clockpicker-button">'+h.donetext+"</button>").click(x.proxy(this.done,this)).appendTo(s),"top"!==h.placement&&"bottom"!==h.placement||"top"!==h.align&&"bottom"!==h.align||(h.align="left"),"left"!==h.placement&&"right"!==h.placement||"left"!==h.align&&"right"!==h.align||(h.align="top"),s.addClass(h.placement),s.addClass("clockpicker-align-"+h.align),this.spanHours.click(x.proxy(this.toggleView,this,"hours")),this.spanMinutes.click(x.proxy(this.toggleView,this,"minutes")),r.on("focus.clockpicker click.clockpicker",x.proxy(this.show,this)),p.on("click.clockpicker",x.proxy(this.toggle,this));var d,f,v,m,b=x('<div class="clockpicker-tick"></div>');if(h.twelvehour)for(d=1;d<13;d+=1)f=b.clone(),v=d/6*Math.PI,m=U,f.css("font-size","120%"),f.css({left:L+Math.sin(v)*m-W,top:L-Math.cos(v)*m-W}),f.html(0===d?"00":d),o.append(f),f.on(D,w);else for(d=0;d<24;d+=1){f=b.clone(),v=d/6*Math.PI;var g=0<d&&d<13;m=g?54:U,f.css({left:L+Math.sin(v)*m-W,top:L-Math.cos(v)*m-W}),g&&f.css("font-size","120%"),f.html(0===d?"00":d),o.append(f),f.on(D,w)}for(d=0;d<60;d+=5)f=b.clone(),v=d/30*Math.PI,f.css({left:L+Math.sin(v)*U-W,top:L-Math.cos(v)*U-W}),f.css("font-size","120%"),f.html(O(d)),c.append(f),f.on(D,w);function w(t,o){var i=u.offset(),e=/^touch/.test(t.type),c=i.left+L,n=i.top+L,a=(e?t.originalEvent.touches[0]:t).pageX-c,r=(e?t.originalEvent.touches[0]:t).pageY-n,s=Math.sqrt(a*a+r*r),p=!1;if(!o||!(s<67||93<s)){t.preventDefault();var l=setTimeout(function(){P.addClass("clockpicker-moving")},200);E&&u.append(k.canvas),k.setHand(a,r,!o,!0),S.off(I).on(I,function(t){t.preventDefault();var i=/^touch/.test(t.type),e=(i?t.originalEvent.touches[0]:t).pageX-c,s=(i?t.originalEvent.touches[0]:t).pageY-n;(p||e!==a||s!==r)&&(p=!0,k.setHand(e,s,!1,!0))}),S.off(B).on(B,function(t){S.off(B),t.preventDefault();var i=/^touch/.test(t.type),e=(i?t.originalEvent.changedTouches[0]:t).pageX-c,s=(i?t.originalEvent.changedTouches[0]:t).pageY-n;(o||p)&&e===a&&s===r&&k.setHand(e,s),"hours"===k.currentView?k.toggleView("minutes",N/2):h.autoclose&&(k.minutesView.addClass("clockpicker-dial-out"),setTimeout(function(){k.done()},N/2)),u.prepend(y),clearTimeout(l),P.removeClass("clockpicker-moving"),S.off(I)})}}if(u.on(D,function(t){0===x(t.target).closest(".clockpicker-tick").length&&w(t,!0)}),E){var y=s.find(".clockpicker-canvas"),M=z("svg");M.setAttribute("class","clockpicker-svg"),M.setAttribute("width",200),M.setAttribute("height",200);var A=z("g");A.setAttribute("transform","translate(100,100)");var V=z("circle");V.setAttribute("class","clockpicker-canvas-bearing"),V.setAttribute("cx",0),V.setAttribute("cy",0),V.setAttribute("r",2);var T=z("line");T.setAttribute("x1",0),T.setAttribute("y1",0);var C=z("circle");C.setAttribute("class","clockpicker-canvas-bg"),C.setAttribute("r",W);var H=z("circle");H.setAttribute("class","clockpicker-canvas-fg"),H.setAttribute("r",3.5),A.appendChild(T),A.appendChild(C),A.appendChild(H),A.appendChild(V),M.appendChild(A),y.append(M),this.hand=T,this.bg=C,this.fg=H,this.bearing=V,this.g=A,this.canvas=y}Y(this.options.init)}function Y(t){t&&"function"==typeof t&&t()}a.DEFAULTS={default:"",fromnow:0,placement:"bottom",align:"left",donetext:"完成",autoclose:!1,twelvehour:!1,vibrate:!0},a.prototype.toggle=function(){this[this.isShown?"hide":"show"]()},a.prototype.locate=function(){var t=this.element,i=this.popover,e=t.offset(),s=t.outerWidth(),o=t.outerHeight(),c=this.options.placement,n=this.options.align,a={};switch(i.show(),c){case"bottom":a.top=e.top+o;break;case"right":a.left=e.left+s;break;case"top":a.top=e.top-i.outerHeight();break;case"left":a.left=e.left-i.outerWidth()}switch(n){case"left":a.left=e.left;break;case"right":a.left=e.left+s-i.outerWidth();break;case"top":a.top=e.top;break;case"bottom":a.top=e.top+o-i.outerHeight()}i.css(a)},a.prototype.show=function(t){if(!this.isShown){Y(this.options.beforeShow);var e=this;this.isAppended||(P=x(document.body).append(this.popover),o.on("resize.clockpicker"+this.id,function(){e.isShown&&e.locate()}),this.isAppended=!0);var i=((this.input.prop("value")||this.options.default||"")+"").split(":");if("now"===i[0]){var s=new Date(+new Date+this.options.fromnow);i=[s.getHours(),s.getMinutes()]}this.hours=+i[0]||0,this.minutes=+i[1]||0,this.spanHours.html(O(this.hours)),this.spanMinutes.html(O(this.minutes)),this.toggleView("hours"),this.locate(),this.isShown=!0,S.on("click.clockpicker."+this.id+" focusin.clockpicker."+this.id,function(t){var i=x(t.target);0===i.closest(e.popover).length&&0===i.closest(e.addon).length&&0===i.closest(e.input).length&&e.hide()}),S.on("keyup.clockpicker."+this.id,function(t){27===t.keyCode&&e.hide()}),Y(this.options.afterShow)}},a.prototype.hide=function(){Y(this.options.beforeHide),this.isShown=!1,S.off("click.clockpicker."+this.id+" focusin.clockpicker."+this.id),S.off("keyup.clockpicker."+this.id),this.popover.hide(),Y(this.options.afterHide)},a.prototype.toggleView=function(t,i){var e=!1;"minutes"===t&&"visible"===x(this.hoursView).css("visibility")&&(Y(this.options.beforeHourSelect),e=!0);var s="hours"===t,o=s?this.hoursView:this.minutesView,c=s?this.minutesView:this.hoursView;this.currentView=t,this.spanHours.toggleClass("text-primary",s),this.spanMinutes.toggleClass("text-primary",!s),c.addClass("clockpicker-dial-out"),o.css("visibility","visible").removeClass("clockpicker-dial-out"),this.resetClock(i),clearTimeout(this.toggleViewTimer),this.toggleViewTimer=setTimeout(function(){c.css("visibility","hidden")},N),e&&Y(this.options.afterHourSelect)},a.prototype.resetClock=function(t){var i=this.currentView,e=this[i],s="hours"===i,o=e*(Math.PI/(s?6:30)),c=s&&0<e&&e<13?54:U,n=Math.sin(o)*c,a=-Math.cos(o)*c,r=this;E&&t?(r.canvas.addClass("clockpicker-canvas-out"),setTimeout(function(){r.canvas.removeClass("clockpicker-canvas-out"),r.setHand(n,a)},t)):this.setHand(n,a)},a.prototype.setHand=function(t,i,e,s){var o,c=Math.atan2(t,-i),n="hours"===this.currentView,a=Math.PI/(n||e?6:30),r=Math.sqrt(t*t+i*i),p=this.options,l=n&&r<67,h=l?54:U;if(p.twelvehour&&(h=U),c<0&&(c=2*Math.PI+c),c=(o=Math.round(c/a))*a,p.twelvehour?n?0===o&&(o=12):(e&&(o*=5),60===o&&(o=0)):n?(12===o&&(o=0),o=l?0===o?12:o:0===o?0:o+12):(e&&(o*=5),60===o&&(o=0)),this[this.currentView]!==o&&d&&this.options.vibrate&&(this.vibrateTimer||(navigator[d](10),this.vibrateTimer=setTimeout(x.proxy(function(){this.vibrateTimer=null},this),100))),this[this.currentView]=o,this[n?"spanHours":"spanMinutes"].html(O(o)),E){s||!n&&o%5?(this.g.insertBefore(this.hand,this.bearing),this.g.insertBefore(this.bg,this.fg),this.bg.setAttribute("class","clockpicker-canvas-bg clockpicker-canvas-bg-trans")):(this.g.insertBefore(this.hand,this.bg),this.g.insertBefore(this.fg,this.bg),this.bg.setAttribute("class","clockpicker-canvas-bg"));var u=Math.sin(c)*h,k=-Math.cos(c)*h;this.hand.setAttribute("x2",u),this.hand.setAttribute("y2",k),this.bg.setAttribute("cx",u),this.bg.setAttribute("cy",k),this.fg.setAttribute("cx",u),this.fg.setAttribute("cy",k)}else this[n?"hoursView":"minutesView"].find(".clockpicker-tick").each(function(){var t=x(this);t.toggleClass("active",o===+t.html())})},a.prototype.done=function(){Y(this.options.beforeDone),this.hide();var t=this.input.prop("value"),i=O(this.hours)+":"+O(this.minutes);this.options.twelvehour&&(i+=this.amOrPm),this.input.prop("value",i),i!==t&&(this.input.triggerHandler("change"),this.isInput||this.element.trigger("change")),this.options.autoclose&&this.input.trigger("blur"),Y(this.options.afterDone)},a.prototype.remove=function(){this.element.removeData("clockpicker"),this.input.off("focus.clockpicker click.clockpicker"),this.addon.off("click.clockpicker"),this.isShown&&this.hide(),this.isAppended&&(o.off("resize.clockpicker"+this.id),this.popover.remove())},x.fn.clockpicker=function(s){var o=Array.prototype.slice.call(arguments,1);return this.each(function(){var t=x(this),i=t.data("clockpicker");if(i)"function"==typeof i[s]&&i[s].apply(i,o);else{var e=x.extend({},a.DEFAULTS,t.data(),"object"==typeof s&&s);t.data("clockpicker",new a(t,e))}})}}();