/* -- DO NOT REMOVE --
* jQuery DCalendar 1.1 and DCalendar Picker 1.3 plugin
*
* Author: Dionlee Uy
* Email: dionleeuy@gmail.com
*
* Date: Sat Mar 2 2013
*
* @requires jQuery
* -- DO NOT REMOVE --
*/
if (typeof jQuery === 'undefined') { throw new Error('DCalendar.Picker: This plugin requires jQuery'); }
+function ($) {
Date.prototype.getDays = function() { return new Date(this.getFullYear(), this.getMonth() + 1, 0).getDate(); };
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'],
short_months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
daysofweek = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
DCalendar = function(elem, options) {
this.calendar = $(elem);
this.today = new Date(); //system date
//current selected date, default is today if no value given
if(this.calendar.prev().val() === '') {
this.date = new Date();
} else {
var dateObj = this.reformatDate(this.calendar.prev().val());
this.date = isNaN(parseInt(dateObj.m)) ? new Date(dateObj.m + " " + dateObj.d + ", " + dateObj.y) : new Date(dateObj.y, dateObj.m - 1, dateObj.d);
}
this.viewMode = 'days';
this.options = options;
this.selected = (this.date.getMonth() + 1) + "/" + this.date.getDate() + "/" + this.date.getFullYear();
this.minDate = this.calendar.prev().data('mindate');
this.maxDate = this.calendar.prev().data('maxdate');
if(options.mode === 'calendar')
this.tHead = $('‹ › ');
else if (options.mode === 'datepicker')
this.tHead = $('Su Mo Tu We Th Fr Sa ‹ › ');
this.tHead.find('#currM').text(months[this.today.getMonth()] +" " + this.today.getFullYear());
this.calendar.prepend(this.tHead);
var that = this;
this.calendar.on('click', '#next', function() { initCreate('next'); })
.on('click', '#prev', function() { initCreate('prev'); })
.on('click', '#today', function() {
that.viewMode = 'days';
var curr = new Date(that.date),
sys = new Date(that.today);
if(curr.toString() != sys.toString()) { that.date = sys; }
that.create('days');
}).on('click', '.date, .pMDate, .nMDate', function() {
var isPrev = $(this).hasClass('pMDate'),
isNext = $(this).hasClass('nMDate'),
sdate = $(this).text(),
cmonth = that.date.getMonth(),
cyear = that.date.getFullYear(),
min = that.minDate === "today" ? new Date(that.today.getFullYear(), that.today.getMonth(), that.today.getDate()) : new Date(that.minDate),
max = that.maxDate === "today" ? new Date(that.today.getFullYear(), that.today.getMonth(), that.today.getDate()) : new Date(that.maxDate);
/* Calculate year */
if(isPrev) { cyear = (cmonth === 0 ? cyear - 1 : cyear); }
else if(isNext) { cyear = (cmonth + 2 === 13 ? cyear + 1 : cyear); }
/* Calculate month */
if(isPrev) { cmonth = (cmonth === 0 ? '12' : cmonth); }
else if (isNext) { cmonth = (cmonth + 2 === 13 ? '1' : cmonth + 2); }
else { cmonth = cmonth + 1; }
// Selected date
var selected = new Date(cyear, cmonth - 1, sdate);
// console.log(cmonth);
// console.log(selected);
if ((that.minDate && selected < min) || (that.maxDate && selected > max)) return;
that.selected = cmonth + '/' + sdate + '/' + cyear;
if(that.options.mode === 'datepicker') {
that.calendar.find('td').removeClass('selected');
$(this).addClass('selected');
}
that.selectDate();
return true;
}).on('click', '#currM', function(){
that.viewMode = 'months';
that.create(that.viewMode);
}).on('click', '.month', function(e){
that.viewMode = 'days';
var curr = new Date(that.date), y = that.calendar.find('#currM').text();
curr.setMonth($(e.currentTarget).attr('num'));
that.date = curr;
that.create(that.viewMode);
});
function initCreate(o){
var curr = new Date(that.date),
currMonth = curr.getMonth(),
currYear = curr.getFullYear();
curr.setDate(1);
if(that.viewMode === 'days') {
curr.setMonth(currMonth + (o === 'next' ? 1 : -1));
} else {
curr.setFullYear(currYear + (o === 'next' ? 1 : - 1));
}
that.date = curr;
that.create(that.viewMode);
}
this.create(this.viewMode);
}
DCalendar.prototype = {
constructor : DCalendar,
setDate : function() {
var that = this,
dateObj = that.reformatDate(that.calendar.prev().val()),
value = isNaN(parseInt(dateObj.m)) ? new Date(dateObj.m + " " + dateObj.d + ", " + dateObj.y) : new Date(dateObj.y, dateObj.m - 1, dateObj.d);
that.selected = (value.getMonth() + 1) + "/" + value.getDate() + "/" + value.getFullYear();
that.selectDate();
that.date = value;
that.create(that.viewMode);
},
selectDate : function () {
var that = this,
newDate = that.formatDate(that.options.format),
e = $.Event('selectdate', {date: newDate});
that.calendar.trigger(e);
},
reformatDate : function (date) {
var that = this,
format = that.options.format;
return {
m: date.substring(format.indexOf('m'), format.lastIndexOf('m') + 1),
d: date.substring(format.indexOf('d'), format.lastIndexOf('d') + 1),
y: date.substring(format.indexOf('y'), format.lastIndexOf('y') + 1)
};
},
formatDate : function (format) {
var that = this;
var d = new Date(that.selected), day = d.getDate(), m = d.getMonth(), y = d.getFullYear();
return format.replace(/(yyyy|yy|mmmm|mmm|mm|m|dd|d)/gi, function (e) {
switch(e.toLowerCase()){
case 'd': return day;
case 'dd': return (day < 10 ? "0"+day: day);
case 'm': return m+1;
case 'mm': return (m+1 < 10 ? "0"+(m+1): (m+1));
case 'mmm': return short_months[m];
case 'mmmm': return months[m];
case 'yy': return y.toString().substr(2,2);
case 'yyyy': return y;
}
});
},
create : function(mode){
var that = this, cal = [],
tBody = $('S M T W T F S