/
weekly.full.min.js
9 lines (8 loc) · 19.4 KB
/
weekly.full.min.js
1
2
3
4
5
6
7
8
9
/*!
* weekly - jQuery Weekly Calendar Plugin
* v0.5.2
* https://github.com/firstandthird/weekly
* copyright First + Third 2014
* MIT License
*/
!function(a){a.fn.fitText=function(b,c){var d=b||1,e=a.extend({minFontSize:Number.NEGATIVE_INFINITY,maxFontSize:Number.POSITIVE_INFINITY},c);return this.each(function(){var b=a(this),c=function(){b.css("font-size",Math.max(Math.min(b.width()/(10*d),parseFloat(e.maxFontSize)),parseFloat(e.minFontSize)))};c(),a(window).on("resize.fittext orientationchange.fittext",c)})}}(jQuery),function(){var a=this,b=a.dateFormat,c="January|February|March|April|May|June|July|August|September|October|November|December".split("|"),d="Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday".split("|"),e=function(a,b){return!b instanceof Date?void 0:a.replace(/%d|%D|%j|%l|%S|%w|%F|%m|%M|%n|%Y|%y|%a|%A|%g|%G|%h|%H|%i|%s|%u|%e/g,function(a){switch(a){case"%d":return("0"+b.getDate()).slice(-2);case"%D":return d[b.getDay()].substr(0,3);case"%j":return b.getDate();case"%l":return d[b.getDay()];case"%S":return 1===b.getDate()?"st":2===b.getDate()?"nd":3===b.getDate()?"rd":"th";case"%w":return b.getDay();case"%F":return c[b.getMonth()];case"%m":return("0"+(b.getMonth()+1)).slice(-2);case"%M":return c[b.getMonth()].substr(0,3);case"%n":return b.getMonth()+1;case"%Y":return b.getFullYear();case"%y":return b.getFullYear().toString().slice(-2);case"%a":return b.getHours()>11?"pm":"am";case"%A":return b.getHours()>11?"PM":"AM";case"%g":return b.getHours()>12?b.getHours()-12:b.getHours()?b.getHours():12;case"%G":return b.getHours();case"%h":return("0"+(b.getHours()>12?b.getHours()-12:b.getHours())).slice(-2);case"%H":return("0"+b.getHours()).slice(-2);case"%i":return("0"+b.getMinutes()).slice(-2);case"%s":return("0"+b.getSeconds()).slice(-2);case"%u":return b.getMilliseconds();case"%e":return b.getTimezoneOffset()}})};e.translate=function(a,b){c=a,d=b},e.noConflict=function(){return a.dateFormat=b,e},"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=e),exports.dateFormat=e):a.dateFormat=e}.call(this),function(a,b){var c=0,d=function(a){this.obj=a};d.prototype.__init=function(a){b.extend(this,this.obj),this.id=c++,this.namespace=".fidel"+this.id,this.obj.defaults=this.obj.defaults||{},b.extend(this,this.obj.defaults,a),b("body").trigger("FidelPreInit",this),this.setElement(this.el||b("<div/>")),this.init&&this.init(),b("body").trigger("FidelPostInit",this)},d.prototype.eventSplitter=/^(\w+)\s*(.*)$/,d.prototype.setElement=function(a){this.el=a,this.getElements(),this.dataElements(),this.delegateEvents(),this.delegateActions()},d.prototype.find=function(a){return this.el.find(a)},d.prototype.proxy=function(a){return b.proxy(a,this)},d.prototype.getElements=function(){if(this.elements)for(var a in this.elements){var b=this.elements[a];this[b]=this.find(a)}},d.prototype.dataElements=function(){var a=this;this.find("[data-element]").each(function(c,d){var e=b(d),f=e.data("element");a[f]=e})},d.prototype.delegateEvents=function(){if(this.events)for(var a in this.events){var b=this.events[a],c=a.match(this.eventSplitter),d=c[1],e=c[2],f=this.proxy(this[b]);""===e?this.el.on(d+this.namespace,f):this[e]&&"function"!=typeof this[e]?this[e].on(d+this.namespace,f):this.el.on(d+this.namespace,e,f)}},d.prototype.delegateActions=function(){var a=this;a.el.on("click"+this.namespace,"[data-action]",function(c){var d=b(this),e=d.attr("data-action");a[e]&&a[e](c,d)})},d.prototype.on=function(a,b){this.el.on(a+this.namespace,b)},d.prototype.one=function(a,b){this.el.one(a+this.namespace,b)},d.prototype.emit=function(a,b,c){var d=c?this.namespace:"";this.el.trigger(a+d,b)},d.prototype.hide=function(){if(this.views)for(var a in this.views)this.views[a].hide();this.el.hide()},d.prototype.show=function(){if(this.views)for(var a in this.views)this.views[a].show();this.el.show()},d.prototype.destroy=function(){this.el.empty(),this.emit("destroy"),this.el.unbind(this.namespace)},d.declare=function(a){var b=function(a,b){this.__init(a,b)};return b.prototype=new d(a),b},d.onPreInit=function(a){b("body").on("FidelPreInit",function(b,c){a.call(c)})},d.onPostInit=function(a){b("body").on("FidelPostInit",function(b,c){a.call(c)})},a.Fidel=d}(window,window.jQuery||window.Zepto),function(a){a.declare=function(b,c){a.fn[b]=function(){var d,e,f=Array.prototype.slice.call(arguments),g=f.shift();return e=this.each(function(){var e=a(this),h=e.data(b);if(!h){var i=Fidel.declare(c),j=a.extend({},g,{el:e});h=new i(j),e.data(b,h)}"string"==typeof g&&(d=h[g].apply(h,f))}),"undefined"!=typeof d?d:e},a.fn[b].defaults=c.defaults||{}},a.Fidel=window.Fidel}(jQuery),function(a){var b=a.template,c={};opts={openTag:"<%",closeTag:"%>"};var d=function e(a,b){var d=/\W/.test(a)?new Function("obj","var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push('"+a.replace(/[\r\t\n]/g," ").split(opts.openTag).join(" ").replace(new RegExp("((^|"+opts.closeTag+")[^ ]*)'","g"),"$1\r").replace(new RegExp(" =(.*?)"+opts.closeTag,"g"),"',$1,'").split(" ").join("');").split(opts.closeTag).join("p.push('").split("\r").join("\\'")+"');}return p.join('');"):c[a]=c[a]||e(a);return b?d(b):d};d.options=opts,d.noConflict=function(){return a.template=b,d},a.template=d}(window),function(a){a.template=template.noConflict(),a.prototype.render=function(b,c){var d=this.template?this.template:$("#"+this.templateId).html();c=c||this.el,c.html(a.template(d,b))}}(window.Fidel),function(a){var b=a.dateUtils,c={noConflict:function(){return a.dateUtils=b,c},getFirstDayOfWeek:function(a,b){b=b||0;var c=a.getDate()-a.getDay(),d=new Date(a.getTime());return d.setDate(c+7*b),d},getLastDayOfWeek:function(a,b){b=b||0;var c=a.getDate()-a.getDay(),d=new Date(a.getTime());return d.setDate(c+6+7*b),d},getDates:function(a,b,c,d){a=new Date(a),a.setHours(0,0,0),c=c||0;for(var e=d||7,f=[],g=this.getFirstDayOfWeek(a,b),h=0,i=e;i>h;h++){var j=new Date(g.getTime());j.setDate(j.getDate()-j.getDay()+c+h),f.push(j)}return f},getTimes:function(a,b){for(var c=b+12,d=[],e=a;c>e;e++){var f=e>12||0===e?Math.abs(e-12):e,g=f+":00 ";g+=e>11?"PM":"AM",d.push(g)}return d},getWeekSpan:function(a,b,c){c=c||0;var d=this.getFirstDayOfWeek(a,b),e=this.getLastDayOfWeek(a,b);d.setDate(d.getDate()+c),e.setDate(e.getDate()+c);var f=dateFormat("%M %d",d)+" - ";return f+=d.getMonth()==e.getMonth()?dateFormat("%d",e):dateFormat("%M %d",e)},getDaySpan:function(a,b,c){c=c||0;var d=this.getFirstDayOfWeek(a,b);d.setDate(d.getDate()+c);var e=dateFormat("%D, %M %d",d);return e},realTimezoneOffset:function(a){var b=(new Date).getTimezoneOffset()/-60,c=a-b;return c},isPastDate:function(a){var b=a.split("-");return dateFormat("%Y%m%d",new Date(b[0],b[1],b[2]))<dateFormat("%Y%m%d",new Date)},getWeekOffset:function(a,b){var c=this.getFirstDayOfWeek(this.getDateWithoutTime(a)),d=this.getFirstDayOfWeek(this.getDateWithoutTime(b)),e=Math.floor((d.getTime()-c.getTime())/6048e5);return e},getDateWithoutTime:function(a){return new Date(a.getFullYear(),a.getMonth(),a.getDate())}};a.dateUtils=c}(window),function(a){a.declare("weekly",{defaults:{startTime:0,endTime:12,startTimeScrollOffset:"8:00 AM",scrollFirstEvent:!1,weekOffset:0,currentDate:new Date,autoRender:!0,fitText:!0,fitTextMin:12,fitTextMax:15,template:"##template##",readOnly:!1,enableResize:!0,enableDelete:!0,showToday:!0,allowPreviousWeeks:!0,allowPastEventCreation:!1,timezoneOffset:0,utcOffset:(new Date).getTimezoneOffset()/-60,selectableDates:null,todayFirst:!1,dayOffset:0,allowOverlap:!0,hoverPreviewDelay:250,interval:30,minDuration:30},events:{"click .weekly-event":"eventClicked"},init:function(){var a=this;if(this.events=[],this.oldDate=this.currentDate,this.todayFirst&&(this.dayOffset=this.currentDate.getDay()),(this.interval<1||this.interval>60)&&(this.interval=60),this.setSelectableDates(this.selectableDates,!0),this.autoRender){var b=this.update();this.emit("weekChange",b)}this.hoverPreviewTimer=null,this.hoverPreviewDelay!==!1&&this.el.on("mousemove",".weekly-day",this.proxy(function(b){a.readOnly||(this.removePreviewEvent(),this.hoverPreviewTimer=setTimeout(this.proxy(function(){this.showPreviewEvent(b)}),this.hoverPreviewDelay))})).on("mouseleave",".weekly-day",this.proxy(function(){a.readOnly||this.removePreviewEvent()}))},get:function(a){return this[a]},update:function(){this.firstEvent=null;var b={timef:dateFormat,canAdd:this.canAdd,getWeekSpan:dateUtils.getWeekSpan,currentDate:this.currentDate,dates:dateUtils.getDates(this.currentDate,this.weekOffset,this.dayOffset),times:dateUtils.getTimes(this.startTime,this.endTime),showPreviousWeekButton:this.allowPreviousWeeks||0!==this.weekOffset};this.render(b);for(var c=0,d=this.events.length;d>c;c++)this.renderEvent(this.events[c]);if(this.timeDifference=this.endTime+12-this.startTime,this.registerClickToCreate(),this.registerModifyEvent(),this.highlightToday(),this.highlightWeekend(),this.showToday&&this.weekOffset?this.el.find(".weekly-change-today-button").css("display","block"):this.el.find(".weekly-change-today-button").css("display","none"),this.el.find(".weekly-time-navigation .weekly-previous-week .week").html(dateUtils.getWeekSpan(this.currentDate,this.weekOffset-1,this.dayOffset)),this.el.find(".weekly-time-navigation .weekly-next-week .week").html(dateUtils.getWeekSpan(this.currentDate,this.weekOffset+1,this.dayOffset)),this.el.find(".weekly-time-navigation .weekly-header").html(dateUtils.getWeekSpan(this.currentDate,this.weekOffset,this.dayOffset)),this.fitText&&this.el.find(".weekly-days .weekly-day, .weekly-times .weekly-time").fitText(1,{minFontSize:this.fitTextMin,maxFontSize:this.fitTextMax}),this.startTimeScrollOffset&&!this.first){var e=a(window).scrollTop(),f=this.el.find('[data-time="'+this.startTimeScrollOffset+'"]');f[0].scrollIntoView(),a(window).scrollTop(e)}return{dates:b.dates,times:b.times}},highlightToday:function(){var a=this.currentDate,b=dateFormat("%Y-%m-%d",a);this.el.find('.weekly-grid [data-date="'+b+'"], .weekly-days [data-date="'+b+'"]').addClass("weekly-today")},highlightWeekend:function(){this.el.find(".weekly-grid .weekly-day").each(function(){var b=a(this),c=b.data("date").split("-"),d=new Date(c[0],c[1]-1,c[2]);d.getDay()%6===0&&b.addClass("weekly-weekend")})},registerClickToCreate:function(){this.mouseDown=!1,this.pendingEvent=null,this.pendingEventStart=null;var b=this.el.find(".weekly-grid .weekly-day");b.unbind("mousedown mousemove mouseup mouseout click"),this.readOnly||(b.on("mousedown",this.proxy(function(b){var c=a(b.target);if(1===b.which&&!c.is(".weekly-dragger")&&!c.is(".weekly-delete")){var d=a(b.currentTarget);!this.allowPastEventCreation&&dateUtils.isPastDate(d.data("date"))||!this.canAdd(d.data("date"))||(this.mouseDown=!0,this.pendingEvent&&(this.pendingEvent.remove(),this.pendingEvent=null))}})),b.on("mouseup",this.proxy(function(){if(this.mouseDown&&(this.mouseDown=!1,this.pendingEvent)){var a=this.pendingEvent.data(),b=a.date.split("-"),c=a.starttime-~~this.timezoneOffset,d=this.fromDecimal(a.starttime)-60*(this.timezoneOffset%1),e=a.endtime-~~this.timezoneOffset,f=this.fromDecimal(a.endtime)-60*(this.timezoneOffset%1);this.addEvent({start:new Date(b[0],b[1]-1,b[2],c,d),end:new Date(b[0],b[1]-1,b[2],e,f)}),this.pendingEvent.remove(),this.pendingEvent=null,this.pendingEventStart=null}})),b.on("mousemove",this.proxy(function(a){this.mouseDown&&this.createEvent(a)})),b.on("click",this.proxy(function(c){var d=a(c.currentTarget);!this.allowPastEventCreation&&dateUtils.isPastDate(d.data("date"))||!this.canAdd(d.data("date"))||a(c.target).is(".weekly-time,.weekly-day,.weekly-event-preview")&&(this.mouseDown=!0,this.createEvent(c),b.trigger("mouseup"))})),b.on("mouseleave",this.proxy(function(){this.mouseDown&&b.trigger("mouseup")})))},registerModifyEvent:function(){this.mouseModifyDown=!1;var b=this.el.find(".weekly-grid");this.currentDragger=null,b.find(".weekly-dragger").unbind("mousedown mousemove mouseup mouseout click"),this.readOnly||(b.on("mousedown",".weekly-dragger",this.proxy(function(b){1===b.which&&(this.mouseModifyDown=!0,this.currentDragger=a(b.target),this.eventOffset=0)})),b.on("mouseup",".weekly-day",this.proxy(function(){this.mouseModifyDown=!1,this.currentDragger=null})),b.on("mousemove",".weekly-day",this.proxy(function(a){this.mouseModifyDown&&this.modifyEvent(a)})),b.on("mouseleave",this.proxy(function(){this.mouseModifyDown&&this.currentDragger.trigger("mouseup")})))},createEvent:function(b){var c=a(b.currentTarget),d=c.parent().offset(),e=b.pageY-d.top,f=a(b.currentTarget).height(),g=Math.round(f/this.timeDifference),h=g/(60/this.interval),i=Math.floor(e/h)*h,j=Math.ceil(e/h)*h,k=c.data("date").split("-"),l={start:this.pendingEventStart,end:this.pendingEventEnd};i===j&&(j+=h),null===this.pendingEventStart&&(this.pendingEventStart=i),(null===this.pendingEventEnd||j>this.pendingEventStart)&&(this.pendingEventEnd=j);var m=(this.pendingEventStart/g||0)+this.startTime,n=(this.pendingEventEnd/g||1)+this.startTime;n-m<this.minDuration/60&&(n=m+this.minDuration/60);var o=new Date(k[0],k[1]-1,k[2],m-this.timezoneOffset,this.fromDecimal(m)),p=new Date(k[0],k[1]-1,k[2],n-this.timezoneOffset,this.fromDecimal(n));return!this.allowOverlap&&this.overlaps(o.getTime(),p.getTime())?(this.pendingEventStart=l.start,void(this.pendingEventEnd=l.end)):(this.pendingEvent||(c.append('<div class="weekly-event-pending"></div>'),this.pendingEvent=c.find(".weekly-event-pending"),this.pendingEvent.data("date",c.data("date"))),this.pendingEvent.css({top:this.pendingEventStart,bottom:f-this.pendingEventEnd}),this.pendingEvent.data("starttime",m),void this.pendingEvent.data("endtime",n))},modifyEvent:function(b){var c=this.currentDragger.parents(".weekly-event"),d=c.parent().offset(),e=b.pageY-d.top,f=a(b.currentTarget).height(),g=Math.round(f/this.timeDifference),h=g/(60/this.interval),i=Math.ceil(e/h)*h,j=c.outerHeight()/g,k=(new Date(c.data("start")),new Date(c.data("start")));return k.setHours(k.getHours()+~~j),k.setMinutes(this.fromDecimal(j)),this.intersects(c).length?(this.currentDragger.trigger("mouseup"),void c.css({bottom:f-i+h})):(c.data("end",k),i<d.top+f&&c.css({bottom:f-i}),this.events[c.data("_index")].end=k,void this.el.trigger("modifyEvent",this.events[c.data("_index")]))},nextWeek:function(){this.changeDate(1)},prevWeek:function(){this.changeDate(-1)},jumpToday:function(){this.changeDate(0)},jumpTo:function(a){this.todayFirst!==!1&&(this.dayOffset=a.getDay()),this.weekOffset=dateUtils.getWeekOffset(this.currentDate,a);var b=this.update();return this.emit("weekChange",b),this},changeDate:function(a){a?this.weekOffset+=a:this.weekOffset=0;var b=this.update();this.emit("weekChange",b)},renderEvent:function(b){var c=new Date(b.start.getTime()),d=new Date(b.end.getTime());c.setHours(c.getHours()+~~this.timezoneOffset),d.setHours(d.getHours()+~~this.timezoneOffset),c.setMinutes(c.getMinutes()+60*(this.timezoneOffset%1)),d.setMinutes(d.getMinutes()+60*(this.timezoneOffset%1));var e=dateFormat("%Y-%m-%d",c),f=c.toTimeString().slice(0,5),g=(dateFormat("%Y-%m-%d",d),d.toTimeString().slice(0,5));"00:00"===g&&(g="24:00");var h=100-this.getTimeOffsetPercent(f),i=this.getTimeOffsetPercent(g),j=a('<div class="weekly-event"></div>');j.data(b),j.data("offset-start",c),j.data("offset-end",d),j.css({top:h+"%",bottom:i+"%"}).append(['<button data-action="removeEvent" class="weekly-delete">×</button>','<div class="weekly-event-time">'+dateFormat("%g:%i",c)+" - "+dateFormat("%g:%i%a",d)+"</div>",'<div class="weekly-event-title">'+b.title+"</div>",'<div class="weekly-event-desc">'+b.description+"</div>",'<div class="weekly-dragger"></div>'].join("")),(this.readOnly||!this.enableResize)&&j.find(".weekly-dragger").remove(),(this.readOnly||!this.enableDelete)&&j.find(".weekly-delete").remove(),b.type&&j.addClass("weekly-event-"+b.type.replace(/ /g,"-"));var k=this.el.find('.weekly-grid .weekly-day[data-date="'+e+'"]');k.append(j);var l=d.getTime()-c.getTime();b.title&&18e5>=l&&j.find(".weekly-event-time").hide(),this.fitText&&k.find(".weekly-event-title").fitText(1,{minFontSize:this.fitTextMin,maxFontSize:this.fitTextMax}),this.updateEventScroll(e)},toFraction:function(a){if(-1===a.toString().indexOf(":"))return a;var b=a.toString().split(":");return parseFloat(b[0]+"."+100/(60/b[1]))},fromDecimal:function(a){return Math.round(60*(a%1))},getTimeOffsetPercent:function(a){a=this.toFraction(a)-this.startTime;var b=this.timeDifference-a;0>b&&(b=a-this.timeDifference);var c=b/this.timeDifference*100;return c},addEvent:function(b){if(b instanceof Array)for(var c=0,d=b.length;d>c;c++)this.addEvent(b[c]);else{if(b=a.extend({title:"",description:"",start:null,end:null},b),b._index=this.events.length,!this.allowOverlap&&this.overlaps(b.start.getTime(),b.end.getTime()))return void this.emit("addEventOverlapError",[b]);b.start.getHours()>=this.startTime&&b.end.getHours()<=this.endTime+12&&this.renderEvent(b),this.events.push(b),this.emit("addEvent",[b])}},removeEvent:function(b){var c=a(b.target).parents(".weekly-event"),d=c.data();return this.events.splice(d._index,1),c.remove(),this.el.trigger("removeEvent",d),!1},clearEvents:function(){this.el.find(".weekly-event").remove(),this.events=[],this.el.trigger("clearEvents")},eventClicked:function(b){var c=a(b.currentTarget),d=c.data();this.emit("eventClick",[d,c])},setTimezoneOffset:function(a){return this.timezoneOffset=dateUtils.realTimezoneOffset(a),this.utcOffset=a,this.update(),this},setReadOnly:function(a){return this.readOnly=a,this.update(),this},setScrollFirstEvent:function(a){return this.scrollFirstEvent=a,this.update(),this},updateEventScroll:function(){clearTimeout(this.scrollTimer),this.scrollTimer=setTimeout(this.proxy(function(){if(this.scrollFirstEvent){var b,c=a(window).scrollTop(),d=this.scrollFirstEvent;if("today"===this.scrollFirstEvent)d=dateFormat("%Y-%m-%d",new Date);else if(this.scrollFirstEvent instanceof Date)d=dateFormat("%Y-%m-%d",this.scrollFirstEvent);else if("everyday"!==this.scrollFirstEvent){var e=this.scrollFirstEvent.split("-");d=dateFormat("%Y-%m-%d",new Date(e[0],e[1]-1,e[2]))}if(b=this.el.find("everyday"===this.scrollFirstEvent?".weekly-event":'.weekly-grid [data-date="'+d+'"] .weekly-event'),b.length){var f=this.firstEvent;b.each(function(){(!f||this.offsetTop<f.offsetTop)&&(f=this)}),f.scrollIntoView(),a(window).scrollTop(c),this.firstEvent=f}}}),0)},overlaps:function(a,b,c){for(var d=this.events.length;d--;)if(parseInt(c,10)!==d&&b>this.events[d].start.getTime()&&a<this.events[d].end.getTime())return!0;return!1},intersects:function(b){var c=[],d=b.offset(),e=[d.top,d.top+b.outerHeight()];return b.siblings(".weekly-event").each(function(){var b=a(this),d=b.offset(),f=[d.top,d.top+b.outerHeight()];e[0]<f[1]&&e[1]>f[0]&&c.push(b)}),c},setSelectableDates:function(b,c){if(b){for(var d=0,e=b.length;e>d;d++)"object"==typeof b[d]&&(b[d]=dateFormat("%Y-%m-%d",b[d]));this.canAdd="function"===a.type(b)?b:function(a){return b.indexOf(a)>-1}}else this.canAdd=function(){return!0};c||this.update()},showPreviewEvent:function(b){var c,d=a(b.currentTarget),e=d.parent().offset(),f=b.pageY-e.top,g=a(b.currentTarget).height(),h=Math.round(g/this.timeDifference),i=h/(60/this.interval),j=h/(60/this.minDuration),k=Math.floor(f/i)*i,l=k+j,m=d.data("date").split("-"),n=(k/h||0)+this.startTime,o=(l/h||1)+this.startTime;o-n<this.minDuration/60&&(o=n+this.minDuration/60);var p=new Date(m[0],m[1]-1,m[2],n-this.timezoneOffset,this.fromDecimal(n)),q=new Date(m[0],m[1]-1,m[2],o-this.timezoneOffset,this.fromDecimal(o));d.append('<div class="weekly-event-preview"><div class="weekly-event-time">'+dateFormat("%g:%i",p)+" - "+dateFormat("%g:%i%a",q)+"</div></div>"),c=d.find(".weekly-event-preview"),c.css({top:k,bottom:g-l})},removePreviewEvent:function(){clearTimeout(this.hoverPreviewTimer),this.el.find(".weekly-event-preview").remove()}})}(jQuery);