forked from 7shifts/jQueryTimeAutocomplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.timeAutocomplete.min.js
1 lines (1 loc) · 11.8 KB
/
jquery.timeAutocomplete.min.js
1
(function(t){var e="timeAutocomplete",i=e+".time",o=function(){this.initialize.apply(this,arguments)};o.prototype={el:null,_formatter:null,_calling_from_init:!1,default_opts:{auto_complete:{delay:0,autoFocus:!0,minLength:0},auto_value:!0,value:"",formatter:"ampm"},initialize:function(e,i){this.options=t.extend(!0,{},this.default_opts,i),t.timeAutocompleteDefaults!==void 0&&(this.options=t.extend(!0,{},this.options,t.timeAutocompleteDefaults)),this.el=e},_callAutocomplete:function(){if(this.options.auto_complete.source=this._callFormatterMethod("filterSource",[this.el],function(){throw Error("You must set a hook_filterSource method in your formatter.")}),t.fn.autocomplete===void 0)throw Error("You need to include the jQuery UI bundle that has the Autocomplete plugin.");this.el.autocomplete(this.options.auto_complete)},_bindEvents:function(){var i=this,o=!0;t("body").on("click."+e,"ul.ui-autocomplete a",function(){o=!1,setTimeout(function(){o=!0},100)}),this.el.bind("keydown."+e,function(){i._keydownAutocomplete.apply(i,arguments)}).bind("keyup."+e,function(){i._keyupAutocomplete.apply(i,arguments)}).bind("blur."+e,function(){i._blurAutocomplete.apply(i,arguments)}).bind("focus."+e,function(){o&&i._focusAutocomplete.apply(i,arguments)}),this.options.auto_value&&this.el.trigger("blur."+e)},_setupPlaceholder:function(){this.el.attr("placeholder")===void 0&&this.el.attr("placeholder",this._callFormatterMethod("placeholderValue",[],""))},_focusAutocomplete:function(){var e=t.trim(this.el.val()).substr(0,2);this.el.data("uiAutocomplete")&&this.el.autocomplete("search",e)},_keydownAutocomplete:function(e){var i=t.trim(this.el.val()),o=[13,9,38,40];if(!~t.inArray(e.which,o)&&(8==e.which||i.length>1&&!~i.indexOf("h")&&!~i.indexOf(":")&&t.isNumeric(i)))try{this.el.autocomplete("close").autocomplete("disable")}catch(e){}},_keyupAutocomplete:function(){""==t.trim(this.el.val())&&this.el.data("uiAutocomplete")&&this.el.autocomplete("enable")},_blurAutocomplete:function(){var e=t.trim(this.el.val());e=this._callFormatterMethod("blur",[e],e);var i="";i=e?this._createStringFromFormat(this._readMind(e)):this._callFormatterMethod("blurEmpty",[e],e),this.el.val(i),this._attacheUsableTimeData()},_attacheUsableTimeData:function(){var e=t.trim(this.el.val());this.el.data(i,this._callFormatterMethod("getUsableTimeValue",[e]))},setFormatter:function(e){if(this.options.formatter=e||this.options.formatter,!t.timeAutocomplete.formatters[this.options.formatter])throw Error("Formatter: '"+e+"' was not found. Make sure you're loading it (formatters/"+this.options.formatter+".js) after you load src/TimeAutocomplete.js");this._formatter=new t.timeAutocomplete.formatters[this.options.formatter](this,this.options),this._calling_from_init||this._callAutocomplete(),this._calling_from_init=!1},getFormatter:function(){return this._formatter},getTime:function(){return this.el.data(i)||""},_callFormatterMethod:function(e,i,o){var s=this.getFormatter();return t.isFunction(s["hook_"+e])?s["hook_"+e].apply(s,i):o},_readMind:function(t){return this._callFormatterMethod("readMind",[t],t)},_createStringFromFormat:function(t){var e=""+t.h+t.sep+(""+t.m);return t.postfix&&(e+=t.postfix),e},_setValueAsTime:function(){var e=t.trim(this.el.val()),i=e.split(":");if(""==e&&this.options.value)this.setTime(this.options.value);else if(3==i.length&&this.isNumber(i[0])&&this.isNumber(i[1])&&this.isNumber(i[2]))this.setTime(e);else{var o=this._getCurrentTimeAsValue();this.el.val(o),this._attacheUsableTimeData()}},isNumber:function(t){return!isNaN(parseFloat(t))&&isFinite(t)},setTime:function(t){var e=t.replace(/[^0-9.]/g,""),i=e.match(/^[0-9]+$/);if(!i||!i.length||5!=i[0].length&&6!=i[0].length)throw Error("Setting a time must be in H:i:s format. Example: 03:30:00");var o=this._callFormatterMethod("getTimeObjectFromHis",[t]);o=this._createStringFromFormat(o),this.el.val(o),this._attacheUsableTimeData()},_getCurrentTimeAsValue:function(){for(var e=this.getFormatter(),i=[1987,1,17],o=this._getCurrentDate(),s=o.getHours(),n=o.getMinutes(),r=new Date(i[0],i[1],i[2],s,n).getTime(),a=e.options.times.slice().concat(e.options.times),p=[],m=0,h=a.length;h>m;m++){var l=this._callFormatterMethod("getTime",[a[m],i]),u=a[m+1]?this._callFormatterMethod("getTime",[a[m+1],i]):!1,c=!(-1===t.inArray(u,p));if(p.push(u),r>l&&(u&&u>=r||c))return a[m+1]}},_getCurrentDate:function(){return new Date},destroy:function(){this.el.removeData(e),this.el.removeData(i),this.el.unbind("."+e),this.el.data("uiAutocomplete")&&this.el.autocomplete("destroy")},render:function(){return this._calling_from_init=!0,this.setFormatter(),this._callAutocomplete(),this.options.auto_value&&this._setValueAsTime(),this._bindEvents(),this._setupPlaceholder(),this}},t.timeAutocomplete={formatters:{},_raw:o},t.fn.timeAutocomplete=function(i){return this.each(function(){var s=t(this);s.data(e)&&s.data(e).destroy();var n=new o(s,i).render();s.data(e,n)})}})(jQuery),function(t){t.timeAutocomplete.formatters.ampm=function(){this.initialize.apply(this,arguments)},t.timeAutocomplete.formatters.ampm.prototype={main_instance:null,options:{},default_opts:{from_selector:"",increment:15,start_hour:0,end_hour:24,pm_text:"PM",am_text:"AM",blur_empty_populate:!0,times:[],empty:{h:"12",m:"00",sep:":",postfix:" PM"}},initialize:function(e,i){this.main_instance=e,this.options=t.extend(!0,{},this.default_opts,i),this.generateTimes()},hook_placeholderValue:function(){return this.main_instance._createStringFromFormat(this.options.empty)},hook_filterSource:function(e){var i=this;return e=e[0],function(i,o){return function(s,n){var r=t.trim(e.value),a=t.ui.autocomplete.escapeRegex(s.term),p=~a.toLowerCase().indexOf("a"),m=~a.toLowerCase().indexOf("p"),h="",l=!(1!=r),u=(p||m)&&2>=a.replace(/a|m|p/gi,"").length;u&&(a=t.trim(a.replace(/a|m|p/gi,""))+":00 ",a+=p?o.options.am_text:o.options.pm_text),o.options.from_selector&&(h=o.detectAMPMFromInstanceOverlap()==o.options.am_text?o.options.pm_text:o.options.am_text);var c=RegExp("^"+a,"i"),f=[];r&&(f=t.grep(i,function(t){var e=h&&RegExp(h,"gi").test(t)||l&&"1:"!=t.substring(0,2)||~r.toLowerCase().indexOf("p")&&!~t.toLowerCase().indexOf("p")||~r.toLowerCase().indexOf("a")&&!~t.toLowerCase().indexOf("a");if(!e)return c.test(t)})),n(f)}}(i.options.times,i)},hook_blur:function(t){return 0==t.charAt(0)&&(t=t.substr(1)),t},hook_blurEmpty:function(){return this.options.blur_empty_populate?this.main_instance._createStringFromFormat(this.options.empty):""},hook_readMind:function(t){var e="";return t=t.toLowerCase(),!this.options.from_selector||~t.indexOf("a")||~t.indexOf("p")||(e=this.detectAMPMFromInstanceOverlap()),this.getTimeObject(t,e)},hook_getUsableTimeValue:function(t){return this.parseTime(t)},hook_getTime:function(t,e){var i=this.parseTime(t).split(this.options.empty.sep),o=i[0],s=i[1];return new Date(e[0],e[1],e[2],o,s).getTime()},hook_getTimeObjectFromHis:function(t){var e=t.split(":"),i=e[0],o=e[1],s=i>=12?this.options.pm_text:this.options.am_text;2==i.length&&10>parseInt(i,10)&&(i=i.substr(1)),i>12&&(i-=12),0==i&&(i=12);var n={h:parseInt(i,10),m:o,sep:this.options.empty.sep,postfix:" "+s};return n},detectAMPMFromInstanceOverlap:function(){var e="",i=this.getTimeObject(t(this.options.from_selector).val()),o=this.getTimeObject(t.trim(this.main_instance.el.val()));if(i.postfix&&(~i.postfix.toLowerCase().indexOf("a")||~i.postfix.toLowerCase().indexOf("p"))){var s=~i.postfix.toLowerCase().indexOf("a")?this.options.am_text:this.options.pm_text,n=o.h,r=i.h;e=12==r&&12!=n?s==this.options.am_text?this.options.am_text:this.options.pm_text:12==n&&12!=r?s==this.options.am_text?this.options.pm_text:this.options.am_text:r>n?s==this.options.am_text?this.options.pm_text:this.options.am_text:s==this.options.am_text?this.options.am_text:this.options.pm_text}return e},getTimeObject:function(t,e){var i,o=this.parseTime(t,"g:i:A").split(":"),s=o[0],n=o[1];return i=s||n?{h:s,m:n,sep:":",postfix:" "+(e?e:o[2])}:this.options.empty},generateTimes:function(){if(!this.options.times.length){var t=60,e=this.options.increment,i=new Date(2012,1,1,this.options.start_hour-1,t-e),o=[],s=60,n=this.options.end_hour,r=n-this.options.start_hour,a=!1;24==r&&(a=!0);for(var p=0,m=r*(s/e);m>=p;p++){i.setMinutes(i.getMinutes()+e);var h=i.getHours(),l=i.getMinutes(),u=h>11?this.options.pm_text:this.options.am_text;0==h&&(h=12),h>12&&(h-=12),1==(""+l).length&&(l="0"+l);var c=h+":"+l+" "+u;o.push(c)}a&&o.pop(),this.options.times=o}},parseTime:function(t,e){var i,o,e=e||"H:i:s",s=null!==t.match(/p/i),n=t.replace(/[^0-9]/g,"");switch(n.length){case 4:i=parseInt(n.charAt(0)+n.charAt(1),10),o=parseInt(n.charAt(2)+n.charAt(3),10);break;case 3:i=parseInt(n.charAt(0),10),o=parseInt(n.charAt(1)+n.charAt(2),10);break;case 2:case 1:i=parseInt(n.charAt(0)+(n.charAt(1)||""),10),o=0;break;default:return""}if(12==i&&s===!1?i=0:s===!0&&i>0&&12>i&&(i+=12),0>=i&&(i=0),i>=24&&2==(""+i).length){var r=(""+i).split("");i=parseInt(r[0],10),o=parseInt(r[1],10),6>o&&(o+="0")}return(0>o||o>59)&&(o=0),i>=13&&23>=i&&(s=!0),e.replace(/g/g,0===i?"12":"g").replace(/g/g,i>12?i-12:i).replace(/h/g,(""+i).length>1?i>12?i-12:i:"0"+(i>12?i-12:i)).replace(/H/g,(""+i).length>1?i:"0"+i).replace(/i/g,(""+o).length>1?o:"0"+o).replace(/s/g,"00").replace(/A/g,s?this.options.pm_text:this.options.am_text)}}}(jQuery),function(t){t.timeAutocomplete.formatters["24hr"]=function(){this.initialize.apply(this,arguments)},t.timeAutocomplete.formatters["24hr"].prototype={main_instance:null,options:{},default_opts:{increment:15,start_hour:"00",hour_max:24,blur_empty_populate:!0,times:[],empty:{h:"12",m:"00",sep:":",postfix:""}},initialize:function(e,i){this.main_instance=e,this.options=t.extend(!0,{},this.default_opts,i),this.generateTimes()},hook_placeholderValue:function(){return this.main_instance._createStringFromFormat(this.options.empty)},hook_getTime:function(t,e){var i=t.split(this.options.empty.sep),o=i[0],s=i[1];return new Date(e[0],e[1],e[2],o,s).getTime()},hook_getTimeObjectFromHis:function(t){var e=t.split(":"),i=e[0],o=e[1],s={h:i,m:o,sep:this.options.empty.sep};return s},hook_filterSource:function(e){var i=this;return e=e[0],function(i,o){return function(s,n){var r=t.trim(e.value);1==s.term.length&&10>s.term.substr(0,1)&&(s.term="0"+s.term);var a=t.ui.autocomplete.escapeRegex(s.term),p=RegExp("^"+a,"i"),m=[];r&&(m=t.grep(i,function(t){return 0==t.substr(0,1)&&1==t.length&&(t=t.substr(1)),p.test(t)})),n(m);var h=r.toLowerCase();!m.length&&h.length>5&&o.main_instance.el.val(h.substr(0,5))}}(i.options.times,i)},hook_blurEmpty:function(){return this.options.blur_empty_populate?this.main_instance._createStringFromFormat(this.options.empty):""},hook_readMind:function(t){return t=t.toLowerCase(),this.getTimeObject(t)},hook_getUsableTimeValue:function(t){return t+":00"},getTimeObject:function(t){var e="",i="",o="";if(~t.indexOf("h")){var s=t.split("h");e=s[0]?s[0]:this.options.empty.h,i=s[1]?s[1]:this.options.empty.m}else{var n=t.replace(/[^\d]/g,"");n=n.split(""),4==n.length?(e=n[0]+n[1],i=n[2]+n[3]):3==n.length?(e="0"+n[0],i=n[1]+n[2]):2==n.length?(e=n.join(""),i="00"):1==n.length&&(e=n.join(""),i="00")}return e>24&&"00"==i&&(e=e.split(""),i=e[1]+"0",e="0"+e[0]),1==e.length&&(e="0"+e),1==i.length&&(i+="0"),o=e||i?{h:e,m:i,sep:this.options.empty.sep}:this.options.empty},generateTimes:function(){if(!this.options.times.length){for(var t=60,e=this.options.increment,i=new Date(2012,1,1,this.options.start_hour-1,t-e),o=[],s=60,n=this.options.hour_max,r=0,a=n*(s/e);a>r;r++){i.setMinutes(i.getMinutes()+e);var p=i.getHours(),m=i.getMinutes();1==(""+p).length&&(p="0"+p),1==(""+m).length&&(m="0"+m);var h=p+this.options.empty.sep+m;o.push(h)}this.options.times=o}}}}(jQuery),function(t){t.timeAutocomplete.formatters.french=function(){this.initialize.apply(this,arguments)},t.timeAutocomplete.formatters.french.prototype=t.extend(!0,{},t.timeAutocomplete.formatters["24hr"].prototype,{default_opts:{empty:{sep:"h"}},hook_getUsableTimeValue:function(t){return t.replace(this.options.empty.sep,":")+":00"}})}(jQuery);