Subversion-Projekte lars-tiefland.ci

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
776 lars 1
/**
2
* @version: 2.1.13
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 = (typeof window != 'undefined') ? window.jQuery : undefined;  //isomorphic issue
19
      if (!jQuery) {
20
          try {
21
              jQuery = require('jquery');
22
              if (!jQuery.fn) jQuery.fn = {}; //isomorphic issue
23
          } catch (err) {
24
              if (!jQuery) throw new Error('jQuery dependency not found');
25
          }
26
      }
27
 
28
    factory(root, exports, momentjs, jQuery);
29
 
30
  // Finally, as a browser global.
31
  } else {
32
    root.daterangepicker = factory(root, {}, root.moment || moment, (root.jQuery || root.Zepto || root.ender || root.$));
33
  }
34
 
35
}(this || {}, function(root, daterangepicker, moment, $) { // 'this' doesn't exist on a server
36
 
37
    var DateRangePicker = function(element, options, cb) {
38
 
39
        //default settings for options
40
        this.parentEl = 'body';
41
        this.element = $(element);
42
        this.startDate = moment().startOf('day');
43
        this.endDate = moment().endOf('day');
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"></i>' +
105
                      '<div class="calendar-time">' +
106
                        '<div></div>' +
107
                        '<i class="fa fa-clock-o"></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"></i>' +
116
                      '<div class="calendar-time">' +
117
                        '<div></div>' +
118
                        '<i class="fa fa-clock-o"></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
        if (typeof options.ranges === 'object') {
292
            for (range in options.ranges) {
293
 
294
                if (typeof options.ranges[range][0] === 'string')
295
                    start = moment(options.ranges[range][0], this.locale.format);
296
                else
297
                    start = moment(options.ranges[range][0]);
298
 
299
                if (typeof options.ranges[range][1] === 'string')
300
                    end = moment(options.ranges[range][1], this.locale.format);
301
                else
302
                    end = moment(options.ranges[range][1]);
303
 
304
                // If the start or end date exceed those allowed by the minDate or dateLimit
305
                // options, shorten the range to the allowable period.
306
                if (this.minDate && start.isBefore(this.minDate))
307
                    start = this.minDate.clone();
308
 
309
                var maxDate = this.maxDate;
310
                if (this.dateLimit && start.clone().add(this.dateLimit).isAfter(maxDate))
311
                    maxDate = start.clone().add(this.dateLimit);
312
                if (maxDate && end.isAfter(maxDate))
313
                    end = maxDate.clone();
314
 
315
                // If the end of the range is before the minimum or the start of the range is
316
                // after the maximum, don't display this range option at all.
317
                if ((this.minDate && end.isBefore(this.minDate)) || (maxDate && start.isAfter(maxDate)))
318
                    continue;
319
 
320
                //Support unicode chars in the range names.
321
                var elem = document.createElement('textarea');
322
                elem.innerHTML = range;
323
                rangeHtml = elem.value;
324
 
325
                this.ranges[rangeHtml] = [start, end];
326
            }
327
 
328
            var list = '<ul>';
329
            for (range in this.ranges) {
330
                list += '<li>' + range + '</li>';
331
            }
332
            list += '<li>' + this.locale.customRangeLabel + '</li>';
333
            list += '</ul>';
334
            this.container.find('.ranges').prepend(list);
335
        }
336
 
337
        if (typeof cb === 'function') {
338
            this.callback = cb;
339
        }
340
 
341
        if (!this.timePicker) {
342
            this.startDate = this.startDate.startOf('day');
343
            this.endDate = this.endDate.endOf('day');
344
            this.container.find('.calendar-time').hide();
345
        }
346
 
347
        //can't be used together for now
348
        if (this.timePicker && this.autoApply)
349
            this.autoApply = false;
350
 
351
        if (this.autoApply && typeof options.ranges !== 'object') {
352
            this.container.find('.ranges').hide();
353
        } else if (this.autoApply) {
354
            this.container.find('.applyBtn, .cancelBtn').addClass('hide');
355
        }
356
 
357
        if (this.singleDatePicker) {
358
            this.container.addClass('single');
359
            this.container.find('.calendar.left').addClass('single');
360
            this.container.find('.calendar.left').show();
361
            this.container.find('.calendar.right').hide();
362
            this.container.find('.daterangepicker_input input, .daterangepicker_input i').hide();
363
            if (!this.timePicker) {
364
                this.container.find('.ranges').hide();
365
            }
366
        }
367
 
368
        if (typeof options.ranges === 'undefined' && !this.singleDatePicker) {
369
            this.container.addClass('show-calendar');
370
        }
371
 
372
        this.container.addClass('opens' + this.opens);
373
 
374
        //swap the position of the predefined ranges if opens right
375
        if (typeof options.ranges !== 'undefined' && this.opens == 'right') {
376
            var ranges = this.container.find('.ranges');
377
            var html = ranges.clone();
378
            ranges.remove();
379
            this.container.find('.calendar.left').parent().prepend(html);
380
        }
381
 
382
        //apply CSS classes and labels to buttons
383
        this.container.find('.applyBtn, .cancelBtn').addClass(this.buttonClasses);
384
        if (this.applyClass.length)
385
            this.container.find('.applyBtn').addClass(this.applyClass);
386
        if (this.cancelClass.length)
387
            this.container.find('.cancelBtn').addClass(this.cancelClass);
388
        this.container.find('.applyBtn').html(this.locale.applyLabel);
389
        this.container.find('.cancelBtn').html(this.locale.cancelLabel);
390
 
391
        //
392
        // event listeners
393
        //
394
 
395
        this.container.find('.calendar')
396
            .on('click.daterangepicker', '.prev', $.proxy(this.clickPrev, this))
397
            .on('click.daterangepicker', '.next', $.proxy(this.clickNext, this))
398
            .on('click.daterangepicker', 'td.available', $.proxy(this.clickDate, this))
399
            .on('mouseenter.daterangepicker', 'td.available', $.proxy(this.hoverDate, this))
400
            .on('mouseleave.daterangepicker', 'td.available', $.proxy(this.updateFormInputs, this))
401
            .on('change.daterangepicker', 'select.yearselect', $.proxy(this.monthOrYearChanged, this))
402
            .on('change.daterangepicker', 'select.monthselect', $.proxy(this.monthOrYearChanged, this))
403
            .on('change.daterangepicker', 'select.hourselect,select.minuteselect,select.secondselect,select.ampmselect', $.proxy(this.timeChanged, this))
404
            .on('click.daterangepicker', '.daterangepicker_input input', $.proxy(this.showCalendars, this))
405
            //.on('keyup.daterangepicker', '.daterangepicker_input input', $.proxy(this.formInputsChanged, this))
406
            .on('change.daterangepicker', '.daterangepicker_input input', $.proxy(this.formInputsChanged, this));
407
 
408
        this.container.find('.ranges')
409
            .on('click.daterangepicker', 'button.applyBtn', $.proxy(this.clickApply, this))
410
            .on('click.daterangepicker', 'button.cancelBtn', $.proxy(this.clickCancel, this))
411
            .on('click.daterangepicker', 'li', $.proxy(this.clickRange, this))
412
            .on('mouseenter.daterangepicker', 'li', $.proxy(this.hoverRange, this))
413
            .on('mouseleave.daterangepicker', 'li', $.proxy(this.updateFormInputs, this));
414
 
415
        if (this.element.is('input')) {
416
            this.element.on({
417
                'click.daterangepicker': $.proxy(this.show, this),
418
                'focus.daterangepicker': $.proxy(this.show, this),
419
                'keyup.daterangepicker': $.proxy(this.elementChanged, this),
420
                'keydown.daterangepicker': $.proxy(this.keydown, this)
421
            });
422
        } else {
423
            this.element.on('click.daterangepicker', $.proxy(this.toggle, this));
424
        }
425
 
426
        //
427
        // if attached to a text input, set the initial value
428
        //
429
 
430
        if (this.element.is('input') && !this.singleDatePicker && this.autoUpdateInput) {
431
            this.element.val(this.startDate.format(this.locale.format) + this.locale.separator + this.endDate.format(this.locale.format));
432
            this.element.trigger('change');
433
        } else if (this.element.is('input') && this.autoUpdateInput) {
434
            this.element.val(this.startDate.format(this.locale.format));
435
            this.element.trigger('change');
436
        }
437
 
438
    };
439
 
440
    DateRangePicker.prototype = {
441
 
442
        constructor: DateRangePicker,
443
 
444
        setStartDate: function(startDate) {
445
            if (typeof startDate === 'string')
446
                this.startDate = moment(startDate, this.locale.format);
447
 
448
            if (typeof startDate === 'object')
449
                this.startDate = moment(startDate);
450
 
451
            if (!this.timePicker)
452
                this.startDate = this.startDate.startOf('day');
453
 
454
            if (this.timePicker && this.timePickerIncrement)
455
                this.startDate.minute(Math.round(this.startDate.minute() / this.timePickerIncrement) * this.timePickerIncrement);
456
 
457
            if (this.minDate && this.startDate.isBefore(this.minDate))
458
                this.startDate = this.minDate;
459
 
460
            if (this.maxDate && this.startDate.isAfter(this.maxDate))
461
                this.startDate = this.maxDate;
462
 
463
            if (!this.isShowing)
464
                this.updateElement();
465
 
466
            this.updateMonthsInView();
467
        },
468
 
469
        setEndDate: function(endDate) {
470
            if (typeof endDate === 'string')
471
                this.endDate = moment(endDate, this.locale.format);
472
 
473
            if (typeof endDate === 'object')
474
                this.endDate = moment(endDate);
475
 
476
            if (!this.timePicker)
477
                this.endDate = this.endDate.endOf('day');
478
 
479
            if (this.timePicker && this.timePickerIncrement)
480
                this.endDate.minute(Math.round(this.endDate.minute() / this.timePickerIncrement) * this.timePickerIncrement);
481
 
482
            if (this.endDate.isBefore(this.startDate))
483
                this.endDate = this.startDate.clone();
484
 
485
            if (this.maxDate && this.endDate.isAfter(this.maxDate))
486
                this.endDate = this.maxDate;
487
 
488
            if (this.dateLimit && this.startDate.clone().add(this.dateLimit).isBefore(this.endDate))
489
                this.endDate = this.startDate.clone().add(this.dateLimit);
490
 
491
            if (!this.isShowing)
492
                this.updateElement();
493
 
494
            this.updateMonthsInView();
495
        },
496
 
497
        isInvalidDate: function() {
498
            return false;
499
        },
500
 
501
        updateView: function() {
502
            if (this.timePicker) {
503
                this.renderTimePicker('left');
504
                this.renderTimePicker('right');
505
                if (!this.endDate) {
506
                    this.container.find('.right .calendar-time select').attr('disabled', 'disabled').addClass('disabled');
507
                } else {
508
                    this.container.find('.right .calendar-time select').removeAttr('disabled').removeClass('disabled');
509
                }
510
            }
511
            if (this.endDate) {
512
                this.container.find('input[name="daterangepicker_end"]').removeClass('active');
513
                this.container.find('input[name="daterangepicker_start"]').addClass('active');
514
            } else {
515
                this.container.find('input[name="daterangepicker_end"]').addClass('active');
516
                this.container.find('input[name="daterangepicker_start"]').removeClass('active');
517
            }
518
            this.updateMonthsInView();
519
            this.updateCalendars();
520
            this.updateFormInputs();
521
        },
522
 
523
        updateMonthsInView: function() {
524
            if (this.endDate) {
525
 
526
                //if both dates are visible already, do nothing
527
                if (!this.singleDatePicker && this.leftCalendar.month && this.rightCalendar.month &&
528
                    (this.startDate.format('YYYY-MM') == this.leftCalendar.month.format('YYYY-MM') || this.startDate.format('YYYY-MM') == this.rightCalendar.month.format('YYYY-MM'))
529
                    &&
530
                    (this.endDate.format('YYYY-MM') == this.leftCalendar.month.format('YYYY-MM') || this.endDate.format('YYYY-MM') == this.rightCalendar.month.format('YYYY-MM'))
531
                    ) {
532
                    return;
533
                }
534
 
535
                this.leftCalendar.month = this.startDate.clone().date(2);
536
                if (!this.linkedCalendars && (this.endDate.month() != this.startDate.month() || this.endDate.year() != this.startDate.year())) {
537
                    this.rightCalendar.month = this.endDate.clone().date(2);
538
                } else {
539
                    this.rightCalendar.month = this.startDate.clone().date(2).add(1, 'month');
540
                }
541
 
542
            } else {
543
                if (this.leftCalendar.month.format('YYYY-MM') != this.startDate.format('YYYY-MM') && this.rightCalendar.month.format('YYYY-MM') != this.startDate.format('YYYY-MM')) {
544
                    this.leftCalendar.month = this.startDate.clone().date(2);
545
                    this.rightCalendar.month = this.startDate.clone().date(2).add(1, 'month');
546
                }
547
            }
548
        },
549
 
550
        updateCalendars: function() {
551
 
552
            if (this.timePicker) {
553
                var hour, minute, second;
554
                if (this.endDate) {
555
                    hour = parseInt(this.container.find('.left .hourselect').val(), 10);
556
                    minute = parseInt(this.container.find('.left .minuteselect').val(), 10);
557
                    second = this.timePickerSeconds ? parseInt(this.container.find('.left .secondselect').val(), 10) : 0;
558
                    if (!this.timePicker24Hour) {
559
                        var ampm = this.container.find('.left .ampmselect').val();
560
                        if (ampm === 'PM' && hour < 12)
561
                            hour += 12;
562
                        if (ampm === 'AM' && hour === 12)
563
                            hour = 0;
564
                    }
565
                } else {
566
                    hour = parseInt(this.container.find('.right .hourselect').val(), 10);
567
                    minute = parseInt(this.container.find('.right .minuteselect').val(), 10);
568
                    second = this.timePickerSeconds ? parseInt(this.container.find('.right .secondselect').val(), 10) : 0;
569
                    if (!this.timePicker24Hour) {
570
                        var ampm = this.container.find('.right .ampmselect').val();
571
                        if (ampm === 'PM' && hour < 12)
572
                            hour += 12;
573
                        if (ampm === 'AM' && hour === 12)
574
                            hour = 0;
575
                    }
576
                }
577
                this.leftCalendar.month.hour(hour).minute(minute).second(second);
578
                this.rightCalendar.month.hour(hour).minute(minute).second(second);
579
            }
580
 
581
            this.renderCalendar('left');
582
            this.renderCalendar('right');
583
 
584
            //highlight any predefined range matching the current start and end dates
585
            this.container.find('.ranges li').removeClass('active');
586
            if (this.endDate == null) return;
587
 
588
            var customRange = true;
589
            var i = 0;
590
            for (var range in this.ranges) {
591
                if (this.timePicker) {
592
                    if (this.startDate.isSame(this.ranges[range][0]) && this.endDate.isSame(this.ranges[range][1])) {
593
                        customRange = false;
594
                        this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').html();
595
                        break;
596
                    }
597
                } else {
598
                    //ignore times when comparing dates if time picker is not enabled
599
                    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')) {
600
                        customRange = false;
601
                        this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').html();
602
                        break;
603
                    }
604
                }
605
                i++;
606
            }
607
            if (customRange) {
608
                this.chosenLabel = this.container.find('.ranges li:last').addClass('active').html();
609
                this.showCalendars();
610
            }
611
 
612
        },
613
 
614
        renderCalendar: function(side) {
615
 
616
            //
617
            // Build the matrix of dates that will populate the calendar
618
            //
619
 
620
            var calendar = side == 'left' ? this.leftCalendar : this.rightCalendar;
621
            var month = calendar.month.month();
622
            var year = calendar.month.year();
623
            var hour = calendar.month.hour();
624
            var minute = calendar.month.minute();
625
            var second = calendar.month.second();
626
            var daysInMonth = moment([year, month]).daysInMonth();
627
            var firstDay = moment([year, month, 1]);
628
            var lastDay = moment([year, month, daysInMonth]);
629
            var lastMonth = moment(firstDay).subtract(1, 'month').month();
630
            var lastYear = moment(firstDay).subtract(1, 'month').year();
631
            var daysInLastMonth = moment([lastYear, lastMonth]).daysInMonth();
632
            var dayOfWeek = firstDay.day();
633
 
634
            //initialize a 6 rows x 7 columns array for the calendar
635
            var calendar = [];
636
            calendar.firstDay = firstDay;
637
            calendar.lastDay = lastDay;
638
 
639
            for (var i = 0; i < 6; i++) {
640
                calendar[i] = [];
641
            }
642
 
643
            //populate the calendar with date objects
644
            var startDay = daysInLastMonth - dayOfWeek + this.locale.firstDay + 1;
645
            if (startDay > daysInLastMonth)
646
                startDay -= 7;
647
 
648
            if (dayOfWeek == this.locale.firstDay)
649
                startDay = daysInLastMonth - 6;
650
 
651
            var curDate = moment([lastYear, lastMonth, startDay, 12, minute, second]);
652
 
653
            var col, row;
654
            for (var i = 0, col = 0, row = 0; i < 42; i++, col++, curDate = moment(curDate).add(24, 'hour')) {
655
                if (i > 0 && col % 7 === 0) {
656
                    col = 0;
657
                    row++;
658
                }
659
                calendar[row][col] = curDate.clone().hour(hour).minute(minute).second(second);
660
                curDate.hour(12);
661
 
662
                if (this.minDate && calendar[row][col].format('YYYY-MM-DD') == this.minDate.format('YYYY-MM-DD') && calendar[row][col].isBefore(this.minDate) && side == 'left') {
663
                    calendar[row][col] = this.minDate.clone();
664
                }
665
 
666
                if (this.maxDate && calendar[row][col].format('YYYY-MM-DD') == this.maxDate.format('YYYY-MM-DD') && calendar[row][col].isAfter(this.maxDate) && side == 'right') {
667
                    calendar[row][col] = this.maxDate.clone();
668
                }
669
 
670
            }
671
 
672
            //make the calendar object available to hoverDate/clickDate
673
            if (side == 'left') {
674
                this.leftCalendar.calendar = calendar;
675
            } else {
676
                this.rightCalendar.calendar = calendar;
677
            }
678
 
679
            //
680
            // Display the calendar
681
            //
682
 
683
            var minDate = side == 'left' ? this.minDate : this.startDate;
684
            var maxDate = this.maxDate;
685
            var selected = side == 'left' ? this.startDate : this.endDate;
686
 
687
            var html = '<table class="table-condensed">';
688
            html += '<thead>';
689
            html += '<tr>';
690
 
691
            // add empty cell for week number
692
            if (this.showWeekNumbers)
693
                html += '<th></th>';
694
 
695
            if ((!minDate || minDate.isBefore(calendar.firstDay)) && (!this.linkedCalendars || side == 'left')) {
696
                html += '<th class="prev available"><i class="fa fa-angle-left"></i></th>';
697
            } else {
698
                html += '<th></th>';
699
            }
700
 
701
            var dateHtml = this.locale.monthNames[calendar[1][1].month()] + calendar[1][1].format(" YYYY");
702
 
703
            if (this.showDropdowns) {
704
                var currentMonth = calendar[1][1].month();
705
                var currentYear = calendar[1][1].year();
706
                var maxYear = (maxDate && maxDate.year()) || (currentYear + 5);
707
                var minYear = (minDate && minDate.year()) || (currentYear - 50);
708
                var inMinYear = currentYear == minYear;
709
                var inMaxYear = currentYear == maxYear;
710
 
711
                var monthHtml = '<select class="monthselect">';
712
                for (var m = 0; m < 12; m++) {
713
                    if ((!inMinYear || m >= minDate.month()) && (!inMaxYear || m <= maxDate.month())) {
714
                        monthHtml += "<option value='" + m + "'" +
715
                            (m === currentMonth ? " selected='selected'" : "") +
716
                            ">" + this.locale.monthNames[m] + "</option>";
717
                    } else {
718
                        monthHtml += "<option value='" + m + "'" +
719
                            (m === currentMonth ? " selected='selected'" : "") +
720
                            " disabled='disabled'>" + this.locale.monthNames[m] + "</option>";
721
                    }
722
                }
723
                monthHtml += "</select>";
724
 
725
                var yearHtml = '<select class="yearselect">';
726
                for (var y = minYear; y <= maxYear; y++) {
727
                    yearHtml += '<option value="' + y + '"' +
728
                        (y === currentYear ? ' selected="selected"' : '') +
729
                        '>' + y + '</option>';
730
                }
731
                yearHtml += '</select>';
732
 
733
                dateHtml = monthHtml + yearHtml;
734
            }
735
 
736
            html += '<th colspan="5" class="month">' + dateHtml + '</th>';
737
            if ((!maxDate || maxDate.isAfter(calendar.lastDay)) && (!this.linkedCalendars || side == 'right' || this.singleDatePicker)) {
738
                html += '<th class="next available"><i class="fa fa-angle-right"></i></th>';
739
            } else {
740
                html += '<th></th>';
741
            }
742
 
743
            html += '</tr>';
744
            html += '<tr>';
745
 
746
            // add week number label
747
            if (this.showWeekNumbers)
748
                html += '<th class="week">' + this.locale.weekLabel + '</th>';
749
 
750
            $.each(this.locale.daysOfWeek, function(index, dayOfWeek) {
751
                html += '<th>' + dayOfWeek + '</th>';
752
            });
753
 
754
            html += '</tr>';
755
            html += '</thead>';
756
            html += '<tbody>';
757
 
758
            //adjust maxDate to reflect the dateLimit setting in order to
759
            //grey out end dates beyond the dateLimit
760
            if (this.endDate == null && this.dateLimit) {
761
                var maxLimit = this.startDate.clone().add(this.dateLimit).endOf('day');
762
                if (!maxDate || maxLimit.isBefore(maxDate)) {
763
                    maxDate = maxLimit;
764
                }
765
            }
766
 
767
            for (var row = 0; row < 6; row++) {
768
                html += '<tr>';
769
 
770
                // add week number
771
                if (this.showWeekNumbers)
772
                    html += '<td class="week">' + calendar[row][0].week() + '</td>';
773
 
774
                for (var col = 0; col < 7; col++) {
775
 
776
                    var classes = [];
777
 
778
                    //highlight today's date
779
                    if (calendar[row][col].isSame(new Date(), "day"))
780
                        classes.push('today');
781
 
782
                    //highlight weekends
783
                    if (calendar[row][col].isoWeekday() > 5)
784
                        classes.push('weekend');
785
 
786
                    //grey out the dates in other months displayed at beginning and end of this calendar
787
                    if (calendar[row][col].month() != calendar[1][1].month())
788
                        classes.push('off');
789
 
790
                    //don't allow selection of dates before the minimum date
791
                    if (this.minDate && calendar[row][col].isBefore(this.minDate, 'day'))
792
                        classes.push('off', 'disabled');
793
 
794
                    //don't allow selection of dates after the maximum date
795
                    if (maxDate && calendar[row][col].isAfter(maxDate, 'day'))
796
                        classes.push('off', 'disabled');
797
 
798
                    //don't allow selection of date if a custom function decides it's invalid
799
                    if (this.isInvalidDate(calendar[row][col]))
800
                        classes.push('off', 'disabled');
801
 
802
                    //highlight the currently selected start date
803
                    if (calendar[row][col].format('YYYY-MM-DD') == this.startDate.format('YYYY-MM-DD'))
804
                        classes.push('active', 'start-date');
805
 
806
                    //highlight the currently selected end date
807
                    if (this.endDate != null && calendar[row][col].format('YYYY-MM-DD') == this.endDate.format('YYYY-MM-DD'))
808
                        classes.push('active', 'end-date');
809
 
810
                    //highlight dates in-between the selected dates
811
                    if (this.endDate != null && calendar[row][col] > this.startDate && calendar[row][col] < this.endDate)
812
                        classes.push('in-range');
813
 
814
                    var cname = '', disabled = false;
815
                    for (var i = 0; i < classes.length; i++) {
816
                        cname += classes[i] + ' ';
817
                        if (classes[i] == 'disabled')
818
                            disabled = true;
819
                    }
820
                    if (!disabled)
821
                        cname += 'available';
822
 
823
                    html += '<td class="' + cname.replace(/^\s+|\s+$/g, '') + '" data-title="' + 'r' + row + 'c' + col + '">' + calendar[row][col].date() + '</td>';
824
 
825
                }
826
                html += '</tr>';
827
            }
828
 
829
            html += '</tbody>';
830
            html += '</table>';
831
 
832
            this.container.find('.calendar.' + side + ' .calendar-table').html(html);
833
 
834
        },
835
 
836
        renderTimePicker: function(side) {
837
 
838
            var html, selected, minDate, maxDate = this.maxDate;
839
 
840
            if (this.dateLimit && (!this.maxDate || this.startDate.clone().add(this.dateLimit).isAfter(this.maxDate)))
841
                maxDate = this.startDate.clone().add(this.dateLimit);
842
 
843
            if (side == 'left') {
844
                selected = this.startDate.clone();
845
                minDate = this.minDate;
846
            } else if (side == 'right') {
847
                selected = this.endDate ? this.endDate.clone() : this.startDate.clone();
848
                minDate = this.startDate;
849
            }
850
 
851
            //
852
            // hours
853
            //
854
 
855
            html = '<select class="hourselect">';
856
 
857
            var start = this.timePicker24Hour ? 0 : 1;
858
            var end = this.timePicker24Hour ? 23 : 12;
859
 
860
            for (var i = start; i <= end; i++) {
861
                var i_in_24 = i;
862
                if (!this.timePicker24Hour)
863
                    i_in_24 = selected.hour() >= 12 ? (i == 12 ? 12 : i + 12) : (i == 12 ? 0 : i);
864
 
865
                var time = selected.clone().hour(i_in_24);
866
                var disabled = false;
867
                if (minDate && time.minute(59).isBefore(minDate))
868
                    disabled = true;
869
                if (maxDate && time.minute(0).isAfter(maxDate))
870
                    disabled = true;
871
 
872
                if (i_in_24 == selected.hour() && !disabled) {
873
                    html += '<option value="' + i + '" selected="selected">' + i + '</option>';
874
                } else if (disabled) {
875
                    html += '<option value="' + i + '" disabled="disabled" class="disabled">' + i + '</option>';
876
                } else {
877
                    html += '<option value="' + i + '">' + i + '</option>';
878
                }
879
            }
880
 
881
            html += '</select> ';
882
 
883
            //
884
            // minutes
885
            //
886
 
887
            html += ': <select class="minuteselect">';
888
 
889
            for (var i = 0; i < 60; i += this.timePickerIncrement) {
890
                var padded = i < 10 ? '0' + i : i;
891
                var time = selected.clone().minute(i);
892
 
893
                var disabled = false;
894
                if (minDate && time.second(59).isBefore(minDate))
895
                    disabled = true;
896
                if (maxDate && time.second(0).isAfter(maxDate))
897
                    disabled = true;
898
 
899
                if (selected.minute() == i && !disabled) {
900
                    html += '<option value="' + i + '" selected="selected">' + padded + '</option>';
901
                } else if (disabled) {
902
                    html += '<option value="' + i + '" disabled="disabled" class="disabled">' + padded + '</option>';
903
                } else {
904
                    html += '<option value="' + i + '">' + padded + '</option>';
905
                }
906
            }
907
 
908
            html += '</select> ';
909
 
910
            //
911
            // seconds
912
            //
913
 
914
            if (this.timePickerSeconds) {
915
                html += ': <select class="secondselect">';
916
 
917
                for (var i = 0; i < 60; i++) {
918
                    var padded = i < 10 ? '0' + i : i;
919
                    var time = selected.clone().second(i);
920
 
921
                    var disabled = false;
922
                    if (minDate && time.isBefore(minDate))
923
                        disabled = true;
924
                    if (maxDate && time.isAfter(maxDate))
925
                        disabled = true;
926
 
927
                    if (selected.second() == i && !disabled) {
928
                        html += '<option value="' + i + '" selected="selected">' + padded + '</option>';
929
                    } else if (disabled) {
930
                        html += '<option value="' + i + '" disabled="disabled" class="disabled">' + padded + '</option>';
931
                    } else {
932
                        html += '<option value="' + i + '">' + padded + '</option>';
933
                    }
934
                }
935
 
936
                html += '</select> ';
937
            }
938
 
939
            //
940
            // AM/PM
941
            //
942
 
943
            if (!this.timePicker24Hour) {
944
                html += '<select class="ampmselect">';
945
 
946
                var am_html = '';
947
                var pm_html = '';
948
 
949
                if (minDate && selected.clone().hour(12).minute(0).second(0).isBefore(minDate))
950
                    am_html = ' disabled="disabled" class="disabled"';
951
 
952
                if (maxDate && selected.clone().hour(0).minute(0).second(0).isAfter(maxDate))
953
                    pm_html = ' disabled="disabled" class="disabled"';
954
 
955
                if (selected.hour() >= 12) {
956
                    html += '<option value="AM"' + am_html + '>AM</option><option value="PM" selected="selected"' + pm_html + '>PM</option>';
957
                } else {
958
                    html += '<option value="AM" selected="selected"' + am_html + '>AM</option><option value="PM"' + pm_html + '>PM</option>';
959
                }
960
 
961
                html += '</select>';
962
            }
963
 
964
            this.container.find('.calendar.' + side + ' .calendar-time div').html(html);
965
 
966
        },
967
 
968
        updateFormInputs: function() {
969
 
970
            //ignore mouse movements while an above-calendar text input has focus
971
            if (this.container.find('input[name=daterangepicker_start]').is(":focus") || this.container.find('input[name=daterangepicker_end]').is(":focus"))
972
                return;
973
 
974
            this.container.find('input[name=daterangepicker_start]').val(this.startDate.format(this.locale.format));
975
            if (this.endDate)
976
                this.container.find('input[name=daterangepicker_end]').val(this.endDate.format(this.locale.format));
977
 
978
            if (this.singleDatePicker || (this.endDate && (this.startDate.isBefore(this.endDate) || this.startDate.isSame(this.endDate)))) {
979
                this.container.find('button.applyBtn').removeAttr('disabled');
980
            } else {
981
                this.container.find('button.applyBtn').attr('disabled', 'disabled');
982
            }
983
 
984
        },
985
 
986
        move: function() {
987
            var parentOffset = { top: 0, left: 0 },
988
                containerTop;
989
            var parentRightEdge = $(window).width();
990
            if (!this.parentEl.is('body')) {
991
                parentOffset = {
992
                    top: this.parentEl.offset().top - this.parentEl.scrollTop(),
993
                    left: this.parentEl.offset().left - this.parentEl.scrollLeft()
994
                };
995
                parentRightEdge = this.parentEl[0].clientWidth + this.parentEl.offset().left;
996
            }
997
 
998
            if (this.drops == 'up')
999
                containerTop = this.element.offset().top - this.container.outerHeight() - parentOffset.top;
1000
            else
1001
                containerTop = this.element.offset().top + this.element.outerHeight() - parentOffset.top;
1002
            this.container[this.drops == 'up' ? 'addClass' : 'removeClass']('dropup');
1003
 
1004
            if (this.opens == 'left') {
1005
                this.container.css({
1006
                    top: containerTop,
1007
                    right: parentRightEdge - this.element.offset().left - this.element.outerWidth(),
1008
                    left: 'auto'
1009
                });
1010
                if (this.container.offset().left < 0) {
1011
                    this.container.css({
1012
                        right: 'auto',
1013
                        left: 9
1014
                    });
1015
                }
1016
            } else if (this.opens == 'center') {
1017
                this.container.css({
1018
                    top: containerTop,
1019
                    left: this.element.offset().left - parentOffset.left + this.element.outerWidth() / 2
1020
                            - this.container.outerWidth() / 2,
1021
                    right: 'auto'
1022
                });
1023
                if (this.container.offset().left < 0) {
1024
                    this.container.css({
1025
                        right: 'auto',
1026
                        left: 9
1027
                    });
1028
                }
1029
            } else {
1030
                this.container.css({
1031
                    top: containerTop,
1032
                    left: this.element.offset().left - parentOffset.left,
1033
                    right: 'auto'
1034
                });
1035
                if (this.container.offset().left + this.container.outerWidth() > $(window).width()) {
1036
                    this.container.css({
1037
                        left: 'auto',
1038
                        right: 0
1039
                    });
1040
                }
1041
            }
1042
        },
1043
 
1044
        show: function(e) {
1045
            if (this.isShowing) return;
1046
 
1047
            // Create a click proxy that is private to this instance of datepicker, for unbinding
1048
            this._outsideClickProxy = $.proxy(function(e) { this.outsideClick(e); }, this);
1049
 
1050
            // Bind global datepicker mousedown for hiding and
1051
            $(document)
1052
              .on('mousedown.daterangepicker', this._outsideClickProxy)
1053
              // also support mobile devices
1054
              .on('touchend.daterangepicker', this._outsideClickProxy)
1055
              // also explicitly play nice with Bootstrap dropdowns, which stopPropagation when clicking them
1056
              .on('click.daterangepicker', '[data-toggle=dropdown]', this._outsideClickProxy)
1057
              // and also close when focus changes to outside the picker (eg. tabbing between controls)
1058
              .on('focusin.daterangepicker', this._outsideClickProxy);
1059
 
1060
            // Reposition the picker if the window is resized while it's open
1061
            $(window).on('resize.daterangepicker', $.proxy(function(e) { this.move(e); }, this));
1062
 
1063
            this.oldStartDate = this.startDate.clone();
1064
            this.oldEndDate = this.endDate.clone();
1065
 
1066
            this.updateView();
1067
            this.container.show();
1068
            this.move();
1069
            this.element.trigger('show.daterangepicker', this);
1070
            this.isShowing = true;
1071
        },
1072
 
1073
        hide: function(e) {
1074
            if (!this.isShowing) return;
1075
 
1076
            //incomplete date selection, revert to last values
1077
            if (!this.endDate) {
1078
                this.startDate = this.oldStartDate.clone();
1079
                this.endDate = this.oldEndDate.clone();
1080
            }
1081
 
1082
            //if a new date range was selected, invoke the user callback function
1083
            if (!this.startDate.isSame(this.oldStartDate) || !this.endDate.isSame(this.oldEndDate))
1084
                this.callback(this.startDate, this.endDate, this.chosenLabel);
1085
 
1086
            //if picker is attached to a text input, update it
1087
            this.updateElement();
1088
 
1089
            $(document).off('.daterangepicker');
1090
            $(window).off('.daterangepicker');
1091
            this.container.hide();
1092
            this.element.trigger('hide.daterangepicker', this);
1093
            this.isShowing = false;
1094
        },
1095
 
1096
        toggle: function(e) {
1097
            if (this.isShowing) {
1098
                this.hide();
1099
            } else {
1100
                this.show();
1101
            }
1102
        },
1103
 
1104
        outsideClick: function(e) {
1105
            var target = $(e.target);
1106
            // if the page is clicked anywhere except within the daterangerpicker/button
1107
            // itself then call this.hide()
1108
            if (
1109
                // ie modal dialog fix
1110
                e.type == "focusin" ||
1111
                target.closest(this.element).length ||
1112
                target.closest(this.container).length ||
1113
                target.closest('.calendar-table').length
1114
                ) return;
1115
            this.hide();
1116
        },
1117
 
1118
        showCalendars: function() {
1119
            this.container.addClass('show-calendar');
1120
            this.move();
1121
            this.element.trigger('showCalendar.daterangepicker', this);
1122
        },
1123
 
1124
        hideCalendars: function() {
1125
            this.container.removeClass('show-calendar');
1126
            this.element.trigger('hideCalendar.daterangepicker', this);
1127
        },
1128
 
1129
        hoverRange: function(e) {
1130
 
1131
            //ignore mouse movements while an above-calendar text input has focus
1132
            if (this.container.find('input[name=daterangepicker_start]').is(":focus") || this.container.find('input[name=daterangepicker_end]').is(":focus"))
1133
                return;
1134
 
1135
            var label = e.target.innerHTML;
1136
            if (label == this.locale.customRangeLabel) {
1137
                this.updateView();
1138
            } else {
1139
                var dates = this.ranges[label];
1140
                this.container.find('input[name=daterangepicker_start]').val(dates[0].format(this.locale.format));
1141
                this.container.find('input[name=daterangepicker_end]').val(dates[1].format(this.locale.format));
1142
            }
1143
 
1144
        },
1145
 
1146
        clickRange: function(e) {
1147
            var label = e.target.innerHTML;
1148
            this.chosenLabel = label;
1149
            if (label == this.locale.customRangeLabel) {
1150
                this.showCalendars();
1151
            } else {
1152
                var dates = this.ranges[label];
1153
                this.startDate = dates[0];
1154
                this.endDate = dates[1];
1155
 
1156
                if (!this.timePicker) {
1157
                    this.startDate.startOf('day');
1158
                    this.endDate.endOf('day');
1159
                }
1160
 
1161
                this.hideCalendars();
1162
                this.clickApply();
1163
            }
1164
        },
1165
 
1166
        clickPrev: function(e) {
1167
            var cal = $(e.target).parents('.calendar');
1168
            if (cal.hasClass('left')) {
1169
                this.leftCalendar.month.subtract(1, 'month');
1170
                if (this.linkedCalendars)
1171
                    this.rightCalendar.month.subtract(1, 'month');
1172
            } else {
1173
                this.rightCalendar.month.subtract(1, 'month');
1174
            }
1175
            this.updateCalendars();
1176
        },
1177
 
1178
        clickNext: function(e) {
1179
            var cal = $(e.target).parents('.calendar');
1180
            if (cal.hasClass('left')) {
1181
                this.leftCalendar.month.add(1, 'month');
1182
            } else {
1183
                this.rightCalendar.month.add(1, 'month');
1184
                if (this.linkedCalendars)
1185
                    this.leftCalendar.month.add(1, 'month');
1186
            }
1187
            this.updateCalendars();
1188
        },
1189
 
1190
        hoverDate: function(e) {
1191
 
1192
            //ignore mouse movements while an above-calendar text input has focus
1193
            if (this.container.find('input[name=daterangepicker_start]').is(":focus") || this.container.find('input[name=daterangepicker_end]').is(":focus"))
1194
                return;
1195
 
1196
            //ignore dates that can't be selected
1197
            if (!$(e.target).hasClass('available')) return;
1198
 
1199
            //have the text inputs above calendars reflect the date being hovered over
1200
            var title = $(e.target).attr('data-title');
1201
            var row = title.substr(1, 1);
1202
            var col = title.substr(3, 1);
1203
            var cal = $(e.target).parents('.calendar');
1204
            var date = cal.hasClass('left') ? this.leftCalendar.calendar[row][col] : this.rightCalendar.calendar[row][col];
1205
 
1206
            if (this.endDate) {
1207
                this.container.find('input[name=daterangepicker_start]').val(date.format(this.locale.format));
1208
            } else {
1209
                this.container.find('input[name=daterangepicker_end]').val(date.format(this.locale.format));
1210
            }
1211
 
1212
            //highlight the dates between the start date and the date being hovered as a potential end date
1213
            var leftCalendar = this.leftCalendar;
1214
            var rightCalendar = this.rightCalendar;
1215
            var startDate = this.startDate;
1216
            if (!this.endDate) {
1217
                this.container.find('.calendar td').each(function(index, el) {
1218
 
1219
                    //skip week numbers, only look at dates
1220
                    if ($(el).hasClass('week')) return;
1221
 
1222
                    var title = $(el).attr('data-title');
1223
                    var row = title.substr(1, 1);
1224
                    var col = title.substr(3, 1);
1225
                    var cal = $(el).parents('.calendar');
1226
                    var dt = cal.hasClass('left') ? leftCalendar.calendar[row][col] : rightCalendar.calendar[row][col];
1227
 
1228
                    if (dt.isAfter(startDate) && dt.isBefore(date)) {
1229
                        $(el).addClass('in-range');
1230
                    } else {
1231
                        $(el).removeClass('in-range');
1232
                    }
1233
 
1234
                });
1235
            }
1236
 
1237
        },
1238
 
1239
        clickDate: function(e) {
1240
 
1241
            if (!$(e.target).hasClass('available')) return;
1242
 
1243
            var title = $(e.target).attr('data-title');
1244
            var row = title.substr(1, 1);
1245
            var col = title.substr(3, 1);
1246
            var cal = $(e.target).parents('.calendar');
1247
            var date = cal.hasClass('left') ? this.leftCalendar.calendar[row][col] : this.rightCalendar.calendar[row][col];
1248
 
1249
            //
1250
            // this function needs to do a few things:
1251
            // * alternate between selecting a start and end date for the range,
1252
            // * if the time picker is enabled, apply the hour/minute/second from the select boxes to the clicked date
1253
            // * if autoapply is enabled, and an end date was chosen, apply the selection
1254
            // * if single date picker mode, and time picker isn't enabled, apply the selection immediately
1255
            //
1256
 
1257
            if (this.endDate || date.isBefore(this.startDate)) {
1258
                if (this.timePicker) {
1259
                    var hour = parseInt(this.container.find('.left .hourselect').val(), 10);
1260
                    if (!this.timePicker24Hour) {
1261
                        var ampm = cal.find('.ampmselect').val();
1262
                        if (ampm === 'PM' && hour < 12)
1263
                            hour += 12;
1264
                        if (ampm === 'AM' && hour === 12)
1265
                            hour = 0;
1266
                    }
1267
                    var minute = parseInt(this.container.find('.left .minuteselect').val(), 10);
1268
                    var second = this.timePickerSeconds ? parseInt(this.container.find('.left .secondselect').val(), 10) : 0;
1269
                    date = date.clone().hour(hour).minute(minute).second(second);
1270
                }
1271
                this.endDate = null;
1272
                this.setStartDate(date.clone());
1273
            } else {
1274
                if (this.timePicker) {
1275
                    var hour = parseInt(this.container.find('.right .hourselect').val(), 10);
1276
                    if (!this.timePicker24Hour) {
1277
                        var ampm = this.container.find('.right .ampmselect').val();
1278
                        if (ampm === 'PM' && hour < 12)
1279
                            hour += 12;
1280
                        if (ampm === 'AM' && hour === 12)
1281
                            hour = 0;
1282
                    }
1283
                    var minute = parseInt(this.container.find('.right .minuteselect').val(), 10);
1284
                    var second = this.timePickerSeconds ? parseInt(this.container.find('.right .secondselect').val(), 10) : 0;
1285
                    date = date.clone().hour(hour).minute(minute).second(second);
1286
                }
1287
                this.setEndDate(date.clone());
1288
                if (this.autoApply)
1289
                    this.clickApply();
1290
            }
1291
 
1292
            if (this.singleDatePicker) {
1293
                this.setEndDate(this.startDate);
1294
                if (!this.timePicker)
1295
                    this.clickApply();
1296
            }
1297
 
1298
            this.updateView();
1299
 
1300
        },
1301
 
1302
        clickApply: function(e) {
1303
            this.hide();
1304
            this.element.trigger('apply.daterangepicker', this);
1305
        },
1306
 
1307
        clickCancel: function(e) {
1308
            this.startDate = this.oldStartDate;
1309
            this.endDate = this.oldEndDate;
1310
            this.hide();
1311
            this.element.trigger('cancel.daterangepicker', this);
1312
        },
1313
 
1314
        monthOrYearChanged: function(e) {
1315
            var isLeft = $(e.target).closest('.calendar').hasClass('left'),
1316
                leftOrRight = isLeft ? 'left' : 'right',
1317
                cal = this.container.find('.calendar.'+leftOrRight);
1318
 
1319
            // Month must be Number for new moment versions
1320
            var month = parseInt(cal.find('.monthselect').val(), 10);
1321
            var year = cal.find('.yearselect').val();
1322
 
1323
            if (!isLeft) {
1324
                if (year < this.startDate.year() || (year == this.startDate.year() && month < this.startDate.month())) {
1325
                    month = this.startDate.month();
1326
                    year = this.startDate.year();
1327
                }
1328
            }
1329
 
1330
            if (this.minDate) {
1331
                if (year < this.minDate.year() || (year == this.minDate.year() && month < this.minDate.month())) {
1332
                    month = this.minDate.month();
1333
                    year = this.minDate.year();
1334
                }
1335
            }
1336
 
1337
            if (this.maxDate) {
1338
                if (year > this.maxDate.year() || (year == this.maxDate.year() && month > this.maxDate.month())) {
1339
                    month = this.maxDate.month();
1340
                    year = this.maxDate.year();
1341
                }
1342
            }
1343
 
1344
            if (isLeft) {
1345
                this.leftCalendar.month.month(month).year(year);
1346
                if (this.linkedCalendars)
1347
                    this.rightCalendar.month = this.leftCalendar.month.clone().add(1, 'month');
1348
            } else {
1349
                this.rightCalendar.month.month(month).year(year);
1350
                if (this.linkedCalendars)
1351
                    this.leftCalendar.month = this.rightCalendar.month.clone().subtract(1, 'month');
1352
            }
1353
            this.updateCalendars();
1354
        },
1355
 
1356
        timeChanged: function(e) {
1357
 
1358
            var cal = $(e.target).closest('.calendar'),
1359
                isLeft = cal.hasClass('left');
1360
 
1361
            var hour = parseInt(cal.find('.hourselect').val(), 10);
1362
            var minute = parseInt(cal.find('.minuteselect').val(), 10);
1363
            var second = this.timePickerSeconds ? parseInt(cal.find('.secondselect').val(), 10) : 0;
1364
 
1365
            if (!this.timePicker24Hour) {
1366
                var ampm = cal.find('.ampmselect').val();
1367
                if (ampm === 'PM' && hour < 12)
1368
                    hour += 12;
1369
                if (ampm === 'AM' && hour === 12)
1370
                    hour = 0;
1371
            }
1372
 
1373
            if (isLeft) {
1374
                var start = this.startDate.clone();
1375
                start.hour(hour);
1376
                start.minute(minute);
1377
                start.second(second);
1378
                this.setStartDate(start);
1379
                if (this.singleDatePicker) {
1380
                    this.endDate = this.startDate.clone();
1381
                } else if (this.endDate && this.endDate.format('YYYY-MM-DD') == start.format('YYYY-MM-DD') && this.endDate.isBefore(start)) {
1382
                    this.setEndDate(start.clone());
1383
                }
1384
            } else if (this.endDate) {
1385
                var end = this.endDate.clone();
1386
                end.hour(hour);
1387
                end.minute(minute);
1388
                end.second(second);
1389
                this.setEndDate(end);
1390
            }
1391
 
1392
            //update the calendars so all clickable dates reflect the new time component
1393
            this.updateCalendars();
1394
 
1395
            //update the form inputs above the calendars with the new time
1396
            this.updateFormInputs();
1397
 
1398
            //re-render the time pickers because changing one selection can affect what's enabled in another
1399
            this.renderTimePicker('left');
1400
            this.renderTimePicker('right');
1401
 
1402
        },
1403
 
1404
        formInputsChanged: function(e) {
1405
            var isRight = $(e.target).closest('.calendar').hasClass('right');
1406
            var start = moment(this.container.find('input[name="daterangepicker_start"]').val(), this.locale.format);
1407
            var end = moment(this.container.find('input[name="daterangepicker_end"]').val(), this.locale.format);
1408
 
1409
            if (start.isValid() && end.isValid()) {
1410
 
1411
                if (isRight && end.isBefore(start))
1412
                    start = end.clone();
1413
 
1414
                this.setStartDate(start);
1415
                this.setEndDate(end);
1416
 
1417
                if (isRight) {
1418
                    this.container.find('input[name="daterangepicker_start"]').val(this.startDate.format(this.locale.format));
1419
                } else {
1420
                    this.container.find('input[name="daterangepicker_end"]').val(this.endDate.format(this.locale.format));
1421
                }
1422
 
1423
            }
1424
 
1425
            this.updateCalendars();
1426
            if (this.timePicker) {
1427
                this.renderTimePicker('left');
1428
                this.renderTimePicker('right');
1429
            }
1430
        },
1431
 
1432
        elementChanged: function() {
1433
            if (!this.element.is('input')) return;
1434
            if (!this.element.val().length) return;
1435
            if (this.element.val().length < this.locale.format.length) return;
1436
 
1437
            var dateString = this.element.val().split(this.locale.separator),
1438
                start = null,
1439
                end = null;
1440
 
1441
            if (dateString.length === 2) {
1442
                start = moment(dateString[0], this.locale.format);
1443
                end = moment(dateString[1], this.locale.format);
1444
            }
1445
 
1446
            if (this.singleDatePicker || start === null || end === null) {
1447
                start = moment(this.element.val(), this.locale.format);
1448
                end = start;
1449
            }
1450
 
1451
            if (!start.isValid() || !end.isValid()) return;
1452
 
1453
            this.setStartDate(start);
1454
            this.setEndDate(end);
1455
            this.updateView();
1456
        },
1457
 
1458
        keydown: function(e) {
1459
            //hide on tab or enter
1460
            if ((e.keyCode === 9) || (e.keyCode === 13)) {
1461
                this.hide();
1462
            }
1463
        },
1464
 
1465
        updateElement: function() {
1466
            if (this.element.is('input') && !this.singleDatePicker && this.autoUpdateInput) {
1467
                this.element.val(this.startDate.format(this.locale.format) + this.locale.separator + this.endDate.format(this.locale.format));
1468
                this.element.trigger('change');
1469
            } else if (this.element.is('input') && this.autoUpdateInput) {
1470
                this.element.val(this.startDate.format(this.locale.format));
1471
                this.element.trigger('change');
1472
            }
1473
        },
1474
 
1475
        remove: function() {
1476
            this.container.remove();
1477
            this.element.off('.daterangepicker');
1478
            this.element.removeData();
1479
        }
1480
 
1481
    };
1482
 
1483
    $.fn.daterangepicker = function(options, callback) {
1484
        this.each(function() {
1485
            var el = $(this);
1486
            if (el.data('daterangepicker'))
1487
                el.data('daterangepicker').remove();
1488
            el.data('daterangepicker', new DateRangePicker(el, options, callback));
1489
        });
1490
        return this;
1491
    };
1492
 
1493
}));