Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

1494 rader
61 KiB

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