-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavailability-calendar.min.js
1 lines (1 loc) · 8.8 KB
/
availability-calendar.min.js
1
!function(){"use strict";function a(){return{restrict:"EA",require:["availabilityCalendar","?ngModel"],scope:{ngModel:"=",onLoad:"&",fetchDates:"&",decorateDate:"&",decorateHandlers:"&",availabilityCalendarOptions:"&"},controller:b,controllerAs:"ctrl",bindToController:!0,link:c}}function b(a,b,c,d,e,f,g,h){function i(a){var b,d;if(a.calendar.append(a.prevButton).append(a.nextButton).append(a.title).append(a.table),angular.isDefined(c.selectableMonth)&&(a.monthSelect=k(),a.title.append(a.monthSelect),a.yearSelect=l(),a.title.append(a.yearSelect)),a.header){a.table.append(a.header),a.headerRow=j("calendarHeaderRow"),a.header.append(a.headerRow),a.headerCells=[];for(var e=0;7>e;e++)d=j("calendarHeaderCell"),d.html(f.DATETIME_FORMATS.SHORTDAY[e]),a.headerCells.push(d),a.headerRow.append(d)}a.table.append(a.body),a.weeks=[],a.days=[],a.weekDays=[],a.dayLabels=[];for(var g=0;6>g;g++){b=j("calendarBodyRow"),a.weeks.push(b),a.body.append(b),a.weekDays.push([]);for(var e=0;7>e;e++)d=j("calendarBodyCell"),a.days.push(d),a.weekDays[g].push(d),a.weeks[g].append(d),a.dayLabels.push(j("calendarBodyCellLabel"))}return J=d.attr("class")||"",a}function j(a,b){var c=a+"Template";if(!h[c]){if(b)return null;throw new Error("availabilty-calendar: Template not cofigured for: "+a)}return angular.element(h[c])}function k(){var a=j("monthSelect",!0);return a.on("change",function(a){p(moment([E.year(),a.target.value,1]))}),angular.forEach(f.DATETIME_FORMATS.MONTH,function(b,c){a.append('<option value="'+c+'">'+b+"</option>")}),a}function l(){var a=j("yearSelect",!0);a.on("change",function(a){p(moment([a.target.value,E.month(),1]))});for(var b=(new Date).getFullYear();b>1970;--b)a.append('<option value="'+b+'">'+b+"</option>");return a}function m(){var b=[];for(var d in c)"onDate"===d.substr(0,6)&&n(d.substr(6).toLowerCase(),function(b){return function(c){return b(a.$parent,{$event:c,$date:c.calendarDate,$decorations:c.calendarDateDecorations,$calendar:G})}}(g(c[d])));return b}function n(a,b){I.push({eventName:a,handler:function(a){for(var c=angular.element(a.target);c&&!c.data("$availabilityCalendarDate");)c=c.parent();return a.calendarDate=c.data("$availabilityCalendarDate"),a.calendarDateDecorations=c.data("$availabilityCalendarDateDecorations"),b(a)}})}function o(a,b){angular.forEach(a,function(a){b.on(a.eventName,a.handler)})}function p(a){return a&&D(a).isValid()?(E=D(a).startOf("month"),F=E.clone().endOf("month"),q(a),s(E,F)):void 0}function q(a){var b=D(a);K.monthSelect?(K.monthSelect.val(b.month()),K.yearSelect.val(b.year())):K.title.html(f.DATETIME_FORMATS.MONTH[b.month()]+", "+b.year())}function r(a,b,c){var e=a?D(a):E,f=b?D(b):F;if(!e||!f||e.isAfter(f))return d.when(null);e=e.clone(),f=f.clone();var g;return g=r.$lastPromise?r.$lastPromise["catch"](function(){return null}):d.when(null),r.$lastPromise=g.then(function(){return d.when(G.fetchDates({$fromDate:e.clone().toDate(),$toDate:f.clone().toDate(),$fetchOptions:c}))}).then(function(a){var b,c=[],d=e.clone();do b=d.clone().toDate(),c.push(angular.extend({},h.defaultDayDecorations,G.decorateDate({$date:b,$fetchedData:a}),{$date:b})),d.add(1,"day");while(!d.isAfter(f,"day"));return c.$fetchedData=a,c})}function s(a,b,c){return y(),r(a,b,c).then(function(a){return t(a)})["finally"](function(){z()})}function t(a){for(var b,c,d,e,f,g=0,i=0,j=K.days.length-1;j>=0;j--)K.days[j].html("").attr("class",J).addClass(h.disabledDayClass);K.body.html("");for(var k=E.clone();!k.isAfter(F,"day");k.add(1,"day")){if(e=k.clone().toDate(),!b&&(b=K.weeks[g],K.body.append(b),b.children().length<7))for(var j=0;7>j;j++)b.append(K.weekDays[g][j]);c=K.weekDays[g][k.day()],c.removeClass(h.disabledDayClass),o(I,c),a&&a.length>i&&k.isSame(a[i].$date,"day")?(f=a[i],i++):f=angular.extend({},h.defaultDayDecorations,G.decorateDate({$date:e,$fetchedData:a&&a.$fetchedData})),u(f,c,e),c.data("$availabilityCalendarDate",e),c.data("$availabilityCalendarDateDecorations",f),d=K.dayLabels[k.date()],d.html(k.date()),c.append(d),6===k.day()&&(g++,b=null)}}function u(a,b,c){angular.forEach(a,function(a,d){"function"==typeof H[d]&&H[d](a,b,c)})}function v(a,c){var d=a;"body"===a?d=document.getElementsByTagName("body"):"element"===a&&(d=b),angular.element(d).append(K.calendar),s(),G.options.classes&&K.calendar.addClass(G.options.classes),K.prevButton.on("click",B),K.nextButton.on("click",A),"element"!==a&&K.calendar.on("mousedown",function(a){a.preventDefault(),a.stopPropagation(),a.stopImmediatePropagation()}),c&&(w.$position=c,w(),angular.element(window).on("resize",w))}function w(){var a=w.$position;if(a){var c={},d=C(b);angular.forEach(a.split(" "),function(a){switch(a){case"bottom":c.top=d.top+b[0].offsetHeight+"px";break;case"top":c.bottom=d.top+"px";break;case"left":c.left=d.left+"px";break;case"right":c.left=d.left-(K.calendar[0].offsetWidth-b[0].offsetWidth)+"px"}}),K.calendar.css(c)}}function x(){K.calendar.remove(),angular.element(window).off("resize",w)}function y(){e.addClass(K.calendar,h.wrapperLoadingClass)}function z(){e.removeClass(K.calendar,h.wrapperLoadingClass)}function A(a){a&&a.preventDefault();var b=E.clone().add(1,"month").toDate();return p(b),!1}function B(a){a&&a.preventDefault();var b=E.clone().subtract(1,"month").toDate();return p(b),!1}function C(a){for(var b=angular.element(a)[0],c={left:0,top:0};b;)c.left+=b.offsetLeft+b.clientLeft,c.top+=b.offsetTop+b.clientTop,b=b.offsetParent;return c}function D(a){return angular.isString(a)?moment(a,G.options.dateFormat||moment.localeData().longDateFormat("L"),G.options.dateLocale||moment.locale(),!0):moment(a)}var E,F,G=this,H=angular.extend({},h.decorateHandlers,G.decorateHandlers()),I=[],J="",K=i({calendar:j("wrapper"),prevButton:j("prevButton",!0),nextButton:j("nextButton",!0),title:j("title",!0),table:j("calendarTable"),header:j("calendarHeader",!0),body:j("calendarBody")});G.setMonthDate=p,G.nextMonth=A,G.prevMonth=B,G.updateCalendar=s,G.addDateEvent=n,G.hideCalendar=x,G.parseMoment=D,G.getDecoratedDays=r;angular.isDefined(c.availabilityCalendar)?G.options=angular.extend({showOn:"focus",hideOn:"blur",appendTo:"body",position:"bottom left"},G.availabilityCalendarOptions()):G.options=angular.extend({showOn:null,hideOn:null,appendTo:"element"},G.availabilityCalendarOptions()),m(),G.options.showOn?b.on(G.options.showOn,function(){v(G.options.appendTo,G.options.position)}):v(G.options.appendTo,G.options.position),G.options.hideOn&&b.on(G.options.hideOn,x),a.$on("$destroy",x),G.onLoad({$calendar:G})}function c(a,b,c,d){var e=d[0],f=d[1];f?(f.$validators.validDate=function(a,b){var c=b||a;return e.parseMoment(c).isValid()},angular.isDefined(c.availabilityCalendarValidate)&&(f.$asyncValidators.availableDate=function(a,b){var c=b||a,d=e.parseMoment(c);return e.getDecoratedDays(d,d,{validator:!0}).then(function(a){return!a||a.length<1||!d.isSame(a[0].date)?!1:!!a[0].available})}),f.$parsers.push(function(a){if(a){var b=e.parseMoment(a).toDate();return b}}),f.$formatters.push(function(a){if(a){var b=e.options.dateFormat;return angular.isArray(b)&&b.length&&(b=b[0]),e.parseMoment(a).format(b||moment.localeData().longDateFormat("L"))}}),a.$watch("ctrl.ngModel",function(a){e.setMonthDate(a||new Date)}),e.addDateEvent("click",function(c){c.calendarDate&&c.calendarDateDecorations&&c.calendarDateDecorations.available&&a.$apply(function(){a.ctrl.ngModel=c.calendarDate,angular.isDefined(e.options.onSelectFocus)&&setTimeout(function(){e.options.onSelectFocus?document.getElementById(e.options.onSelectFocus).focus():b[0].blur()})})}),b.attr("placeholder")||b.attr("placeholder",moment.localeData().longDateFormat("L"))):e.setMonthDate(new Date)}angular.module("availability-calendar",[]).constant("availabilityCalendarConfig",{wrapperTemplate:'<div class="availability-calendar"></div>',prevButtonTemplate:'<a href="" class="availability-calendar-prev-button"><</a>',nextButtonTemplate:'<a href="" class="availability-calendar-next-button">></a>',titleTemplate:'<div class="availability-calendar-title"></div>',monthSelectTemplate:'<select class="availability-calendar-month-select"></select>',yearSelectTemplate:'<select class="availability-calendar-year-select"></select>',calendarTableTemplate:'<table class="availability-calendar-table"></table>',calendarHeaderTemplate:"<thead></thead>",calendarHeaderRowTemplate:"<tr></tr>",calendarHeaderCellTemplate:"<th></th>",calendarBodyTemplate:"<tbody></tbody>",calendarBodyRowTemplate:"<tr></tr>",calendarBodyCellTemplate:"<td></td>",calendarBodyCellLabelTemplate:'<span class="availability-calendar-day-label"></span>',wrapperLoadingClass:"loading",disabledDayClass:"disabled",defaultDayDecorations:{available:!0},decorateHandlers:{disabled:function(a,b,c){a&&b.addClass("disabled")},available:function(a,b,c){a&&b.addClass("available")},"class":function(a,b,c){var d=a;angular.isArray(a)&&(d=a.join(" ")),d&&b.addClass(d)}}}).directive("availabilityCalendar",a),b.$inject=["$scope","$element","$attrs","$q","$animate","$locale","$parse","availabilityCalendarConfig"]}();