forked from dangrossman/daterangepicker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
daterangepicker.js
502 lines (397 loc) · 18.6 KB
/
daterangepicker.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/**
* @version: 1.0.1-custom
* @author: Dan Grossman http://www.dangrossman.info/
* @date: 2012-08-20
* @copyright: Copyright (c) 2012 Dan Grossman. All rights reserved.
* @license: Licensed under Apache License v2.0. See http://www.apache.org/licenses/LICENSE-2.0
* @website: http://www.improvely.com/
* IMPORTANT: This file is forked from Dan Grossman's work, and is listed at:
* https://github.com/bramski/bootstrap-daterangepicker
*/
!function ($) {
var DateRangePicker = function (element, options, cb) {
var hasOptions = typeof options == 'object'
var localeObject;
//state
this.startDate = Date.today();
this.endDate = Date.today();
this.changed = false;
this.ranges = {};
this.opens = 'right';
this.cb = function () { };
this.format = 'MM/dd/yyyy';
this.locale = {
applyLabel:"Apply",
fromLabel:"From",
toLabel:"To",
customRangeLabel:"Custom Range",
daysOfWeek:['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr','Sa'],
monthNames:['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
firstDay:0
};
localeObject = this.locale;
this.leftCalendar = {
month: Date.today().set({ day: 1, month: this.startDate.getMonth(), year: this.startDate.getFullYear() }),
calendar: Array()
};
this.rightCalendar = {
month: Date.today().set({ day: 1, month: this.endDate.getMonth(), year: this.endDate.getFullYear() }),
calendar: Array()
};
//element that triggered the date range picker
this.element = $(element);
if (this.element.hasClass('pull-right'))
this.opens = 'left';
if (this.element.is('input')) {
this.element.on({
click: $.proxy(this.show, this),
focus: $.proxy(this.show, this)
//blur: $.proxy(this.hide, this)
});
} else {
this.element.on('click', $.proxy(this.show, this));
}
if (hasOptions) {
if(typeof options.locale == 'object') {
$.each(localeObject, function (property, value) {
localeObject[property] = options.locale[property] || value;
});
}
}
var DRPTemplate = '<div class="daterangepicker dropdown-menu">' +
'<div class="calendar left"></div>' +
'<div class="calendar right"></div>' +
'<div class="ranges">' +
'<div class="range_inputs">' +
'<div style="float: left">' +
'<label for="daterangepicker_start">' + this.locale.fromLabel + '</label>' +
'<input class="input-mini" type="text" name="daterangepicker_start" value="" />' +
'</div>' +
'<div style="float: left; padding-left: 11px">' +
'<label for="daterangepicker_end">' + this.locale.toLabel + '</label>' +
'<input class="input-mini" type="text" name="daterangepicker_end" value="" />' +
'</div>' +
'<button class="btn btn-small btn-success" disabled="disabled">' + this.locale.applyLabel + '</button>' +
'</div>' +
'</div>' +
'</div>';
//the date range picker
this.container = $(DRPTemplate).appendTo('body');
if (hasOptions) {
if (typeof options.ranges == 'object') {
for (var range in options.ranges) {
var start = options.ranges[range][0];
var end = options.ranges[range][1];
if (typeof start == 'string')
start = Date.parse(start);
if (typeof end == 'string')
end = Date.parse(end);
this.ranges[range] = [start, end];
}
var list = '<ul>';
for (var range in this.ranges) {
list += '<li>' + range + '</li>';
}
list += '<li>' + this.locale.customRangeLabel + '</li>';
list += '</ul>';
this.container.find('.ranges').prepend(list);
}
if (typeof options.format == 'string')
this.format = options.format;
if (typeof options.startDate == 'string')
this.startDate = Date.parse(options.startDate, this.format);
if (typeof options.endDate == 'string')
this.endDate = Date.parse(options.endDate, this.format);
// update day names order to firstDay
if (typeof options.locale == 'object') {
if (typeof options.locale.firstDay == 'number') {
this.locale.firstDay = options.locale.firstDay;
var iterator = options.locale.firstDay;
while (iterator > 0) {
this.locale.daysOfWeek.push(this.locale.daysOfWeek.shift());
iterator--;
}
}
}
if (typeof options.opens == 'string')
this.opens = options.opens;
}
if (this.opens == 'right') {
//swap calendar positions
var left = this.container.find('.calendar.left');
var right = this.container.find('.calendar.right');
left.removeClass('left').addClass('right');
right.removeClass('right').addClass('left');
}
if (typeof options == 'undefined' || typeof options.ranges == 'undefined')
this.container.find('.calendar').show();
if (typeof cb == 'function')
this.cb = cb;
this.container.addClass('opens' + this.opens);
//event listeners
this.container.on('mousedown', $.proxy(this.mousedown, this));
this.container.find('.calendar').on('keyup', 'input', $.proxy(this.changeYearMonth, this));
this.container.find('.calendar').on('mousedown', 'input', $.proxy(this.focusControl, this));
this.container.find('.calendar').on('change', 'select', $.proxy(this.changeYearMonth, this));
this.container.find('.calendar').on('mousedown', 'select', $.proxy(this.focusControl, this));
this.container.find('.calendar').on('click', '.prev', $.proxy(this.clickPrev, this));
this.container.find('.calendar').on('click', '.next', $.proxy(this.clickNext, this));
this.container.find('.ranges').on('click', 'button', $.proxy(this.clickApply, this));
this.container.find('.calendar').on('click', 'td', $.proxy(this.clickDate, this));
this.container.find('.calendar').on('mouseenter', 'td', $.proxy(this.enterDate, this));
this.container.find('.calendar').on('mouseleave', 'td', $.proxy(this.updateView, this));
this.container.find('.ranges').on('click', 'li', $.proxy(this.clickRange, this));
this.container.find('.ranges').on('mouseenter', 'li', $.proxy(this.enterRange, this));
this.container.find('.ranges').on('mouseleave', 'li', $.proxy(this.updateView, this));
this.element.on('keyup', $.proxy(this.updateFromControl, this));
this.updateView();
this.updateCalendars();
};
DateRangePicker.prototype = {
constructor: DateRangePicker,
mousedown: function (e) {
e.stopPropagation();
e.preventDefault();
},
focusControl: function(e){
e.stopPropagation();
},
updateView: function () {
this.leftCalendar.month.set({ month: this.startDate.getMonth(), year: this.startDate.getFullYear() });
this.rightCalendar.month.set({ month: this.endDate.getMonth(), year: this.endDate.getFullYear() });
this.container.find('input[name=daterangepicker_start]').val(this.startDate.toString(this.format));
this.container.find('input[name=daterangepicker_end]').val(this.endDate.toString(this.format));
if (this.startDate.equals(this.endDate) || this.startDate.isBefore(this.endDate)) {
this.container.find('button').removeAttr('disabled');
} else {
this.container.find('button').attr('disabled', 'disabled');
}
},
updateFromControl: function () {
if (!this.element.is('input')) return;
var dateString = this.element.val().split(" - ");
var start = Date.parseExact(dateString[0], this.format);
var end = Date.parseExact(dateString[1], this.format);
if (start == null || end == null) return;
if (end.isBefore(start)) return;
this.startDate = start;
this.endDate = end;
this.updateView();
this.cb(this.startDate, this.endDate);
this.updateCalendars();
},
notify: function () {
this.updateView();
if (this.element.is('input')) {
this.element.val(this.startDate.toString(this.format) + ' - ' + this.endDate.toString(this.format));
}
this.cb(this.startDate, this.endDate);
},
move: function () {
if (this.opens == 'left') {
this.container.css({
top: this.element.offset().top + this.element.outerHeight(),
right: $(window).width() - this.element.offset().left - this.element.outerWidth(),
left: 'auto'
});
} else {
this.container.css({
top: this.element.offset().top + this.element.outerHeight(),
left: this.element.offset().left,
right: 'auto'
});
}
},
show: function (e) {
this.container.show();
this.move();
if (e) {
e.stopPropagation();
e.preventDefault();
}
this.changed = false;
$(document).on('mousedown', $.proxy(this.hide, this));
},
hide: function (e) {
this.container.hide();
$(document).off('mousedown', this.hide);
if (this.changed)
this.notify();
},
enterRange: function (e) {
var label = e.target.innerHTML;
if (label == this.locale.customRangeLabel) {
this.updateView();
} else {
var dates = this.ranges[label];
this.container.find('input[name=daterangepicker_start]').val(dates[0].toString(this.format));
this.container.find('input[name=daterangepicker_end]').val(dates[1].toString(this.format));
}
},
clickRange: function (e) {
var label = e.target.innerHTML;
if (label == this.locale.customRangeLabel) {
this.container.find('.calendar').show();
} else {
var dates = this.ranges[label];
this.startDate = dates[0];
this.endDate = dates[1];
this.leftCalendar.month.set({ month: this.startDate.getMonth(), year: this.startDate.getFullYear() });
this.rightCalendar.month.set({ month: this.endDate.getMonth(), year: this.endDate.getFullYear() });
this.updateCalendars();
this.changed = true;
this.container.find('.calendar').hide();
this.hide();
}
},
changeYearMonth: function(e){
calendar = $(e.currentTarget).closest('.calendar').is('.left') ? this.leftCalendar : this.rightCalendar;
if($(e.currentTarget).is('select')){
newMonth = $(e.currentTarget).val();
calendar.month.set({ month: Number(newMonth-1), year: calendar.year});
this.updateCalendars();
}
if($(e.currentTarget).is('input')){
newYear = $(e.currentTarget).val()
if(newYear.length == 4){
calendar.month.set({ month: Number(calendar.month.toString('MM'))-1, year: Number(newYear)});
this.updateCalendars();
}
}
e.stopPropagation();
return false;
},
clickPrev: function (e) {
var cal = $(e.target).parents('.calendar');
if (cal.hasClass('left')) {
this.leftCalendar.month.add({ months: -1 });
} else {
this.rightCalendar.month.add({ months: -1 });
}
this.updateCalendars();
},
clickNext: function (e) {
var cal = $(e.target).parents('.calendar');
if (cal.hasClass('left')) {
this.leftCalendar.month.add({ months: 1 });
} else {
this.rightCalendar.month.add({ months: 1 });
}
this.updateCalendars();
},
enterDate: function (e) {
var title = $(e.target).attr('title');
var row = title.substr(1, 1);
var col = title.substr(3, 1);
var cal = $(e.target).parents('.calendar');
if (cal.hasClass('left')) {
this.container.find('input[name=daterangepicker_start]').val(this.leftCalendar.calendar[row][col].toString(this.format));
} else {
this.container.find('input[name=daterangepicker_end]').val(this.rightCalendar.calendar[row][col].toString(this.format));
}
},
clickDate: function (e) {
var title = $(e.target).attr('title');
var row = title.substr(1, 1);
var col = title.substr(3, 1);
var cal = $(e.target).parents('.calendar');
if (cal.hasClass('left')) {
startDate = this.leftCalendar.calendar[row][col];
endDate = this.endDate;
} else {
startDate = this.startDate;
endDate = this.rightCalendar.calendar[row][col];
}
cal.find('td').removeClass('active');
if (startDate.equals(endDate) || startDate.isBefore(endDate)) {
$(e.target).addClass('active');
if (!startDate.equals(this.startDate) || !endDate.equals(this.endDate))
this.changed = true;
this.startDate = startDate;
this.endDate = endDate;
}
},
clickApply: function (e) {
this.hide();
},
updateCalendars: function () {
this.leftCalendar.calendar = this.buildCalendar(this.leftCalendar.month.getMonth(), this.leftCalendar.month.getFullYear());
this.rightCalendar.calendar = this.buildCalendar(this.rightCalendar.month.getMonth(), this.rightCalendar.month.getFullYear());
this.container.find('.calendar.left').html(this.renderCalendar(this.leftCalendar.calendar, this.startDate));
this.container.find('.calendar.right').html(this.renderCalendar(this.rightCalendar.calendar, this.endDate));
},
buildCalendar: function (month, year) {
var firstDay = Date.today().set({ day: 1, month: month, year: year });
var lastMonth = firstDay.clone().add(-1).day().getMonth();
var lastYear = firstDay.clone().add(-1).day().getFullYear();
var daysInMonth = Date.getDaysInMonth(year, month);
var daysInLastMonth = Date.getDaysInMonth(lastYear, lastMonth);
var dayOfWeek = firstDay.getDay();
//initialize a 6 rows x 7 columns array for the calendar
var calendar = Array();
for (var i = 0; i < 6; i++) {
calendar[i] = Array();
}
//populate the calendar with date objects
var startDay = daysInLastMonth - dayOfWeek + this.locale.firstDay + 1;
if (dayOfWeek == 0)
startDay = daysInLastMonth - 6 + this.locale.firstDay;
var curDate = Date.today().set({ day: startDay, month: lastMonth, year: lastYear });
for (var i = 0, col = 0, row = 0; i < 42; i++, col++, curDate = curDate.clone().add(1).day()) {
if (i > 0 && col % 7 == 0) {
col = 0;
row++;
}
calendar[row][col] = curDate;
}
return calendar;
},
renderCalendar: function (calendar, selected) {
var html = '<table class="table-condensed">';
html += '<thead>';
html += '<tr>';
html += '<th class="prev"><i class="icon-arrow-left"></i></th>';
html += '<th colspan="5" class="quick-select"><select name="calendar-month" class="input-mini">';
$.each(Date.CultureInfo.abbreviatedMonthNames, function(index, month){
monthSelected = '';
monthNumber = Number(index)+1;
if(monthNumber == Number(calendar[1][1].toString('MM')))
monthSelected = ' selected="selected" ';
html += '<option ' + monthSelected + ' value="' + monthNumber + '">' + month + '</option>';
});
html += '</select>'
html += '<input name="calendar-year" type="text" class="input-mini" value="' + calendar[1][1].toString("yyyy") + '"/></th>';
html += '<th class="next"><i class="icon-arrow-right"></i></th>';
html += '</tr>';
html += '<tr>';
$.each(this.locale.daysOfWeek, function (index, dayOfWeek) {
html += '<th>' + dayOfWeek + '</th>';
});
html += '</tr>';
html += '</thead>';
html += '<tbody>';
for (var row = 0; row < 6; row++) {
html += '<tr>';
for (var col = 0; col < 7; col++) {
var cname = (calendar[row][col].getMonth() == calendar[1][1].getMonth()) ? '' : 'off';
if (calendar[row][col].equals(selected))
cname = 'active';
var title = 'r' + row + 'c' + col;
html += '<td class="' + cname + '" title="' + title + '">' + calendar[row][col].getDate() + '</td>';
}
html += '</tr>';
}
html += '</tbody>';
html += '</table>';
return html;
}
};
$.fn.daterangepicker = function (options, cb) {
this.each(function() {
var el = $(this);
if (!el.data('daterangepicker'))
el.data('daterangepicker', new DateRangePicker(el, options, cb));
});
return this;
};
} (window.jQuery);