forked from perifer/timePicker
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Proto.TimePicker.min.js
30 lines (29 loc) · 4.24 KB
/
Proto.TimePicker.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
* A time picker for Prototype.js
*
*
* Dual licensed under the MIT and GPL licenses (unfortunately).
* Copyright (c) 2012 Jarvis Badgley
* @name Proto.TimePicker
* @author Jarvis Badgley (http://chipersoft.com)
* @example new Proto.TimePicker('mytime')
* @example new Proto.TimePicker('mytime', {step:30, startTime:"15:00", endTime:"18:00"});
*
* Ported from jquery.timePicker by Anders Fajerson (https://github.com/perifer/timePicker/network)
*
* Options:
* step: # of minutes to step the time by
* startTime: beginning of the range of acceptable times
* endTime: end of the range of acceptable times
* defaultTime: if the user has not yet selected a time, select this by default
* separator: separator string to use between hours and minutes (e.g. ':')
* show24Hours: use a 24-hour scheme
* leadingZero: append a leading 0 to hours less than 10.
*/
(function(){function r(a,c,b){a.value=$(c).getAttribute("data-time");a.fire("time:change");window.Prototype.Browser.IE||a.focus();b.hide()}function o(a,c){var b=a.getHours(),f=c.show24Hours?b:(b+11)%12+1,j=a.getMinutes();return(c.leadingZero?(f<10?"0":"")+f:f)+c.separator+((j<10?"0":"")+j)+(c.show24Hours?"":b<12?" AM":" PM")}function m(a,c){return typeof a=="object"?p(a):q(a,c)}function q(a,c){if(a){var a=a.toUpperCase(),b=a.split(c.separator),f=parseFloat(b[0]),b=parseFloat(b[1]);c.show24Hours||
(f===12&&a.indexOf("AM")!==-1?f=0:f!==12&&a.indexOf("PM")!==-1&&(f+=12));return p(new Date(0,0,0,f,b,0))}return null}function p(a){a.setFullYear(2001);a.setMonth(0);a.setDate(0);return a}window.Proto=window.Proto||{};window.Proto.TimePicker=Class.create({version:"0.1",initialize:function(a,c){a=$(a);a.timePicker=this;var b={step:30,startTime:new Date(0,0,0,0,0,0),endTime:new Date(0,0,0,23,30,0),defaultTime:null,separator:":",show24Hours:!0,leadingZero:!0};Object.extend(b,c||{});var f=!1,j=!1,k=m(b.startTime,
b),s=m(b.endTime,b),t=b.defaultTime?m(b.defaultTime,b):k;a.setAttribute("autocomplete","OFF");for(var l=[],i=new Date(k);i<=s;)l[l.length]=o(i,b),i=new Date(i.setMinutes(i.getMinutes()+b.step));for(var e=new Element("div",{className:"time-picker"+(b.show24Hours?"":" time-picker-12hours")}),h=new Element("ul"),i=0;i<l.length;i++)h.insert('<li data-time="'+l[i]+'">'+l[i]+"</li>");e.insert(h);document.body.appendChild(e.hide());e.on("mouseover",function(){f=!0});e.on("mouseout",function(){f=!1});h.on("mouseover",
"li",function(a,b){j||(e.select("li.selected").invoke("removeClassName","selected"),b.addClassName("selected"))});h.on("mousedown","li",function(){f=!0});h.on("click","li",function(d,g){r(a,g,e,b);f=!1});var n=function(){if(e.visible())return!1;e.select("li").invoke("removeClassName","selected");var d=a.getLayout();e.setStyle({top:d.get("top")+d.get("border-box-height")+"px",left:d.get("left")+"px",minWidth:d.get("padding-box-width")+"px"});e.show();var g=a.value?q(a.value,b):t,d=k.getHours()*60+
k.getMinutes(),g=g.getHours()*60+g.getMinutes()-d,d=p(new Date(0,0,0,0,Math.round(g/b.step)*b.step+d,0)),d=k<d&&d<=s?d:k;if(d=e.down("li[data-time='"+o(d,b)+"']"))d.addClassName("selected"),e.scrollTop=d.offsetTop;return!0};a.on("focus",n);a.on("click",n);a.on("blur",function(){f||e.hide()});a.on(window.Prototype.Browser.Opera||window.Prototype.Browser.Gecko?"keypress":"keydown",function(d){var g;j=!0;var f=e.scrollTop;switch(d.keyCode){case 38:if(n())return!1;g=h.down("li.selected")||h.childElements().last();
var c=g.previous();if(c){if(c.addClassName("selected"),g.removeClassName("selected"),c.positionedOffset().top<f)e.scrollTop=f-c.getHeight()}else g.removeClassName("selected"),c=h.down("li:last").addClassName("selected")[0],e.scrollTop=c.positionedOffset().top-c.getHeight;d.stop();return!1;case 40:if(n())return!1;g=h.down("li.selected")||h.down();if(c=g.next()){if(c.addClassName("selected"),g.removeClassName("selected"),c.positionedOffset().top+c.getHeight()>f+e.getHeight)e.scrollTop=f+c.getHeight()}else g.removeClassName("selected"),
h.down("li").addClassName("selected"),e.scrollTop=0;d.stop();return!1;case 13:if(e.visible())return g=h.down("li.selected"),r(a,g,e,b),d.stop(),!1;return;case 27:return e.hide(),d.stop(),!1}return!0});a.on("keyup",function(){j=!1});this.getTime=function(){return q(a.value,b)};this.setTime=function(c){a.value=o(m(c,b),b);a.fire("time:change")}}})})();