forked from karlseguin/jquery.simpleDatePicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.datePicker.js
162 lines (157 loc) · 7.04 KB
/
jquery.datePicker.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
(function($){
$.fn.datePicker = function(opts) {
var defaults = {selected: null, minimumDate: null, maximumDate: null, dateFormat: null};
var months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
var abbreviations = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
var daySelector = 'td:not(.m):not(:empty)';
return this.each(function() {
if (this.datePicker) { return false; }
var options = $.extend({}, defaults, opts);
var $input = $(this);
var $container = currentDate = mode = null;
var self = {
initialize: function() {
$input.click(function() { self.show(); return false; }).keydown(function(e){ if (e.keyCode == 13) { self.entered(); return false; }});
$(document).keydown(function(e) { if (e.keyCode == 27) { self.hide(); }}).click(self.hide);
$container = self.initializeContainer().hide()
.append(self.buildMonth(new Date()))
.delegate(daySelector, 'click', self.clicked)
.delegate('td:not(:empty)', 'hover', self.hover)
.delegate('.prev', 'click', self.loadPrevious)
.delegate('.next', 'click', self.loadNext)
.delegate('.month', 'click', self.pickMonth)
.delegate('.year', 'click', self.pickYear)
.click(function(){return false;});
},
parseDate: function(value) { return new Date(value); },
show: function() { $container.show(); },
hide: function() { $container.hide(); },
loadPrevious: function() {
$container.empty().append(self.buildMonth(new Date(currentDate.getFullYear(), currentDate.getMonth()-1, 1)));
},
loadNext: function() {
$container.empty().append(self.buildMonth(new Date(currentDate.getFullYear(), currentDate.getMonth()+1, 1)));
},
hover: function() {
$(this).toggleClass('hover');
},
clicked: function() {
var $cell = $(this);
if (mode == 'month') {
$container.empty().append(self.buildMonth(new Date(currentDate.getFullYear(), (4*($cell.parent().index()-1)) + $cell.index(), 1)));
} else if (mode == 'year') {
currentDate = new Date($cell.text(), 0, 1);
self.pickMonth();
} else {
$container.find('td.selected').removeClass('selected');
$cell.addClass('selected');
var date = new Date(currentDate.getFullYear(), currentDate.getMonth(), $cell.text());
$input.val(self.format(date)).change();
if (options.selected != null) { options.selected(selected); }
self.hide();
}
},
entered: function() {
var date = self.parseDate($input.val().replace(/^\s*/, '').replace(/\s*$/, ''));
if (date == null) { return; }
$container.empty().append(self.buildMonth(date)).find(daySelector).eq(date.getDate()-1).click();
},
initializeContainer: function() {
return $('<div>').addClass('calendar').insertAfter($input);
},
pickYear: function() {
mode = 'year';
var table = document.createElement('table');
var start = currentDate.getFullYear() - 6;
if (options.minimumDate && options.minimumDate > start) { start = options.minimumDate; }
for (var i = 0; i < 3; ++i) {
var row = table.insertRow(-1);
for (var j = 0; j < 4; ++j) {
var cell = row.insertCell(-1);
cell.innerHTML = start + (4*i) + j;
}
}
$container.empty().append(table);
},
pickMonth: function() {
mode = 'month';
var table = document.createElement('table');
var header = table.insertRow(-1);
var year = header.insertCell(-1);
year.colSpan = 4;
year.className = 'm year';
year.innerHTML = currentDate.getFullYear();
//could make this static...
for (var i = 0; i < 3; ++i) {
var row = table.insertRow(-1);
for (var j = 0; j < 4; ++j) {
var cell = row.insertCell(-1);
cell.innerHTML = abbreviations[4*i+j];
}
}
$container.empty().append(table);
},
buildMonth: function(date) {
mode = 'day';
var first = new Date(date.getFullYear(), date.getMonth(), 1);
var last = new Date(date.getFullYear(), date.getMonth()+1, 0);
currentDate = first;
var firstDay = first.getDay();
var totalDays = last.getDate();
var weeks = Math.ceil((totalDays + firstDay) / 7);
var table = document.createElement('table');
for (var i = 0, count = 1; i < weeks; ++i) {
var row = table.insertRow(-1);
for(var j = 0; j < 7; ++j, ++count) {
var cell = row.insertCell(-1);
if (count > firstDay && count <= totalDays+firstDay) {
cell.innerHTML = count - firstDay;
}
}
}
var header = table.insertRow(0);
self.addHeaderCell(header, '<div class="prev">«</div>', 1);
self.addHeaderCell(header, '<div class="month">' + months[date.getMonth()] + ' ' + date.getFullYear() + '</div>', 5);
self.addHeaderCell(header, '<div class="next">»</div>', 1);
var $table = $(table);
if (options.minimumDate && options.minimumDate >= first) { $table.find('.prev').hide(); }
if (options.maximumDate && options.maximumDate <= last) { $table.find('.next').hide(); }
return $table;
},
addHeaderCell: function(header, html, colspan) {
var cell = header.insertCell(-1);
cell.innerHTML = html;
cell.className = 'm '; //very stupid IE (all versions) fix
cell.colSpan = colspan;
},
format: function(date) {
if(options.dateFormat != null){
var o = {
"M+" : date.getMonth()+1, //month
"d+" : date.getDate(), //day
"h+" : date.getHours(), //hour
"m+" : date.getMinutes(), //minute
"s+" : date.getSeconds(), //second
"q+" : Math.floor((date.getMonth()+3)/3), //quarter
"S" : date.getMilliseconds() //millisecond
};
var result = "";
if(/(y+)/.test(options.dateFormat)){
result = options.dateFormat.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o){
if(new RegExp("("+ k +")").test(options.dateFormat)){
result = result.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return result;
}else{
return months[date.getMonth()] + ' ' + date.getDate() + ' ' + date.getFullYear();
}
}
};
this.datePicker = self;
self.initialize();
});
}
})(jQuery);