This repository has been archived by the owner on Nov 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
dcalendar.picker.js
349 lines (301 loc) · 12.8 KB
/
dcalendar.picker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* -- 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 = $('<thead><tr><th id="prev">‹</th><th colspan="5" id="currM"></th><th id="next">›</th></tr><tr><th>Su</th><th>Mo</th><th>Tu</th><th>We</th><th>Th</th><th>Fr</th><th>Sa</th></tr></thead>');
else if (options.mode === 'datepicker')
this.tHead = $('<thead><tr><th id="prev">‹</th><th colspan="5" id="currM"></th><th id="next">›</th></tr><tr><th>S</th><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th></tr></thead>');
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 = $('<tbody></tbody>'),
d = new Date(that.date.getFullYear(), that.date.getMonth(), that.date.getDate()),
days = that.date.getDays(),
day = 1, nStartDate = 1,
selDate = that.selected.split('/'),
selected = new Date(selDate[2], selDate[0] -1, selDate[1]),
today = new Date(that.today.getFullYear(), that.today.getMonth(), that.today.getDate()),
min = that.minDate === "today" ? today : new Date(that.minDate),
max = that.maxDate === "today" ? today : new Date(that.maxDate);
that.calendar.empty();
if(mode === "days") {
if(that.options.mode === 'calendar') {
that.tHead = $('<thead><tr><th id="prev">‹</th><th colspan="5" id="currM"></th><th id="next">›</th></tr><tr><th>Su</th><th>Mo</th><th>Tu</th><th>We</th><th>Th</th><th>Fr</th><th>Sa</th></tr></thead>');
} else if (that.options.mode === 'datepicker') {
that.tHead = $('<thead><tr><th id="prev">‹</th><th colspan="5" id="currM"></th><th id="next">›</th></tr><tr><th>S</th><th>M</th><th>T</th><th>W</th><th>T</th><th>F</th><th>S</th></tr></thead>');
}
that.tHead.find('#currM').text(months[that.date.getMonth()] +" " + that.date.getFullYear());
that.calendar.append(that.tHead);
for(var i = 1; i <= 6; i++){
var temp = [$('<td></td>'),$('<td></td>'),$('<td></td>'),$('<td></td>'),$('<td></td>'),$('<td></td>'),$('<td></td>')];
while(day <= days){
d.setDate(day);
var dayOfWeek = d.getDay();
if(d.getTime() == today.getTime()) temp[dayOfWeek].attr('id', 'currDay');
if ((that.minDate && d < min) || (that.maxDate && d > max)) temp[dayOfWeek].addClass('disabled');
if(that.options.mode === 'datepicker' && d.getTime() == selected.getTime()) temp[dayOfWeek].addClass('selected');
if(i === 1 && dayOfWeek === 0) break;
else if(dayOfWeek < 6) temp[dayOfWeek].html('<span>'+(day++)+'</span>').addClass('date');
else {
temp[dayOfWeek].html('<span>'+(day++)+'</span>').addClass('date');
break;
}
}
/* For days of previous and next month */
if (i === 1 || i > 4) {
// First week
if (i === 1) {
var p = new Date(that.date.getFullYear(), that.date.getMonth() - 1, 1),
pMonth = p.getMonth(), pDays = p.getDays();
for (var a = 6; a >= 0; a--) {
if (temp[a].text() === ''){
p.setDate(pDays);
temp[a].html('<span>' + (pDays--) + '</span>').addClass('pMDate');
if ((that.minDate && p < min) || (that.maxDate && p > max)) temp[a].addClass('disabled');
if (that.options.mode === 'datepicker' && p.getTime() == selected.getTime()) temp[a].addClass('selected');
}
}
}
// Last week
else if (i > 4) {
var nextMonth = new Date(that.date.getFullYear(), that.date.getMonth() + 1, 1);
for (var a = 0; a <= 6; a++) {
if (temp[a].text() === ''){
nextMonth.setDate(nStartDate);
temp[a].html('<span>' + (nStartDate++) + '</span>').addClass('nMDate');
if ((that.minDate && nextMonth < min) || (that.maxDate && nextMonth > max)) temp[a].addClass('disabled');
if (that.options.mode === 'datepicker' && nextMonth.getTime() == selected.getTime()) temp[a].addClass('selected');
}
}
}
}
cal.push(temp);
}
$.each(cal, function(i, v){
var row = $('<tr></tr>'), l = v.length;
for(var i = 0; i < l; i++) { row.append(v[i]); }
tBody.append(row);
});
var sysDate = "Today: " + daysofweek[that.today.getDay()] + ", " + months[that.today.getMonth()] + " " + that.today.getDate() + ", " + that.today.getFullYear();
tBody.append('<tr><td colspan="7" id="today">' + sysDate + '</td></tr>').appendTo(that.calendar);
} else {
this.tHead = $('<thead><tr><th id="prev">‹</th><th colspan="2" id="currM"></th><th id="next">›</th></tr>');
that.tHead.find('#currM').text(that.date.getFullYear());
that.tHead.appendTo(that.calendar);
var currI = 0;
for (var i = 0; i < 3; i++) {
var row = $('<tr></tr>');
for (var x = 0; x < 4; x++) {
var col = $('<td align="center"></td>');
var m = $('<span class="month" num="' + currI + '">' + short_months[currI] + '</span>');
col.append(m).appendTo(row);
currI++;
}
tBody.append(row);
}
var sysDate = "Today: " + daysofweek[that.today.getDay()] + ", "+ months[that.today.getMonth()] + " " + that.today.getDate() + ", " + that.today.getFullYear();
tBody.append('<tr><td colspan="4" id="today">' + sysDate + '</td></tr>').appendTo(that.calendar);
}
}
}
/* DEFINITION FOR DCALENDAR */
$.fn.dcalendar = function(opts){
return $(this).each(function(index, elem){
var that = this;
var $this = $(that),
data = $(that).data('dcalendar'),
options = $.extend({}, $.fn.dcalendar.defaults, $this.data(), typeof opts === 'object' && opts);
if(!data){
$this.data('dcalendar', (data = new DCalendar(this, options)));
}
if(typeof opts === 'string') data[opts]();
});
}
$.fn.dcalendar.defaults = {
mode : 'calendar',
format: 'mm/dd/yyyy',
};
$.fn.dcalendar.Constructor = DCalendar;
/* DEFINITION FOR DCALENDAR PICKER */
$.fn.dcalendarpicker = function(opts){
return $(this).each(function(){
var that = $(this),
hovered = false, selectedDate = false,
cal = null;
if(typeof opts === 'string') {
var data = that.next('.calendar').data('dcalendar');
data[opts]();
} else {
cal = $('<table class="calendar"></table>');
that.wrap($('<div class="datepicker" style="display:inline-block;position:relative;"></div>'));
cal.css({
position:'absolute',
left:0, display:'none',
'box-shadow':'0 4px 6px 1px rgba(0, 0, 0, 0.14)',
width:'230px',
}).appendTo(that.parent());
if(opts){
opts.mode = 'datepicker';
cal.dcalendar(opts);
} else{
cal.dcalendar({mode: 'datepicker'});
}
cal.hover(function(){
hovered = true;
}, function(){
hovered = false;
}).on('click', function(){
if(!selectedDate)
that.focus();
else {
selectedDate = false;
$(this).hide();
}
}).on('selectdate', function(e){
that.val(e.date).trigger('onchange');
that.trigger($.Event('dateselected', {date: e.date, elem: that}));
selectedDate = true;
});
that.on('keydown', function(e){ if(e.which) return false; })
.on('focus', function(){
$('.datepicker').find('.calendar').not(cal).hide();
cal.show();
})
.on('blur', function(){ if(!hovered) cal.hide(); });
}
});
}
}(jQuery);