volt-daterange 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/CODE_OF_CONDUCT.md +13 -0
  5. data/Gemfile +15 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +29 -0
  8. data/Rakefile +1 -0
  9. data/app/daterange/assets/css/daterangepicker.css +341 -0
  10. data/app/daterange/assets/js/01_moment.js +3112 -0
  11. data/app/daterange/assets/js/daterangepicker.js +1458 -0
  12. data/app/daterange/config/dependencies.rb +1 -0
  13. data/app/daterange/config/initializers/boot.rb +10 -0
  14. data/app/daterange/config/routes.rb +1 -0
  15. data/app/daterange/controllers/main_controller.rb +106 -0
  16. data/app/daterange/views/main/index.html +4 -0
  17. data/lib/volt/daterange.rb +18 -0
  18. data/lib/volt/daterange/version.rb +5 -0
  19. data/spec/dummy/.gitignore +9 -0
  20. data/spec/dummy/README.md +4 -0
  21. data/spec/dummy/app/main/assets/css/app.css.scss +1 -0
  22. data/spec/dummy/app/main/config/dependencies.rb +11 -0
  23. data/spec/dummy/app/main/config/initializers/boot.rb +10 -0
  24. data/spec/dummy/app/main/config/routes.rb +14 -0
  25. data/spec/dummy/app/main/controllers/main_controller.rb +27 -0
  26. data/spec/dummy/app/main/models/user.rb +12 -0
  27. data/spec/dummy/app/main/views/main/about.html +7 -0
  28. data/spec/dummy/app/main/views/main/index.html +6 -0
  29. data/spec/dummy/app/main/views/main/main.html +29 -0
  30. data/spec/dummy/config.ru +4 -0
  31. data/spec/dummy/config/app.rb +137 -0
  32. data/spec/dummy/config/base/index.html +15 -0
  33. data/spec/dummy/config/initializers/boot.rb +4 -0
  34. data/spec/integration/sample_integration_spec.rb +11 -0
  35. data/spec/sample_spec.rb +7 -0
  36. data/spec/spec_helper.rb +18 -0
  37. data/volt-daterange.gemspec +38 -0
  38. metadata +280 -0
@@ -0,0 +1,1458 @@
1
+ /**
2
+ * @version: 2.0.9
3
+ * @author: Dan Grossman http://www.dangrossman.info/
4
+ * @copyright: Copyright (c) 2012-2015 Dan Grossman. All rights reserved.
5
+ * @license: Licensed under the MIT license. See http://www.opensource.org/licenses/mit-license.php
6
+ * @website: https://www.improvely.com/
7
+ */
8
+
9
+ (function(root, factory) {
10
+
11
+ if (typeof define === 'function' && define.amd) {
12
+ define(['moment', 'jquery', 'exports'], function(momentjs, $, exports) {
13
+ root.daterangepicker = factory(root, exports, momentjs, $);
14
+ });
15
+
16
+ } else if (typeof exports !== 'undefined') {
17
+ var momentjs = require('moment');
18
+ var jQuery = window.jQuery;
19
+ if (jQuery === undefined) {
20
+ try {
21
+ jQuery = require('jquery');
22
+ } catch (err) {
23
+ if (!jQuery) throw new Error('jQuery dependency not found');
24
+ }
25
+ }
26
+
27
+ factory(root, exports, momentjs, jQuery);
28
+
29
+ // Finally, as a browser global.
30
+ } else {
31
+ root.daterangepicker = factory(root, {}, root.moment || moment, (root.jQuery || root.Zepto || root.ender || root.$));
32
+ }
33
+
34
+ }(this, function(root, daterangepicker, moment, $) {
35
+
36
+ var DateRangePicker = function(element, options, cb) {
37
+
38
+ //default settings for options
39
+ this.parentEl = 'body';
40
+ this.element = $(element);
41
+ this.startDate = moment().startOf('day');
42
+ this.endDate = moment().endOf('day');
43
+ this.timeZone = moment().utcOffset();
44
+ this.minDate = false;
45
+ this.maxDate = false;
46
+ this.dateLimit = false;
47
+ this.autoApply = false;
48
+ this.singleDatePicker = false;
49
+ this.showDropdowns = false;
50
+ this.showWeekNumbers = false;
51
+ this.timePicker = false;
52
+ this.timePicker24Hour = false;
53
+ this.timePickerIncrement = 1;
54
+ this.timePickerSeconds = false;
55
+ this.linkedCalendars = true;
56
+ this.autoUpdateInput = true;
57
+ this.ranges = {};
58
+
59
+ this.opens = 'right';
60
+ if (this.element.hasClass('pull-right'))
61
+ this.opens = 'left';
62
+
63
+ this.drops = 'down';
64
+ if (this.element.hasClass('dropup'))
65
+ this.drops = 'up';
66
+
67
+ this.buttonClasses = 'btn btn-sm';
68
+ this.applyClass = 'btn-success';
69
+ this.cancelClass = 'btn-default';
70
+
71
+ this.locale = {
72
+ format: 'MM/DD/YYYY',
73
+ separator: ' - ',
74
+ applyLabel: 'Apply',
75
+ cancelLabel: 'Cancel',
76
+ weekLabel: 'W',
77
+ customRangeLabel: 'Custom Range',
78
+ daysOfWeek: moment.weekdaysMin(),
79
+ monthNames: moment.monthsShort(),
80
+ firstDay: moment.localeData().firstDayOfWeek()
81
+ };
82
+
83
+ this.callback = function() { };
84
+
85
+ //some state information
86
+ this.isShowing = false;
87
+ this.leftCalendar = {};
88
+ this.rightCalendar = {};
89
+
90
+ //custom options from user
91
+ if (typeof options !== 'object' || options === null)
92
+ options = {};
93
+
94
+ //allow setting options with data attributes
95
+ //data-api options will be overwritten with custom javascript options
96
+ options = $.extend(this.element.data(), options);
97
+
98
+ //html template for the picker UI
99
+ if (typeof options.template !== 'string')
100
+ options.template = '<div class="daterangepicker dropdown-menu">' +
101
+ '<div class="calendar left">' +
102
+ '<div class="daterangepicker_input">' +
103
+ '<input class="input-mini" type="text" name="daterangepicker_start" value="" />' +
104
+ '<i class="fa fa-calendar glyphicon glyphicon-calendar"></i>' +
105
+ '<div class="calendar-time">' +
106
+ '<div></div>' +
107
+ '<i class="fa fa-clock-o glyphicon glyphicon-time"></i>' +
108
+ '</div>' +
109
+ '</div>' +
110
+ '<div class="calendar-table"></div>' +
111
+ '</div>' +
112
+ '<div class="calendar right">' +
113
+ '<div class="daterangepicker_input">' +
114
+ '<input class="input-mini" type="text" name="daterangepicker_end" value="" />' +
115
+ '<i class="fa fa-calendar glyphicon glyphicon-calendar"></i>' +
116
+ '<div class="calendar-time">' +
117
+ '<div></div>' +
118
+ '<i class="fa fa-clock-o glyphicon glyphicon-time"></i>' +
119
+ '</div>' +
120
+ '</div>' +
121
+ '<div class="calendar-table"></div>' +
122
+ '</div>' +
123
+ '<div class="ranges">' +
124
+ '<div class="range_inputs">' +
125
+ '<button class="applyBtn" disabled="disabled" type="button"></button> ' +
126
+ '<button class="cancelBtn" type="button"></button>' +
127
+ '</div>' +
128
+ '</div>' +
129
+ '</div>';
130
+
131
+ this.parentEl = (options.parentEl && $(options.parentEl).length) ? $(options.parentEl) : $(this.parentEl);
132
+ this.container = $(options.template).appendTo(this.parentEl);
133
+
134
+ //
135
+ // handle all the possible options overriding defaults
136
+ //
137
+
138
+ if (typeof options.locale === 'object') {
139
+
140
+ if (typeof options.locale.format === 'string')
141
+ this.locale.format = options.locale.format;
142
+
143
+ if (typeof options.locale.separator === 'string')
144
+ this.locale.separator = options.locale.separator;
145
+
146
+ if (typeof options.locale.daysOfWeek === 'object')
147
+ this.locale.daysOfWeek = options.locale.daysOfWeek.slice();
148
+
149
+ if (typeof options.locale.monthNames === 'object')
150
+ this.locale.monthNames = options.locale.monthNames.slice();
151
+
152
+ if (typeof options.locale.firstDay === 'number')
153
+ this.locale.firstDay = options.locale.firstDay;
154
+
155
+ if (typeof options.locale.applyLabel === 'string')
156
+ this.locale.applyLabel = options.locale.applyLabel;
157
+
158
+ if (typeof options.locale.cancelLabel === 'string')
159
+ this.locale.cancelLabel = options.locale.cancelLabel;
160
+
161
+ if (typeof options.locale.weekLabel === 'string')
162
+ this.locale.weekLabel = options.locale.weekLabel;
163
+
164
+ if (typeof options.locale.customRangeLabel === 'string')
165
+ this.locale.customRangeLabel = options.locale.customRangeLabel;
166
+
167
+ }
168
+
169
+ if (typeof options.startDate === 'string')
170
+ this.startDate = moment(options.startDate, this.locale.format);
171
+
172
+ if (typeof options.endDate === 'string')
173
+ this.endDate = moment(options.endDate, this.locale.format);
174
+
175
+ if (typeof options.minDate === 'string')
176
+ this.minDate = moment(options.minDate, this.locale.format);
177
+
178
+ if (typeof options.maxDate === 'string')
179
+ this.maxDate = moment(options.maxDate, this.locale.format);
180
+
181
+ if (typeof options.startDate === 'object')
182
+ this.startDate = moment(options.startDate);
183
+
184
+ if (typeof options.endDate === 'object')
185
+ this.endDate = moment(options.endDate);
186
+
187
+ if (typeof options.minDate === 'object')
188
+ this.minDate = moment(options.minDate);
189
+
190
+ if (typeof options.maxDate === 'object')
191
+ this.maxDate = moment(options.maxDate);
192
+
193
+ // sanity check for bad options
194
+ if (this.minDate && this.startDate.isBefore(this.minDate))
195
+ this.startDate = this.minDate.clone();
196
+
197
+ // sanity check for bad options
198
+ if (this.maxDate && this.endDate.isAfter(this.maxDate))
199
+ this.endDate = this.maxDate.clone();
200
+
201
+ if (typeof options.applyClass === 'string')
202
+ this.applyClass = options.applyClass;
203
+
204
+ if (typeof options.cancelClass === 'string')
205
+ this.cancelClass = options.cancelClass;
206
+
207
+ if (typeof options.dateLimit === 'object')
208
+ this.dateLimit = options.dateLimit;
209
+
210
+ if (typeof options.opens === 'string')
211
+ this.opens = options.opens;
212
+
213
+ if (typeof options.drops === 'string')
214
+ this.drops = options.drops;
215
+
216
+ if (typeof options.showWeekNumbers === 'boolean')
217
+ this.showWeekNumbers = options.showWeekNumbers;
218
+
219
+ if (typeof options.buttonClasses === 'string')
220
+ this.buttonClasses = options.buttonClasses;
221
+
222
+ if (typeof options.buttonClasses === 'object')
223
+ this.buttonClasses = options.buttonClasses.join(' ');
224
+
225
+ if (typeof options.showDropdowns === 'boolean')
226
+ this.showDropdowns = options.showDropdowns;
227
+
228
+ if (typeof options.singleDatePicker === 'boolean') {
229
+ this.singleDatePicker = options.singleDatePicker;
230
+ if (this.singleDatePicker)
231
+ this.endDate = this.startDate.clone();
232
+ }
233
+
234
+ if (typeof options.timePicker === 'boolean')
235
+ this.timePicker = options.timePicker;
236
+
237
+ if (typeof options.timePickerSeconds === 'boolean')
238
+ this.timePickerSeconds = options.timePickerSeconds;
239
+
240
+ if (typeof options.timePickerIncrement === 'number')
241
+ this.timePickerIncrement = options.timePickerIncrement;
242
+
243
+ if (typeof options.timePicker24Hour === 'boolean')
244
+ this.timePicker24Hour = options.timePicker24Hour;
245
+
246
+ if (typeof options.autoApply === 'boolean')
247
+ this.autoApply = options.autoApply;
248
+
249
+ if (typeof options.autoUpdateInput === 'boolean')
250
+ this.autoUpdateInput = options.autoUpdateInput;
251
+
252
+ if (typeof options.linkedCalendars === 'boolean')
253
+ this.linkedCalendars = options.linkedCalendars;
254
+
255
+ if (typeof options.isInvalidDate === 'function')
256
+ this.isInvalidDate = options.isInvalidDate;
257
+
258
+ // update day names order to firstDay
259
+ if (this.locale.firstDay != 0) {
260
+ var iterator = this.locale.firstDay;
261
+ while (iterator > 0) {
262
+ this.locale.daysOfWeek.push(this.locale.daysOfWeek.shift());
263
+ iterator--;
264
+ }
265
+ }
266
+
267
+ var start, end, range;
268
+
269
+ //if no start/end dates set, check if an input element contains initial values
270
+ if (typeof options.startDate === 'undefined' && typeof options.endDate === 'undefined') {
271
+ if ($(this.element).is('input[type=text]')) {
272
+ var val = $(this.element).val(),
273
+ split = val.split(this.locale.separator);
274
+
275
+ start = end = null;
276
+
277
+ if (split.length == 2) {
278
+ start = moment(split[0], this.locale.format);
279
+ end = moment(split[1], this.locale.format);
280
+ } else if (this.singleDatePicker && val !== "") {
281
+ start = moment(val, this.locale.format);
282
+ end = moment(val, this.locale.format);
283
+ }
284
+ if (start !== null && end !== null) {
285
+ this.setStartDate(start);
286
+ this.setEndDate(end);
287
+ }
288
+ }
289
+ }
290
+
291
+ // bind the time zone used to build the calendar to either the timeZone passed in through the options or the zone of the startDate (which will be the local time zone by default)
292
+ if (typeof options.timeZone === 'string' || typeof options.timeZone === 'number') {
293
+ if (typeof options.timeZone === 'string' && typeof moment.tz !== 'undefined') {
294
+ this.timeZone = moment.tz.zone(options.timeZone).parse(new Date) * -1; // Offset is positive if the timezone is behind UTC and negative if it is ahead.
295
+ } else {
296
+ this.timeZone = options.timeZone;
297
+ }
298
+ this.startDate.utcOffset(this.timeZone);
299
+ this.endDate.utcOffset(this.timeZone);
300
+ } else {
301
+ this.timeZone = moment(this.startDate).utcOffset();
302
+ }
303
+
304
+ if (typeof options.ranges === 'object') {
305
+ for (range in options.ranges) {
306
+
307
+ if (typeof options.ranges[range][0] === 'string')
308
+ start = moment(options.ranges[range][0], this.locale.format);
309
+ else
310
+ start = moment(options.ranges[range][0]);
311
+
312
+ if (typeof options.ranges[range][1] === 'string')
313
+ end = moment(options.ranges[range][1], this.locale.format);
314
+ else
315
+ end = moment(options.ranges[range][1]);
316
+
317
+ // If the start or end date exceed those allowed by the minDate or dateLimit
318
+ // options, shorten the range to the allowable period.
319
+ if (this.minDate && start.isBefore(this.minDate))
320
+ start = this.minDate.clone();
321
+
322
+ var maxDate = this.maxDate;
323
+ if (this.dateLimit && start.clone().add(this.dateLimit).isAfter(maxDate))
324
+ maxDate = start.clone().add(this.dateLimit);
325
+ if (maxDate && end.isAfter(maxDate))
326
+ end = maxDate.clone();
327
+
328
+ // If the end of the range is before the minimum or the start of the range is
329
+ // after the maximum, don't display this range option at all.
330
+ if ((this.minDate && end.isBefore(this.minDate)) || (maxDate && start.isAfter(maxDate)))
331
+ continue;
332
+
333
+ this.ranges[range] = [start, end];
334
+ }
335
+
336
+ var list = '<ul>';
337
+ for (range in this.ranges) {
338
+ list += '<li>' + range + '</li>';
339
+ }
340
+ list += '<li>' + this.locale.customRangeLabel + '</li>';
341
+ list += '</ul>';
342
+ this.container.find('.ranges ul').remove();
343
+ this.container.find('.ranges').prepend(list);
344
+ }
345
+
346
+ if (typeof cb === 'function') {
347
+ this.callback = cb;
348
+ }
349
+
350
+ if (!this.timePicker) {
351
+ this.startDate = this.startDate.startOf('day');
352
+ this.endDate = this.endDate.endOf('day');
353
+ this.container.find('.calendar-time').hide();
354
+ }
355
+
356
+ //can't be used together for now
357
+ if (this.timePicker && this.autoApply)
358
+ this.autoApply = false;
359
+
360
+ if (this.autoApply && typeof options.ranges !== 'object') {
361
+ this.container.find('.ranges').hide();
362
+ } else if (this.autoApply) {
363
+ this.container.find('.applyBtn, .cancelBtn').addClass('hide');
364
+ }
365
+
366
+ if (this.singleDatePicker) {
367
+ this.container.addClass('single');
368
+ this.container.find('.calendar.left').addClass('single');
369
+ this.container.find('.calendar.left').show();
370
+ this.container.find('.calendar.right').hide();
371
+ this.container.find('.daterangepicker_input input, .daterangepicker_input i').hide();
372
+ if (!this.timePicker) {
373
+ this.container.find('.ranges').hide();
374
+ }
375
+ }
376
+
377
+ if (typeof options.ranges === 'undefined' && !this.singleDatePicker) {
378
+ this.container.addClass('show-calendar');
379
+ }
380
+
381
+ this.container.removeClass('opensleft opensright').addClass('opens' + this.opens);
382
+
383
+ //apply CSS classes and labels to buttons
384
+ this.container.find('.applyBtn, .cancelBtn').addClass(this.buttonClasses);
385
+ if (this.applyClass.length)
386
+ this.container.find('.applyBtn').addClass(this.applyClass);
387
+ if (this.cancelClass.length)
388
+ this.container.find('.cancelBtn').addClass(this.cancelClass);
389
+ this.container.find('.applyBtn').html(this.locale.applyLabel);
390
+ this.container.find('.cancelBtn').html(this.locale.cancelLabel);
391
+
392
+ //
393
+ // event listeners
394
+ //
395
+
396
+ this.container.find('.calendar')
397
+ .on('click.daterangepicker', '.prev', $.proxy(this.clickPrev, this))
398
+ .on('click.daterangepicker', '.next', $.proxy(this.clickNext, this))
399
+ .on('click.daterangepicker', 'td.available', $.proxy(this.clickDate, this))
400
+ .on('mouseenter.daterangepicker', 'td.available', $.proxy(this.hoverDate, this))
401
+ .on('mouseleave.daterangepicker', 'td.available', $.proxy(this.updateFormInputs, this))
402
+ .on('change.daterangepicker', 'select.yearselect', $.proxy(this.monthOrYearChanged, this))
403
+ .on('change.daterangepicker', 'select.monthselect', $.proxy(this.monthOrYearChanged, this))
404
+ .on('change.daterangepicker', 'select.hourselect,select.minuteselect,select.secondselect,select.ampmselect', $.proxy(this.timeChanged, this))
405
+ .on('click.daterangepicker', '.daterangepicker_input input', $.proxy(this.showCalendars, this))
406
+ //.on('keyup.daterangepicker', '.daterangepicker_input input', $.proxy(this.formInputsChanged, this))
407
+ .on('change.daterangepicker', '.daterangepicker_input input', $.proxy(this.formInputsChanged, this));
408
+
409
+ this.container.find('.ranges')
410
+ .on('click.daterangepicker', 'button.applyBtn', $.proxy(this.clickApply, this))
411
+ .on('click.daterangepicker', 'button.cancelBtn', $.proxy(this.clickCancel, this))
412
+ .on('click.daterangepicker', 'li', $.proxy(this.clickRange, this))
413
+ .on('mouseenter.daterangepicker', 'li', $.proxy(this.hoverRange, this))
414
+ .on('mouseleave.daterangepicker', 'li', $.proxy(this.updateFormInputs, this));
415
+
416
+ if (this.element.is('input')) {
417
+ this.element.on({
418
+ 'click.daterangepicker': $.proxy(this.show, this),
419
+ 'focus.daterangepicker': $.proxy(this.show, this),
420
+ 'keyup.daterangepicker': $.proxy(this.controlChanged, this),
421
+ 'keydown.daterangepicker': $.proxy(this.keydown, this)
422
+ });
423
+ } else {
424
+ this.element.on('click.daterangepicker', $.proxy(this.toggle, this));
425
+ }
426
+
427
+ //
428
+ // if attached to a text input, set the initial value
429
+ //
430
+
431
+ if (this.element.is('input') && !this.singleDatePicker && this.autoUpdateInput) {
432
+ this.element.val(this.startDate.format(this.locale.format) + this.locale.separator + this.endDate.format(this.locale.format));
433
+ this.element.trigger('change');
434
+ } else if (this.element.is('input') && this.autoUpdateInput) {
435
+ this.element.val(this.startDate.format(this.locale.format));
436
+ this.element.trigger('change');
437
+ }
438
+
439
+ };
440
+
441
+ DateRangePicker.prototype = {
442
+
443
+ constructor: DateRangePicker,
444
+
445
+ setStartDate: function(startDate) {
446
+ if (typeof startDate === 'string')
447
+ this.startDate = moment(startDate, this.locale.format).utcOffset(this.timeZone);
448
+
449
+ if (typeof startDate === 'object')
450
+ this.startDate = moment(startDate);
451
+
452
+ if (!this.timePicker)
453
+ this.startDate = this.startDate.startOf('day');
454
+
455
+ if (this.timePicker && this.timePickerIncrement)
456
+ this.startDate.minute(Math.round(this.startDate.minute() / this.timePickerIncrement) * this.timePickerIncrement);
457
+
458
+ if (this.minDate && this.startDate.isBefore(this.minDate))
459
+ this.startDate = this.minDate;
460
+
461
+ if (this.maxDate && this.startDate.isAfter(this.maxDate))
462
+ this.startDate = this.maxDate;
463
+
464
+ this.updateMonthsInView();
465
+ },
466
+
467
+ setEndDate: function(endDate) {
468
+ if (typeof endDate === 'string')
469
+ this.endDate = moment(endDate, this.locale.format).utcOffset(this.timeZone);
470
+
471
+ if (typeof endDate === 'object')
472
+ this.endDate = moment(endDate);
473
+
474
+ if (!this.timePicker)
475
+ this.endDate = this.endDate.endOf('day');
476
+
477
+ if (this.timePicker && this.timePickerIncrement)
478
+ this.endDate.minute(Math.round(this.endDate.minute() / this.timePickerIncrement) * this.timePickerIncrement);
479
+
480
+ if (this.endDate.isBefore(this.startDate))
481
+ this.endDate = this.startDate.clone();
482
+
483
+ if (this.maxDate && this.endDate.isAfter(this.maxDate))
484
+ this.endDate = this.maxDate;
485
+
486
+ if (this.dateLimit && this.startDate.clone().add(this.dateLimit).isBefore(this.endDate))
487
+ this.endDate = this.startDate.clone().add(this.dateLimit);
488
+
489
+ this.updateMonthsInView();
490
+ },
491
+
492
+ isInvalidDate: function() {
493
+ return false;
494
+ },
495
+
496
+ updateView: function() {
497
+ if (this.timePicker) {
498
+ this.renderTimePicker('left');
499
+ this.renderTimePicker('right');
500
+ if (!this.endDate) {
501
+ this.container.find('.right .calendar-time select').attr('disabled', 'disabled').addClass('disabled');
502
+ } else {
503
+ this.container.find('.right .calendar-time select').removeAttr('disabled').removeClass('disabled');
504
+ }
505
+ }
506
+ if (this.endDate) {
507
+ this.container.find('input[name="daterangepicker_end"]').removeClass('active');
508
+ this.container.find('input[name="daterangepicker_start"]').addClass('active');
509
+ } else {
510
+ this.container.find('input[name="daterangepicker_end"]').addClass('active');
511
+ this.container.find('input[name="daterangepicker_start"]').removeClass('active');
512
+ }
513
+ this.updateMonthsInView();
514
+ this.updateCalendars();
515
+ this.updateFormInputs();
516
+ },
517
+
518
+ updateMonthsInView: function() {
519
+ if (this.endDate) {
520
+
521
+ //if both dates are visible already, do nothing
522
+ if (this.leftCalendar.month && this.rightCalendar.month &&
523
+ (this.startDate.format('YYYY-MM') == this.leftCalendar.month.format('YYYY-MM') || this.startDate.format('YYYY-MM') == this.rightCalendar.month.format('YYYY-MM'))
524
+ &&
525
+ (this.endDate.format('YYYY-MM') == this.leftCalendar.month.format('YYYY-MM') || this.endDate.format('YYYY-MM') == this.rightCalendar.month.format('YYYY-MM'))
526
+ ) {
527
+ return;
528
+ }
529
+
530
+ this.leftCalendar.month = this.startDate.clone().date(2);
531
+ if (!this.linkedCalendars && (this.endDate.month() != this.startDate.month() || this.endDate.year() != this.startDate.year())) {
532
+ this.rightCalendar.month = this.endDate.clone().date(2);
533
+ } else {
534
+ this.rightCalendar.month = this.startDate.clone().date(2).add(1, 'month');
535
+ }
536
+
537
+ } else {
538
+ if (this.leftCalendar.month.format('YYYY-MM') != this.startDate.format('YYYY-MM') && this.rightCalendar.month.format('YYYY-MM') != this.startDate.format('YYYY-MM')) {
539
+ this.leftCalendar.month = this.startDate.clone().date(2);
540
+ this.rightCalendar.month = this.startDate.clone().date(2).add(1, 'month');
541
+ }
542
+ }
543
+ },
544
+
545
+ updateCalendars: function() {
546
+
547
+ if (this.timePicker) {
548
+ var hour, minute, second;
549
+ if (this.endDate) {
550
+ hour = parseInt(this.container.find('.left .hourselect').val(), 10);
551
+ minute = parseInt(this.container.find('.left .minuteselect').val(), 10);
552
+ second = this.timePickerSeconds ? parseInt(this.container.find('.left .secondselect').val(), 10) : 0;
553
+ if (!this.timePicker24Hour) {
554
+ var ampm = this.container.find('.left .ampmselect').val();
555
+ if (ampm === 'PM' && hour < 12)
556
+ hour += 12;
557
+ if (ampm === 'AM' && hour === 12)
558
+ hour = 0;
559
+ }
560
+ } else {
561
+ hour = parseInt(this.container.find('.right .hourselect').val(), 10);
562
+ minute = parseInt(this.container.find('.right .minuteselect').val(), 10);
563
+ second = this.timePickerSeconds ? parseInt(this.container.find('.right .secondselect').val(), 10) : 0;
564
+ if (!this.timePicker24Hour) {
565
+ var ampm = this.container.find('.left .ampmselect').val();
566
+ if (ampm === 'PM' && hour < 12)
567
+ hour += 12;
568
+ if (ampm === 'AM' && hour === 12)
569
+ hour = 0;
570
+ }
571
+ }
572
+ this.leftCalendar.month.hour(hour).minute(minute).second(second);
573
+ this.rightCalendar.month.hour(hour).minute(minute).second(second);
574
+ }
575
+
576
+ this.renderCalendar('left');
577
+ this.renderCalendar('right');
578
+
579
+ //highlight any predefined range matching the current start and end dates
580
+ this.container.find('.ranges li').removeClass('active');
581
+ if (this.endDate == null) return;
582
+
583
+ var customRange = true;
584
+ var i = 0;
585
+ for (var range in this.ranges) {
586
+ if (this.timePicker) {
587
+ if (this.startDate.isSame(this.ranges[range][0]) && this.endDate.isSame(this.ranges[range][1])) {
588
+ customRange = false;
589
+ this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').html();
590
+ break;
591
+ }
592
+ } else {
593
+ //ignore times when comparing dates if time picker is not enabled
594
+ if (this.startDate.format('YYYY-MM-DD') == this.ranges[range][0].format('YYYY-MM-DD') && this.endDate.format('YYYY-MM-DD') == this.ranges[range][1].format('YYYY-MM-DD')) {
595
+ customRange = false;
596
+ this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').html();
597
+ break;
598
+ }
599
+ }
600
+ i++;
601
+ }
602
+ if (customRange) {
603
+ this.chosenLabel = this.container.find('.ranges li:last').addClass('active').html();
604
+ this.showCalendars();
605
+ }
606
+
607
+ },
608
+
609
+ renderCalendar: function(side) {
610
+
611
+ //
612
+ // Build the matrix of dates that will populate the calendar
613
+ //
614
+
615
+ var calendar = side == 'left' ? this.leftCalendar : this.rightCalendar;
616
+ var month = calendar.month.month();
617
+ var year = calendar.month.year();
618
+ var hour = calendar.month.hour();
619
+ var minute = calendar.month.minute();
620
+ var second = calendar.month.second();
621
+ var daysInMonth = moment([year, month]).daysInMonth();
622
+ var firstDay = moment([year, month, 1]);
623
+ var lastDay = moment([year, month, daysInMonth]);
624
+ var lastMonth = moment(firstDay).subtract(1, 'month').month();
625
+ var lastYear = moment(firstDay).subtract(1, 'month').year();
626
+ var daysInLastMonth = moment([lastYear, lastMonth]).daysInMonth();
627
+ var dayOfWeek = firstDay.day();
628
+
629
+ //initialize a 6 rows x 7 columns array for the calendar
630
+ var calendar = [];
631
+ calendar.firstDay = firstDay;
632
+ calendar.lastDay = lastDay;
633
+
634
+ for (var i = 0; i < 6; i++) {
635
+ calendar[i] = [];
636
+ }
637
+
638
+ //populate the calendar with date objects
639
+ var startDay = daysInLastMonth - dayOfWeek + this.locale.firstDay + 1;
640
+ if (startDay > daysInLastMonth)
641
+ startDay -= 7;
642
+
643
+ if (dayOfWeek == this.locale.firstDay)
644
+ startDay = daysInLastMonth - 6;
645
+
646
+ // Possible patch for issue #626 https://github.com/dangrossman/bootstrap-daterangepicker/issues/626
647
+ var curDate = moment([lastYear, lastMonth, startDay, 12, minute, second]).utcOffset(this.timeZone); // .utcOffset(this.timeZone);
648
+
649
+ var col, row;
650
+ for (var i = 0, col = 0, row = 0; i < 42; i++, col++, curDate = moment(curDate).add(24, 'hour')) {
651
+ if (i > 0 && col % 7 === 0) {
652
+ col = 0;
653
+ row++;
654
+ }
655
+ calendar[row][col] = curDate.clone().hour(hour).minute(minute).second(second);
656
+ curDate.hour(12);
657
+
658
+ if (this.minDate && calendar[row][col].format('YYYY-MM-DD') == this.minDate.format('YYYY-MM-DD') && calendar[row][col].isBefore(this.minDate) && side == 'left') {
659
+ calendar[row][col] = this.minDate.clone();
660
+ }
661
+
662
+ if (this.maxDate && calendar[row][col].format('YYYY-MM-DD') == this.maxDate.format('YYYY-MM-DD') && calendar[row][col].isAfter(this.maxDate) && side == 'right') {
663
+ calendar[row][col] = this.maxDate.clone();
664
+ }
665
+
666
+ }
667
+
668
+ //make the calendar object available to hoverDate/clickDate
669
+ if (side == 'left') {
670
+ this.leftCalendar.calendar = calendar;
671
+ } else {
672
+ this.rightCalendar.calendar = calendar;
673
+ }
674
+
675
+ //
676
+ // Display the calendar
677
+ //
678
+
679
+ var minDate = side == 'left' ? this.minDate : this.startDate;
680
+ var maxDate = this.maxDate;
681
+ var selected = side == 'left' ? this.startDate : this.endDate;
682
+
683
+ var html = '<table class="table-condensed">';
684
+ html += '<thead>';
685
+ html += '<tr>';
686
+
687
+ // add empty cell for week number
688
+ if (this.showWeekNumbers)
689
+ html += '<th></th>';
690
+
691
+ if ((!minDate || minDate.isBefore(calendar.firstDay)) && (!this.linkedCalendars || side == 'left')) {
692
+ html += '<th class="prev available"><i class="fa fa-chevron-left glyphicon glyphicon-chevron-left"></i></th>';
693
+ } else {
694
+ html += '<th></th>';
695
+ }
696
+
697
+ var dateHtml = this.locale.monthNames[calendar[1][1].month()] + calendar[1][1].format(" YYYY");
698
+
699
+ if (this.showDropdowns) {
700
+ var currentMonth = calendar[1][1].month();
701
+ var currentYear = calendar[1][1].year();
702
+ var maxYear = (maxDate && maxDate.year()) || (currentYear + 5);
703
+ var minYear = (minDate && minDate.year()) || (currentYear - 50);
704
+ var inMinYear = currentYear == minYear;
705
+ var inMaxYear = currentYear == maxYear;
706
+
707
+ var monthHtml = '<select class="monthselect">';
708
+ for (var m = 0; m < 12; m++) {
709
+ if ((!inMinYear || m >= minDate.month()) && (!inMaxYear || m <= maxDate.month())) {
710
+ monthHtml += "<option value='" + m + "'" +
711
+ (m === currentMonth ? " selected='selected'" : "") +
712
+ ">" + this.locale.monthNames[m] + "</option>";
713
+ } else {
714
+ monthHtml += "<option value='" + m + "'" +
715
+ (m === currentMonth ? " selected='selected'" : "") +
716
+ " disabled='disabled'>" + this.locale.monthNames[m] + "</option>";
717
+ }
718
+ }
719
+ monthHtml += "</select>";
720
+
721
+ var yearHtml = '<select class="yearselect">';
722
+ for (var y = minYear; y <= maxYear; y++) {
723
+ yearHtml += '<option value="' + y + '"' +
724
+ (y === currentYear ? ' selected="selected"' : '') +
725
+ '>' + y + '</option>';
726
+ }
727
+ yearHtml += '</select>';
728
+
729
+ dateHtml = monthHtml + yearHtml;
730
+ }
731
+
732
+ html += '<th colspan="5" class="month">' + dateHtml + '</th>';
733
+ if ((!maxDate || maxDate.isAfter(calendar.lastDay)) && (!this.linkedCalendars || side == 'right' || this.singleDatePicker)) {
734
+ html += '<th class="next available"><i class="fa fa-chevron-right glyphicon glyphicon-chevron-right"></i></th>';
735
+ } else {
736
+ html += '<th></th>';
737
+ }
738
+
739
+ html += '</tr>';
740
+ html += '<tr>';
741
+
742
+ // add week number label
743
+ if (this.showWeekNumbers)
744
+ html += '<th class="week">' + this.locale.weekLabel + '</th>';
745
+
746
+ $.each(this.locale.daysOfWeek, function(index, dayOfWeek) {
747
+ html += '<th>' + dayOfWeek + '</th>';
748
+ });
749
+
750
+ html += '</tr>';
751
+ html += '</thead>';
752
+ html += '<tbody>';
753
+
754
+ //adjust maxDate to reflect the dateLimit setting in order to
755
+ //grey out end dates beyond the dateLimit
756
+ if (this.endDate == null && this.dateLimit) {
757
+ var maxLimit = this.startDate.clone().add(this.dateLimit).endOf('day');
758
+ if (!maxDate || maxLimit.isBefore(maxDate)) {
759
+ maxDate = maxLimit;
760
+ }
761
+ }
762
+
763
+ for (var row = 0; row < 6; row++) {
764
+ html += '<tr>';
765
+
766
+ // add week number
767
+ if (this.showWeekNumbers)
768
+ html += '<td class="week">' + calendar[row][0].week() + '</td>';
769
+
770
+ for (var col = 0; col < 7; col++) {
771
+
772
+ var classes = [];
773
+
774
+ //highlight today's date
775
+ if (calendar[row][col].isSame(new Date(), "day"))
776
+ classes.push('today');
777
+
778
+ //highlight weekends
779
+ if (calendar[row][col].isoWeekday() > 5)
780
+ classes.push('weekend');
781
+
782
+ //grey out the dates in other months displayed at beginning and end of this calendar
783
+ if (calendar[row][col].month() != calendar[1][1].month())
784
+ classes.push('off');
785
+
786
+ //don't allow selection of dates before the minimum date
787
+ if (this.minDate && calendar[row][col].isBefore(this.minDate, 'day'))
788
+ classes.push('off', 'disabled');
789
+
790
+ //don't allow selection of dates after the maximum date
791
+ if (maxDate && calendar[row][col].isAfter(maxDate, 'day'))
792
+ classes.push('off', 'disabled');
793
+
794
+ //don't allow selection of date if a custom function decides it's invalid
795
+ if (this.isInvalidDate(calendar[row][col]))
796
+ classes.push('off', 'disabled');
797
+
798
+ //highlight the currently selected start date
799
+ if (calendar[row][col].format('YYYY-MM-DD') == this.startDate.format('YYYY-MM-DD'))
800
+ classes.push('active', 'start-date');
801
+
802
+ //highlight the currently selected end date
803
+ if (this.endDate != null && calendar[row][col].format('YYYY-MM-DD') == this.endDate.format('YYYY-MM-DD'))
804
+ classes.push('active', 'end-date');
805
+
806
+ //highlight dates in-between the selected dates
807
+ if (this.endDate != null && calendar[row][col] > this.startDate && calendar[row][col] < this.endDate)
808
+ classes.push('in-range');
809
+
810
+ var cname = '', disabled = false;
811
+ for (var i = 0; i < classes.length; i++) {
812
+ cname += classes[i] + ' ';
813
+ if (classes[i] == 'disabled')
814
+ disabled = true;
815
+ }
816
+ if (!disabled)
817
+ cname += 'available';
818
+
819
+ html += '<td class="' + cname.replace(/^\s+|\s+$/g, '') + '" data-title="' + 'r' + row + 'c' + col + '">' + calendar[row][col].date() + '</td>';
820
+
821
+ }
822
+ html += '</tr>';
823
+ }
824
+
825
+ html += '</tbody>';
826
+ html += '</table>';
827
+
828
+ this.container.find('.calendar.' + side + ' .calendar-table').html(html);
829
+
830
+ },
831
+
832
+ renderTimePicker: function(side) {
833
+
834
+ var selected, minDate, maxDate = this.maxDate;
835
+
836
+ if (this.dateLimit && (!this.maxDate || this.startDate.clone().add(this.dateLimit).isAfter(this.maxDate)))
837
+ maxDate = this.startDate.clone().add(this.dateLimit);
838
+
839
+ if (side == 'left') {
840
+ selected = this.startDate.clone();
841
+ minDate = this.minDate;
842
+ } else if (side == 'right') {
843
+ selected = this.endDate ? this.endDate.clone() : this.startDate.clone();
844
+ minDate = this.startDate;
845
+ }
846
+
847
+ //
848
+ // hours
849
+ //
850
+
851
+ html = '<select class="hourselect">';
852
+
853
+ var start = this.timePicker24Hour ? 0 : 1;
854
+ var end = this.timePicker24Hour ? 23 : 12;
855
+
856
+ for (var i = start; i <= end; i++) {
857
+ var i_in_24 = i;
858
+ if (!this.timePicker24Hour)
859
+ i_in_24 = selected.hour() >= 12 ? (i == 12 ? 12 : i + 12) : (i == 12 ? 0 : i);
860
+
861
+ var time = selected.clone().hour(i_in_24);
862
+ var disabled = false;
863
+ if (minDate && time.minute(59).isBefore(minDate))
864
+ disabled = true;
865
+ if (maxDate && time.minute(0).isAfter(maxDate))
866
+ disabled = true;
867
+
868
+ if (i_in_24 == selected.hour() && !disabled) {
869
+ html += '<option value="' + i + '" selected="selected">' + i + '</option>';
870
+ } else if (disabled) {
871
+ html += '<option value="' + i + '" disabled="disabled" class="disabled">' + i + '</option>';
872
+ } else {
873
+ html += '<option value="' + i + '">' + i + '</option>';
874
+ }
875
+ }
876
+
877
+ html += '</select> ';
878
+
879
+ //
880
+ // minutes
881
+ //
882
+
883
+ html += ': <select class="minuteselect">';
884
+
885
+ for (var i = 0; i < 60; i += this.timePickerIncrement) {
886
+ var padded = i < 10 ? '0' + i : i;
887
+ var time = selected.clone().minute(i);
888
+
889
+ var disabled = false;
890
+ if (minDate && time.second(59).isBefore(minDate))
891
+ disabled = true;
892
+ if (maxDate && time.second(0).isAfter(maxDate))
893
+ disabled = true;
894
+
895
+ if (selected.minute() == i && !disabled) {
896
+ html += '<option value="' + i + '" selected="selected">' + padded + '</option>';
897
+ } else if (disabled) {
898
+ html += '<option value="' + i + '" disabled="disabled" class="disabled">' + padded + '</option>';
899
+ } else {
900
+ html += '<option value="' + i + '">' + padded + '</option>';
901
+ }
902
+ }
903
+
904
+ html += '</select> ';
905
+
906
+ //
907
+ // seconds
908
+ //
909
+
910
+ if (this.timePickerSeconds) {
911
+ html += ': <select class="secondselect">';
912
+
913
+ for (var i = 0; i < 60; i++) {
914
+ var padded = i < 10 ? '0' + i : i;
915
+ var time = selected.clone().second(i);
916
+
917
+ var disabled = false;
918
+ if (minDate && time.isBefore(minDate))
919
+ disabled = true;
920
+ if (maxDate && time.isAfter(maxDate))
921
+ disabled = true;
922
+
923
+ if (selected.second() == i && !disabled) {
924
+ html += '<option value="' + i + '" selected="selected">' + padded + '</option>';
925
+ } else if (disabled) {
926
+ html += '<option value="' + i + '" disabled="disabled" class="disabled">' + padded + '</option>';
927
+ } else {
928
+ html += '<option value="' + i + '">' + padded + '</option>';
929
+ }
930
+ }
931
+
932
+ html += '</select> ';
933
+ }
934
+
935
+ //
936
+ // AM/PM
937
+ //
938
+
939
+ if (!this.timePicker24Hour) {
940
+ html += '<select class="ampmselect">';
941
+
942
+ var am_html = '';
943
+ var pm_html = '';
944
+
945
+ if (minDate && selected.clone().hour(12).minute(0).second(0).isBefore(minDate))
946
+ am_html = ' disabled="disabled" class="disabled"';
947
+
948
+ if (maxDate && selected.clone().hour(0).minute(0).second(0).isAfter(maxDate))
949
+ pm_html = ' disabled="disabled" class="disabled"';
950
+
951
+ if (selected.hour() >= 12) {
952
+ html += '<option value="AM"' + am_html + '>AM</option><option value="PM" selected="selected"' + pm_html + '>PM</option>';
953
+ } else {
954
+ html += '<option value="AM" selected="selected"' + am_html + '>AM</option><option value="PM"' + pm_html + '>PM</option>';
955
+ }
956
+
957
+ html += '</select>';
958
+ }
959
+
960
+ this.container.find('.calendar.' + side + ' .calendar-time div').html(html);
961
+
962
+ },
963
+
964
+ updateFormInputs: function() {
965
+ this.container.find('input[name=daterangepicker_start]').val(this.startDate.format(this.locale.format));
966
+ if (this.endDate)
967
+ this.container.find('input[name=daterangepicker_end]').val(this.endDate.format(this.locale.format));
968
+
969
+ if (this.singleDatePicker || (this.endDate && (this.startDate.isBefore(this.endDate) || this.startDate.isSame(this.endDate)))) {
970
+ this.container.find('button.applyBtn').removeAttr('disabled');
971
+ } else {
972
+ this.container.find('button.applyBtn').attr('disabled', 'disabled');
973
+ }
974
+ },
975
+
976
+ move: function() {
977
+ var parentOffset = { top: 0, left: 0 },
978
+ containerTop;
979
+ var parentRightEdge = $(window).width();
980
+ if (!this.parentEl.is('body')) {
981
+ parentOffset = {
982
+ top: this.parentEl.offset().top - this.parentEl.scrollTop(),
983
+ left: this.parentEl.offset().left - this.parentEl.scrollLeft()
984
+ };
985
+ parentRightEdge = this.parentEl[0].clientWidth + this.parentEl.offset().left;
986
+ }
987
+
988
+ if (this.drops == 'up')
989
+ containerTop = this.element.offset().top - this.container.outerHeight() - parentOffset.top;
990
+ else
991
+ containerTop = this.element.offset().top + this.element.outerHeight() - parentOffset.top;
992
+ this.container[this.drops == 'up' ? 'addClass' : 'removeClass']('dropup');
993
+
994
+ if (this.opens == 'left') {
995
+ this.container.css({
996
+ top: containerTop,
997
+ right: parentRightEdge - this.element.offset().left - this.element.outerWidth(),
998
+ left: 'auto'
999
+ });
1000
+ if (this.container.offset().left < 0) {
1001
+ this.container.css({
1002
+ right: 'auto',
1003
+ left: 9
1004
+ });
1005
+ }
1006
+ } else if (this.opens == 'center') {
1007
+ this.container.css({
1008
+ top: containerTop,
1009
+ left: this.element.offset().left - parentOffset.left + this.element.outerWidth() / 2
1010
+ - this.container.outerWidth() / 2,
1011
+ right: 'auto'
1012
+ });
1013
+ if (this.container.offset().left < 0) {
1014
+ this.container.css({
1015
+ right: 'auto',
1016
+ left: 9
1017
+ });
1018
+ }
1019
+ } else {
1020
+ this.container.css({
1021
+ top: containerTop,
1022
+ left: this.element.offset().left - parentOffset.left,
1023
+ right: 'auto'
1024
+ });
1025
+ if (this.container.offset().left + this.container.outerWidth() > $(window).width()) {
1026
+ this.container.css({
1027
+ left: 'auto',
1028
+ right: 0
1029
+ });
1030
+ }
1031
+ }
1032
+ },
1033
+
1034
+ show: function(e) {
1035
+ if (this.isShowing) return;
1036
+
1037
+ // Create a click proxy that is private to this instance of datepicker, for unbinding
1038
+ this._outsideClickProxy = $.proxy(function(e) { this.outsideClick(e); }, this);
1039
+ // Bind global datepicker mousedown for hiding and
1040
+ $(document)
1041
+ .on('mousedown.daterangepicker', this._outsideClickProxy)
1042
+ // also support mobile devices
1043
+ .on('touchend.daterangepicker', this._outsideClickProxy)
1044
+ // also explicitly play nice with Bootstrap dropdowns, which stopPropagation when clicking them
1045
+ .on('click.daterangepicker', '[data-toggle=dropdown]', this._outsideClickProxy)
1046
+ // and also close when focus changes to outside the picker (eg. tabbing between controls)
1047
+ .on('focusin.daterangepicker', this._outsideClickProxy);
1048
+
1049
+ this.oldStartDate = this.startDate.clone();
1050
+ this.oldEndDate = this.endDate.clone();
1051
+
1052
+ this.updateView();
1053
+ this.container.show();
1054
+ this.move();
1055
+ this.element.trigger('show.daterangepicker', this);
1056
+ this.isShowing = true;
1057
+ },
1058
+
1059
+ hide: function(e) {
1060
+ if (!this.isShowing) return;
1061
+
1062
+ //incomplete date selection, revert to last values
1063
+ if (!this.endDate) {
1064
+ this.startDate = this.oldStartDate.clone();
1065
+ this.endDate = this.oldEndDate.clone();
1066
+ }
1067
+
1068
+ //if a new date range was selected, invoke the user callback function
1069
+ if (!this.startDate.isSame(this.oldStartDate) || !this.endDate.isSame(this.oldEndDate))
1070
+ this.callback(this.startDate, this.endDate, this.chosenLabel);
1071
+
1072
+ //if picker is attached to a text input, update it
1073
+ if (this.element.is('input') && !this.singleDatePicker && this.autoUpdateInput) {
1074
+ this.element.val(this.startDate.format(this.locale.format) + this.locale.separator + this.endDate.format(this.locale.format));
1075
+ this.element.trigger('change');
1076
+ } else if (this.element.is('input') && this.autoUpdateInput) {
1077
+ this.element.val(this.startDate.format(this.locale.format));
1078
+ this.element.trigger('change');
1079
+ }
1080
+
1081
+ $(document).off('.daterangepicker');
1082
+ this.container.hide();
1083
+ this.element.trigger('hide.daterangepicker', this);
1084
+ this.isShowing = false;
1085
+ },
1086
+
1087
+ toggle: function(e) {
1088
+ if (this.isShowing) {
1089
+ this.hide();
1090
+ } else {
1091
+ this.show();
1092
+ }
1093
+ },
1094
+
1095
+ outsideClick: function(e) {
1096
+ var target = $(e.target);
1097
+ // if the page is clicked anywhere except within the daterangerpicker/button
1098
+ // itself then call this.hide()
1099
+ if (
1100
+ // ie modal dialog fix
1101
+ e.type == "focusin" ||
1102
+ target.closest(this.element).length ||
1103
+ target.closest(this.container).length ||
1104
+ target.closest('.calendar-table').length
1105
+ ) return;
1106
+ this.hide();
1107
+ },
1108
+
1109
+ showCalendars: function() {
1110
+ this.container.addClass('show-calendar');
1111
+ this.move();
1112
+ this.element.trigger('showCalendar.daterangepicker', this);
1113
+ },
1114
+
1115
+ hideCalendars: function() {
1116
+ this.container.removeClass('show-calendar');
1117
+ this.element.trigger('hideCalendar.daterangepicker', this);
1118
+ },
1119
+
1120
+ hoverRange: function(e) {
1121
+ var label = e.target.innerHTML;
1122
+ if (label == this.locale.customRangeLabel) {
1123
+ this.updateView();
1124
+ } else {
1125
+ var dates = this.ranges[label];
1126
+ this.container.find('input[name=daterangepicker_start]').val(dates[0].format(this.locale.format));
1127
+ this.container.find('input[name=daterangepicker_end]').val(dates[1].format(this.locale.format));
1128
+ }
1129
+ },
1130
+
1131
+ clickRange: function(e) {
1132
+ var label = e.target.innerHTML;
1133
+ this.chosenLabel = label;
1134
+ if (label == this.locale.customRangeLabel) {
1135
+ this.showCalendars();
1136
+ } else {
1137
+ var dates = this.ranges[label];
1138
+ this.startDate = dates[0];
1139
+ this.endDate = dates[1];
1140
+
1141
+ if (!this.timePicker) {
1142
+ this.startDate.startOf('day');
1143
+ this.endDate.endOf('day');
1144
+ }
1145
+
1146
+ this.hideCalendars();
1147
+ this.clickApply();
1148
+ }
1149
+ },
1150
+
1151
+ clickPrev: function(e) {
1152
+ var cal = $(e.target).parents('.calendar');
1153
+ if (cal.hasClass('left')) {
1154
+ this.leftCalendar.month.subtract(1, 'month');
1155
+ if (this.linkedCalendars)
1156
+ this.rightCalendar.month.subtract(1, 'month');
1157
+ } else {
1158
+ this.rightCalendar.month.subtract(1, 'month');
1159
+ }
1160
+ this.updateCalendars();
1161
+ },
1162
+
1163
+ clickNext: function(e) {
1164
+ var cal = $(e.target).parents('.calendar');
1165
+ if (cal.hasClass('left')) {
1166
+ this.leftCalendar.month.add(1, 'month');
1167
+ } else {
1168
+ this.rightCalendar.month.add(1, 'month');
1169
+ if (this.linkedCalendars)
1170
+ this.leftCalendar.month.add(1, 'month');
1171
+ }
1172
+ this.updateCalendars();
1173
+ },
1174
+
1175
+ hoverDate: function(e) {
1176
+
1177
+ //ignore dates that can't be selected
1178
+ if (!$(e.target).hasClass('available')) return;
1179
+
1180
+ //have the text inputs above calendars reflect the date being hovered over
1181
+ var title = $(e.target).attr('data-title');
1182
+ var row = title.substr(1, 1);
1183
+ var col = title.substr(3, 1);
1184
+ var cal = $(e.target).parents('.calendar');
1185
+ var date = cal.hasClass('left') ? this.leftCalendar.calendar[row][col] : this.rightCalendar.calendar[row][col];
1186
+
1187
+ if (this.endDate) {
1188
+ this.container.find('input[name=daterangepicker_start]').val(date.format(this.locale.format));
1189
+ } else {
1190
+ this.container.find('input[name=daterangepicker_end]').val(date.format(this.locale.format));
1191
+ }
1192
+
1193
+ //highlight the dates between the start date and the date being hovered as a potential end date
1194
+ var leftCalendar = this.leftCalendar;
1195
+ var rightCalendar = this.rightCalendar;
1196
+ var startDate = this.startDate;
1197
+ if (!this.endDate) {
1198
+ this.container.find('.calendar td').each(function(index, el) {
1199
+
1200
+ //skip week numbers, only look at dates
1201
+ if ($(el).hasClass('week')) return;
1202
+
1203
+ var title = $(el).attr('data-title');
1204
+ var row = title.substr(1, 1);
1205
+ var col = title.substr(3, 1);
1206
+ var cal = $(el).parents('.calendar');
1207
+ var dt = cal.hasClass('left') ? leftCalendar.calendar[row][col] : rightCalendar.calendar[row][col];
1208
+
1209
+ if (dt.isAfter(startDate) && dt.isBefore(date)) {
1210
+ $(el).addClass('in-range');
1211
+ } else {
1212
+ $(el).removeClass('in-range');
1213
+ }
1214
+
1215
+ });
1216
+ }
1217
+
1218
+ },
1219
+
1220
+ clickDate: function(e) {
1221
+
1222
+ if (!$(e.target).hasClass('available')) return;
1223
+
1224
+ var title = $(e.target).attr('data-title');
1225
+ var row = title.substr(1, 1);
1226
+ var col = title.substr(3, 1);
1227
+ var cal = $(e.target).parents('.calendar');
1228
+ var date = cal.hasClass('left') ? this.leftCalendar.calendar[row][col] : this.rightCalendar.calendar[row][col];
1229
+
1230
+ //
1231
+ // this function needs to do a few things:
1232
+ // * alternate between selecting a start and end date for the range,
1233
+ // * if the time picker is enabled, apply the hour/minute/second from the select boxes to the clicked date
1234
+ // * if autoapply is enabled, and an end date was chosen, apply the selection
1235
+ // * if single date picker mode, and time picker isn't enabled, apply the selection immediately
1236
+ //
1237
+
1238
+ if (this.endDate || date.isBefore(this.startDate)) {
1239
+ if (this.timePicker) {
1240
+ var hour = parseInt(this.container.find('.left .hourselect').val(), 10);
1241
+ if (!this.timePicker24Hour) {
1242
+ var ampm = cal.find('.ampmselect').val();
1243
+ if (ampm === 'PM' && hour < 12)
1244
+ hour += 12;
1245
+ if (ampm === 'AM' && hour === 12)
1246
+ hour = 0;
1247
+ }
1248
+ var minute = parseInt(this.container.find('.left .minuteselect').val(), 10);
1249
+ var second = this.timePickerSeconds ? parseInt(this.container.find('.left .secondselect').val(), 10) : 0;
1250
+ date = date.clone().hour(hour).minute(minute).second(second);
1251
+ }
1252
+ this.endDate = null;
1253
+ this.setStartDate(date.clone());
1254
+ } else {
1255
+ if (this.timePicker) {
1256
+ var hour = parseInt(this.container.find('.right .hourselect').val(), 10);
1257
+ if (!this.timePicker24Hour) {
1258
+ var ampm = this.container.find('.right .ampmselect').val();
1259
+ if (ampm === 'PM' && hour < 12)
1260
+ hour += 12;
1261
+ if (ampm === 'AM' && hour === 12)
1262
+ hour = 0;
1263
+ }
1264
+ var minute = parseInt(this.container.find('.right .minuteselect').val(), 10);
1265
+ var second = this.timePickerSeconds ? parseInt(this.container.find('.right .secondselect').val(), 10) : 0;
1266
+ date = date.clone().hour(hour).minute(minute).second(second);
1267
+ }
1268
+ this.setEndDate(date.clone());
1269
+ if (this.autoApply)
1270
+ this.clickApply();
1271
+ }
1272
+
1273
+ if (this.singleDatePicker) {
1274
+ this.setEndDate(this.startDate);
1275
+ if (!this.timePicker)
1276
+ this.clickApply();
1277
+ }
1278
+
1279
+ this.updateView();
1280
+
1281
+ },
1282
+
1283
+ clickApply: function(e) {
1284
+ this.hide();
1285
+ this.element.trigger('apply.daterangepicker', this);
1286
+ },
1287
+
1288
+ clickCancel: function(e) {
1289
+ this.startDate = this.oldStartDate;
1290
+ this.endDate = this.oldEndDate;
1291
+ this.hide();
1292
+ this.element.trigger('cancel.daterangepicker', this);
1293
+ },
1294
+
1295
+ monthOrYearChanged: function(e) {
1296
+ var isLeft = $(e.target).closest('.calendar').hasClass('left'),
1297
+ leftOrRight = isLeft ? 'left' : 'right',
1298
+ cal = this.container.find('.calendar.'+leftOrRight);
1299
+
1300
+ // Month must be Number for new moment versions
1301
+ var month = parseInt(cal.find('.monthselect').val(), 10);
1302
+ var year = cal.find('.yearselect').val();
1303
+
1304
+ if (!isLeft) {
1305
+ if (year < this.startDate.year() || (year == this.startDate.year() && month < this.startDate.month())) {
1306
+ month = this.startDate.month();
1307
+ year = this.startDate.year();
1308
+ }
1309
+ }
1310
+
1311
+ if (this.minDate) {
1312
+ if (year < this.minDate.year() || (year == this.minDate.year() && month < this.minDate.month())) {
1313
+ month = this.minDate.month();
1314
+ year = this.minDate.year();
1315
+ }
1316
+ }
1317
+
1318
+ if (this.maxDate) {
1319
+ if (year > this.maxDate.year() || (year == this.maxDate.year() && month > this.maxDate.month())) {
1320
+ month = this.maxDate.month();
1321
+ year = this.maxDate.year();
1322
+ }
1323
+ }
1324
+
1325
+ if (isLeft) {
1326
+ this.leftCalendar.month.month(month).year(year);
1327
+ if (this.linkedCalendars)
1328
+ this.rightCalendar.month = this.leftCalendar.month.clone().add(1, 'month');
1329
+ } else {
1330
+ this.rightCalendar.month.month(month).year(year);
1331
+ if (this.linkedCalendars)
1332
+ this.leftCalendar.month = this.rightCalendar.month.clone().subtract(1, 'month');
1333
+ }
1334
+ this.updateCalendars();
1335
+ },
1336
+
1337
+ timeChanged: function(e) {
1338
+
1339
+ var cal = $(e.target).closest('.calendar'),
1340
+ isLeft = cal.hasClass('left');
1341
+
1342
+ var hour = parseInt(cal.find('.hourselect').val(), 10);
1343
+ var minute = parseInt(cal.find('.minuteselect').val(), 10);
1344
+ var second = this.timePickerSeconds ? parseInt(cal.find('.secondselect').val(), 10) : 0;
1345
+
1346
+ if (!this.timePicker24Hour) {
1347
+ var ampm = cal.find('.ampmselect').val();
1348
+ if (ampm === 'PM' && hour < 12)
1349
+ hour += 12;
1350
+ if (ampm === 'AM' && hour === 12)
1351
+ hour = 0;
1352
+ }
1353
+
1354
+ if (isLeft) {
1355
+ var start = this.startDate.clone();
1356
+ start.hour(hour);
1357
+ start.minute(minute);
1358
+ start.second(second);
1359
+ this.setStartDate(start);
1360
+ if (this.singleDatePicker)
1361
+ this.endDate = this.startDate.clone();
1362
+ } else if (this.endDate) {
1363
+ var end = this.endDate.clone();
1364
+ end.hour(hour);
1365
+ end.minute(minute);
1366
+ end.second(second);
1367
+ this.setEndDate(end);
1368
+ }
1369
+
1370
+ //update the calendars so all clickable dates reflect the new time component
1371
+ this.updateCalendars();
1372
+
1373
+ //update the form inputs above the calendars with the new time
1374
+ this.updateFormInputs();
1375
+
1376
+ //re-render the time pickers because changing one selection can affect what's enabled in another
1377
+ this.renderTimePicker('left');
1378
+ this.renderTimePicker('right');
1379
+
1380
+ },
1381
+
1382
+ formInputsChanged: function(e) {
1383
+ var isRight = $(e.target).closest('.calendar').hasClass('right');
1384
+ var start = moment(this.container.find('input[name="daterangepicker_start"]').val(), this.locale.format).utcOffset(this.timeZone);
1385
+ var end = moment(this.container.find('input[name="daterangepicker_end"]').val(), this.locale.format).utcOffset(this.timeZone);
1386
+
1387
+ if (start.isValid() && end.isValid()) {
1388
+
1389
+ if (isRight && end.isBefore(start))
1390
+ start = end.clone();
1391
+
1392
+ this.setStartDate(start);
1393
+ this.setEndDate(end);
1394
+
1395
+ if (isRight) {
1396
+ this.container.find('input[name="daterangepicker_start"]').val(this.startDate.format(this.locale.format));
1397
+ } else {
1398
+ this.container.find('input[name="daterangepicker_end"]').val(this.endDate.format(this.locale.format));
1399
+ }
1400
+
1401
+ }
1402
+
1403
+ this.updateCalendars();
1404
+ if (this.timePicker) {
1405
+ this.renderTimePicker('left');
1406
+ this.renderTimePicker('right');
1407
+ }
1408
+ },
1409
+
1410
+ controlChanged: function() {
1411
+ if (!this.element.is('input')) return;
1412
+ if (!this.element.val().length) return;
1413
+
1414
+ var dateString = this.element.val().split(this.locale.separator),
1415
+ start = null,
1416
+ end = null;
1417
+
1418
+ if (dateString.length === 2) {
1419
+ start = moment(dateString[0], this.locale.format).utcOffset(this.timeZone);
1420
+ end = moment(dateString[1], this.locale.format).utcOffset(this.timeZone);
1421
+ }
1422
+
1423
+ if (this.singleDatePicker || start === null || end === null) {
1424
+ start = moment(this.element.val(), this.locale.format).utcOffset(this.timeZone);
1425
+ end = start;
1426
+ }
1427
+
1428
+ this.setStartDate(start);
1429
+ this.setEndDate(end);
1430
+ this.updateView();
1431
+ },
1432
+
1433
+ keydown: function(e) {
1434
+ //hide on tab or enter
1435
+ if ((e.keyCode === 9) || (e.keyCode === 13)) {
1436
+ this.hide();
1437
+ }
1438
+ },
1439
+
1440
+ remove: function() {
1441
+ this.container.remove();
1442
+ this.element.off('.daterangepicker');
1443
+ this.element.removeData();
1444
+ }
1445
+
1446
+ };
1447
+
1448
+ $.fn.daterangepicker = function(options, callback) {
1449
+ this.each(function() {
1450
+ var el = $(this);
1451
+ if (el.data('daterangepicker'))
1452
+ el.data('daterangepicker').remove();
1453
+ el.data('daterangepicker', new DateRangePicker(el, options, callback));
1454
+ });
1455
+ return this;
1456
+ };
1457
+
1458
+ }));