forked from jonthornton/jquery-timepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.timepicker.min.js
1 lines (1 loc) · 5.55 KB
/
jquery.timepicker.min.js
1
(function(d){var k=new Date().getTimezoneOffset();var c={className:null,minTime:null,step:30,showDuration:false,timeFormat:"g:ia",onSelect:function(){}};var b={init:function(l){return this.each(function(){var p=d(this);if(p[0].tagName=="SELECT"){var o=d("<input />");var q={type:"text",value:p.val()};var m=p[0].attributes;for(var r=0;r<m.length;r++){q[m[r].nodeName]=m[r].nodeValue}o.attr(q);p.replaceWith(o);p=o}var s=d.extend({},c);if(l){s=d.extend(s,l)}if(s.minTime){s.minTime=e(s.minTime)}p.data("settings",s);p.attr("autocomplete","off");p.click(b.show).focus(b.show).keydown(a);p.addClass("ui-timepicker-input");if(p.val()){var t=f(e(p.val()),s.timeFormat);p.val(t)}var n=d('<span class="ui-timepicker-container" />');p.wrap(n);d("body").attr("tabindex",-1).focusin(function(u){if(d(u.target).closest(".ui-timepicker-container").length==0){b.hide()}})})},show:function(p){var l=d(this);var o=l.siblings(".ui-timepicker-list");if(l.hasClass("ui-timepicker-hideme")){l.removeClass("ui-timepicker-hideme");o.hide();return}if(o.is(":visible")){return}b.hide();if(o.length==0){g(l);o=l.siblings(".ui-timepicker-list")}if((l.offset().top+l.outerHeight(true)+o.outerHeight())>d(window).height()+d(window).scrollTop()){o.css({top:l.position().top-o.outerHeight()})}else{o.css({top:l.position().top+l.outerHeight(true)})}o.show();var n=o.find(".ui-timepicker-selected");if(n.length){var m=o.scrollTop()+n.position().top-n.outerHeight();o.scrollTop(m)}},hide:function(l){d(".ui-timepicker-list:visible").each(function(){var n=d(this);var m=n.siblings(".ui-timepicker-input");i(m);n.hide()})},option:function(m,p){var l=d(this);var n=l.data("settings");var o=l.siblings(".ui-timepicker-list");if(typeof m=="object"){n=d.extend(n,m)}else{if(typeof m=="string"&&typeof p!="undefined"){n[m]=p}else{if(typeof m=="string"){return n[m]}}}if(n.minTime){n.minTime=e(n.minTime)}l.data("settings",n);o.remove()},getSecondsFromMidnight:function(){return e(d(this).val())},setTime:function(m){var l=d(this);var n=f(e(m),l.data("settings").timeFormat);l.val(n)}};function g(s){var m=s.data("settings");var p=s.siblings(".ui-timepicker-list");if(p&&p.length){p.remove()}p=d("<ul />");p.attr("tabindex",-1);p.addClass("ui-timepicker-list");if(m.className){p.addClass(m.className)}var q=s.css("zIndex");q=(q+0==q)?q+1:2;p.css({display:"none",position:"absolute",left:(s.position().left),zIndex:q});if(m.minTime!==null&&m.showDuration){p.addClass("ui-timepicker-with-duration")}var l=(m.minTime!==null)?m.minTime:0;for(var o=l;o<l+86400;o+=m.step*60){var r=o%86400;var t=d("<li />");t.data("time",r);t.text(f(r,m.timeFormat));if(m.minTime!==null&&m.showDuration){var n=d("<span />");n.addClass("ui-timepicker-duration");n.text(" ("+h(o-m.minTime)+")");t.append(n)}p.append(t)}s.after(p);j(s,p);p.delegate("li","click",{timepicker:s},function(u){s.addClass("ui-timepicker-hideme");s[0].focus();p.find("li").removeClass("ui-timepicker-selected");d(this).addClass("ui-timepicker-selected");i(s)})}function j(l,n){var m=e(l.val());n.find("li").each(function(o,q){var p=d(q);if(p.data("time")===m){p.addClass("ui-timepicker-selected");return false}})}function a(o){var l=d(this);var n=l.siblings(".ui-timepicker-list");switch(o.keyCode){case 13:i(l);b.hide.apply(this);o.preventDefault();return false;break;case 38:var m=n.find(".ui-timepicker-selected");if(m.length&&!m.is(":first-child")){m.removeClass("ui-timepicker-selected");m.prev().addClass("ui-timepicker-selected");if(m.prev().position().top<m.outerHeight()){n.scrollTop(n.scrollTop()-m.outerHeight())}}break;case 40:if(!n.is(":visible")){l.focus()}var m=n.find(".ui-timepicker-selected");if(m.length==0){n.children().first().addClass("ui-timepicker-selected")}else{if(!m.is(":last-child")){m.removeClass("ui-timepicker-selected");m.next().addClass("ui-timepicker-selected");if(m.next().position().top+2*m.outerHeight()>n.outerHeight()){n.scrollTop(n.scrollTop()+m.outerHeight())}else{}}}break;case 27:n.find("li").removeClass("ui-timepicker-selected");n.hide();break;case 9:case 16:case 17:case 18:case 19:case 20:case 33:case 34:case 35:case 36:case 37:case 39:case 45:return;default:n.find("li").removeClass("ui-timepicker-selected");return}}function i(l){var o=l.data("settings");var p=l.siblings(".ui-timepicker-list");var n=null;var q=p.find(".ui-timepicker-selected");if(q.length){var n=q.data("time")}else{if(l.val()){var n=e(l.val());j(l,p)}}if(n!==null){var m=f(n,o.timeFormat);l.attr("value",m)}o.onSelect.call(l)}function h(n){var m=Math.round(n/60);if(m<60){return m+" mins"}else{if(m==60){return"1 hr"}else{var l=m/60;return l.toFixed(1)+" hrs"}}}function f(s,r){var q=new Date((s+k*60)*1000);var m="";for(var o=0;o<r.length;o++){var p=r.charAt(o);switch(p){case"a":m+=(q.getHours()>11)?"pm":"am";break;case"A":m+=(q.getHours()>11)?"PM":"AM";break;case"g":var l=q.getHours()%12;m+=(l==0)?"12":l;break;case"G":m+=q.getHours();break;case"h":var l=q.getHours()%12;if(l!=0&&l<10){l="0"+l}m+=(l==0)?"12":l;break;case"H":var l=q.getHours();m+=(l>9)?l:"0"+l;break;case"i":var n=q.getMinutes();m+=(n>9)?n:"0"+n;break;case"s":var s=q.getSeconds();m+=(s>9)?s:"0"+s;break;default:m+=p}}return m}function e(o){if(o==""){return null}if(o+0==o){return o}var q=new Date(0);var p=o.toLowerCase().match(/(\d+)(?::(\d\d))?\s*([pa]?)/);if(!p){return null}var m=parseInt(p[1]*1);if(p[3]){if(m==12){var l=(p[3]=="p")?12:0}else{var l=(m+(p[3]=="p"?12:0))}}else{var l=m}var n=(p[2]*1||0);return l*3600+n*60}d.fn.timepicker=function(l){if(b[l]){return b[l].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof l==="object"||!l){return b.init.apply(this,arguments)}else{d.error("Method "+l+" does not exist on jQuery.timepicker")}}}})(jQuery);