1 line
13 KiB
JavaScript
1 line
13 KiB
JavaScript
!function(t){var e=0;t.add("module","datepicker",{translations:{en:{days:["Mon","Tue","Wed","Thu","Fri","Sat","Sun"],months:["","January","February","March","April","May","June","July","August","September","October","November","December"],"months-short":["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]}},init:function(t,i){this.app=t,this.$doc=t.$doc,this.$win=t.$win,this.$body=t.$body,this.lang=t.lang,this.animate=t.animate;var a={year:!1,month:!1,day:!1,format:"%d.%m.%Y",embed:!1,target:!1,selectYear:!1,sundayFirst:!1,startDate:!1,endDate:!1,animationOpen:"slideDown",animationClose:"slideUp"};this.context=i,this.params=i.getParams(a),this.$element=i.getElement(),this.$target=i.getTarget(),this.uuid=e++,this.namespace=".kube.datepicker-"+this.uuid,this.dateRegexp=/^(.*?)(\/|\.|,|\s|\-)(.*?)(?:\/|\.|,|\s|\-)(.*?)$/,this.value="",this.today={},this.current={},this.next={},this.prev={},this.selected={}},start:function(){this.$element.attr("uuid",this.uuid),this._buildStartEndDate(),this.$datepicker=t.create("class.datepicker.box",this.app,this),this.$datepicker.build(),this.params.embed?(this.build(),this.update(),this.$datepicker.addClass("is-embed"),this.$element.append(this.$datepicker)):(this.$datepicker.addClass("is-hidden"),this.$body.append(this.$datepicker),this.$element.on("click"+this.namespace,this._open.bind(this)))},stop:function(){this._disableEvents(),this.$datepicker.remove(),this.$element.off(this.namespace),this.$element.removeClass("datepicker-in")},build:function(){this._buildValue(),this._buildTodayDate(),this._buildSelectedDate(),this._buildCurrentDate()},update:function(){this._buildPrevNextDate(),this.$grid=t.create("class.datepicker.grid",this.app,this),this.$grid.build(),this.$datepicker.setControls(this.prev,this.next),this.$datepicker.setMonth(this.lang.get("months")[this.current.month]),this.$datepicker.setYear(this.current.year),this.$datepicker.setGrid(this.$grid)},setDate:function(e){e.preventDefault();var i=t.dom(e.target);if(i.attr("data-disabled")===!0)return this._close();var a={day:i.attr("data-day"),month:i.attr("data-month"),year:i.attr("data-year")},s=this._convertDateToFormat(a);if(this.params.embed===!1){var n=0!==this.$target.length?this.$target:this.$element;"INPUT"===n.get().tagName?n.val(s):n.text(s),this._close()}this.app.broadcast("datepicker.set",this,s,a)},setNextMonth:function(t){t.preventDefault(),this.current=this.next,this.update()},setPrevMonth:function(t){t.preventDefault(),this.current=this.prev,this.update()},setYear:function(){this.current.year=this.$datepicker.getYearFromSelect(),this.selected.day=!1,this.$datepicker.setYear(this.current.year),this.update()},_buildValue:function(){var t=0!==this.$target.length?this.$target:this.$element;this.value="INPUT"===t.get().tagName?t.val():t.text().trim()},_buildTodayDate:function(){var t=new Date;this.today={year:t.getFullYear(),month:parseInt(t.getMonth()+1),day:parseInt(t.getDate())}},_buildSelectedDate:function(){this.selected=this._parseDateString(this.value),""===this.value&&(this.selected.year=this.params.year?this.params.year:this.selected.year,this.selected.month=this.params.month?parseInt(this.params.month):this.selected.month,this.selected.day=!1)},_buildCurrentDate:function(){this.current=this.selected},_buildPrevNextDate:function(){var t=this._getPrevYearAndMonth(this.current.year,this.current.month);this.prev={year:t.year,month:t.month};var t=this._getNextYearAndMonth(this.current.year,this.current.month);this.next={year:t.year,month:t.month}},_buildStartEndDate:function(){this.params.startDate=this.params.startDate?this._parseDateString(this.params.startDate):!1,this.params.endDate=this.params.endDate?this._parseDateString(this.params.endDate):!1},_buildPosition:function(){this.position={};var t=this.$element.offset(),e=this.$element.innerHeight(),i=this.$element.innerWidth(),a=this.$datepicker.innerWidth(),s=this.$datepicker.innerHeight(),n=this.$win.width(),r=this.$doc.height(),h=0,d=t.left,o=t.top+e+1;this.position.type="left",d+a>n&&(this.position.type="right",h=n-(d+i)),o+s>r&&(this.params.animationOpen="show",this.params.animationClose="hide",o=o-s-e-2),this.position.top=o,this.position.left=d,this.position.right=h},_open:function(t){t&&t.preventDefault(),this._isOpened()||(this._closeAll(),this.app.broadcast("datepicker.open",this),this.build(),this.update(),this._buildPosition(),this._setPosition(),this.animate.run(this.$datepicker,this.params.animationOpen,this._opened.bind(this)))},_opened:function(){this._enableEvents(),this.$element.addClass("datepicker-in"),this.$datepicker.addClass("is-open"),this.app.broadcast("datepicker.opened",this)},_isOpened:function(){return this.$datepicker.hasClass("is-open")},_close:function(e){e&&0!==t.dom(e.target).closest(".datepicker").length||this._isOpened()&&(this.app.broadcast("datepicker.close",this),this.animate.run(this.$datepicker,this.params.animationClose,this._closed.bind(this)))},_closed:function(){this._disableEvents(),this.$datepicker.removeClass("is-open"),this.$element.removeClass("datepicker-in"),this.app.broadcast("datepicker.closed",this)},_closeAll:function(){t.dom(".datepicker.is-open").each(function(e){var i=t.dom(e),a=i.attr("data-uuid");this.$doc.off(".kube.datepicker-"+a),this.$win.off(".kube.datepicker-"+a),i.removeClass("is-open"),i.addClass("is-hidden")}.bind(this)),t.dom(".datepicker-in").removeClass("datepicker-in")},_handleKeyboard:function(t){27===t.which&&this._close()},_enableEvents:function(){this.$doc.on("keyup"+this.namespace,this._handleKeyboard.bind(this)),this.$doc.on("click"+this.namespace+" touchstart"+this.namespace,this._close.bind(this)),this.$win.on("resize"+this.namespace,this._resizePosition.bind(this))},_disableEvents:function(){this.$doc.off(this.namespace),this.$win.off(this.namespace)},_resizePosition:function(){this._buildPosition(),this._setPosition()},_setPosition:function(){var t="auto",e=this.position.right+"px";"left"===this.position.type&&(t=this.position.left+"px",e="auto"),this.$datepicker.css({top:this.position.top+"px",left:t,right:e})},_parseDateString:function(t){var e={},i=t.match(this.dateRegexp),a=this.params.format.match(this.dateRegexp);return e.year=null===i?this.today.year:parseInt(i[4]),"%m"===a[1]||"%M"===a[1]||"%F"===a[1]?(e.month=null===i?this.today.month:this._parseMonth(a[1],i[1]),e.day=null===i?!1:parseInt(i[3])):(e.month=null===i?this.today.month:this._parseMonth(a[3],i[3]),e.day=null===i?!1:parseInt(i[1])),e.splitter=null===i?".":i[2],e},_parseMonth:function(t,e){var i=parseInt(e);return"%M"===t?i=this.lang.get("months-short").indexOf(e):"%F"===t&&(i=this.lang.get("months").indexOf(e)),i},_convertDateToFormat:function(t){var e=this.params.format.replace("%d",t.day);return e=e.replace("%F",this.lang.get("months")[t.month]),e=e.replace("%m",this._addZero(t.month)),e=e.replace("%M",this.lang.get("months-short")[t.month]),e=e.replace("%Y",t.year)},_addZero:function(t){return t=Number(t),10>t?"0"+t:t},_getPrevYearAndMonth:function(t,e){var i={year:t,month:parseInt(e)-1};return i.month<=0&&(i.month=12,i.year--),i},_getNextYearAndMonth:function(t,e){var i={year:t,month:parseInt(e)+1};return i.month>12&&(i.month=1,i.year++),i}})}(Kube),function(t){t.add("class","datepicker.box",{"extends":["dom"],init:function(t,e){this.app=t,this.lang=t.lang,this.datepicker=e,this.params=e.params,this.namespace=e.namespace,this.selected=e.selected},build:function(){this._buildBox(),this._buildHead(),this._buildControlPrev(),this._buildMonthBox(),this._buildControlNext(),this._buildWeekdays(),this._buildBody()},getYearFromSelect:function(){return Number(this.$yearSelect.val())},setMonth:function(t){this.$month.html(t)},setYear:function(t){this.$yearValue.html(t),this.params.selectYear&&this.$yearSelect&&this.$yearSelect.val(t)},setGrid:function(t){this.$dbody.html(""),this.$dbody.append(t)},setControls:function(t,e){var i=function(t,e){return e=e?e:t.day,new Date(t.year+"/"+t.month+"/"+e)};if(this.params.startDate){var a=i(t,31),s=i(this.params.startDate),n=s.getTime()>a.getTime()?"hide":"show";this.$prev[n]()}if(this.params.endDate){var r=i(e,1),h=i(this.params.endDate),n=h.getTime()<r.getTime()?"hide":"show";this.$next[n]()}},_buildBox:function(){this.parse("<div>"),this.addClass("datepicker")},_buildHead:function(){this.$head=t.dom('<div class="datepicker-head">'),this.append(this.$head)},_buildControlPrev:function(){this.$prev=t.dom('<span class="datepicker-control datepicker-control-prev" />').html("<"),this.$prev.on("click"+this.namespace,this.datepicker.setPrevMonth.bind(this.datepicker)),this.$head.append(this.$prev)},_buildControlNext:function(){this.$next=t.dom('<span class="datepicker-control datepicker-control-next" />').html(">"),this.$next.on("click"+this.namespace,this.datepicker.setNextMonth.bind(this.datepicker)),this.$head.append(this.$next)},_buildMonthBox:function(){this.$monthBox=t.dom('<div class="datepicker-month-box">'),this.$head.append(this.$monthBox),this._buildMonth(),this._buildYear(),this._buildYearSelect()},_buildMonth:function(){this.$month=t.dom("<span />"),this.$monthBox.append(this.$month)},_buildYear:function(){this.$year=t.dom("<span />"),this.$yearValue=t.dom("<span />"),this.$year.append(this.$yearValue),this.$monthBox.append(this.$year)},_buildYearSelect:function(){if(this.params.selectYear){var e=new Date,i=this.params.startDate?this.params.startDate.year:e.getFullYear()-99,a=this.params.endDate?this.params.endDate.year:e.getFullYear();if(!(2>a-i)){this.$yearSelect=t.dom("<select />"),this.$year.append(this.$yearSelect),this.$year.append('<span class="datepicker-select-year-caret" />'),this.$year.addClass("datepicker-select-year");for(var s=i;a>=s;s++){var n=t.dom('<option value="'+s+'">'+s+"</option>");this.$yearSelect.append(n)}this.$yearSelect.on("change"+this.namespace,this.datepicker.setYear.bind(this.datepicker))}}},_buildWeekdays:function(){this.$weekdays=t.dom('<div class="datepicker-weekdays">');var e=[];if(this.params.sundayFirst){var i=this.lang.get("days").slice(6);e=this.lang.get("days").slice(0,6),e.unshift(i[0])}else e=this.lang.get("days");for(var a=0;a<e.length;a++){var s=t.dom("<span>").html(e[a]);this.$weekdays.append(s)}this.append(this.$weekdays)},_buildBody:function(){this.$dbody=t.dom('<div class="datepicker-body">'),this.append(this.$dbody)}})}(Kube),function(t){t.add("class","datepicker.grid",{"extends":["dom"],init:function(t,e){this.app=t,this.lang=t.lang,this.datepicker=e,this.params=e.params,this.namespace=e.namespace,this.today=e.today,this.selected=e.selected,this.current=e.current,this.prev=e.prev,this.next=e.next,this.daysInMonth=[0,31,28,31,30,31,30,31,31,30,31,30,31]},build:function(){this.parse('<div class="datepicker-grid">');for(var e,i=this._getDaysInCurrentMonth(),a=this._getDaysInPrevMonth(),s=(this._getDaysInNextMonth(),new Date(this.current.year,this.current.month-1,1)),n=this.params.sundayFirst?s.getDay()+1:s.getDay()?s.getDay():7,r=a-n+2,h=1,d=1,o=0;6>o;o++){for(var p=t.dom('<div class="datepicker-row">'),c=0;7>c;c++){if(0===o){var l=r+c;l>a?(e=this._buildGridObj(c,h,this.current,!1,!1),h++):e=this._buildGridObj(c,l,this.prev,!1,!0)}else h>i?(e=this._buildGridObj(c,d,this.next,!0,!1),d++):(e=this._buildGridObj(c,h,this.current,!1,!1),h++);p.append(this._buildGridDay(e))}this.append(p)}},_buildGridObj:function(t,e,i,a,s){return{day:e,next:a,prev:s,year:i.year,month:i.month,date:this._getGridDay(i.year,i.month,e),selected:this._isSelectedDate(i.year,i.month,e),today:this._isTodayDate(i.year,i.month,e),weekend:t>4,disabled:this._isDisabledDate(i.year,i.month,e)}},_buildGridDay:function(e){var i=t.dom('<div class="datepicker-cell">');(e.next||e.prev)&&i.addClass("is-out"),e.selected&&i.addClass("is-selected"),e.today&&i.addClass("is-today"),e.weekend&&this.params.weekend&&i.addClass("is-weekend"),e.disabled&&i.addClass("is-disabled");var a=t.dom("<a>");return a.html(e.day),a.attr("href","#"),a.attr("data-disabled",e.disabled),a.attr("data-date",e.date),a.attr("data-day",e.day),a.attr("data-month",e.month),a.attr("data-year",e.year),a.on("click",this.datepicker.setDate.bind(this.datepicker)),i.append(a)},_isSelectedDate:function(t,e,i){return this.selected.year===t&&this.selected.month===e&&this.selected.day===i},_isTodayDate:function(t,e,i){return this.today.year===t&&this.today.month===e&&this.today.day===i},_isDisabledDate:function(t,e,i){var a=new Date(t+"/"+e+"/"+i);if(this.params.startDate){var s=new Date(this.params.startDate.year+"/"+this.params.startDate.month+"/"+this.params.startDate.day);if(a.getTime()<s.getTime())return!0}if(this.params.endDate){var n=new Date(this.params.endDate.year+"/"+this.params.endDate.month+"/"+this.params.endDate.day);if(a.getTime()>n.getTime())return!0}return!1},_getGridDay:function(t,e,i){return t+"-"+e+"-"+i},_getDaysInCurrentMonth:function(){return this._getDaysInMonth(this.current.year,this.current.month)},_getDaysInPrevMonth:function(){return this._getDaysInMonth(this.prev.year,this.prev.month)},_getDaysInNextMonth:function(){return this._getDaysInMonth(this.next.year,this.next.month)},_getDaysInMonth:function(t,e){return 0!==t%4||0===t%100&&0!==t%400||1!==e?this.daysInMonth[e]:29}})}(Kube); |