GPQAPP

Форк
0
/
daterangepicker.js 
1578 строк · 66.3 Кб
1
/**
2
* @version: 3.1
3
* @author: Dan Grossman http://www.dangrossman.info/
4
* @copyright: Copyright (c) 2012-2019 Dan Grossman. All rights reserved.
5
* @license: Licensed under the MIT license. See http://www.opensource.org/licenses/mit-license.php
6
* @website: http://www.daterangepicker.com/
7
*/
8
// Following the UMD template https://github.com/umdjs/umd/blob/master/templates/returnExportsGlobal.js
9
(function (root, factory) {
10
    if (typeof define === 'function' && define.amd) {
11
        // AMD. Make globaly available as well
12
        define(['moment', 'jquery'], function (moment, jquery) {
13
            if (!jquery.fn) jquery.fn = {}; // webpack server rendering
14
            if (typeof moment !== 'function' && moment.hasOwnProperty('default')) moment = moment['default']
15
            return factory(moment, jquery);
16
        });
17
    } else if (typeof module === 'object' && module.exports) {
18
        // Node / Browserify
19
        //isomorphic issue
20
        var jQuery = (typeof window != 'undefined') ? window.jQuery : undefined;
21
        if (!jQuery) {
22
            jQuery = require('jquery');
23
            if (!jQuery.fn) jQuery.fn = {};
24
        }
25
        var moment = (typeof window != 'undefined' && typeof window.moment != 'undefined') ? window.moment : require('moment');
26
        module.exports = factory(moment, jQuery);
27
    } else {
28
        // Browser globals
29
        root.daterangepicker = factory(root.moment, root.jQuery);
30
    }
31
}(this, function(moment, $) {
32
    var DateRangePicker = function(element, options, cb) {
33

34
        //default settings for options
35
        this.parentEl = 'body';
36
        this.element = $(element);
37
        this.startDate = moment().startOf('day');
38
        this.endDate = moment().endOf('day');
39
        this.minDate = false;
40
        this.maxDate = false;
41
        this.maxSpan = false;
42
        this.autoApply = false;
43
        this.singleDatePicker = false;
44
        this.showDropdowns = false;
45
        this.minYear = moment().subtract(100, 'year').format('YYYY');
46
        this.maxYear = moment().add(100, 'year').format('YYYY');
47
        this.showWeekNumbers = false;
48
        this.showISOWeekNumbers = false;
49
        this.showCustomRangeLabel = true;
50
        this.timePicker = false;
51
        this.timePicker24Hour = false;
52
        this.timePickerIncrement = 1;
53
        this.timePickerSeconds = false;
54
        this.linkedCalendars = true;
55
        this.autoUpdateInput = true;
56
        this.alwaysShowCalendars = false;
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.applyButtonClasses = 'btn-primary';
69
        this.cancelButtonClasses = 'btn-default';
70

71
        this.locale = {
72
            direction: 'ltr',
73
            format: moment.localeData().longDateFormat('L'),
74
            separator: ' - ',
75
            applyLabel: 'Apply',
76
            cancelLabel: 'Cancel',
77
            weekLabel: 'W',
78
            customRangeLabel: 'Custom Range',
79
            daysOfWeek: moment.weekdaysMin(),
80
            monthNames: moment.monthsShort(),
81
            firstDay: moment.localeData().firstDayOfWeek()
82
        };
83

84
        this.callback = function() { };
85

86
        //some state information
87
        this.isShowing = false;
88
        this.leftCalendar = {};
89
        this.rightCalendar = {};
90

91
        //custom options from user
92
        if (typeof options !== 'object' || options === null)
93
            options = {};
94

95
        //allow setting options with data attributes
96
        //data-api options will be overwritten with custom javascript options
97
        options = $.extend(this.element.data(), options);
98

99
        //html template for the picker UI
100
        if (typeof options.template !== 'string' && !(options.template instanceof $))
101
            options.template =
102
            '<div class="daterangepicker">' +
103
                '<div class="ranges"></div>' +
104
                '<div class="drp-calendar left">' +
105
                    '<div class="calendar-table"></div>' +
106
                    '<div class="calendar-time"></div>' +
107
                '</div>' +
108
                '<div class="drp-calendar right">' +
109
                    '<div class="calendar-table"></div>' +
110
                    '<div class="calendar-time"></div>' +
111
                '</div>' +
112
                '<div class="drp-buttons">' +
113
                    '<span class="drp-selected"></span>' +
114
                    '<button class="cancelBtn" type="button"></button>' +
115
                    '<button class="applyBtn" disabled="disabled" type="button"></button> ' +
116
                '</div>' +
117
            '</div>';
118

119
        this.parentEl = (options.parentEl && $(options.parentEl).length) ? $(options.parentEl) : $(this.parentEl);
120
        this.container = $(options.template).appendTo(this.parentEl);
121

122
        //
123
        // handle all the possible options overriding defaults
124
        //
125

126
        if (typeof options.locale === 'object') {
127

128
            if (typeof options.locale.direction === 'string')
129
                this.locale.direction = options.locale.direction;
130

131
            if (typeof options.locale.format === 'string')
132
                this.locale.format = options.locale.format;
133

134
            if (typeof options.locale.separator === 'string')
135
                this.locale.separator = options.locale.separator;
136

137
            if (typeof options.locale.daysOfWeek === 'object')
138
                this.locale.daysOfWeek = options.locale.daysOfWeek.slice();
139

140
            if (typeof options.locale.monthNames === 'object')
141
              this.locale.monthNames = options.locale.monthNames.slice();
142

143
            if (typeof options.locale.firstDay === 'number')
144
              this.locale.firstDay = options.locale.firstDay;
145

146
            if (typeof options.locale.applyLabel === 'string')
147
              this.locale.applyLabel = options.locale.applyLabel;
148

149
            if (typeof options.locale.cancelLabel === 'string')
150
              this.locale.cancelLabel = options.locale.cancelLabel;
151

152
            if (typeof options.locale.weekLabel === 'string')
153
              this.locale.weekLabel = options.locale.weekLabel;
154

155
            if (typeof options.locale.customRangeLabel === 'string'){
156
                //Support unicode chars in the custom range name.
157
                var elem = document.createElement('textarea');
158
                elem.innerHTML = options.locale.customRangeLabel;
159
                var rangeHtml = elem.value;
160
                this.locale.customRangeLabel = rangeHtml;
161
            }
162
        }
163
        this.container.addClass(this.locale.direction);
164

165
        if (typeof options.startDate === 'string')
166
            this.startDate = moment(options.startDate, this.locale.format);
167

168
        if (typeof options.endDate === 'string')
169
            this.endDate = moment(options.endDate, this.locale.format);
170

171
        if (typeof options.minDate === 'string')
172
            this.minDate = moment(options.minDate, this.locale.format);
173

174
        if (typeof options.maxDate === 'string')
175
            this.maxDate = moment(options.maxDate, this.locale.format);
176

177
        if (typeof options.startDate === 'object')
178
            this.startDate = moment(options.startDate);
179

180
        if (typeof options.endDate === 'object')
181
            this.endDate = moment(options.endDate);
182

183
        if (typeof options.minDate === 'object')
184
            this.minDate = moment(options.minDate);
185

186
        if (typeof options.maxDate === 'object')
187
            this.maxDate = moment(options.maxDate);
188

189
        // sanity check for bad options
190
        if (this.minDate && this.startDate.isBefore(this.minDate))
191
            this.startDate = this.minDate.clone();
192

193
        // sanity check for bad options
194
        if (this.maxDate && this.endDate.isAfter(this.maxDate))
195
            this.endDate = this.maxDate.clone();
196

197
        if (typeof options.applyButtonClasses === 'string')
198
            this.applyButtonClasses = options.applyButtonClasses;
199

200
        if (typeof options.applyClass === 'string') //backwards compat
201
            this.applyButtonClasses = options.applyClass;
202

203
        if (typeof options.cancelButtonClasses === 'string')
204
            this.cancelButtonClasses = options.cancelButtonClasses;
205

206
        if (typeof options.cancelClass === 'string') //backwards compat
207
            this.cancelButtonClasses = options.cancelClass;
208

209
        if (typeof options.maxSpan === 'object')
210
            this.maxSpan = options.maxSpan;
211

212
        if (typeof options.dateLimit === 'object') //backwards compat
213
            this.maxSpan = options.dateLimit;
214

215
        if (typeof options.opens === 'string')
216
            this.opens = options.opens;
217

218
        if (typeof options.drops === 'string')
219
            this.drops = options.drops;
220

221
        if (typeof options.showWeekNumbers === 'boolean')
222
            this.showWeekNumbers = options.showWeekNumbers;
223

224
        if (typeof options.showISOWeekNumbers === 'boolean')
225
            this.showISOWeekNumbers = options.showISOWeekNumbers;
226

227
        if (typeof options.buttonClasses === 'string')
228
            this.buttonClasses = options.buttonClasses;
229

230
        if (typeof options.buttonClasses === 'object')
231
            this.buttonClasses = options.buttonClasses.join(' ');
232

233
        if (typeof options.showDropdowns === 'boolean')
234
            this.showDropdowns = options.showDropdowns;
235

236
        if (typeof options.minYear === 'number')
237
            this.minYear = options.minYear;
238

239
        if (typeof options.maxYear === 'number')
240
            this.maxYear = options.maxYear;
241

242
        if (typeof options.showCustomRangeLabel === 'boolean')
243
            this.showCustomRangeLabel = options.showCustomRangeLabel;
244

245
        if (typeof options.singleDatePicker === 'boolean') {
246
            this.singleDatePicker = options.singleDatePicker;
247
            if (this.singleDatePicker)
248
                this.endDate = this.startDate.clone();
249
        }
250

251
        if (typeof options.timePicker === 'boolean')
252
            this.timePicker = options.timePicker;
253

254
        if (typeof options.timePickerSeconds === 'boolean')
255
            this.timePickerSeconds = options.timePickerSeconds;
256

257
        if (typeof options.timePickerIncrement === 'number')
258
            this.timePickerIncrement = options.timePickerIncrement;
259

260
        if (typeof options.timePicker24Hour === 'boolean')
261
            this.timePicker24Hour = options.timePicker24Hour;
262

263
        if (typeof options.autoApply === 'boolean')
264
            this.autoApply = options.autoApply;
265

266
        if (typeof options.autoUpdateInput === 'boolean')
267
            this.autoUpdateInput = options.autoUpdateInput;
268

269
        if (typeof options.linkedCalendars === 'boolean')
270
            this.linkedCalendars = options.linkedCalendars;
271

272
        if (typeof options.isInvalidDate === 'function')
273
            this.isInvalidDate = options.isInvalidDate;
274

275
        if (typeof options.isCustomDate === 'function')
276
            this.isCustomDate = options.isCustomDate;
277

278
        if (typeof options.alwaysShowCalendars === 'boolean')
279
            this.alwaysShowCalendars = options.alwaysShowCalendars;
280

281
        // update day names order to firstDay
282
        if (this.locale.firstDay != 0) {
283
            var iterator = this.locale.firstDay;
284
            while (iterator > 0) {
285
                this.locale.daysOfWeek.push(this.locale.daysOfWeek.shift());
286
                iterator--;
287
            }
288
        }
289

290
        var start, end, range;
291

292
        //if no start/end dates set, check if an input element contains initial values
293
        if (typeof options.startDate === 'undefined' && typeof options.endDate === 'undefined') {
294
            if ($(this.element).is(':text')) {
295
                var val = $(this.element).val(),
296
                    split = val.split(this.locale.separator);
297

298
                start = end = null;
299

300
                if (split.length == 2) {
301
                    start = moment(split[0], this.locale.format);
302
                    end = moment(split[1], this.locale.format);
303
                } else if (this.singleDatePicker && val !== "") {
304
                    start = moment(val, this.locale.format);
305
                    end = moment(val, this.locale.format);
306
                }
307
                if (start !== null && end !== null) {
308
                    this.setStartDate(start);
309
                    this.setEndDate(end);
310
                }
311
            }
312
        }
313

314
        if (typeof options.ranges === 'object') {
315
            for (range in options.ranges) {
316

317
                if (typeof options.ranges[range][0] === 'string')
318
                    start = moment(options.ranges[range][0], this.locale.format);
319
                else
320
                    start = moment(options.ranges[range][0]);
321

322
                if (typeof options.ranges[range][1] === 'string')
323
                    end = moment(options.ranges[range][1], this.locale.format);
324
                else
325
                    end = moment(options.ranges[range][1]);
326

327
                // If the start or end date exceed those allowed by the minDate or maxSpan
328
                // options, shorten the range to the allowable period.
329
                if (this.minDate && start.isBefore(this.minDate))
330
                    start = this.minDate.clone();
331

332
                var maxDate = this.maxDate;
333
                if (this.maxSpan && maxDate && start.clone().add(this.maxSpan).isAfter(maxDate))
334
                    maxDate = start.clone().add(this.maxSpan);
335
                if (maxDate && end.isAfter(maxDate))
336
                    end = maxDate.clone();
337

338
                // If the end of the range is before the minimum or the start of the range is
339
                // after the maximum, don't display this range option at all.
340
                if ((this.minDate && end.isBefore(this.minDate, this.timepicker ? 'minute' : 'day'))
341
                  || (maxDate && start.isAfter(maxDate, this.timepicker ? 'minute' : 'day')))
342
                    continue;
343

344
                //Support unicode chars in the range names.
345
                var elem = document.createElement('textarea');
346
                elem.innerHTML = range;
347
                var rangeHtml = elem.value;
348

349
                this.ranges[rangeHtml] = [start, end];
350
            }
351

352
            var list = '<ul>';
353
            for (range in this.ranges) {
354
                list += '<li data-range-key="' + range + '">' + range + '</li>';
355
            }
356
            if (this.showCustomRangeLabel) {
357
                list += '<li data-range-key="' + this.locale.customRangeLabel + '">' + this.locale.customRangeLabel + '</li>';
358
            }
359
            list += '</ul>';
360
            this.container.find('.ranges').prepend(list);
361
        }
362

363
        if (typeof cb === 'function') {
364
            this.callback = cb;
365
        }
366

367
        if (!this.timePicker) {
368
            this.startDate = this.startDate.startOf('day');
369
            this.endDate = this.endDate.endOf('day');
370
            this.container.find('.calendar-time').hide();
371
        }
372

373
        //can't be used together for now
374
        if (this.timePicker && this.autoApply)
375
            this.autoApply = false;
376

377
        if (this.autoApply) {
378
            this.container.addClass('auto-apply');
379
        }
380

381
        if (typeof options.ranges === 'object')
382
            this.container.addClass('show-ranges');
383

384
        if (this.singleDatePicker) {
385
            this.container.addClass('single');
386
            this.container.find('.drp-calendar.left').addClass('single');
387
            this.container.find('.drp-calendar.left').show();
388
            this.container.find('.drp-calendar.right').hide();
389
            if (!this.timePicker && this.autoApply) {
390
                this.container.addClass('auto-apply');
391
            }
392
        }
393

394
        if ((typeof options.ranges === 'undefined' && !this.singleDatePicker) || this.alwaysShowCalendars) {
395
            this.container.addClass('show-calendar');
396
        }
397

398
        this.container.addClass('opens' + this.opens);
399

400
        //apply CSS classes and labels to buttons
401
        this.container.find('.applyBtn, .cancelBtn').addClass(this.buttonClasses);
402
        if (this.applyButtonClasses.length)
403
            this.container.find('.applyBtn').addClass(this.applyButtonClasses);
404
        if (this.cancelButtonClasses.length)
405
            this.container.find('.cancelBtn').addClass(this.cancelButtonClasses);
406
        this.container.find('.applyBtn').html(this.locale.applyLabel);
407
        this.container.find('.cancelBtn').html(this.locale.cancelLabel);
408

409
        //
410
        // event listeners
411
        //
412

413
        this.container.find('.drp-calendar')
414
            .on('click.daterangepicker', '.prev', $.proxy(this.clickPrev, this))
415
            .on('click.daterangepicker', '.next', $.proxy(this.clickNext, this))
416
            .on('mousedown.daterangepicker', 'td.available', $.proxy(this.clickDate, this))
417
            .on('mouseenter.daterangepicker', 'td.available', $.proxy(this.hoverDate, this))
418
            .on('change.daterangepicker', 'select.yearselect', $.proxy(this.monthOrYearChanged, this))
419
            .on('change.daterangepicker', 'select.monthselect', $.proxy(this.monthOrYearChanged, this))
420
            .on('change.daterangepicker', 'select.hourselect,select.minuteselect,select.secondselect,select.ampmselect', $.proxy(this.timeChanged, this));
421

422
        this.container.find('.ranges')
423
            .on('click.daterangepicker', 'li', $.proxy(this.clickRange, this));
424

425
        this.container.find('.drp-buttons')
426
            .on('click.daterangepicker', 'button.applyBtn', $.proxy(this.clickApply, this))
427
            .on('click.daterangepicker', 'button.cancelBtn', $.proxy(this.clickCancel, this));
428

429
        if (this.element.is('input') || this.element.is('button')) {
430
            this.element.on({
431
                'click.daterangepicker': $.proxy(this.show, this),
432
                'focus.daterangepicker': $.proxy(this.show, this),
433
                'keyup.daterangepicker': $.proxy(this.elementChanged, this),
434
                'keydown.daterangepicker': $.proxy(this.keydown, this) //IE 11 compatibility
435
            });
436
        } else {
437
            this.element.on('click.daterangepicker', $.proxy(this.toggle, this));
438
            this.element.on('keydown.daterangepicker', $.proxy(this.toggle, this));
439
        }
440

441
        //
442
        // if attached to a text input, set the initial value
443
        //
444

445
        this.updateElement();
446

447
    };
448

449
    DateRangePicker.prototype = {
450

451
        constructor: DateRangePicker,
452

453
        setStartDate: function(startDate) {
454
            if (typeof startDate === 'string')
455
                this.startDate = moment(startDate, this.locale.format);
456

457
            if (typeof startDate === 'object')
458
                this.startDate = moment(startDate);
459

460
            if (!this.timePicker)
461
                this.startDate = this.startDate.startOf('day');
462

463
            if (this.timePicker && this.timePickerIncrement)
464
                this.startDate.minute(Math.round(this.startDate.minute() / this.timePickerIncrement) * this.timePickerIncrement);
465

466
            if (this.minDate && this.startDate.isBefore(this.minDate)) {
467
                this.startDate = this.minDate.clone();
468
                if (this.timePicker && this.timePickerIncrement)
469
                    this.startDate.minute(Math.round(this.startDate.minute() / this.timePickerIncrement) * this.timePickerIncrement);
470
            }
471

472
            if (this.maxDate && this.startDate.isAfter(this.maxDate)) {
473
                this.startDate = this.maxDate.clone();
474
                if (this.timePicker && this.timePickerIncrement)
475
                    this.startDate.minute(Math.floor(this.startDate.minute() / this.timePickerIncrement) * this.timePickerIncrement);
476
            }
477

478
            if (!this.isShowing)
479
                this.updateElement();
480

481
            this.updateMonthsInView();
482
        },
483

484
        setEndDate: function(endDate) {
485
            if (typeof endDate === 'string')
486
                this.endDate = moment(endDate, this.locale.format);
487

488
            if (typeof endDate === 'object')
489
                this.endDate = moment(endDate);
490

491
            if (!this.timePicker)
492
                this.endDate = this.endDate.endOf('day');
493

494
            if (this.timePicker && this.timePickerIncrement)
495
                this.endDate.minute(Math.round(this.endDate.minute() / this.timePickerIncrement) * this.timePickerIncrement);
496

497
            if (this.endDate.isBefore(this.startDate))
498
                this.endDate = this.startDate.clone();
499

500
            if (this.maxDate && this.endDate.isAfter(this.maxDate))
501
                this.endDate = this.maxDate.clone();
502

503
            if (this.maxSpan && this.startDate.clone().add(this.maxSpan).isBefore(this.endDate))
504
                this.endDate = this.startDate.clone().add(this.maxSpan);
505

506
            this.previousRightTime = this.endDate.clone();
507

508
            this.container.find('.drp-selected').html(this.startDate.format(this.locale.format) + this.locale.separator + this.endDate.format(this.locale.format));
509

510
            if (!this.isShowing)
511
                this.updateElement();
512

513
            this.updateMonthsInView();
514
        },
515

516
        isInvalidDate: function() {
517
            return false;
518
        },
519

520
        isCustomDate: function() {
521
            return false;
522
        },
523

524
        updateView: function() {
525
            if (this.timePicker) {
526
                this.renderTimePicker('left');
527
                this.renderTimePicker('right');
528
                if (!this.endDate) {
529
                    this.container.find('.right .calendar-time select').prop('disabled', true).addClass('disabled');
530
                } else {
531
                    this.container.find('.right .calendar-time select').prop('disabled', false).removeClass('disabled');
532
                }
533
            }
534
            if (this.endDate)
535
                this.container.find('.drp-selected').html(this.startDate.format(this.locale.format) + this.locale.separator + this.endDate.format(this.locale.format));
536
            this.updateMonthsInView();
537
            this.updateCalendars();
538
            this.updateFormInputs();
539
        },
540

541
        updateMonthsInView: function() {
542
            if (this.endDate) {
543

544
                //if both dates are visible already, do nothing
545
                if (!this.singleDatePicker && this.leftCalendar.month && this.rightCalendar.month &&
546
                    (this.startDate.format('YYYY-MM') == this.leftCalendar.month.format('YYYY-MM') || this.startDate.format('YYYY-MM') == this.rightCalendar.month.format('YYYY-MM'))
547
                    &&
548
                    (this.endDate.format('YYYY-MM') == this.leftCalendar.month.format('YYYY-MM') || this.endDate.format('YYYY-MM') == this.rightCalendar.month.format('YYYY-MM'))
549
                    ) {
550
                    return;
551
                }
552

553
                this.leftCalendar.month = this.startDate.clone().date(2);
554
                if (!this.linkedCalendars && (this.endDate.month() != this.startDate.month() || this.endDate.year() != this.startDate.year())) {
555
                    this.rightCalendar.month = this.endDate.clone().date(2);
556
                } else {
557
                    this.rightCalendar.month = this.startDate.clone().date(2).add(1, 'month');
558
                }
559

560
            } else {
561
                if (this.leftCalendar.month.format('YYYY-MM') != this.startDate.format('YYYY-MM') && this.rightCalendar.month.format('YYYY-MM') != this.startDate.format('YYYY-MM')) {
562
                    this.leftCalendar.month = this.startDate.clone().date(2);
563
                    this.rightCalendar.month = this.startDate.clone().date(2).add(1, 'month');
564
                }
565
            }
566
            if (this.maxDate && this.linkedCalendars && !this.singleDatePicker && this.rightCalendar.month > this.maxDate) {
567
              this.rightCalendar.month = this.maxDate.clone().date(2);
568
              this.leftCalendar.month = this.maxDate.clone().date(2).subtract(1, 'month');
569
            }
570
        },
571

572
        updateCalendars: function() {
573

574
            if (this.timePicker) {
575
                var hour, minute, second;
576
                if (this.endDate) {
577
                    hour = parseInt(this.container.find('.left .hourselect').val(), 10);
578
                    minute = parseInt(this.container.find('.left .minuteselect').val(), 10);
579
                    if (isNaN(minute)) {
580
                        minute = parseInt(this.container.find('.left .minuteselect option:last').val(), 10);
581
                    }
582
                    second = this.timePickerSeconds ? parseInt(this.container.find('.left .secondselect').val(), 10) : 0;
583
                    if (!this.timePicker24Hour) {
584
                        var ampm = this.container.find('.left .ampmselect').val();
585
                        if (ampm === 'PM' && hour < 12)
586
                            hour += 12;
587
                        if (ampm === 'AM' && hour === 12)
588
                            hour = 0;
589
                    }
590
                } else {
591
                    hour = parseInt(this.container.find('.right .hourselect').val(), 10);
592
                    minute = parseInt(this.container.find('.right .minuteselect').val(), 10);
593
                    if (isNaN(minute)) {
594
                        minute = parseInt(this.container.find('.right .minuteselect option:last').val(), 10);
595
                    }
596
                    second = this.timePickerSeconds ? parseInt(this.container.find('.right .secondselect').val(), 10) : 0;
597
                    if (!this.timePicker24Hour) {
598
                        var ampm = this.container.find('.right .ampmselect').val();
599
                        if (ampm === 'PM' && hour < 12)
600
                            hour += 12;
601
                        if (ampm === 'AM' && hour === 12)
602
                            hour = 0;
603
                    }
604
                }
605
                this.leftCalendar.month.hour(hour).minute(minute).second(second);
606
                this.rightCalendar.month.hour(hour).minute(minute).second(second);
607
            }
608

609
            this.renderCalendar('left');
610
            this.renderCalendar('right');
611

612
            //highlight any predefined range matching the current start and end dates
613
            this.container.find('.ranges li').removeClass('active');
614
            if (this.endDate == null) return;
615

616
            this.calculateChosenLabel();
617
        },
618

619
        renderCalendar: function(side) {
620

621
            //
622
            // Build the matrix of dates that will populate the calendar
623
            //
624

625
            var calendar = side == 'left' ? this.leftCalendar : this.rightCalendar;
626
            var month = calendar.month.month();
627
            var year = calendar.month.year();
628
            var hour = calendar.month.hour();
629
            var minute = calendar.month.minute();
630
            var second = calendar.month.second();
631
            var daysInMonth = moment([year, month]).daysInMonth();
632
            var firstDay = moment([year, month, 1]);
633
            var lastDay = moment([year, month, daysInMonth]);
634
            var lastMonth = moment(firstDay).subtract(1, 'month').month();
635
            var lastYear = moment(firstDay).subtract(1, 'month').year();
636
            var daysInLastMonth = moment([lastYear, lastMonth]).daysInMonth();
637
            var dayOfWeek = firstDay.day();
638

639
            //initialize a 6 rows x 7 columns array for the calendar
640
            var calendar = [];
641
            calendar.firstDay = firstDay;
642
            calendar.lastDay = lastDay;
643

644
            for (var i = 0; i < 6; i++) {
645
                calendar[i] = [];
646
            }
647

648
            //populate the calendar with date objects
649
            var startDay = daysInLastMonth - dayOfWeek + this.locale.firstDay + 1;
650
            if (startDay > daysInLastMonth)
651
                startDay -= 7;
652

653
            if (dayOfWeek == this.locale.firstDay)
654
                startDay = daysInLastMonth - 6;
655

656
            var curDate = moment([lastYear, lastMonth, startDay, 12, minute, second]);
657

658
            var col, row;
659
            for (var i = 0, col = 0, row = 0; i < 42; i++, col++, curDate = moment(curDate).add(24, 'hour')) {
660
                if (i > 0 && col % 7 === 0) {
661
                    col = 0;
662
                    row++;
663
                }
664
                calendar[row][col] = curDate.clone().hour(hour).minute(minute).second(second);
665
                curDate.hour(12);
666

667
                if (this.minDate && calendar[row][col].format('YYYY-MM-DD') == this.minDate.format('YYYY-MM-DD') && calendar[row][col].isBefore(this.minDate) && side == 'left') {
668
                    calendar[row][col] = this.minDate.clone();
669
                }
670

671
                if (this.maxDate && calendar[row][col].format('YYYY-MM-DD') == this.maxDate.format('YYYY-MM-DD') && calendar[row][col].isAfter(this.maxDate) && side == 'right') {
672
                    calendar[row][col] = this.maxDate.clone();
673
                }
674

675
            }
676

677
            //make the calendar object available to hoverDate/clickDate
678
            if (side == 'left') {
679
                this.leftCalendar.calendar = calendar;
680
            } else {
681
                this.rightCalendar.calendar = calendar;
682
            }
683

684
            //
685
            // Display the calendar
686
            //
687

688
            var minDate = side == 'left' ? this.minDate : this.startDate;
689
            var maxDate = this.maxDate;
690
            var selected = side == 'left' ? this.startDate : this.endDate;
691
            var arrow = this.locale.direction == 'ltr' ? {left: 'chevron-left', right: 'chevron-right'} : {left: 'chevron-right', right: 'chevron-left'};
692

693
            var html = '<table class="table-condensed">';
694
            html += '<thead>';
695
            html += '<tr>';
696

697
            // add empty cell for week number
698
            if (this.showWeekNumbers || this.showISOWeekNumbers)
699
                html += '<th></th>';
700

701
            if ((!minDate || minDate.isBefore(calendar.firstDay)) && (!this.linkedCalendars || side == 'left')) {
702
                html += '<th class="prev available"><span></span></th>';
703
            } else {
704
                html += '<th></th>';
705
            }
706

707
            var dateHtml = this.locale.monthNames[calendar[1][1].month()] + calendar[1][1].format(" YYYY");
708

709
            if (this.showDropdowns) {
710
                var currentMonth = calendar[1][1].month();
711
                var currentYear = calendar[1][1].year();
712
                var maxYear = (maxDate && maxDate.year()) || (this.maxYear);
713
                var minYear = (minDate && minDate.year()) || (this.minYear);
714
                var inMinYear = currentYear == minYear;
715
                var inMaxYear = currentYear == maxYear;
716

717
                var monthHtml = '<select class="monthselect">';
718
                for (var m = 0; m < 12; m++) {
719
                    if ((!inMinYear || (minDate && m >= minDate.month())) && (!inMaxYear || (maxDate && m <= maxDate.month()))) {
720
                        monthHtml += "<option value='" + m + "'" +
721
                            (m === currentMonth ? " selected='selected'" : "") +
722
                            ">" + this.locale.monthNames[m] + "</option>";
723
                    } else {
724
                        monthHtml += "<option value='" + m + "'" +
725
                            (m === currentMonth ? " selected='selected'" : "") +
726
                            " disabled='disabled'>" + this.locale.monthNames[m] + "</option>";
727
                    }
728
                }
729
                monthHtml += "</select>";
730

731
                var yearHtml = '<select class="yearselect">';
732
                for (var y = minYear; y <= maxYear; y++) {
733
                    yearHtml += '<option value="' + y + '"' +
734
                        (y === currentYear ? ' selected="selected"' : '') +
735
                        '>' + y + '</option>';
736
                }
737
                yearHtml += '</select>';
738

739
                dateHtml = monthHtml + yearHtml;
740
            }
741

742
            html += '<th colspan="5" class="month">' + dateHtml + '</th>';
743
            if ((!maxDate || maxDate.isAfter(calendar.lastDay)) && (!this.linkedCalendars || side == 'right' || this.singleDatePicker)) {
744
                html += '<th class="next available"><span></span></th>';
745
            } else {
746
                html += '<th></th>';
747
            }
748

749
            html += '</tr>';
750
            html += '<tr>';
751

752
            // add week number label
753
            if (this.showWeekNumbers || this.showISOWeekNumbers)
754
                html += '<th class="week">' + this.locale.weekLabel + '</th>';
755

756
            $.each(this.locale.daysOfWeek, function(index, dayOfWeek) {
757
                html += '<th>' + dayOfWeek + '</th>';
758
            });
759

760
            html += '</tr>';
761
            html += '</thead>';
762
            html += '<tbody>';
763

764
            //adjust maxDate to reflect the maxSpan setting in order to
765
            //grey out end dates beyond the maxSpan
766
            if (this.endDate == null && this.maxSpan) {
767
                var maxLimit = this.startDate.clone().add(this.maxSpan).endOf('day');
768
                if (!maxDate || maxLimit.isBefore(maxDate)) {
769
                    maxDate = maxLimit;
770
                }
771
            }
772

773
            for (var row = 0; row < 6; row++) {
774
                html += '<tr>';
775

776
                // add week number
777
                if (this.showWeekNumbers)
778
                    html += '<td class="week">' + calendar[row][0].week() + '</td>';
779
                else if (this.showISOWeekNumbers)
780
                    html += '<td class="week">' + calendar[row][0].isoWeek() + '</td>';
781

782
                for (var col = 0; col < 7; col++) {
783

784
                    var classes = [];
785

786
                    //highlight today's date
787
                    if (calendar[row][col].isSame(new Date(), "day"))
788
                        classes.push('today');
789

790
                    //highlight weekends
791
                    if (calendar[row][col].isoWeekday() > 5)
792
                        classes.push('weekend');
793

794
                    //grey out the dates in other months displayed at beginning and end of this calendar
795
                    if (calendar[row][col].month() != calendar[1][1].month())
796
                        classes.push('off', 'ends');
797

798
                    //don't allow selection of dates before the minimum date
799
                    if (this.minDate && calendar[row][col].isBefore(this.minDate, 'day'))
800
                        classes.push('off', 'disabled');
801

802
                    //don't allow selection of dates after the maximum date
803
                    if (maxDate && calendar[row][col].isAfter(maxDate, 'day'))
804
                        classes.push('off', 'disabled');
805

806
                    //don't allow selection of date if a custom function decides it's invalid
807
                    if (this.isInvalidDate(calendar[row][col]))
808
                        classes.push('off', 'disabled');
809

810
                    //highlight the currently selected start date
811
                    if (calendar[row][col].format('YYYY-MM-DD') == this.startDate.format('YYYY-MM-DD'))
812
                        classes.push('active', 'start-date');
813

814
                    //highlight the currently selected end date
815
                    if (this.endDate != null && calendar[row][col].format('YYYY-MM-DD') == this.endDate.format('YYYY-MM-DD'))
816
                        classes.push('active', 'end-date');
817

818
                    //highlight dates in-between the selected dates
819
                    if (this.endDate != null && calendar[row][col] > this.startDate && calendar[row][col] < this.endDate)
820
                        classes.push('in-range');
821

822
                    //apply custom classes for this date
823
                    var isCustom = this.isCustomDate(calendar[row][col]);
824
                    if (isCustom !== false) {
825
                        if (typeof isCustom === 'string')
826
                            classes.push(isCustom);
827
                        else
828
                            Array.prototype.push.apply(classes, isCustom);
829
                    }
830

831
                    var cname = '', disabled = false;
832
                    for (var i = 0; i < classes.length; i++) {
833
                        cname += classes[i] + ' ';
834
                        if (classes[i] == 'disabled')
835
                            disabled = true;
836
                    }
837
                    if (!disabled)
838
                        cname += 'available';
839

840
                    html += '<td class="' + cname.replace(/^\s+|\s+$/g, '') + '" data-title="' + 'r' + row + 'c' + col + '">' + calendar[row][col].date() + '</td>';
841

842
                }
843
                html += '</tr>';
844
            }
845

846
            html += '</tbody>';
847
            html += '</table>';
848

849
            this.container.find('.drp-calendar.' + side + ' .calendar-table').html(html);
850

851
        },
852

853
        renderTimePicker: function(side) {
854

855
            // Don't bother updating the time picker if it's currently disabled
856
            // because an end date hasn't been clicked yet
857
            if (side == 'right' && !this.endDate) return;
858

859
            var html, selected, minDate, maxDate = this.maxDate;
860

861
            if (this.maxSpan && (!this.maxDate || this.startDate.clone().add(this.maxSpan).isBefore(this.maxDate)))
862
                maxDate = this.startDate.clone().add(this.maxSpan);
863

864
            if (side == 'left') {
865
                selected = this.startDate.clone();
866
                minDate = this.minDate;
867
            } else if (side == 'right') {
868
                selected = this.endDate.clone();
869
                minDate = this.startDate;
870

871
                //Preserve the time already selected
872
                var timeSelector = this.container.find('.drp-calendar.right .calendar-time');
873
                if (timeSelector.html() != '') {
874

875
                    selected.hour(!isNaN(selected.hour()) ? selected.hour() : timeSelector.find('.hourselect option:selected').val());
876
                    selected.minute(!isNaN(selected.minute()) ? selected.minute() : timeSelector.find('.minuteselect option:selected').val());
877
                    selected.second(!isNaN(selected.second()) ? selected.second() : timeSelector.find('.secondselect option:selected').val());
878

879
                    if (!this.timePicker24Hour) {
880
                        var ampm = timeSelector.find('.ampmselect option:selected').val();
881
                        if (ampm === 'PM' && selected.hour() < 12)
882
                            selected.hour(selected.hour() + 12);
883
                        if (ampm === 'AM' && selected.hour() === 12)
884
                            selected.hour(0);
885
                    }
886

887
                }
888

889
                if (selected.isBefore(this.startDate))
890
                    selected = this.startDate.clone();
891

892
                if (maxDate && selected.isAfter(maxDate))
893
                    selected = maxDate.clone();
894

895
            }
896

897
            //
898
            // hours
899
            //
900

901
            html = '<select class="hourselect">';
902

903
            var start = this.timePicker24Hour ? 0 : 1;
904
            var end = this.timePicker24Hour ? 23 : 12;
905

906
            for (var i = start; i <= end; i++) {
907
                var i_in_24 = i;
908
                if (!this.timePicker24Hour)
909
                    i_in_24 = selected.hour() >= 12 ? (i == 12 ? 12 : i + 12) : (i == 12 ? 0 : i);
910

911
                var time = selected.clone().hour(i_in_24);
912
                var disabled = false;
913
                if (minDate && time.minute(59).isBefore(minDate))
914
                    disabled = true;
915
                if (maxDate && time.minute(0).isAfter(maxDate))
916
                    disabled = true;
917

918
                if (i_in_24 == selected.hour() && !disabled) {
919
                    html += '<option value="' + i + '" selected="selected">' + i + '</option>';
920
                } else if (disabled) {
921
                    html += '<option value="' + i + '" disabled="disabled" class="disabled">' + i + '</option>';
922
                } else {
923
                    html += '<option value="' + i + '">' + i + '</option>';
924
                }
925
            }
926

927
            html += '</select> ';
928

929
            //
930
            // minutes
931
            //
932

933
            html += ': <select class="minuteselect">';
934

935
            for (var i = 0; i < 60; i += this.timePickerIncrement) {
936
                var padded = i < 10 ? '0' + i : i;
937
                var time = selected.clone().minute(i);
938

939
                var disabled = false;
940
                if (minDate && time.second(59).isBefore(minDate))
941
                    disabled = true;
942
                if (maxDate && time.second(0).isAfter(maxDate))
943
                    disabled = true;
944

945
                if (selected.minute() == i && !disabled) {
946
                    html += '<option value="' + i + '" selected="selected">' + padded + '</option>';
947
                } else if (disabled) {
948
                    html += '<option value="' + i + '" disabled="disabled" class="disabled">' + padded + '</option>';
949
                } else {
950
                    html += '<option value="' + i + '">' + padded + '</option>';
951
                }
952
            }
953

954
            html += '</select> ';
955

956
            //
957
            // seconds
958
            //
959

960
            if (this.timePickerSeconds) {
961
                html += ': <select class="secondselect">';
962

963
                for (var i = 0; i < 60; i++) {
964
                    var padded = i < 10 ? '0' + i : i;
965
                    var time = selected.clone().second(i);
966

967
                    var disabled = false;
968
                    if (minDate && time.isBefore(minDate))
969
                        disabled = true;
970
                    if (maxDate && time.isAfter(maxDate))
971
                        disabled = true;
972

973
                    if (selected.second() == i && !disabled) {
974
                        html += '<option value="' + i + '" selected="selected">' + padded + '</option>';
975
                    } else if (disabled) {
976
                        html += '<option value="' + i + '" disabled="disabled" class="disabled">' + padded + '</option>';
977
                    } else {
978
                        html += '<option value="' + i + '">' + padded + '</option>';
979
                    }
980
                }
981

982
                html += '</select> ';
983
            }
984

985
            //
986
            // AM/PM
987
            //
988

989
            if (!this.timePicker24Hour) {
990
                html += '<select class="ampmselect">';
991

992
                var am_html = '';
993
                var pm_html = '';
994

995
                if (minDate && selected.clone().hour(12).minute(0).second(0).isBefore(minDate))
996
                    am_html = ' disabled="disabled" class="disabled"';
997

998
                if (maxDate && selected.clone().hour(0).minute(0).second(0).isAfter(maxDate))
999
                    pm_html = ' disabled="disabled" class="disabled"';
1000

1001
                if (selected.hour() >= 12) {
1002
                    html += '<option value="AM"' + am_html + '>AM</option><option value="PM" selected="selected"' + pm_html + '>PM</option>';
1003
                } else {
1004
                    html += '<option value="AM" selected="selected"' + am_html + '>AM</option><option value="PM"' + pm_html + '>PM</option>';
1005
                }
1006

1007
                html += '</select>';
1008
            }
1009

1010
            this.container.find('.drp-calendar.' + side + ' .calendar-time').html(html);
1011

1012
        },
1013

1014
        updateFormInputs: function() {
1015

1016
            if (this.singleDatePicker || (this.endDate && (this.startDate.isBefore(this.endDate) || this.startDate.isSame(this.endDate)))) {
1017
                this.container.find('button.applyBtn').prop('disabled', false);
1018
            } else {
1019
                this.container.find('button.applyBtn').prop('disabled', true);
1020
            }
1021

1022
        },
1023

1024
        move: function() {
1025
            var parentOffset = { top: 0, left: 0 },
1026
                containerTop,
1027
                drops = this.drops;
1028

1029
            var parentRightEdge = $(window).width();
1030
            if (!this.parentEl.is('body')) {
1031
                parentOffset = {
1032
                    top: this.parentEl.offset().top - this.parentEl.scrollTop(),
1033
                    left: this.parentEl.offset().left - this.parentEl.scrollLeft()
1034
                };
1035
                parentRightEdge = this.parentEl[0].clientWidth + this.parentEl.offset().left;
1036
            }
1037

1038
            switch (drops) {
1039
            case 'auto':
1040
                containerTop = this.element.offset().top + this.element.outerHeight() - parentOffset.top;
1041
                if (containerTop + this.container.outerHeight() >= this.parentEl[0].scrollHeight) {
1042
                    containerTop = this.element.offset().top - this.container.outerHeight() - parentOffset.top;
1043
                    drops = 'up';
1044
                }
1045
                break;
1046
            case 'up':
1047
                containerTop = this.element.offset().top - this.container.outerHeight() - parentOffset.top;
1048
                break;
1049
            default:
1050
                containerTop = this.element.offset().top + this.element.outerHeight() - parentOffset.top;
1051
                break;
1052
            }
1053

1054
            // Force the container to it's actual width
1055
            this.container.css({
1056
              top: 0,
1057
              left: 0,
1058
              right: 'auto'
1059
            });
1060
            var containerWidth = this.container.outerWidth();
1061

1062
            this.container.toggleClass('drop-up', drops == 'up');
1063

1064
            if (this.opens == 'left') {
1065
                var containerRight = parentRightEdge - this.element.offset().left - this.element.outerWidth();
1066
                if (containerWidth + containerRight > $(window).width()) {
1067
                    this.container.css({
1068
                        top: containerTop,
1069
                        right: 'auto',
1070
                        left: 9
1071
                    });
1072
                } else {
1073
                    this.container.css({
1074
                        top: containerTop,
1075
                        right: containerRight,
1076
                        left: 'auto'
1077
                    });
1078
                }
1079
            } else if (this.opens == 'center') {
1080
                var containerLeft = this.element.offset().left - parentOffset.left + this.element.outerWidth() / 2
1081
                                        - containerWidth / 2;
1082
                if (containerLeft < 0) {
1083
                    this.container.css({
1084
                        top: containerTop,
1085
                        right: 'auto',
1086
                        left: 9
1087
                    });
1088
                } else if (containerLeft + containerWidth > $(window).width()) {
1089
                    this.container.css({
1090
                        top: containerTop,
1091
                        left: 'auto',
1092
                        right: 0
1093
                    });
1094
                } else {
1095
                    this.container.css({
1096
                        top: containerTop,
1097
                        left: containerLeft,
1098
                        right: 'auto'
1099
                    });
1100
                }
1101
            } else {
1102
                var containerLeft = this.element.offset().left - parentOffset.left;
1103
                if (containerLeft + containerWidth > $(window).width()) {
1104
                    this.container.css({
1105
                        top: containerTop,
1106
                        left: 'auto',
1107
                        right: 0
1108
                    });
1109
                } else {
1110
                    this.container.css({
1111
                        top: containerTop,
1112
                        left: containerLeft,
1113
                        right: 'auto'
1114
                    });
1115
                }
1116
            }
1117
        },
1118

1119
        show: function(e) {
1120
            if (this.isShowing) return;
1121

1122
            // Create a click proxy that is private to this instance of datepicker, for unbinding
1123
            this._outsideClickProxy = $.proxy(function(e) { this.outsideClick(e); }, this);
1124

1125
            // Bind global datepicker mousedown for hiding and
1126
            $(document)
1127
              .on('mousedown.daterangepicker', this._outsideClickProxy)
1128
              // also support mobile devices
1129
              .on('touchend.daterangepicker', this._outsideClickProxy)
1130
              // also explicitly play nice with Bootstrap dropdowns, which stopPropagation when clicking them
1131
              .on('click.daterangepicker', '[data-toggle=dropdown]', this._outsideClickProxy)
1132
              // and also close when focus changes to outside the picker (eg. tabbing between controls)
1133
              .on('focusin.daterangepicker', this._outsideClickProxy);
1134

1135
            // Reposition the picker if the window is resized while it's open
1136
            $(window).on('resize.daterangepicker', $.proxy(function(e) { this.move(e); }, this));
1137

1138
            this.oldStartDate = this.startDate.clone();
1139
            this.oldEndDate = this.endDate.clone();
1140
            this.previousRightTime = this.endDate.clone();
1141

1142
            this.updateView();
1143
            this.container.show();
1144
            this.move();
1145
            this.element.trigger('show.daterangepicker', this);
1146
            this.isShowing = true;
1147
        },
1148

1149
        hide: function(e) {
1150
            if (!this.isShowing) return;
1151

1152
            //incomplete date selection, revert to last values
1153
            if (!this.endDate) {
1154
                this.startDate = this.oldStartDate.clone();
1155
                this.endDate = this.oldEndDate.clone();
1156
            }
1157

1158
            //if a new date range was selected, invoke the user callback function
1159
            if (!this.startDate.isSame(this.oldStartDate) || !this.endDate.isSame(this.oldEndDate))
1160
                this.callback(this.startDate.clone(), this.endDate.clone(), this.chosenLabel);
1161

1162
            //if picker is attached to a text input, update it
1163
            this.updateElement();
1164

1165
            $(document).off('.daterangepicker');
1166
            $(window).off('.daterangepicker');
1167
            this.container.hide();
1168
            this.element.trigger('hide.daterangepicker', this);
1169
            this.isShowing = false;
1170
        },
1171

1172
        toggle: function(e) {
1173
            if (this.isShowing) {
1174
                this.hide();
1175
            } else {
1176
                this.show();
1177
            }
1178
        },
1179

1180
        outsideClick: function(e) {
1181
            var target = $(e.target);
1182
            // if the page is clicked anywhere except within the daterangerpicker/button
1183
            // itself then call this.hide()
1184
            if (
1185
                // ie modal dialog fix
1186
                e.type == "focusin" ||
1187
                target.closest(this.element).length ||
1188
                target.closest(this.container).length ||
1189
                target.closest('.calendar-table').length
1190
                ) return;
1191
            this.hide();
1192
            this.element.trigger('outsideClick.daterangepicker', this);
1193
        },
1194

1195
        showCalendars: function() {
1196
            this.container.addClass('show-calendar');
1197
            this.move();
1198
            this.element.trigger('showCalendar.daterangepicker', this);
1199
        },
1200

1201
        hideCalendars: function() {
1202
            this.container.removeClass('show-calendar');
1203
            this.element.trigger('hideCalendar.daterangepicker', this);
1204
        },
1205

1206
        clickRange: function(e) {
1207
            var label = e.target.getAttribute('data-range-key');
1208
            this.chosenLabel = label;
1209
            if (label == this.locale.customRangeLabel) {
1210
                this.showCalendars();
1211
            } else {
1212
                var dates = this.ranges[label];
1213
                this.startDate = dates[0];
1214
                this.endDate = dates[1];
1215

1216
                if (!this.timePicker) {
1217
                    this.startDate.startOf('day');
1218
                    this.endDate.endOf('day');
1219
                }
1220

1221
                if (!this.alwaysShowCalendars)
1222
                    this.hideCalendars();
1223
                this.clickApply();
1224
            }
1225
        },
1226

1227
        clickPrev: function(e) {
1228
            var cal = $(e.target).parents('.drp-calendar');
1229
            if (cal.hasClass('left')) {
1230
                this.leftCalendar.month.subtract(1, 'month');
1231
                if (this.linkedCalendars)
1232
                    this.rightCalendar.month.subtract(1, 'month');
1233
            } else {
1234
                this.rightCalendar.month.subtract(1, 'month');
1235
            }
1236
            this.updateCalendars();
1237
        },
1238

1239
        clickNext: function(e) {
1240
            var cal = $(e.target).parents('.drp-calendar');
1241
            if (cal.hasClass('left')) {
1242
                this.leftCalendar.month.add(1, 'month');
1243
            } else {
1244
                this.rightCalendar.month.add(1, 'month');
1245
                if (this.linkedCalendars)
1246
                    this.leftCalendar.month.add(1, 'month');
1247
            }
1248
            this.updateCalendars();
1249
        },
1250

1251
        hoverDate: function(e) {
1252

1253
            //ignore dates that can't be selected
1254
            if (!$(e.target).hasClass('available')) return;
1255

1256
            var title = $(e.target).attr('data-title');
1257
            var row = title.substr(1, 1);
1258
            var col = title.substr(3, 1);
1259
            var cal = $(e.target).parents('.drp-calendar');
1260
            var date = cal.hasClass('left') ? this.leftCalendar.calendar[row][col] : this.rightCalendar.calendar[row][col];
1261

1262
            //highlight the dates between the start date and the date being hovered as a potential end date
1263
            var leftCalendar = this.leftCalendar;
1264
            var rightCalendar = this.rightCalendar;
1265
            var startDate = this.startDate;
1266
            if (!this.endDate) {
1267
                this.container.find('.drp-calendar tbody td').each(function(index, el) {
1268

1269
                    //skip week numbers, only look at dates
1270
                    if ($(el).hasClass('week')) return;
1271

1272
                    var title = $(el).attr('data-title');
1273
                    var row = title.substr(1, 1);
1274
                    var col = title.substr(3, 1);
1275
                    var cal = $(el).parents('.drp-calendar');
1276
                    var dt = cal.hasClass('left') ? leftCalendar.calendar[row][col] : rightCalendar.calendar[row][col];
1277

1278
                    if ((dt.isAfter(startDate) && dt.isBefore(date)) || dt.isSame(date, 'day')) {
1279
                        $(el).addClass('in-range');
1280
                    } else {
1281
                        $(el).removeClass('in-range');
1282
                    }
1283

1284
                });
1285
            }
1286

1287
        },
1288

1289
        clickDate: function(e) {
1290

1291
            if (!$(e.target).hasClass('available')) return;
1292

1293
            var title = $(e.target).attr('data-title');
1294
            var row = title.substr(1, 1);
1295
            var col = title.substr(3, 1);
1296
            var cal = $(e.target).parents('.drp-calendar');
1297
            var date = cal.hasClass('left') ? this.leftCalendar.calendar[row][col] : this.rightCalendar.calendar[row][col];
1298

1299
            //
1300
            // this function needs to do a few things:
1301
            // * alternate between selecting a start and end date for the range,
1302
            // * if the time picker is enabled, apply the hour/minute/second from the select boxes to the clicked date
1303
            // * if autoapply is enabled, and an end date was chosen, apply the selection
1304
            // * if single date picker mode, and time picker isn't enabled, apply the selection immediately
1305
            // * if one of the inputs above the calendars was focused, cancel that manual input
1306
            //
1307

1308
            if (this.endDate || date.isBefore(this.startDate, 'day')) { //picking start
1309
                if (this.timePicker) {
1310
                    var hour = parseInt(this.container.find('.left .hourselect').val(), 10);
1311
                    if (!this.timePicker24Hour) {
1312
                        var ampm = this.container.find('.left .ampmselect').val();
1313
                        if (ampm === 'PM' && hour < 12)
1314
                            hour += 12;
1315
                        if (ampm === 'AM' && hour === 12)
1316
                            hour = 0;
1317
                    }
1318
                    var minute = parseInt(this.container.find('.left .minuteselect').val(), 10);
1319
                    if (isNaN(minute)) {
1320
                        minute = parseInt(this.container.find('.left .minuteselect option:last').val(), 10);
1321
                    }
1322
                    var second = this.timePickerSeconds ? parseInt(this.container.find('.left .secondselect').val(), 10) : 0;
1323
                    date = date.clone().hour(hour).minute(minute).second(second);
1324
                }
1325
                this.endDate = null;
1326
                this.setStartDate(date.clone());
1327
            } else if (!this.endDate && date.isBefore(this.startDate)) {
1328
                //special case: clicking the same date for start/end,
1329
                //but the time of the end date is before the start date
1330
                this.setEndDate(this.startDate.clone());
1331
            } else { // picking end
1332
                if (this.timePicker) {
1333
                    var hour = parseInt(this.container.find('.right .hourselect').val(), 10);
1334
                    if (!this.timePicker24Hour) {
1335
                        var ampm = this.container.find('.right .ampmselect').val();
1336
                        if (ampm === 'PM' && hour < 12)
1337
                            hour += 12;
1338
                        if (ampm === 'AM' && hour === 12)
1339
                            hour = 0;
1340
                    }
1341
                    var minute = parseInt(this.container.find('.right .minuteselect').val(), 10);
1342
                    if (isNaN(minute)) {
1343
                        minute = parseInt(this.container.find('.right .minuteselect option:last').val(), 10);
1344
                    }
1345
                    var second = this.timePickerSeconds ? parseInt(this.container.find('.right .secondselect').val(), 10) : 0;
1346
                    date = date.clone().hour(hour).minute(minute).second(second);
1347
                }
1348
                this.setEndDate(date.clone());
1349
                if (this.autoApply) {
1350
                  this.calculateChosenLabel();
1351
                  this.clickApply();
1352
                }
1353
            }
1354

1355
            if (this.singleDatePicker) {
1356
                this.setEndDate(this.startDate);
1357
                if (!this.timePicker && this.autoApply)
1358
                    this.clickApply();
1359
            }
1360

1361
            this.updateView();
1362

1363
            //This is to cancel the blur event handler if the mouse was in one of the inputs
1364
            e.stopPropagation();
1365

1366
        },
1367

1368
        calculateChosenLabel: function () {
1369
            var customRange = true;
1370
            var i = 0;
1371
            for (var range in this.ranges) {
1372
              if (this.timePicker) {
1373
                    var format = this.timePickerSeconds ? "YYYY-MM-DD HH:mm:ss" : "YYYY-MM-DD HH:mm";
1374
                    //ignore times when comparing dates if time picker seconds is not enabled
1375
                    if (this.startDate.format(format) == this.ranges[range][0].format(format) && this.endDate.format(format) == this.ranges[range][1].format(format)) {
1376
                        customRange = false;
1377
                        this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').attr('data-range-key');
1378
                        break;
1379
                    }
1380
                } else {
1381
                    //ignore times when comparing dates if time picker is not enabled
1382
                    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')) {
1383
                        customRange = false;
1384
                        this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').attr('data-range-key');
1385
                        break;
1386
                    }
1387
                }
1388
                i++;
1389
            }
1390
            if (customRange) {
1391
                if (this.showCustomRangeLabel) {
1392
                    this.chosenLabel = this.container.find('.ranges li:last').addClass('active').attr('data-range-key');
1393
                } else {
1394
                    this.chosenLabel = null;
1395
                }
1396
                this.showCalendars();
1397
            }
1398
        },
1399

1400
        clickApply: function(e) {
1401
            this.hide();
1402
            this.element.trigger('apply.daterangepicker', this);
1403
        },
1404

1405
        clickCancel: function(e) {
1406
            this.startDate = this.oldStartDate;
1407
            this.endDate = this.oldEndDate;
1408
            this.hide();
1409
            this.element.trigger('cancel.daterangepicker', this);
1410
        },
1411

1412
        monthOrYearChanged: function(e) {
1413
            var isLeft = $(e.target).closest('.drp-calendar').hasClass('left'),
1414
                leftOrRight = isLeft ? 'left' : 'right',
1415
                cal = this.container.find('.drp-calendar.'+leftOrRight);
1416

1417
            // Month must be Number for new moment versions
1418
            var month = parseInt(cal.find('.monthselect').val(), 10);
1419
            var year = cal.find('.yearselect').val();
1420

1421
            if (!isLeft) {
1422
                if (year < this.startDate.year() || (year == this.startDate.year() && month < this.startDate.month())) {
1423
                    month = this.startDate.month();
1424
                    year = this.startDate.year();
1425
                }
1426
            }
1427

1428
            if (this.minDate) {
1429
                if (year < this.minDate.year() || (year == this.minDate.year() && month < this.minDate.month())) {
1430
                    month = this.minDate.month();
1431
                    year = this.minDate.year();
1432
                }
1433
            }
1434

1435
            if (this.maxDate) {
1436
                if (year > this.maxDate.year() || (year == this.maxDate.year() && month > this.maxDate.month())) {
1437
                    month = this.maxDate.month();
1438
                    year = this.maxDate.year();
1439
                }
1440
            }
1441

1442
            if (isLeft) {
1443
                this.leftCalendar.month.month(month).year(year);
1444
                if (this.linkedCalendars)
1445
                    this.rightCalendar.month = this.leftCalendar.month.clone().add(1, 'month');
1446
            } else {
1447
                this.rightCalendar.month.month(month).year(year);
1448
                if (this.linkedCalendars)
1449
                    this.leftCalendar.month = this.rightCalendar.month.clone().subtract(1, 'month');
1450
            }
1451
            this.updateCalendars();
1452
        },
1453

1454
        timeChanged: function(e) {
1455

1456
            var cal = $(e.target).closest('.drp-calendar'),
1457
                isLeft = cal.hasClass('left');
1458

1459
            var hour = parseInt(cal.find('.hourselect').val(), 10);
1460
            var minute = parseInt(cal.find('.minuteselect').val(), 10);
1461
            if (isNaN(minute)) {
1462
                minute = parseInt(cal.find('.minuteselect option:last').val(), 10);
1463
            }
1464
            var second = this.timePickerSeconds ? parseInt(cal.find('.secondselect').val(), 10) : 0;
1465

1466
            if (!this.timePicker24Hour) {
1467
                var ampm = cal.find('.ampmselect').val();
1468
                if (ampm === 'PM' && hour < 12)
1469
                    hour += 12;
1470
                if (ampm === 'AM' && hour === 12)
1471
                    hour = 0;
1472
            }
1473

1474
            if (isLeft) {
1475
                var start = this.startDate.clone();
1476
                start.hour(hour);
1477
                start.minute(minute);
1478
                start.second(second);
1479
                this.setStartDate(start);
1480
                if (this.singleDatePicker) {
1481
                    this.endDate = this.startDate.clone();
1482
                } else if (this.endDate && this.endDate.format('YYYY-MM-DD') == start.format('YYYY-MM-DD') && this.endDate.isBefore(start)) {
1483
                    this.setEndDate(start.clone());
1484
                }
1485
            } else if (this.endDate) {
1486
                var end = this.endDate.clone();
1487
                end.hour(hour);
1488
                end.minute(minute);
1489
                end.second(second);
1490
                this.setEndDate(end);
1491
            }
1492

1493
            //update the calendars so all clickable dates reflect the new time component
1494
            this.updateCalendars();
1495

1496
            //update the form inputs above the calendars with the new time
1497
            this.updateFormInputs();
1498

1499
            //re-render the time pickers because changing one selection can affect what's enabled in another
1500
            this.renderTimePicker('left');
1501
            this.renderTimePicker('right');
1502

1503
        },
1504

1505
        elementChanged: function() {
1506
            if (!this.element.is('input')) return;
1507
            if (!this.element.val().length) return;
1508

1509
            var dateString = this.element.val().split(this.locale.separator),
1510
                start = null,
1511
                end = null;
1512

1513
            if (dateString.length === 2) {
1514
                start = moment(dateString[0], this.locale.format);
1515
                end = moment(dateString[1], this.locale.format);
1516
            }
1517

1518
            if (this.singleDatePicker || start === null || end === null) {
1519
                start = moment(this.element.val(), this.locale.format);
1520
                end = start;
1521
            }
1522

1523
            if (!start.isValid() || !end.isValid()) return;
1524

1525
            this.setStartDate(start);
1526
            this.setEndDate(end);
1527
            this.updateView();
1528
        },
1529

1530
        keydown: function(e) {
1531
            //hide on tab or enter
1532
            if ((e.keyCode === 9) || (e.keyCode === 13)) {
1533
                this.hide();
1534
            }
1535

1536
            //hide on esc and prevent propagation
1537
            if (e.keyCode === 27) {
1538
                e.preventDefault();
1539
                e.stopPropagation();
1540

1541
                this.hide();
1542
            }
1543
        },
1544

1545
        updateElement: function() {
1546
            if (this.element.is('input') && this.autoUpdateInput) {
1547
                var newValue = this.startDate.format(this.locale.format);
1548
                if (!this.singleDatePicker) {
1549
                    newValue += this.locale.separator + this.endDate.format(this.locale.format);
1550
                }
1551
                if (newValue !== this.element.val()) {
1552
                    this.element.val(newValue).trigger('change');
1553
                }
1554
            }
1555
        },
1556

1557
        remove: function() {
1558
            this.container.remove();
1559
            this.element.off('.daterangepicker');
1560
            this.element.removeData();
1561
        }
1562

1563
    };
1564

1565
    $.fn.daterangepicker = function(options, callback) {
1566
        var implementOptions = $.extend(true, {}, $.fn.daterangepicker.defaultOptions, options);
1567
        this.each(function() {
1568
            var el = $(this);
1569
            if (el.data('daterangepicker'))
1570
                el.data('daterangepicker').remove();
1571
            el.data('daterangepicker', new DateRangePicker(el, implementOptions, callback));
1572
        });
1573
        return this;
1574
    };
1575

1576
    return DateRangePicker;
1577

1578
}));
1579

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.