-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.lowCal.min.js
7 lines (7 loc) · 8.71 KB
/
jquery.lowCal.min.js
1
2
3
4
5
6
7
/**
* jQuery lowCal v1
* A light-weight jQuery calendar widget with no
* other dependancies.
*
* Copyright 2011 Ad Taylor (@iamadtaylor)
*/(function(a,b){this.debug=!0,this.debugStrict=!0,b.Cal=function(a,c){this.el=b(c),this.cache={},this._create(a),this._init()},b.Cal.templates={days:'<% $.each(days, function(index, val) { %><li class="lc-date <% if(val.lcclass) { %><%=val.lcclass%><% } %> <% if(val.selected) { %><%=val.selected%><% } %>" data-date="<%=val.date%>"><span><%=val.Day%></span></li><% }); %>',navBar:'<li data-move="prev" class="lc-arrow lc-arrow-prev"><<</li><li class="lc-currDate"><%=date%></li><li data-move="next" class="lc-arrow lc-arrow-next lc-lastUnit">>></li>',cal:'<div id="lc-<%=instID%>" class="lowCal"><div class="lc-inner"><div class="lc-hd"><ul class="lc-navBar lc-line"><%=navBar%></ul><ul class="lc-days lc-line"><li>Mo</li><li>Tu</li><li>We</li><li>Th</li><li>Fr</li><li>Sa</li><li class="lc-lastUnit">Su</li></ul></div><div class="bd"><ol class="lc-dates lc-line"><%=days%></ol></div></div></div><style type="text/css">.lc-hide{position:absolute;top:-1000px;left:-1000px;}.lowCal{font :13px/1.54 Helvetica,"Helvetica Neue",Arial,sans-serif;}.lc-bd,.lc-hd{display:inline-block;width:100%;zoom:1;vertical-align:top;}.lc-inner{position:relative;}.lc-inner:after,.lc-lastUnit:after,.lc-line:after{clear:both;display:block;visibility:hidden;overflow:hidden;height:0!important;line-height:0;font-size:xx-large;content:" x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x "}.lc-line{*zoom:1;margin:0}.lc-lastUnit{display:table-cell;float:none;width:auto;_position:relative;_left:-2px;_margin-right:-3px;}.lc-days{overflow:hidden;text-align:center;}.lowCal li,.lowCal ol,.lowCal ul{list-style:none;}.lc-dates{overflow:hidden;*zoom:1;}.lc-currDate{text-align:center;font-weight:bold;}.lc-navBar .lc-arrow-prev{float:left;width :13%}.lc-navBar .lc-currDate{float:left;width:75%}.lc-days li,.lc-date{float:left;width:14.29%;}.lc-date span{float:none;display:block;text-align:right;padding:2px;margin-left:4px;margin-bottom:2px;margin-right:0;cursor:pointer;}.lc-other-month span{cursor:not-allowed;}.lc-hide{position:absolute;top:-1000px;left:-1000px;}.lowCal .lc-lastUnit{float:none;width:auto;padding-bottom:3px;} .lc-arrow{ font-weight:bold;cursor:pointer }</style>'},b.Cal.settings={currentDate:{},currentPos:0,instID:(new Date).getTime(),selectedDate:null,format:"dd/mm/yyyy",m_names:["January","February","March","April","May","June","July","August","September","October","November","December"],templates:null,selectedClass:"lc-selected",styles:!1},b.Cal.prototype={_init:function(a){var b=this.options;b.format=b.format.split("/"),b.currentDate=this._setDate(this.el.val()?this.el.val():new Date),this._uiInit()},_create:function(a){this.options=b.extend(!0,{},b.Cal.settings,a),this.cache=this._initCache(),this._events(),this.options.templates=b.extend(!0,{},b.Cal.templates,this.options.templates)},_initCache:function(){return{date:{},template:{},prevDate:{}}},option:function(a,c){b.isPlainObject(a)&&(this.options=b.extend(!0,this.options,a),this.options.templates=b.extend(!0,{},b.Cal.templates,this.options.templates))},_events:function(){var a=this;this.el.bind({"changeMonth.lowCal":function(b,c){a.updateCalendar(a._changeMonth(c))},"setNewDate.lowCal":function(b,c){a._setInputDate(c.date),a._changeSelected(c.el),d(["setnewdate",c.date])}})},_uiInit:function(){var a=this._initCal(),c=this,d=this.options;d.styles||(d.styles={width:this.el.css("width"),display:this.el.css("display"),position:this.el.css("position")}),this.el.wrap('<div id="lc-wrap-'+this.options.instID+'" />'),this.el.hide().after(a),this.cal=b("#lc-"+this.options.instID),this.calBody=b(this.cal).find(".lc-dates"),this.calMonth=b(this.cal).find(".lc-currDate"),this._uiInteractions()},styleAsInput:function(){b("#lc-wrap-"+this.options.instID).css(this.options.styles)},_uiInteractions:function(){var a=this;this.cal.delegate(".lc-arrow","click",function(){a.el.trigger("changeMonth.lowCal",b(this).data("move"))}),this.cal.delegate(".lc-day-active","click",function(){a.el.trigger("setNewDate.lowCal",{date:b(this).data("date"),el:this})})},updateCalendar:function(a){var c=this.options;this.calMonth.text(c.m_names[c.currentDate.Month-1]+" "+c.currentDate.Year);return b.type(a)==="object"?this.calBody.html(this._makeCalMonthTemplate()):this.calBody.html(a)},_changeSelected:function(a){this.calBody.children("li").removeClass(this.options.selectedClass),b(a).addClass(this.options.selectedClass)},_setDate:function(a){var b={};typeof a=="string"?b=this._splitDateString(a):(b.Day=a.Day||a.getDate(),b.Month=a.Month||a.getMonth()+1,b.Year=a.Year||a.getFullYear()),b.daysInMonth=this._daysInMonth(b.Month,b.Year),b.monthBegins=this._dayMonthBegins(b.Month,b.Year),this._updateCurrentDate(b);var c=this._checkCache("date",a);return c?this.sameMonth(b)?this._removeSelected(c):c:this._addToCache("date",b,b)},_updateCurrentDate:function(a){this.cache.prevDate=this.options.currentDate;return this.options.currentDate=a},sameMonth:function(a){var b=this.cache.prevDate;return b.Year===a.Year&&b.Month===a.Month?!0:!1},_setInputDate:function(a){this.el.val(a)},_splitDateString:function(a){var c={},d=a.split("/");b.map(this.options.format,function(a,b){switch(a){case"d":case"dd":c.Day=d[b];break;case"m":case"mm":c.Month=d[b];break;case"y":case"yy":case"yyyy":c.Year=d[b]}});return c},_dayMonthBegins:function(a,b){return(new Date(b,a-1,1)).getDay()},_changeMonth:function(a){var b=this.options.currentDate,c=a==="next"?parseInt(b.Month,10)+1:parseInt(b.Month,10)-1,d=b.Year;a==="prev"&&c===0?(d--,c=12):a==="next"&&c===13&&(d++,c=1);return this._setDate({Day:"0",Month:c,Year:d})},_daysInMonth:function(a,b){return 32-(new Date(b,parseInt(a,10)-1,32)).getDate()},getCurrentDate:function(){return this._formatDate(this.options.currentDate)},_formatDate:function(a){return a.Day+"/"+a.Month+"/"+a.Year},_addToCache:function(a,b,d){if(!this.cache[a]){c("No store called "+a);return!1}this.cache[a][b.Year]||(this.cache[a][b.Year]={});return this.cache[a][b.Year][b.Month]=d},_checkCache:function(a,b){if(!this.cache[a]){c("No store called "+a);return!1}return this.cache[a][b.Year]?this.cache[a][b.Year][b.Month]?this.cache[a][b.Year][b.Month]:!1:!1},_initCal:function(){var a=this.options.templates,b={};b={navBar:this._buildNavBar(),instID:this.options.instID,days:this._buildCalDays()};return this._tmpl(a.cal,b)},_buildNavBar:function(){var a=this.options,b={date:a.m_names[parseInt(a.currentDate.Month,10)-1]+" "+a.currentDate.Year};return this._tmpl(this.options.templates.navBar,b)},_buildCalDays:function(){return this._getCalMonthTemplate()},_getCalMonthTemplate:function(){var a=this._checkCache("template",this.options.currentDate);return a?a:this._makeCalMonthTemplate()},_makeCalMonthTemplate:function(){var a=this._makeCalMonthObject(),c="",d=this;b.each(a,function(b,e){c+=d._tmpl(d.options.templates.days,{days:a[b]})});return this._addToCache("date",this.options.currentDate,c)},_makeCalMonthObject:function(){var a=this.options.currentDate,b={},c=a.monthBegins===0?6:a.monthBegins-1,d=c+a.daysInMonth,e=Math.ceil(d/7)*7,f=e-d,g=1;b.pre_blank={};for(var h=0;h<c;h++)b.pre_blank[h+1]={Day:" ",lcclass:g%7===0?"lc-other-month lc-lastUnit":"lc-other-month"},g++;b.days={};for(h=0;h<a.daysInMonth;h++)b.days[h+1]={date:this._formatDate({Day:h+1,Month:a.Month,Year:a.Year}),Day:h+1,lcclass:g%7===0?"lc-day-active lc-lastUnit":"lc-day-active",selected:h+1===parseInt(a.Day,10)?this.options.selectedClass:!1},g++;b.post_blank={};for(h=0;h<f;h++)b.post_blank[h+1]={Day:h+1,lcclass:g%7===0?"lc-other-month lc-lastUnit":"lc-other-month"},g++;return b},_removeSelected:function(a){return a.replace(this.options.selectedClass,"")},_tmpl:function(a,b){var c=new Function("obj","var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push('"+a.replace(/[\r\t\n]/g," ").split("<%").join("\t").replace(/((^|%>)[^\t]*)'/g,"$1\r").replace(/\t=(.*?)%>/g,"',$1,'").split("\t").join("');").split("%>").join("p.push('").split("\r").join("\\'")+"');}return p.join('');");return c(b)}};var c=function(a){if(this.debug){if(this.debugStrict)throw a;this.console&&console.error(a)}},d=function(a){this.debug&&this.console&&console.log(a)};b.fn.lowCal=function(a){if(typeof a=="string"){var d=Array.prototype.slice.call(arguments,1);this.each(function(){var e=b.data(this,"lowCal");if(!e)c("cannot call methods on lowCal prior to initialization; attempted to call method '"+a+"'");else{if(!b.isFunction(e[a])||a.charAt(0)==="_"){c("no such method '"+a+"' for lowCal instance");return}e[a].apply(e,d)}})}else this.each(function(){var c=b.data(this,"lowCal");c?(c.option(a||{}),c._init()):b.data(this,"lowCal",new b.Cal(a,this))});return this}})(window,jQuery)