Skip to content

Instantly share code, notes, and snippets.

@lazarofl
Created February 1, 2016 21:28
Show Gist options
  • Save lazarofl/d0d28801cb35792ebb9e to your computer and use it in GitHub Desktop.
Save lazarofl/d0d28801cb35792ebb9e to your computer and use it in GitHub Desktop.
!function(){"use strict";angular.module("ngCalendarium",[]).constant("calendarConfig",{template:['<div class="calendarium">','<div class="header">','<button class="previous" ng-click="previousMonth()" title="previous month" ><<</button>',"<h1>{{monthLabel}}/{{yearLabel}}</h1>",'<button class="next" ng-click="nextMonth()" title="next month" >>></button>',"</div>",'<ol class="day-names clear">','<li ng-repeat="weekDay in weekDays">{{weekDay}}</li>',"</o>",'<ol class="days">','<li ng-repeat="dateContainer in dateContainers" title="{{dateContainer.state.tooltip}}">','<div ng-if="dateContainer.state.disabled" class="date disabled" ng-class="dateContainer.state.className"></div>','<div ng-if="!dateContainer.state.disabled && !isDate(dateContainer.date)" class="date empty"></div>','<div ng-if="!dateContainer.state.disabled && isDate(dateContainer.date)" ng-class="dateContainer.state.className" class="date" ng-click="selectDate(dateContainer.date,dateContainer.state)">{{dateContainer.date.getDate()}}</div>',"</li>","</ol>","</div>"].join(""),day_labels:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],month_labels:["January","February","March","April","May","June","July","August","September","October","November","December"],days_in_month:[31,28,31,30,31,30,31,31,30,31,30,31]}).factory("_",["$window",function(e){return e._}]).service("calendariumService",["calendarConfig","$http","$q",function(e,t,a){var n=this;n.getDateIntervals=function(t,a){var n,r,i=new Date(t,a,1),s=[];r=e.days_in_month[a],1===a&&(t%4===0&&t%100!==0||t%400===0)&&(r+=1);for(var o=1;r>=o;o++)s.push(o);return n=new Date(t,a,s.length),{firstWeekDate:i.getDay(),firstDate:i,lastDate:n,days:s}},n.loadCalendarDates=function(e,r,i){var s=a.defer();if(!e)return s.resolve(n.getDateIntervals(r,i)),s.promise;var o=n.getDateIntervals(r,i);return t({url:e,method:"GET",params:{firstDate:o.firstDate.toISOString(),lastDate:o.lastDate.toISOString()}}).then(function(e){var t=angular.extend({dates:e.data},o);return t},function(e){throw e})}}]).directive("calendarium",["$parse","calendarConfig","calendariumService","_",function(e,t,a,n){return{restrict:"E",replace:!0,scope:{month:"@",year:"@",onDateSelected:"&",url:"@"},template:t.template,link:function(e){var r=e.onDateSelected();e._year=null===e.year||isNaN(e.year)?(new Date).getFullYear():e.year,e._month=null===e.month||isNaN(e.month)?(new Date).getMonth():e.month,e.weekDays=t.day_labels,e.isDate=function(e){return e instanceof Date},e.selectDate=function(e,t){r&&r(e,t)},e.previousMonth=function(){0===e._month?(e._month=11,e._year=e._year-1):e._month=e._month-1,e.render()},e.nextMonth=function(){11===e._month?(e._month=0,e._year=e._year+1):e._month=e._month+1,e.render()},e.render=function(){var r=a.loadCalendarDates(e.url,e._year,e._month);r.then(function(a){if(e.monthLabel=t.month_labels[a.firstDate.getMonth()],e.yearLabel=a.firstDate.getFullYear(),e.dateContainers=[],n.each(a.days,function(t){var r={date:new Date(a.firstDate.getFullYear(),a.firstDate.getMonth(),t),state:n.findWhere(a.dates,{day:t})||{disabled:!1}};e.dateContainers.push(r)}),0!==a.firstDate.getDay())for(var r=0;r<a.firstDate.getDay();r++)e.dateContainers.unshift({date:r,state:{disabled:!1}});if(e.dateContainers.length%7!==0)for(var i=e.dateContainers.length%7,s=0;7-i>s;s++)e.dateContainers.push({date:s,state:{disabled:!1}})},function(e){throw e})},e.render()}}}])}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment