forked from jonleighton/date_input
/
jquery.date_input.js
297 lines (244 loc) · 8.95 KB
/
jquery.date_input.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
DateInput = (function($) { // Localise the $ function
function DateInput(el, opts) {
if (typeof(opts) != "object") opts = {};
$.extend(this, DateInput.DEFAULT_OPTS, opts);
this.input = $(el);
this.bindMethodsToObj("show", "hide", "hideIfClickOutside", "keydownHandler", "selectDate", "prevMonth", "nextMonth");
this.build();
this.selectDate();
this.hide();
};
DateInput.DEFAULT_OPTS = {
month_names: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
short_month_names: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
short_day_names: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
start_of_week: 1
};
DateInput.prototype = {
build: function() {
var monthNav = $('<p class="month_nav">' +
'<a href="#" class="prev" title="[Page-Up]">«</a>' +
' <span class="month_name"></span> ' +
'<a href="#" class="next" title="[Page-Down]">»</a>' +
'</p>');
this.monthNameSpan = $(".month_name", monthNav);
$(".prev", monthNav).click(this.prevMonth);
$(".next", monthNav).click(this.nextMonth);
var tableShell = "<table><thead><tr>";
$(this.adjustDays(this.short_day_names)).each(function() {
tableShell += "<th>" + this + "</th>";
});
tableShell += "</tr></thead><tbody></tbody></table>";
this.dateSelector = this.rootLayers = $('<div class="date_selector"></div>').append(monthNav, tableShell).insertAfter(this.input);
if ($.browser.msie && $.browser.version < 7) {
this.ieframe = $('<iframe class="date_selector_ieframe" frameborder="0" src="#"></iframe>').insertBefore(this.dateSelector);
this.rootLayers = this.rootLayers.add(this.ieframe);
};
this.tbody = $("tbody", this.dateSelector);
this.input.change(this.bindToObj(function() { this.selectDate(); }));
this.selectDate();
},
selectMonth: function(date) {
var newMonth = new Date(date.getFullYear(), date.getMonth(), 1);
if (this.currentMonth != newMonth) {
this.currentMonth = newMonth;
var rangeStart = this.rangeStart(date), rangeEnd = this.rangeEnd(date);
var numDays = this.daysBetween(rangeStart, rangeEnd);
var dayCells = "";
for (var i = 0; i <= numDays; i++) {
var currentDay = new Date(rangeStart.getFullYear(), rangeStart.getMonth(), rangeStart.getDate() + i, 12, 00);
if (this.isFirstDayOfWeek(currentDay)) dayCells += "<tr>";
if (currentDay.getMonth() == date.getMonth()) {
dayCells += '<td class="selectable_day" date="' + this.dateToString(currentDay) + '">' + currentDay.getDate() + '</td>';
} else {
dayCells += '<td class="unselected_month" date="' + this.dateToString(currentDay) + '">' + currentDay.getDate() + '</td>';
};
if (this.isLastDayOfWeek(currentDay)) dayCells += "</tr>";
};
this.monthNameSpan.empty().append(this.monthName(date) + " " + date.getFullYear());
this.tbody.empty().append(dayCells);
$(".selectable_day", this.tbody).click(this.bindToObj(function(event) {
this.changeInput($(event.target).attr("date"));
}));
$("td[date=" + this.dateToString(new Date()) + "]", this.tbody).addClass("today");
};
$('td[date=' + this.selectedDateString + ']', this.tbody).addClass("selected");
},
selectDate: function(date) {
if (typeof(date) == "undefined") {
date = this.stringToDate(this.input.val());
};
if (!date) date = new Date();
this.selectedDate = date;
this.selectedDateString = this.dateToString(this.selectedDate);
this.selectMonth(this.selectedDate);
},
changeInput: function(dateString) {
this.input.val(dateString).change();
this.hide();
},
show: function() {
this.rootLayers.css("display", "block");
$(window, document.body).click(this.hideIfClickOutside);
this.input.unbind("focus", this.show);
$(window).keydown(this.keydownHandler);
this.setPosition();
},
hide: function() {
this.rootLayers.css("display", "none");
$(window, document.body).unbind("click", this.hideIfClickOutside);
this.input.focus(this.show);
$(window).unbind("keydown", this.keydownHandler);
},
hideIfClickOutside: function(event) {
if (event.target != this.input[0] && !this.insideSelector(event)) {
this.hide();
};
},
keydownHandler: function(event) {
switch (event.keyCode)
{
case 9: // tab
case 27: // esc
this.hide();
return;
break;
case 13: // enter
this.changeInput(this.selectedDateString);
break;
case 33: // page up
this.moveDateMonthBy(-1);
break;
case 34: // page down
this.moveDateMonthBy(1);
break;
case 38: // up
this.moveDateBy(-7);
break;
case 40: // down
this.moveDateBy(7);
break;
case 37: // left
this.moveDateBy(-1);
break;
case 39: // right
this.moveDateBy(1);
break;
default:
return;
}
event.preventDefault();
},
stringToDate: function(string) {
var matches;
if (matches = string.match(/^(\d{1,2}) ([^\s]+) (\d{4,4})$/)) {
return new Date(matches[3], this.shortMonthNum(matches[2]), matches[1], 12, 00);
} else {
return null;
};
},
dateToString: function(date) {
return date.getDate() + " " + this.short_month_names[date.getMonth()] + " " + date.getFullYear();
},
setPosition: function() {
var offset = this.input.offset();
this.rootLayers.css({
top: offset.top + this.input.outerHeight(),
left: offset.left
});
if (this.ieframe) {
this.ieframe.css({
width: this.dateSelector.outerWidth(),
height: this.dateSelector.outerHeight()
});
};
},
moveDateBy: function(amount) {
this.selectDate(new Date(this.selectedDate.setDate(this.selectedDate.getDate() + amount)));
},
moveMonthBy: function(amount) {
this.selectMonth(new Date(this.currentMonth.setMonth(this.currentMonth.getMonth() + amount)));
},
moveDateMonthBy: function(amount) {
this.selectDate(new Date(this.selectedDate.setMonth(this.currentMonth.getMonth() + amount)));
},
prevMonth: function() {
this.moveMonthBy(-1);
return false;
},
nextMonth: function() {
this.moveMonthBy(1);
return false;
},
monthName: function(date) {
return this.month_names[date.getMonth()];
},
insideSelector: function(event) {
var offset = this.dateSelector.offset();
offset.right = offset.left + this.dateSelector.outerWidth();
offset.bottom = offset.top + this.dateSelector.outerHeight();
return event.pageY < offset.bottom &&
event.pageY > offset.top &&
event.pageX < offset.right &&
event.pageX > offset.left;
},
bindToObj: function(fn) {
var self = this;
return function() { return fn.apply(self, arguments) };
},
bindMethodsToObj: function() {
for (var i = 0; i < arguments.length; i++) {
this[arguments[i]] = this.bindToObj(this[arguments[i]]);
};
},
indexFor: function(array, value) {
for (var i = 0; i < array.length; i++) {
if (value == array[i]) return i;
};
},
monthNum: function(month_name) {
return this.indexFor(this.month_names, month_name);
},
shortMonthNum: function(month_name) {
return this.indexFor(this.short_month_names, month_name);
},
shortDayNum: function(day_name) {
return this.indexFor(this.short_day_names, day_name);
},
daysBetween: function(start, end) {
start = Date.UTC(start.getFullYear(), start.getMonth(), start.getDate());
end = Date.UTC(end.getFullYear(), end.getMonth(), end.getDate());
return (end - start) / 86400000;
},
changeDayTo: function(to, date, direction) {
var difference = direction * (Math.abs(date.getDay() - to - (direction * 7)) % 7);
return new Date(date.getFullYear(), date.getMonth(), date.getDate() + difference);
},
rangeStart: function(date) {
return this.changeDayTo(this.start_of_week, new Date(date.getFullYear(), date.getMonth()), -1);
},
rangeEnd: function(date) {
return this.changeDayTo((this.start_of_week - 1) % 7, new Date(date.getFullYear(), date.getMonth() + 1, 0), 1);
},
isFirstDayOfWeek: function(date) {
return date.getDay() == this.start_of_week;
},
isLastDayOfWeek: function(date) {
return date.getDay() == (this.start_of_week - 1) % 7;
},
adjustDays: function(days) {
var newDays = [];
for (var i = 0; i < days.length; i++) {
newDays[i] = days[(i + this.start_of_week) % 7];
};
return newDays;
}
};
$.fn.date_input = function(opts) {
return this.each(function() { new DateInput(this, opts); });
};
$.date_input = { initialize: function(opts) {
$("input.date_input").date_input(opts);
} };
return DateInput;
})(jQuery); // End localisation of the $ function