1 line
6.6 KiB
JavaScript
1 line
6.6 KiB
JavaScript
!function(t){var e={events:[],detailedWidthBreakpoint:550,onEventClick:null,onPreviousMonthStart:null,onNextMonthStart:null,onCurrentMonthStart:null,onLoadStart:null,onLoadEnd:null},n={start:null,end:null,title:"",url:"","class":"",color:"",data:{}},a={init:function(a){return this.each(function(){var s=t(this),o=s.data("equinox");if(!o){for(var d=t.extend({},e,a),i=moment(),v=[],u=0;u<d.events.length;u++)d.events[u]=t.extend({},n,d.events[u]),d.events[u].start=moment(d.events[u].start),d.events[u].end=moment(d.events[u].end),d.events[u].start.isValid()&&d.events[u].end.isValid()&&(d.events[u].start.valueOf()>d.events[u].end.valueOf()||(d.events[u].allDay=r(d.events[u].start,d.events[u].end),d.events[u].daySpan=l(d.events[u].start,d.events[u].end),v.push(d.events[u])));d.events.sort(function(t,e){return t.start.valueOf()-e.start.valueOf()});for(var c=[],u=0;u<d.events.length;u++){d.events[u].id=u;var f=moment(d.events[u].start).startOf("day").format("UX");f in c||(c[f]=[]),c[f].push(d.events[u])}o={target:s,settings:d,currentDate:i,segmentedEvents:c,activeMonth:moment(i).startOf("month")},s.data("equinox",o),s.addClass("equinox"),s.equinox("refresh"),s.equinox("load"),t(window).resize(function(){s.equinox("refresh")}),s.on("click",".calendar-actions button.prev",function(t){var e=s.data("equinox");e.activeMonth.subtract(1,"months"),typeof e.settings.onPreviousMonthStart==typeof Function&&e.settings.onPreviousMonthStart(t,e),s.equinox("load")}),s.on("click",".calendar-actions button.next",function(t){var e=s.data("equinox");e.activeMonth.add(1,"months"),typeof e.settings.onNextMonthStart==typeof Function&&e.settings.onNextMonthStart(t,e),s.equinox("load")}),s.on("click",".calendar-actions button.today",function(t){var e=s.data("equinox"),n=moment().startOf("month");typeof e.settings.onCurrentMonthStart==typeof Function&&e.settings.onCurrentMonthStart(t,e),0!==e.activeMonth.startOf("month").diff(n)&&(e.activeMonth=n,s.equinox("load"))}),s.on("click","a.event",function(e){var n=s.data("equinox");if(typeof n.settings.onEventClick==typeof Function){var a=n.settings.events[t(this).data("event-id")],o=t.extend({start:a.start,end:a.end,title:a.title,url:a.url,"class":a["class"]},a.data);n.settings.onEventClick(e,o)}})}})},load:function(){return this.each(function(){var e=t(this),n=e.data("equinox");typeof n.settings.onLoadStart==typeof Function&&n.settings.onLoadStart(n);var a=n.activeMonth,o=s(a,n.segmentedEvents);typeof n.settings.onLoadEnd==typeof Function&&n.settings.onLoadEnd(n),e.html(o)})},refresh:function(){return this.each(function(){var e=t(this),n=e.data("equinox");e.width()<n.settings.detailedWidthBreakpoint?e.removeClass("detailed"):e.addClass("detailed")})}};t.fn.equinox=function(e){return a[e]?a[e].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof e&&e?void t.error("Method "+e+" does not exist on jQuery.equinox"):a.init.apply(this,arguments)};var s=function(e,n){var a=t('<div class="calendar"></div>'),s=t('<div class="calendar-header"></div>');s.append('<div class="month-label">'+e.format("MMMM YYYY")+"</div>"),s.append('<div class="calendar-actions"></div>'),t(".calendar-actions",s).append('<button type="button" class="prev">Previous</button> '),t(".calendar-actions",s).append('<button type="button" class="next">Next</button> '),t(".calendar-actions",s).append('<button type="button" class="today">Today</button> '),a.append(s);for(var r=t('<div class="month-weeks"></div>'),v=t('<div class="week week-header"><div class="days-container"></div></div>'),u=0;u<7;u++)t(".days-container",v).append('<div class="day '+moment().day(u).format("dddd").toLowerCase()+'">'+moment().day(u).format("dddd")+"</div>");r.append(v);var c=moment(e).startOf("month").startOf("week"),f=[];for(var u in n){if(parseInt(u.substr(1))>=c.unix())break;for(var p=0;p<n[u].length;p++)n[u][p].end.valueOf()>c.valueOf()&&(n[u][p].daysLeft=l(c,n[u][p].end)-(i(n[u][p].end)?1:0),f.push(n[u][p]))}var h=!1,m=e.daysInMonth(),y=moment();for(u=0;!h;u++){for(var g=t('<div class="week week-'+u+'"></div>'),x=[[]],M=moment(c),p=0;p<7;p++){if(M.day(p),0===p)for(var S=0;S<f.length;S++){for(var b=0;x[b][p];)b++,b in x||(x[b]=[]);x[b][p]=f[S];for(var k=p+1;k<f[S].daysLeft+p&&k<7;k++)x[b][k]="reserved"}for(var L=(M.format("UX")in n?n[M.format("UX")]:[]),S=0;S<L.length;S++)if(!(L[S].daySpan<2)){for(var b=0;x[b][p];)b++,b in x||(x[b]=[]);L[S].daysLeft=L[S].daySpan-(i(L[S].end)?1:0),x[b][p]=L[S];for(var k=p+1;k<L[S].daysLeft+p&&k<7;k++)x[b][k]="reserved"}}f=[],g.append('<ul class="week-events"></ul>');for(var p=0;p<x.length;p++){for(var b=t('<li class="slot"><ul></ul></li>'),S=0;S<7;S++)if("reserved"!==x[p][S])if(x[p][S]){var O=t('<li class="event-container span-'+Math.min(x[p][S].daysLeft,7-S)+'"></li>');x[p][S].daySpan-(i(x[p][S].end)?1:0)>x[p][S].daysLeft&&O.addClass("continuation"),x[p][S].daysLeft>7-S&&O.addClass("continued"),O.append(d(x[p][S])),t("ul",b).append(O),x[p][S].daysLeft>7-S?(x[p][S].daysLeft-=7-S,f.push(x[p][S])):delete x[p][S].daysLeft}else t("ul",b).append('<li class="event-container"></li>');t(".week-events",g).append(b)}g.append('<div class="days-container"></div>');for(var p=0;p<7;p++){c.day(p);for(var q=[],S=0;S<x.length;S++)x[S][p]&&q.push(S);var L=c.format("UX")in n?n[c.format("UX")]:[];t(".days-container",g).append(o(c,L,q,e,y)),c.month()===e.month()&&c.date()===m&&(h=!0)}r.append(g),c.add(1,"days")}return a.append(r),a},o=function(e,n,a,s,o){var r=["date","date-"+e.date(),"day",moment(e).format("dddd").toLowerCase()];e.month()!==s.month()&&r.push("outside-month"),moment(e).startOf("day").valueOf()===moment(o).startOf("day").valueOf()&&r.push("current-date"),(n.length||a.length)&&r.push("has-events");var i=t('<div class="'+r.join(" ")+'"></div>');i.append('<div class="date-label">'+(1===e.date()?e.format("MMM "):"")+e.date()+"</div>"),i.append('<ul class="date-events"></ul>');for(var l=0,v=a.shift(),u=0;u<n.length;u++){if(l===v)t(".date-events",i).append('<li class="event-container placeholder"></li>'),v=a.shift(),u--;else{if(n[u].daySpan>=2)continue;var c=t('<li class="event-container"></li>');c.append(d(n[u])),t(".date-events",i).append(c)}l++}return i},d=function(e){var n=t('<a class="event '+e["class"]+'" href="'+e.url+'" '+(""!=e.color?'style="border-color: '+e.color+';"':"")+"></a>");return n.data("event-id",e.id),n.append('<span class="start-time">'+e.start.format(0===e.start.minutes()?"ha":"h:mma")+"</span>"),n.append('<span class="title">'+e.title+"</span>"),n},r=function(t,e){return e.diff(t)%moment.duration(1,"days").asMilliseconds()===0},i=function(t){return"00:00:00.000"===t.format("HH:mm:ss.SSS")},l=function(t,e){var n=moment(t).startOf("day"),a=moment(e).startOf("day");return a.diff(n)/moment.duration(1,"days").asMilliseconds()+1}}(jQuery); |