UNPKG

datetimeranger

Version:

DateTimeRanger is a jQuery plugin for fast visual and interactive date + time (range) entry.

7 lines 17.1 kB
/** * datetimeranger * @version v1.0.7 * @link https://adam5wu.github.io/DateTimeRanger/ * @license MIT */ !function(e){"function"==typeof define&&define.amd?define(["jquery","moment","daterange-picker-ex","timedropper-ex"],e):"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("jquery"),require("moment"),require("daterange-picker-ex"),require("timedropper-ex")):e(jQuery,moment)}(function(e,t){"use strict";e.fn.dateTimeRanger=function(a){function n(){function n(t){if(!t.validity.valid){for(var a in t.validity)if(t.validity[a]){e(t).attr("title","Invalid value: "+a.replace(/[A-Z]/g,function(e,t){return"-"+e.toLowerCase()}));break}return e(t).addClass("invalid"),a}e(t).removeClass("invalid"),e(t).removeAttr("title")}function r(e,t,a){clearTimeout(g),_[e]=[t,a],g=setTimeout(function(){g=null;for(var e in _){var t=_[e];t[1](t[0])}_={}},200)}function s(e,t,a){e.validity.valid||(h(["picker",t],a,!0),n(e))}function l(e,t,a,r){switch(e){case 13:if(!t.validity.valid)return h(["picker",a],r,!0),void n(t);t.focusNext.focus();break;case 27:var i="";r[0]&&(F.selRange.start=!1,i+="start"),r[1]&&(F.selRange.end=!1,i+="end"),h(["internal","clear-"+i],r),n(t)}}var o=i(F.locale),u=o[0];F.fblocalizer=o[1];var d=F.locale;if(a.language)a.language in e.DTRLang&&(d=a.language);else{var c=navigator.languages||navigator.userLanguage||(navigator.language?[navigator.language]:[navigator.browserLanguage]);for(var p in c){var f=c[p].toLowerCase();if(f in e.DTRLang){d=f;break}}}o=i(d),F.locale=o[0],F.localizer=o[1];var m=F.formatter.locale();a.language&&F.formatter.locale(a.language).locale()===a.language||F.formatter.locale(F.locale).locale()!==F.locale&&F.formatter.locale(d).locale()!==d&&F.formatter.locale(u).locale()!==u&&F.formatter.locale(m),F.wrapper=S(e(".dt-ranger").length),(a.container||F.anchor).append(F.wrapper),F.el.default_bar=F.wrapper.find(".default-bar"),F.el.summary_bar=F.wrapper.find(".summary-bar"),F.el.summary_start=F.el.summary_bar.find(".start-dt"),F.el.summary_end=F.el.summary_bar.find(".end-dt"),F.el.summary_span=F.el.summary_bar.find(".span-days"),F.el.oper_zone=F.wrapper.find(".oper-zone"),F.el.start_zone=F.el.oper_zone.find(".start-zone"),F.el.start_inputs=F.el.start_zone.find("input"),F.el.start_input_date=F.el.start_zone.find("input.date"),F.el.start_input_time=F.el.start_zone.find("input.time"),F.el.start_pickers=F.el.start_zone.find(".pickers"),F.el.end_zone=F.el.oper_zone.find(".end-zone"),F.el.end_inputs=F.el.end_zone.find("input"),F.el.end_input_date=F.el.end_zone.find("input.date"),F.el.end_input_time=F.el.end_zone.find("input.time"),F.el.end_pickers=F.el.end_zone.find(".pickers"),F.start_datepicker=F.el.start_pickers.dateRangePicker({inline:!0,alwaysOpen:!0,autoCloase:!0,watchValueChange:!1,language:a.language,getValue:function(){},setValue:function(e){},container:F.el.start_pickers,singleMonth:!0,singleDate:!0,haveTopbar:!1,startOfWeek:a.startOfWeek,dateRange:a.dateRange,minDays:a.minDays,maxDays:a.maxDays,showWeekNumbers:a.showWeekNumbers,getWeekNumber:a.getWeekNumber,beforeShowDay:a.showDayFilter}).data("DRPEx"),F.start_timepicker=F.el.start_pickers.timeDropper({inline:!0,alwaysOpen:!0,watchValueChange:!1,language:a.language,fetchTime:function(){},putTime:function(e){},startFrom:null,container:F.el.start_pickers,autoSwitch:a.autoSwitch,meridians:a.meridians,mousewheel:a.mousewheel,showLancets:a.showLancets,handleShake:a.handleShake,stickyMinute:a.stickyMinute,stickyHour:a.stickyHour}).data("TDEx"),F.end_datepicker=F.el.end_pickers.dateRangePicker({inline:!0,alwaysOpen:!0,autoCloase:!0,watchValueChange:!1,language:a.language,getValue:function(){},setValue:function(e){},container:F.el.end_pickers,singleMonth:!0,singleDate:!0,lookBehind:!0,haveTopbar:!1,startOfWeek:a.startOfWeek,dateRange:a.dateRange,minDays:a.minDays,maxDays:a.maxDays,showWeekNumbers:a.showWeekNumbers,getWeekNumber:a.getWeekNumber,beforeShowDay:a.showDayFilter}).data("DRPEx"),F.end_timepicker=F.el.end_pickers.timeDropper({inline:!0,alwaysOpen:!0,watchValueChange:!1,language:a.language,fetchTime:function(){},putTime:function(e){},startFrom:null,container:F.el.end_pickers,autoSwitch:a.autoSwitch,meridians:a.meridians,mousewheel:a.mousewheel,showLancets:a.showLancets,handleShake:a.handleShake,stickyMinute:a.stickyMinute,stickyHour:a.stickyHour}).data("TDEx");var g=null,_={};F.el.start_pickers.on("DRPEx-apply",function(e,t){w("picker",t.date),a.autoSwitch&&setTimeout(function(){F.start_timepicker.select(a.startFrom)},0),F.selDelay&&clearTimeout(F.selDelay),F.selDelay=setTimeout(function(){F.selDelay=null},100)}).on("TDEx-update",function(e,t){t.dailing||t.now&&_["start-time"]||v("picker",t.time[0],t.now)}).on("TDEx-dailing",function(e,t){if(t.finish){var a=F.start_timepicker.getTime();v("picker",a[0],!1),F.selDelay&&clearTimeout(F.selDelay),F.selDelay=setTimeout(function(){F.selDelay=null},100)}}),F.el.start_input_date.on("input",function(e){r("start-date",this,function(e){var a=n(e);if(!a){var r=e.valueAsDate,i=t(new Date(r.getUTCFullYear(),r.getUTCMonth(),r.getUTCDate())).toDate();w("input",i)}})}),F.el.start_input_time.on("input",function(e){r("start-time",this,function(e){var t=n(e);t||v("input",e.valueAsNumber/1e3,!1)})}),F.el.end_pickers.on("DRPEx-apply",function(e,t){y("picker",t.date),a.autoSwitch&&setTimeout(function(){F.end_timepicker.select(a.startFrom)},0),F.selDelay&&clearTimeout(F.selDelay),F.selDelay=setTimeout(function(){F.selDelay=null},100)}).on("TDEx-update",function(e,t){t.dailing||t.now&&_["end-time"]||k("picker",t.time[0],t.now)}).on("TDEx-dailing",function(e,t){if(t.finish){var a=F.end_timepicker.getTime();k("picker",a[0],!1),F.selDelay&&clearTimeout(F.selDelay),F.selDelay=setTimeout(function(){F.selDelay=null},100)}}),F.el.end_input_date.on("input",function(e){r("end-date",this,function(e){var a=n(e);if(!a){var r=e.valueAsDate,i=t(new Date(r.getUTCFullYear(),r.getUTCMonth(),r.getUTCDate())).toDate();y("input",i)}})}),F.el.end_input_time.on("input",function(e){r("end-time",this,function(e){var t=n(e);t||k("input",e.valueAsNumber/1e3,!1)})}),F.el.start_input_date.on("blur",function(e){s(this,"date",[!0,!1])}),F.el.start_input_date.attr("focusNext",F.el.start_input_time),F.el.start_input_time.on("blur",function(e){s(this,"time",[!0,!1])}),F.el.start_input_time.attr("focusNext",F.el.end_input_date),F.el.end_input_date.on("blur",function(e){s(this,"date",[!1,!0])}),F.el.end_input_date.attr("focusNext",F.el.end_input_time),F.el.end_input_time.on("blur",function(e){s(this,"time",[!1,!0])}),F.el.end_input_time.attr("focusNext",F.el.start_input_time),F.el.start_zone.on("mouseenter",function(e){F.el.start_input_date[0].focus()}),F.el.end_zone.on("mouseenter",function(e){F.el.end_input_date[0].focus()}),F.el.start_input_date.on("keyup",function(e){l(e.keyCode,this,"date",[!0,!1])}),F.el.start_input_time.on("keyup",function(e){l(e.keyCode,this,"time",[!0,!1])}),F.el.end_input_date.on("keyup",function(e){l(e.keyCode,this,"date",[!1,!0])}),F.el.end_input_time.on("keyup",function(e){l(e.keyCode,this,"time",[!1,!0])})}function r(e){if(!F.wrapper){if(void 0!==F.wrapper)throw new Error("Already destroyed");if(e)throw new Error("Not yet initialized")}}function i(t){for(var a=e.DTRLang[t];a.constructor===String;)t=a,a=e.DTRLang[a];return[t,a]}function s(e){return F.localizer[e]||F.fblocalizer[e]||"??"}function l(e){return e.start&&e.end?t(e.start).isAfter(t(e.end)):null}function o(e){if(l(e)){var t=e.end;return e.end=e.start,e.start=t,!0}return!1}function u(e,t){return t.contains(e.target)||e.target==t}function d(e){F.selDelay||u(e,F.anchor[0])||u(e,F.wrapper[0])||p(a.animationTime)}function c(t){if(!F.active){F.active=!0,F.el.oper_zone.css({display:"block",visibility:0}),F.start_datepicker.redraw(),F.end_datepicker.redraw(),F.el.oper_zone.css({display:"none",visibility:"initial"});var n=function(){F.anchor.trigger("DTR-opened",{anchor:F.anchor,wrapper:F.wrapper})};return F.el.default_bar.slideUp(t),F.el.summary_bar.slideUp(t),F.el.oper_zone.slideDown(t,n),a.alwaysOpen||e(document).bind("click.DTR",d),F.anchor.trigger("DTR-open",{anchor:F.anchor,wrapper:F.wrapper}),!0}return!1}function p(t){if(!a.alwaysOpen&&F.active){F.active=!1;var n=function(){F.anchor.trigger("DTR-closed",{anchor:F.anchor,wrapper:F.wrapper})};return F.selRange.start&&F.selRange.end?F.el.summary_bar.slideDown(t):F.el.default_bar.slideDown(t),F.el.oper_zone.slideUp(t,n),F.anchor.trigger("DTR-close",{anchor:F.anchor,wrapper:F.wrapper}),a.alwaysOpen||e(document).unbind("click.DTR",d),!0}return!1}function f(e,a,n,r){var i=null,s=null;if(e){var l=t(e).format("YYYY-MM-DDTHH:mm");i=l.substr(0,10),s=l.substr(11,5)}a&&a.val(i),n&&(n.val(s),r&&s?n.addClass("floating"):n.removeClass("floating"))}function m(e,t){if(e){var a=3600*e.getHours()+60*e.getMinutes()+e.getSeconds()+e.getMilliseconds()/1e3;t.setTime(a,!0)}else t.setTime(null,!0)}function g(e,a){if(e){var n=t(e[0]).startOf("day").toDate(),r=t(e[1]).startOf("day").toDate();a.forEach(function(e){e.setRange(n,r,!0)})}else a.forEach(function(e){e.clear(!0)})}function _(e,t){return F.formatter.toDate().setTime(e),F.formatter.format(t)}function h(e,n,r){var i=o(F.selRange);if(i){var s=F.followTime.start;F.followTime.start=F.followTime.end,F.followTime.end=s}var l=!0;if(n[0]||i){switch(i?"internal":e[0]){case"picker":switch(e[1]){case"time":l=!F.el.start_input_date.val();default:f(F.selRange.start,"date"!=e[1]&&F.el.start_input_date.val()?null:F.el.start_input_date,"time"!=e[1]&&F.el.start_input_time.val()?null:F.el.start_input_time,F.followTime.start)}break;case"input":switch(e[1]){case"time":m(F.selRange.start,F.start_timepicker),F.el.start_input_date.val()||f(F.selRange.start,F.el.start_input_date,null),l=!1;break;case"date":F.el.start_input_time.val()||f(F.selRange.start,null,F.el.start_input_time,F.followTime.start)}break;default:f(F.selRange.start,F.el.start_input_date,F.el.start_input_time,F.followTime.start),m(!F.followTime.start&&F.selRange.start,F.start_timepicker)}var u=_(F.selRange.start,a.summaryFmt);F.el.summary_start.text(u)}if(n[1]||i){switch(i?"internal":e[0]){case"picker":switch(e[1]){case"time":l=!F.el.end_input_date.val();default:f(F.selRange.end,"date"!=e[1]&&F.el.end_input_date.val()?null:F.el.end_input_date,"time"!=e[1]&&F.el.end_input_time.val()?null:F.el.end_input_time,F.followTime.end)}break;case"input":switch(e[1]){case"time":m(F.selRange.end,F.end_timepicker),F.el.end_input_date.val()||f(F.selRange.end,F.el.end_input_date,null),l=!1;break;case"date":F.el.end_input_time.val()||f(F.selRange.end,null,F.el.end_input_time,F.followTime.end)}break;default:f(F.selRange.end,F.el.end_input_date,F.el.end_input_time,F.followTime.end),m(!F.followTime.end&&F.selRange.end,F.end_timepicker)}var u=_(F.selRange.end,a.summaryFmt);F.el.summary_end.text(u)}if((l||i)&&(F.selRange.start?F.selRange.end?g([F.selRange.start,F.selRange.end],[F.start_datepicker,F.end_datepicker]):(g([F.selRange.start,F.selRange.start],[F.start_datepicker]),g(!1,[F.end_datepicker])):F.selRange.end?(g(!1,[F.start_datepicker]),g([F.selRange.end,F.selRange.end],[F.end_datepicker])):g(!1,[F.start_datepicker,F.end_datepicker])),F.selRange.start&&F.selRange.end){var d=t(F.selRange.end).diff(F.selRange.start,"day",!0);F.el.summary_span.text(Math.abs(Math.round(100*d)/100))}if(!r){var c={source:e,reversal:i};F.selRange.start&&(c.start=new Date(F.selRange.start)),F.selRange.end&&(c.end=new Date(F.selRange.end)),F.anchor.trigger("DTR-change",c)}}function w(e,t){var a=F.selRange.start;if(!a){a=F.selRange.start=new Date;var n=F.start_timepicker.getTime();a.setHours(Math.floor(n[0]/3600));var r=n[0]%3600;a.setMinutes(Math.floor(r/60)),a.setSeconds(0),a.setMilliseconds(0)}a.setFullYear(t.getFullYear()),a.setMonth(t.getMonth()),a.setDate(t.getDate()),h([e,"date"],[!0,!1])}function v(e,t,a){var n=F.followTime.start!=a;F.followTime.start=a;var r=F.selRange.start;if(!r){if(a)return;n=!0,r=F.selRange.start=new Date;var i=F.start_datepicker.getDateRange();i&&(r.setFullYear(i[1].getFullYear()),r.setMonth(i[1].getMonth()),r.setDate(i[1].getDate()))}var s=Math.floor(t/3600),l=t%3600,o=Math.floor(l/60),u=0,d=0;n||(n=r.getHours()!=s||r.getMinutes()!=o||r.getSeconds()!=u||r.getMilliseconds()!=d),n&&(r.setHours(s),r.setMinutes(o),r.setSeconds(u),r.setMilliseconds(d),h([e,"time"],[!0,!1]))}function y(e,t){var a=F.selRange.end;if(!a){a=F.selRange.end=new Date;var n=F.end_timepicker.getTime();a.setHours(Math.floor(n[0]/3600));var r=n[0]%3600;a.setMinutes(Math.floor(r/60)),a.setSeconds(0),a.setMilliseconds(0)}a.setFullYear(t.getFullYear()),a.setMonth(t.getMonth()),a.setDate(t.getDate()),h([e,"date"],[!1,!0])}function k(e,t,a){var n=F.followTime.end!=a;F.followTime.end=a;var r=F.selRange.end;if(!r){if(a)return;n=!0,r=F.selRange.end=new Date;var i=F.end_datepicker.getDateRange();i&&(r.setFullYear(i[1].getFullYear()),r.setMonth(i[1].getMonth()),r.setDate(i[1].getDate()))}var s=Math.floor(t/3600),l=t%3600,o=Math.floor(l/60),u=0,d=0;n||(n=r.getHours()!=s||r.getMinutes()!=o||r.getSeconds()!=u||r.getMilliseconds()!=d),n&&(r.setHours(s),r.setMinutes(o),r.setSeconds(u),r.setMilliseconds(d),h([e,"time"],[!1,!0]))}function D(e,t,a,n){F.selRange.start=t,F.followTime.start=a,h([e,"set-rangestart"],[!0,!1])}function R(e,t,a,n){F.selRange.end=t,F.followTime.end=a,h([e,"set-rangeend"],[!1,!0])}function T(e,t,a,n,r,i){F.selRange.start=t,F.followTime.start=a,F.selRange.end=n,F.followTime.end=r,h([e,"set-range"],[!0,!0])}function b(e,t){return!(!F.selRange.start&&!F.selRange.end)&&(F.selRange.start=!1,F.selRange.end=!1,h([e,"clear-selection"],[!0,!0]),!0)}function M(){F.start_input_date.resetView(),F.end_input_date.resetView()}function x(){F.start_input_date.redraw(),F.end_input_date.redraw()}function z(t,a,n){var r="<"+t;return a&&(e.each(a,function(e,t){t.constructor==Array&&(t=t.reduce(function(e,t){return(e||"")+(t?" "+t:"")})),r+=" "+e+'="'+t+'"'}),void 0!==n&&(r+=n?">"+n+"</"+t:"/")),r+">"}function S(t){var a=z("div",{"class":["dt-ranger"],id:"dt-ranger-"+t});return a+=z("div",{"class":"default-bar"},z("i",{"class":["fa","fa-hand-pointer-o"],"aria-hidden":"true"},"")+s("select")),a+=z("div",{"class":"summary-bar"},z("span",{},s("from"))+" "+z("b",{"class":"start-dt"},"[start datetime]")+" "+z("span",{},s("to"))+" "+z("b",{"class":"end-dt"},"[end datetime]")+" "+z("i",{},"("+z("span",{"class":"span-days"},"X")+" "+s("days")+")")),a+=z("div",{"class":"oper-zone"},z("div",{"class":"start-zone"},z("label",{},s("start")+":"+z("div",{"class":"input-wrapper"},z("input",{"class":"date",type:/(Edge|Android)/.test(navigator.userAgent)?"text":"date",min:"2000-01-01",max:"3000-01-01"})+z("input",{"class":"time",type:/(Edge|Android)/.test(navigator.userAgent)?"text":"time"})))+z("div",{"class":"pickers"},""))+z("div",{"class":"end-zone"},z("label",{},s("end")+":"+z("div",{"class":"input-wrapper"},z("input",{"class":"date",type:/(Edge|Android)/.test(navigator.userAgent)?"text":"date",min:"2000-01-01",max:"3000-01-01"})+z("input",{"class":"time",type:/(Edge|Android)/.test(navigator.userAgent)?" text ":"time"})))+z("div",{"class":"pickers"},""))),a+=z("/div"),e(a)}e.DTRLang=e.extend({"default":"en",en:{from:"From",to:"To",days:"Days",select:"Please pick a date time range",start:"Start",end:"End"}},e.DTRLang),a=e.extend({autoStart:!1,alwaysOpen:!1,dropTrigger:!0,summaryFmt:"LLL",minDispFmt:"YYYY/M/D H:mm",language:void 0,startOfWeek:void 0,dateRange:{start:!1,end:!1},minDays:0,maxDays:0,container:void 0,showWeekNumbers:!1,getWeekNumber:function(e){return t(e).format("w")},showDayFilter:void 0,autoSwitch:!0,meridians:!0,mousewheel:!0,showLancets:!0,startFrom:"hr",handleShake:!1,stickyMinute:20,stickyHour:480},a),a.dateRange.start&&a.dateRange.start.constructor==String&&(a.dateRange.start=t(a.dateRange.start,a.format).toDate()),a.dateRange.end&&a.dateRange.end.constructor==String&&(a.dateRange.end=t(a.dateRange.end,a.format).toDate()),a.alwaysOpen&&(a.autoStart=!0,a.dropTrigger=!1),o(a.dateRange);var F={anchor:e(this),wrapper:void 0,el:{},locale:"default",localizer:{},fblocalizer:{},formatter:t(),selRange:{start:!1,end:!1},followTime:{start:!1,end:!1},selDelay:null,active:!1};return F.anchor.data("DTR",{open:function(e){return r(),c(e)},close:function(e){return r(),p(e)},setStart:function(e,n,i){return r(!0),e.constructor==String&&(e=t(e,a.format).toDate()),D("api",e,n,i),this},setEnd:function(e,n,i){return r(!0),e.constructor==String&&(e=t(e,a.format).toDate()),R("api",e,n,i),this},setRange:function(e,n,i,s,l){r(!0),e.constructor==String&&(e=t(e,a.format).toDate()),i.constructor==String&&(i=t(i,a.format).toDate()),T("api",e,n,i,s,l)},clear:function(e){return r(),b("api",e)},wrapper:function(){return r(!0),F.wrapper},anchor:function(){return r(),F.anchor},redraw:function(){r(),F.wrapper&&x()},resetMonthsView:function(){r(),F.wrapper&&M()},isActive:function(){return r(),F.active},destroy:function(){r(),F.anchor.trigger("DTR-destroy",{}),F.anchor.off("click"),F.anchor.data("DTR",void 0),F.wrapper&&F.wrapper.remove(),F.wrapper=null,e(document).off("click",d)}}),a.dropTrigger&&F.anchor.on("click",function(e){c(a.animationTime)}),a.alwaysOpen&&setTimeout(c.bind(this),0),n(),this}});