forked from kylestetz/CLNDR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clndr.min.js
2 lines (2 loc) · 16 KB
/
clndr.min.js
1
2
/*! clndr.min.js v1.2.5 2014-12-01 */
!function(a){"function"==typeof define&&define.amd?define(["jquery","moment"],a):"object"==typeof exports?a(require("jquery"),require("moment")):a(jQuery,moment)}(function(a,b){function c(c,d){if(this.element=c,this.options=a.extend(!0,{},f,d),this.options.events.length&&(this.options.events=this.options.multiDayEvents?this.addMultiDayMomentObjectsToEvents(this.options.events):this.addMomentObjectToEvents(this.options.events)),this.month=this.options.startWithMonth?b(this.options.startWithMonth).startOf("month"):b().startOf("month"),this.options.constraints){if(this.options.constraints.startDate){var g=b(this.options.constraints.startDate);this.month.isBefore(g,"month")&&(this.month.set("month",g.month()),this.month.set("year",g.year()))}if(this.options.constraints.endDate){var h=b(this.options.constraints.endDate);this.month.isAfter(h,"month")&&this.month.set("month",h.month()).set("year",h.year())}}this._defaults=f,this._name=e,this.init()}var d="<div class='clndr-controls'><div class='clndr-control-button'><span class='clndr-previous-button'>previous</span></div><div class='month'><%= month %> <%= year %></div><div class='clndr-control-button rightalign'><span class='clndr-next-button'>next</span></div></div><table class='clndr-table' border='0' cellspacing='0' cellpadding='0'><thead><tr class='header-days'><% for(var i = 0; i < daysOfTheWeek.length; i++) { %><td class='header-day'><%= daysOfTheWeek[i] %></td><% } %></tr></thead><tbody><% for(var i = 0; i < numberOfRows; i++){ %><tr><% for(var j = 0; j < 7; j++){ %><% var d = j + i * 7; %><td class='<%= days[d].classes %>'><div class='day-contents'><%= days[d].day %></div></td><% } %></tr><% } %></tbody></table>",e="clndr",f={template:d,weekOffset:0,startWithMonth:null,clickEvents:{click:null,nextMonth:null,previousMonth:null,nextYear:null,previousYear:null,today:null,onMonthChange:null,onYearChange:null},targets:{nextButton:"clndr-next-button",previousButton:"clndr-previous-button",nextYearButton:"clndr-next-year-button",previousYearButton:"clndr-previous-year-button",todayButton:"clndr-today-button",day:"day",empty:"empty"},events:[],extras:null,dateParameter:"date",multiDayEvents:null,doneRendering:null,render:null,daysOfTheWeek:null,showAdjacentMonths:!0,adjacentDaysChangeMonth:!1,ready:null,constraints:null,forceSixRows:null};c.prototype.init=function(){if(this.daysOfTheWeek=this.options.daysOfTheWeek||[],!this.options.daysOfTheWeek){this.daysOfTheWeek=[];for(var c=0;7>c;c++)this.daysOfTheWeek.push(b().weekday(c).format("dd").charAt(0))}if(this.options.weekOffset&&(this.daysOfTheWeek=this.shiftWeekdayLabels(this.options.weekOffset)),!a.isFunction(this.options.render)){if(this.options.render=null,"undefined"==typeof _)throw new Error("Underscore was not found. Please include underscore.js OR provide a custom render function.");this.compiledClndrTemplate=_.template(this.options.template)}a(this.element).html("<div class='clndr'></div>"),this.calendarContainer=a(".clndr",this.element),this.bindEvents(),this.render(),this.options.ready&&this.options.ready.apply(this,[])},c.prototype.shiftWeekdayLabels=function(a){for(var b=this.daysOfTheWeek,c=0;a>c;c++)b.push(b.shift());return b},c.prototype.createDaysObject=function(c){daysArray=[];var d=c.startOf("month");if(this.eventsLastMonth=[],this.eventsThisMonth=[],this.eventsNextMonth=[],this.options.events.length)if(this.options.multiDayEvents){if(this.eventsThisMonth=a(this.options.events).filter(function(){return this._clndrStartDateObject.format("YYYY-MM")===c.format("YYYY-MM")||this._clndrEndDateObject.format("YYYY-MM")===c.format("YYYY-MM")?!0:this._clndrStartDateObject.format("YYYY-MM")<=c.format("YYYY-MM")&&this._clndrEndDateObject.format("YYYY-MM")>=c.format("YYYY-MM")?!0:!1}).toArray(),this.options.showAdjacentMonths){var e=c.clone().subtract(1,"months"),f=c.clone().add(1,"months");this.eventsLastMonth=a(this.options.events).filter(function(){return this._clndrStartDateObject.format("YYYY-MM")===e.format("YYYY-MM")||this._clndrEndDateObject.format("YYYY-MM")===e.format("YYYY-MM")?!0:this._clndrStartDateObject.format("YYYY-MM")<=e.format("YYYY-MM")&&this._clndrEndDateObject.format("YYYY-MM")>=e.format("YYYY-MM")?!0:!1}).toArray(),this.eventsNextMonth=a(this.options.events).filter(function(){return this._clndrStartDateObject.format("YYYY-MM")===f.format("YYYY-MM")||this._clndrEndDateObject.format("YYYY-MM")===f.format("YYYY-MM")?!0:this._clndrStartDateObject.format("YYYY-MM")<=f.format("YYYY-MM")&&this._clndrEndDateObject.format("YYYY-MM")>=f.format("YYYY-MM")?!0:!1}).toArray()}}else if(this.eventsThisMonth=a(this.options.events).filter(function(){return this._clndrDateObject.format("YYYY-MM")==c.format("YYYY-MM")}).toArray(),this.options.showAdjacentMonths){var e=c.clone().subtract(1,"months"),f=c.clone().add(1,"months");this.eventsLastMonth=a(this.options.events).filter(function(){return this._clndrDateObject.format("YYYY-MM")==e.format("YYYY-MM")}).toArray(),this.eventsNextMonth=a(this.options.events).filter(function(){return this._clndrDateObject.format("YYYY-MM")==f.format("YYYY-MM")}).toArray()}var g=d.weekday()-this.options.weekOffset;if(0>g&&(g+=7),this.options.showAdjacentMonths)for(var h=0;g>h;h++){var i=b([c.year(),c.month(),h-g+1]);daysArray.push(this.createDayObject(i,this.eventsLastMonth))}else for(var h=0;g>h;h++)daysArray.push(this.calendarDay({classes:this.options.targets.empty+" last-month"}));for(var j=d.daysInMonth(),h=1;j>=h;h++){var i=b([c.year(),c.month(),h]);daysArray.push(this.createDayObject(i,this.eventsThisMonth))}for(var h=1;daysArray.length%7!==0;){if(this.options.showAdjacentMonths){var i=b([c.year(),c.month(),j+h]);daysArray.push(this.createDayObject(i,this.eventsNextMonth))}else daysArray.push(this.calendarDay({classes:this.options.targets.empty+" next-month"}));h++}if(this.options.forceSixRows&&42!==daysArray.length)for(var k=b(daysArray[daysArray.length-1].date).add(1,"days");daysArray.length<42;)this.options.showAdjacentMonths?(daysArray.push(this.createDayObject(b(k),this.eventsNextMonth)),k.add(1,"days")):daysArray.push(this.calendarDay({classes:this.options.targets.empty+" next-month"}));return daysArray},c.prototype.createDayObject=function(a,c){var d=[],e=b(),f=this,g=0,h=c.length;for(g;h>g;g++)if(f.options.multiDayEvents){var i=c[g]._clndrStartDateObject,j=c[g]._clndrEndDateObject;(a.isSame(i,"day")||a.isAfter(i,"day"))&&(a.isSame(j,"day")||a.isBefore(j,"day"))&&d.push(c[g])}else c[g]._clndrDateObject.date()==a.date()&&d.push(c[g]);var k="";return e.format("YYYY-MM-DD")==a.format("YYYY-MM-DD")&&(k+=" today"),a.isBefore(e,"day")&&(k+=" past"),d.length&&(k+=" event"),this.month.month()>a.month()?(k+=" adjacent-month",k+=this.month.year()===a.year()?" last-month":" next-month"):this.month.month()<a.month()&&(k+=" adjacent-month",k+=this.month.year()===a.year()?" next-month":" last-month"),this.options.constraints&&(this.options.constraints.startDate&&a.isBefore(b(this.options.constraints.startDate))&&(k+=" inactive"),this.options.constraints.endDate&&a.isAfter(b(this.options.constraints.endDate))&&(k+=" inactive")),!a.isValid()&&a.hasOwnProperty("_d")&&void 0!=a._d&&(a=b(a._d)),k+=" calendar-day-"+a.format("YYYY-MM-DD"),k+=" calendar-dow-"+a.weekday(),this.calendarDay({day:a.date(),classes:this.options.targets.day+k,events:d,date:a})},c.prototype.render=function(){this.calendarContainer.children().remove();var a=this.createDaysObject(this.month),c=(this.month,{daysOfTheWeek:this.daysOfTheWeek,numberOfRows:Math.ceil(a.length/7),days:a,month:this.month.format("MMMM"),year:this.month.year(),eventsThisMonth:this.eventsThisMonth,eventsLastMonth:this.eventsLastMonth,eventsNextMonth:this.eventsNextMonth,extras:this.options.extras});if(this.calendarContainer.html(this.options.render?this.options.render.apply(this,[c]):this.compiledClndrTemplate(c)),this.options.constraints){for(var d in this.options.targets)d!=this.options.targets.day&&this.element.find("."+this.options.targets[d]).toggleClass("inactive",!1);var e=null,f=null;this.options.constraints.startDate&&(e=b(this.options.constraints.startDate)),this.options.constraints.endDate&&(f=b(this.options.constraints.endDate)),e&&this.month.isSame(e,"month")&&this.element.find("."+this.options.targets.previousButton).toggleClass("inactive",!0),f&&this.month.isSame(f,"month")&&this.element.find("."+this.options.targets.nextButton).toggleClass("inactive",!0),e&&b(e).subtract(1,"years").isBefore(b(this.month).subtract(1,"years"))&&this.element.find("."+this.options.targets.previousYearButton).toggleClass("inactive",!0),f&&b(f).add(1,"years").isAfter(b(this.month).add(1,"years"))&&this.element.find("."+this.options.targets.nextYearButton).toggleClass("inactive",!0),(e&&e.isAfter(b(),"month")||f&&f.isBefore(b(),"month"))&&this.element.find("."+this.options.targets.today).toggleClass("inactive",!0)}this.options.doneRendering&&this.options.doneRendering.apply(this,[])},c.prototype.bindEvents=function(){var b=a(this.element),c=this;b.on("click","."+this.options.targets.day,function(b){if(c.options.clickEvents.click){var d=c.buildTargetObject(b.currentTarget,!0);c.options.clickEvents.click.apply(c,[d])}c.options.adjacentDaysChangeMonth&&(a(b.currentTarget).is(".last-month")?c.backActionWithContext(c):a(b.currentTarget).is(".next-month")&&c.forwardActionWithContext(c))}),b.on("click","."+this.options.targets.empty,function(b){if(c.options.clickEvents.click){var d=c.buildTargetObject(b.currentTarget,!1);c.options.clickEvents.click.apply(c,[d])}c.options.adjacentDaysChangeMonth&&(a(b.currentTarget).is(".last-month")?c.backActionWithContext(c):a(b.currentTarget).is(".next-month")&&c.forwardActionWithContext(c))}),b.on("click","."+this.options.targets.previousButton,{context:this},this.backAction).on("click","."+this.options.targets.nextButton,{context:this},this.forwardAction).on("click","."+this.options.targets.todayButton,{context:this},this.todayAction).on("click","."+this.options.targets.nextYearButton,{context:this},this.nextYearAction).on("click","."+this.options.targets.previousYearButton,{context:this},this.previousYearAction)},c.prototype.buildTargetObject=function(c,d){var e={element:c,events:[],date:null};if(d){var f,g=c.className.indexOf("calendar-day-");0!==g?(f=c.className.substring(g+13,g+23),e.date=b(f)):e.date=null,this.options.events&&(e.events=a.makeArray(this.options.multiDayEvents?a(this.options.events).filter(function(){return(e.date.isSame(this._clndrStartDateObject,"day")||e.date.isAfter(this._clndrStartDateObject,"day"))&&(e.date.isSame(this._clndrEndDateObject,"day")||e.date.isBefore(this._clndrEndDateObject,"day"))}):a(this.options.events).filter(function(){return this._clndrDateObject.format("YYYY-MM-DD")==f})))}return e},c.prototype.forwardAction=function(a){var b=a.data.context;b.forwardActionWithContext(b)},c.prototype.backAction=function(a){var b=a.data.context;b.backActionWithContext(b)},c.prototype.backActionWithContext=function(a){if(!a.element.find("."+a.options.targets.previousButton).hasClass("inactive")){var c=!a.month.isSame(b(a.month).subtract(1,"months"),"year");a.month.subtract(1,"months"),a.render(),a.options.clickEvents.previousMonth&&a.options.clickEvents.previousMonth.apply(a,[b(a.month)]),a.options.clickEvents.onMonthChange&&a.options.clickEvents.onMonthChange.apply(a,[b(a.month)]),c&&a.options.clickEvents.onYearChange&&a.options.clickEvents.onYearChange.apply(a,[b(a.month)])}},c.prototype.forwardActionWithContext=function(a){if(!a.element.find("."+a.options.targets.nextButton).hasClass("inactive")){var c=!a.month.isSame(b(a.month).add(1,"months"),"year");a.month.add(1,"months"),a.render(),a.options.clickEvents.nextMonth&&a.options.clickEvents.nextMonth.apply(a,[b(a.month)]),a.options.clickEvents.onMonthChange&&a.options.clickEvents.onMonthChange.apply(a,[b(a.month)]),c&&a.options.clickEvents.onYearChange&&a.options.clickEvents.onYearChange.apply(a,[b(a.month)])}},c.prototype.todayAction=function(a){var c=a.data.context,d=!c.month.isSame(b(),"month"),e=!c.month.isSame(b(),"year");c.month=b().startOf("month"),c.options.clickEvents.today&&c.options.clickEvents.today.apply(c,[b(c.month)]),d&&(c.render(),c.month=b(),c.options.clickEvents.onMonthChange&&c.options.clickEvents.onMonthChange.apply(c,[b(c.month)]),e&&c.options.clickEvents.onYearChange&&c.options.clickEvents.onYearChange.apply(c,[b(c.month)]))},c.prototype.nextYearAction=function(a){var c=a.data.context;c.element.find("."+c.options.targets.nextYearButton).hasClass("inactive")||(c.month.add(1,"years"),c.render(),c.options.clickEvents.nextYear&&c.options.clickEvents.nextYear.apply(c,[b(c.month)]),c.options.clickEvents.onMonthChange&&c.options.clickEvents.onMonthChange.apply(c,[b(c.month)]),c.options.clickEvents.onYearChange&&c.options.clickEvents.onYearChange.apply(c,[b(c.month)]))},c.prototype.previousYearAction=function(a){var c=a.data.context;c.element.find("."+c.options.targets.previousYear).hasClass("inactive")||(c.month.subtract(1,"years"),c.render(),c.options.clickEvents.previousYear&&c.options.clickEvents.previousYear.apply(c,[b(c.month)]),c.options.clickEvents.onMonthChange&&c.options.clickEvents.onMonthChange.apply(c,[b(c.month)]),c.options.clickEvents.onYearChange&&c.options.clickEvents.onYearChange.apply(c,[b(c.month)]))},c.prototype.forward=function(a){return this.month.add(1,"months"),this.render(),a&&a.withCallbacks&&(this.options.clickEvents.onMonthChange&&this.options.clickEvents.onMonthChange.apply(this,[b(this.month)]),0===this.month.month()&&this.options.clickEvents.onYearChange&&this.options.clickEvents.onYearChange.apply(this,[b(this.month)])),this},c.prototype.back=function(a){return this.month.subtract(1,"months"),this.render(),a&&a.withCallbacks&&(this.options.clickEvents.onMonthChange&&this.options.clickEvents.onMonthChange.apply(this,[b(this.month)]),11===this.month.month()&&this.options.clickEvents.onYearChange&&this.options.clickEvents.onYearChange.apply(this,[b(this.month)])),this},c.prototype.next=function(a){return this.forward(a),this},c.prototype.previous=function(a){return this.back(a),this},c.prototype.setMonth=function(a,c){return this.month.month(a),this.render(),c&&c.withCallbacks&&this.options.clickEvents.onMonthChange&&this.options.clickEvents.onMonthChange.apply(this,[b(this.month)]),this},c.prototype.nextYear=function(a){return this.month.add(1,"year"),this.render(),a&&a.withCallbacks&&this.options.clickEvents.onYearChange&&this.options.clickEvents.onYearChange.apply(this,[b(this.month)]),this},c.prototype.previousYear=function(a){return this.month.subtract(1,"year"),this.render(),a&&a.withCallbacks&&this.options.clickEvents.onYearChange&&this.options.clickEvents.onYearChange.apply(this,[b(this.month)]),this},c.prototype.setYear=function(a,c){return this.month.year(a),this.render(),c&&c.withCallbacks&&this.options.clickEvents.onYearChange&&this.options.clickEvents.onYearChange.apply(this,[b(this.month)]),this},c.prototype.setEvents=function(a){return this.options.events=this.options.multiDayEvents?this.addMultiDayMomentObjectsToEvents(a):this.addMomentObjectToEvents(a),this.render(),this},c.prototype.addEvents=function(b){return this.options.events=this.options.multiDayEvents?a.merge(this.options.events,this.addMultiDayMomentObjectsToEvents(b)):a.merge(this.options.events,this.addMomentObjectToEvents(b)),this.render(),this},c.prototype.removeEvents=function(a){for(var b=this.options.events.length-1;b>=0;b--)1==a(this.options.events[b])&&this.options.events.splice(b,1);return this.render(),this},c.prototype.addMomentObjectToEvents=function(a){var c=this,d=0,e=a.length;for(d;e>d;d++)a[d]._clndrDateObject=b(a[d][c.options.dateParameter]);return a},c.prototype.addMultiDayMomentObjectsToEvents=function(a){var c=this,d=0,e=a.length;for(d;e>d;d++)a[d]._clndrStartDateObject=b(a[d][c.options.multiDayEvents.startDate]),a[d]._clndrEndDateObject=b(a[d][c.options.multiDayEvents.endDate]);return a},c.prototype.calendarDay=function(b){var c={day:"",classes:this.options.targets.empty,events:[],date:null};return a.extend({},c,b)},a.fn.clndr=function(a){if(1===this.length){if(!this.data("plugin_clndr")){var b=new c(this,a);return this.data("plugin_clndr",b),b}}else if(this.length>1)throw new Error("CLNDR does not support multiple elements yet. Make sure your clndr selector returns only one element.")}});