PageSourceSearch

https://portalabonado.alfaro.es/js/bootstrap/daterangepicker.js?1761290686

js alfaro.es collected 2026-10-02 12:07:30 UTC 69,150 bytes, 1,618 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.