daterangepicker.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /**
  2. * @version: 1.3.4
  3. * @author: Dan Grossman http://www.dangrossman.info/
  4. * @date: 2014-03-19
  5. * @copyright: Copyright (c) 2012-2014 Dan Grossman. All rights reserved.
  6. * @license: Licensed under Apache License v2.0. See http://www.apache.org/licenses/LICENSE-2.0
  7. * @website: http://www.improvely.com/
  8. */
  9. !function ($, moment) {
  10. var DateRangePicker = function (element, options, cb) {
  11. // by default, the daterangepicker element is placed at the bottom of HTML body
  12. this.parentEl = 'body';
  13. //element that triggered the date range picker
  14. this.element = $(element);
  15. //create the picker HTML object
  16. var DRPTemplate = '<div class="daterangepicker dropdown-menu">' +
  17. '<div class="calendar left"></div>' +
  18. '<div class="calendar right"></div>' +
  19. '<div class="ranges">' +
  20. '<div class="range_inputs">' +
  21. '<div class="daterangepicker_start_input">' +
  22. '<label for="daterangepicker_start"></label>' +
  23. '<input class="input-mini" type="text" name="daterangepicker_start" value="" disabled="disabled" />' +
  24. '</div>' +
  25. '<div class="daterangepicker_end_input">' +
  26. '<label for="daterangepicker_end"></label>' +
  27. '<input class="input-mini" type="text" name="daterangepicker_end" value="" disabled="disabled" />' +
  28. '</div>' +
  29. '<button class="applyBtn" disabled="disabled"></button>&nbsp;' +
  30. '<button class="cancelBtn"></button>' +
  31. '</div>' +
  32. '</div>' +
  33. '</div>';
  34. //custom options
  35. if (typeof options !== 'object' || options === null)
  36. options = {};
  37. this.parentEl = (typeof options === 'object' && options.parentEl && $(options.parentEl).length) || $(this.parentEl);
  38. this.container = $(DRPTemplate).appendTo(this.parentEl);
  39. this.setOptions(options, cb);
  40. //apply CSS classes and labels to buttons
  41. var c = this.container;
  42. $.each(this.buttonClasses, function (idx, val) {
  43. c.find('button').addClass(val);
  44. });
  45. this.container.find('.daterangepicker_start_input label').html(this.locale.fromLabel);
  46. this.container.find('.daterangepicker_end_input label').html(this.locale.toLabel);
  47. if (this.applyClass.length)
  48. this.container.find('.applyBtn').addClass(this.applyClass);
  49. if (this.cancelClass.length)
  50. this.container.find('.cancelBtn').addClass(this.cancelClass);
  51. this.container.find('.applyBtn').html(this.locale.applyLabel);
  52. this.container.find('.cancelBtn').html(this.locale.cancelLabel);
  53. //event listeners
  54. this.container.find('.calendar')
  55. .on('click.daterangepicker', '.prev', $.proxy(this.clickPrev, this))
  56. .on('click.daterangepicker', '.next', $.proxy(this.clickNext, this))
  57. .on('click.daterangepicker', 'td.available', $.proxy(this.clickDate, this))
  58. .on('mouseenter.daterangepicker', 'td.available', $.proxy(this.enterDate, this))
  59. .on('mouseleave.daterangepicker', 'td.available', $.proxy(this.updateFormInputs, this))
  60. .on('change.daterangepicker', 'select.yearselect', $.proxy(this.updateMonthYear, this))
  61. .on('change.daterangepicker', 'select.monthselect', $.proxy(this.updateMonthYear, this))
  62. .on('change.daterangepicker', 'select.hourselect,select.minuteselect,select.ampmselect', $.proxy(this.updateTime, this));
  63. this.container.find('.ranges')
  64. .on('click.daterangepicker', 'button.applyBtn', $.proxy(this.clickApply, this))
  65. .on('click.daterangepicker', 'button.cancelBtn', $.proxy(this.clickCancel, this))
  66. .on('click.daterangepicker', '.daterangepicker_start_input,.daterangepicker_end_input', $.proxy(this.showCalendars, this))
  67. .on('click.daterangepicker', 'li', $.proxy(this.clickRange, this))
  68. .on('mouseenter.daterangepicker', 'li', $.proxy(this.enterRange, this))
  69. .on('mouseleave.daterangepicker', 'li', $.proxy(this.updateFormInputs, this));
  70. if (this.element.is('input')) {
  71. this.element.on({
  72. 'click.daterangepicker': $.proxy(this.show, this),
  73. 'focus.daterangepicker': $.proxy(this.show, this),
  74. 'keyup.daterangepicker': $.proxy(this.updateFromControl, this)
  75. });
  76. } else {
  77. this.element.on('click.daterangepicker', $.proxy(this.toggle, this));
  78. }
  79. };
  80. DateRangePicker.prototype = {
  81. constructor: DateRangePicker,
  82. setOptions: function(options, callback) {
  83. this.startDate = moment().startOf('day');
  84. this.endDate = moment().endOf('day');
  85. this.minDate = false;
  86. this.maxDate = false;
  87. this.dateLimit = false;
  88. this.showDropdowns = false;
  89. this.showWeekNumbers = false;
  90. this.timePicker = false;
  91. this.timePickerIncrement = 30;
  92. this.timePicker12Hour = true;
  93. this.singleDatePicker = false;
  94. this.ranges = {};
  95. this.opens = 'right';
  96. if (this.element.hasClass('pull-right'))
  97. this.opens = 'left';
  98. this.buttonClasses = ['btn', 'btn-small'];
  99. this.applyClass = 'btn-success';
  100. this.cancelClass = 'btn-default';
  101. this.format = 'MM/DD/YYYY';
  102. this.separator = ' - ';
  103. this.locale = {
  104. applyLabel: 'Apply',
  105. cancelLabel: 'Cancel',
  106. fromLabel: 'From',
  107. toLabel: 'To',
  108. weekLabel: 'W',
  109. customRangeLabel: 'Custom Range',
  110. daysOfWeek: moment()._lang._weekdaysMin.slice(),
  111. monthNames: moment()._lang._monthsShort.slice(),
  112. firstDay: 0
  113. };
  114. this.cb = function () { };
  115. if (typeof options.format === 'string')
  116. this.format = options.format;
  117. if (typeof options.separator === 'string')
  118. this.separator = options.separator;
  119. if (typeof options.startDate === 'string')
  120. this.startDate = moment(options.startDate, this.format);
  121. if (typeof options.endDate === 'string')
  122. this.endDate = moment(options.endDate, this.format);
  123. if (typeof options.minDate === 'string')
  124. this.minDate = moment(options.minDate, this.format);
  125. if (typeof options.maxDate === 'string')
  126. this.maxDate = moment(options.maxDate, this.format);
  127. if (typeof options.startDate === 'object')
  128. this.startDate = moment(options.startDate);
  129. if (typeof options.endDate === 'object')
  130. this.endDate = moment(options.endDate);
  131. if (typeof options.minDate === 'object')
  132. this.minDate = moment(options.minDate);
  133. if (typeof options.maxDate === 'object')
  134. this.maxDate = moment(options.maxDate);
  135. if (typeof options.applyClass === 'string')
  136. this.applyClass = options.applyClass;
  137. if (typeof options.cancelClass === 'string')
  138. this.cancelClass = options.cancelClass;
  139. if (typeof options.dateLimit === 'object')
  140. this.dateLimit = options.dateLimit;
  141. // update day names order to firstDay
  142. if (typeof options.locale === 'object') {
  143. if (typeof options.locale.daysOfWeek === 'object') {
  144. // Create a copy of daysOfWeek to avoid modification of original
  145. // options object for reusability in multiple daterangepicker instances
  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. var iterator = options.locale.firstDay;
  154. while (iterator > 0) {
  155. this.locale.daysOfWeek.push(this.locale.daysOfWeek.shift());
  156. iterator--;
  157. }
  158. }
  159. if (typeof options.locale.applyLabel === 'string') {
  160. this.locale.applyLabel = options.locale.applyLabel;
  161. }
  162. if (typeof options.locale.cancelLabel === 'string') {
  163. this.locale.cancelLabel = options.locale.cancelLabel;
  164. }
  165. if (typeof options.locale.fromLabel === 'string') {
  166. this.locale.fromLabel = options.locale.fromLabel;
  167. }
  168. if (typeof options.locale.toLabel === 'string') {
  169. this.locale.toLabel = options.locale.toLabel;
  170. }
  171. if (typeof options.locale.weekLabel === 'string') {
  172. this.locale.weekLabel = options.locale.weekLabel;
  173. }
  174. if (typeof options.locale.customRangeLabel === 'string') {
  175. this.locale.customRangeLabel = options.locale.customRangeLabel;
  176. }
  177. }
  178. if (typeof options.opens === 'string')
  179. this.opens = options.opens;
  180. if (typeof options.showWeekNumbers === 'boolean') {
  181. this.showWeekNumbers = options.showWeekNumbers;
  182. }
  183. if (typeof options.buttonClasses === 'string') {
  184. this.buttonClasses = [options.buttonClasses];
  185. }
  186. if (typeof options.buttonClasses === 'object') {
  187. this.buttonClasses = options.buttonClasses;
  188. }
  189. if (typeof options.showDropdowns === 'boolean') {
  190. this.showDropdowns = options.showDropdowns;
  191. }
  192. if (typeof options.singleDatePicker === 'boolean') {
  193. this.singleDatePicker = options.singleDatePicker;
  194. }
  195. if (typeof options.timePicker === 'boolean') {
  196. this.timePicker = options.timePicker;
  197. }
  198. if (typeof options.timePickerIncrement === 'number') {
  199. this.timePickerIncrement = options.timePickerIncrement;
  200. }
  201. if (typeof options.timePicker12Hour === 'boolean') {
  202. this.timePicker12Hour = options.timePicker12Hour;
  203. }
  204. var start, end, range;
  205. //if no start/end dates set, check if an input element contains initial values
  206. if (typeof options.startDate === 'undefined' && typeof options.endDate === 'undefined') {
  207. if ($(this.element).is('input[type=text]')) {
  208. var val = $(this.element).val();
  209. var split = val.split(this.separator);
  210. start = end = null;
  211. if (split.length == 2) {
  212. start = moment(split[0], this.format);
  213. end = moment(split[1], this.format);
  214. } else if (this.singleDatePicker) {
  215. start = moment(val, this.format);
  216. end = moment(val, this.format);
  217. }
  218. if (start !== null && end !== null) {
  219. this.startDate = start;
  220. this.endDate = end;
  221. }
  222. }
  223. }
  224. if (typeof options.ranges === 'object') {
  225. for (range in options.ranges) {
  226. start = moment(options.ranges[range][0]);
  227. end = moment(options.ranges[range][1]);
  228. // If we have a min/max date set, bound this range
  229. // to it, but only if it would otherwise fall
  230. // outside of the min/max.
  231. if (this.minDate && start.isBefore(this.minDate))
  232. start = moment(this.minDate);
  233. if (this.maxDate && end.isAfter(this.maxDate))
  234. end = moment(this.maxDate);
  235. // If the end of the range is before the minimum (if min is set) OR
  236. // the start of the range is after the max (also if set) don't display this
  237. // range option.
  238. if ((this.minDate && end.isBefore(this.minDate)) || (this.maxDate && start.isAfter(this.maxDate))) {
  239. continue;
  240. }
  241. this.ranges[range] = [start, end];
  242. }
  243. var list = '<ul>';
  244. for (range in this.ranges) {
  245. list += '<li>' + range + '</li>';
  246. }
  247. list += '<li>' + this.locale.customRangeLabel + '</li>';
  248. list += '</ul>';
  249. this.container.find('.ranges ul').remove();
  250. this.container.find('.ranges').prepend(list);
  251. }
  252. if (typeof callback === 'function') {
  253. this.cb = callback;
  254. }
  255. if (!this.timePicker) {
  256. this.startDate = this.startDate.startOf('day');
  257. this.endDate = this.endDate.endOf('day');
  258. }
  259. if (this.singleDatePicker) {
  260. this.opens = 'right';
  261. this.container.find('.calendar.right').show();
  262. this.container.find('.calendar.left').hide();
  263. this.container.find('.ranges').hide();
  264. if (!this.container.find('.calendar.right').hasClass('single'))
  265. this.container.find('.calendar.right').addClass('single');
  266. } else {
  267. this.container.find('.calendar.right').removeClass('single');
  268. this.container.find('.ranges').show();
  269. }
  270. this.oldStartDate = this.startDate.clone();
  271. this.oldEndDate = this.endDate.clone();
  272. this.oldChosenLabel = this.chosenLabel;
  273. this.leftCalendar = {
  274. month: moment([this.startDate.year(), this.startDate.month(), 1, this.startDate.hour(), this.startDate.minute()]),
  275. calendar: []
  276. };
  277. this.rightCalendar = {
  278. month: moment([this.endDate.year(), this.endDate.month(), 1, this.endDate.hour(), this.endDate.minute()]),
  279. calendar: []
  280. };
  281. if (this.opens == 'right') {
  282. //swap calendar positions
  283. var left = this.container.find('.calendar.left');
  284. var right = this.container.find('.calendar.right');
  285. left.removeClass('left').addClass('right');
  286. right.removeClass('right').addClass('left');
  287. }
  288. if (typeof options.ranges === 'undefined' && !this.singleDatePicker) {
  289. this.container.addClass('show-calendar');
  290. }
  291. this.container.addClass('opens' + this.opens);
  292. this.updateView();
  293. this.updateCalendars();
  294. },
  295. setStartDate: function(startDate) {
  296. if (typeof startDate === 'string')
  297. this.startDate = moment(startDate, this.format);
  298. if (typeof startDate === 'object')
  299. this.startDate = moment(startDate);
  300. if (!this.timePicker)
  301. this.startDate = this.startDate.startOf('day');
  302. this.oldStartDate = this.startDate.clone();
  303. this.updateView();
  304. this.updateCalendars();
  305. },
  306. setEndDate: function(endDate) {
  307. if (typeof endDate === 'string')
  308. this.endDate = moment(endDate, this.format);
  309. if (typeof endDate === 'object')
  310. this.endDate = moment(endDate);
  311. if (!this.timePicker)
  312. this.endDate = this.endDate.endOf('day');
  313. this.oldEndDate = this.endDate.clone();
  314. this.updateView();
  315. this.updateCalendars();
  316. },
  317. updateView: function () {
  318. this.leftCalendar.month.month(this.startDate.month()).year(this.startDate.year());
  319. this.rightCalendar.month.month(this.endDate.month()).year(this.endDate.year());
  320. this.updateFormInputs();
  321. },
  322. updateFormInputs: function () {
  323. this.container.find('input[name=daterangepicker_start]').val(this.startDate.format(this.format));
  324. this.container.find('input[name=daterangepicker_end]').val(this.endDate.format(this.format));
  325. if (this.startDate.isSame(this.endDate) || this.startDate.isBefore(this.endDate)) {
  326. this.container.find('button.applyBtn').removeAttr('disabled');
  327. } else {
  328. this.container.find('button.applyBtn').attr('disabled', 'disabled');
  329. }
  330. },
  331. updateFromControl: function () {
  332. if (!this.element.is('input')) return;
  333. if (!this.element.val().length) return;
  334. var dateString = this.element.val().split(this.separator);
  335. var start = moment(dateString[0], this.format);
  336. var end = moment(dateString[1], this.format);
  337. if (this.singleDatePicker) {
  338. start = moment(this.element.val(), this.format);
  339. end = start;
  340. }
  341. if (end.isBefore(start)) return;
  342. this.oldStartDate = this.startDate.clone();
  343. this.oldEndDate = this.endDate.clone();
  344. this.startDate = start;
  345. this.endDate = end;
  346. if (!this.startDate.isSame(this.oldStartDate) || !this.endDate.isSame(this.oldEndDate))
  347. this.notify();
  348. this.updateCalendars();
  349. },
  350. notify: function () {
  351. this.updateView();
  352. this.cb(this.startDate, this.endDate, this.chosenLabel);
  353. },
  354. move: function () {
  355. var parentOffset = { top: 0, left: 0 };
  356. if (!this.parentEl.is('body')) {
  357. parentOffset = {
  358. top: this.parentEl.offset().top - this.parentEl.scrollTop(),
  359. left: this.parentEl.offset().left - this.parentEl.scrollLeft()
  360. };
  361. }
  362. if (this.opens == 'left') {
  363. this.container.css({
  364. top: this.element.offset().top + this.element.outerHeight() - parentOffset.top,
  365. right: $(window).width() - this.element.offset().left - this.element.outerWidth() - parentOffset.left,
  366. left: 'auto'
  367. });
  368. if (this.container.offset().left < 0) {
  369. this.container.css({
  370. right: 'auto',
  371. left: 9
  372. });
  373. }
  374. } else {
  375. this.container.css({
  376. top: this.element.offset().top + this.element.outerHeight() - parentOffset.top,
  377. left: this.element.offset().left - parentOffset.left,
  378. right: 'auto'
  379. });
  380. if (this.container.offset().left + this.container.outerWidth() > $(window).width()) {
  381. this.container.css({
  382. left: 'auto',
  383. right: 0
  384. });
  385. }
  386. }
  387. },
  388. toggle: function (e) {
  389. if (this.element.hasClass('active')) {
  390. this.hide();
  391. } else {
  392. this.show();
  393. }
  394. },
  395. show: function (e) {
  396. this.element.addClass('active');
  397. this.container.show();
  398. this.move();
  399. $(document).on('click.daterangepicker', $.proxy(this.outsideClick, this));
  400. // also explicitly play nice with Bootstrap dropdowns, which stopPropagation when clicking them
  401. $(document).on('click.daterangepicker', '[data-toggle=dropdown]', $.proxy(this.outsideClick, this));
  402. this.element.trigger('show.daterangepicker', this);
  403. },
  404. outsideClick: function (e) {
  405. var target = $(e.target);
  406. // if the page is clicked anywhere except within the daterangerpicker/button
  407. // itself then call this.hide()
  408. if (
  409. target.closest(this.element).length ||
  410. target.closest(this.container).length ||
  411. target.closest('.calendar-date').length
  412. ) return;
  413. this.hide();
  414. },
  415. hide: function (e) {
  416. this.element.removeClass('active');
  417. this.container.hide();
  418. if (!this.startDate.isSame(this.oldStartDate) || !this.endDate.isSame(this.oldEndDate))
  419. this.notify();
  420. this.oldStartDate = this.startDate.clone();
  421. this.oldEndDate = this.endDate.clone();
  422. $(document).off('click.daterangepicker', this.outsideClick);
  423. this.element.trigger('hide.daterangepicker', this);
  424. },
  425. enterRange: function (e) {
  426. // mouse pointer has entered a range label
  427. var label = e.target.innerHTML;
  428. if (label == this.locale.customRangeLabel) {
  429. this.updateView();
  430. } else {
  431. var dates = this.ranges[label];
  432. this.container.find('input[name=daterangepicker_start]').val(dates[0].format(this.format));
  433. this.container.find('input[name=daterangepicker_end]').val(dates[1].format(this.format));
  434. }
  435. },
  436. showCalendars: function() {
  437. this.container.addClass('show-calendar');
  438. this.move();
  439. },
  440. hideCalendars: function() {
  441. this.container.removeClass('show-calendar');
  442. },
  443. updateInputText: function() {
  444. if (this.element.is('input') && !this.singleDatePicker) {
  445. this.element.val(this.startDate.format(this.format) + this.separator + this.endDate.format(this.format));
  446. } else if (this.element.is('input')) {
  447. this.element.val(this.startDate.format(this.format));
  448. }
  449. },
  450. clickRange: function (e) {
  451. var label = e.target.innerHTML;
  452. this.chosenLabel = label;
  453. if (label == this.locale.customRangeLabel) {
  454. this.showCalendars();
  455. } else {
  456. var dates = this.ranges[label];
  457. this.startDate = dates[0];
  458. this.endDate = dates[1];
  459. if (!this.timePicker) {
  460. this.startDate.startOf('day');
  461. this.endDate.endOf('day');
  462. }
  463. this.leftCalendar.month.month(this.startDate.month()).year(this.startDate.year()).hour(this.startDate.hour()).minute(this.startDate.minute());
  464. this.rightCalendar.month.month(this.endDate.month()).year(this.endDate.year()).hour(this.endDate.hour()).minute(this.endDate.minute());
  465. this.updateCalendars();
  466. this.updateInputText();
  467. this.hideCalendars();
  468. this.hide();
  469. this.element.trigger('apply.daterangepicker', this);
  470. }
  471. },
  472. clickPrev: function (e) {
  473. var cal = $(e.target).parents('.calendar');
  474. if (cal.hasClass('left')) {
  475. this.leftCalendar.month.subtract('month', 1);
  476. } else {
  477. this.rightCalendar.month.subtract('month', 1);
  478. }
  479. this.updateCalendars();
  480. },
  481. clickNext: function (e) {
  482. var cal = $(e.target).parents('.calendar');
  483. if (cal.hasClass('left')) {
  484. this.leftCalendar.month.add('month', 1);
  485. } else {
  486. this.rightCalendar.month.add('month', 1);
  487. }
  488. this.updateCalendars();
  489. },
  490. enterDate: function (e) {
  491. var title = $(e.target).attr('data-title');
  492. var row = title.substr(1, 1);
  493. var col = title.substr(3, 1);
  494. var cal = $(e.target).parents('.calendar');
  495. if (cal.hasClass('left')) {
  496. this.container.find('input[name=daterangepicker_start]').val(this.leftCalendar.calendar[row][col].format(this.format));
  497. } else {
  498. this.container.find('input[name=daterangepicker_end]').val(this.rightCalendar.calendar[row][col].format(this.format));
  499. }
  500. },
  501. clickDate: function (e) {
  502. var title = $(e.target).attr('data-title');
  503. var row = title.substr(1, 1);
  504. var col = title.substr(3, 1);
  505. var cal = $(e.target).parents('.calendar');
  506. var startDate, endDate;
  507. if (cal.hasClass('left')) {
  508. startDate = this.leftCalendar.calendar[row][col];
  509. endDate = this.endDate;
  510. if (typeof this.dateLimit === 'object') {
  511. var maxDate = moment(startDate).add(this.dateLimit).startOf('day');
  512. if (endDate.isAfter(maxDate)) {
  513. endDate = maxDate;
  514. }
  515. }
  516. } else {
  517. startDate = this.startDate;
  518. endDate = this.rightCalendar.calendar[row][col];
  519. if (typeof this.dateLimit === 'object') {
  520. var minDate = moment(endDate).subtract(this.dateLimit).startOf('day');
  521. if (startDate.isBefore(minDate)) {
  522. startDate = minDate;
  523. }
  524. }
  525. }
  526. if (this.singleDatePicker && cal.hasClass('left')) {
  527. endDate = startDate.clone();
  528. } else if (this.singleDatePicker && cal.hasClass('right')) {
  529. startDate = endDate.clone();
  530. }
  531. cal.find('td').removeClass('active');
  532. if (startDate.isSame(endDate) || startDate.isBefore(endDate)) {
  533. $(e.target).addClass('active');
  534. this.startDate = startDate;
  535. this.endDate = endDate;
  536. this.chosenLabel = this.locale.customRangeLabel;
  537. } else if (startDate.isAfter(endDate)) {
  538. $(e.target).addClass('active');
  539. var difference = this.endDate.diff(this.startDate);
  540. this.startDate = startDate;
  541. this.endDate = moment(startDate).add('ms', difference);
  542. this.chosenLabel = this.locale.customRangeLabel;
  543. }
  544. this.leftCalendar.month.month(this.startDate.month()).year(this.startDate.year());
  545. this.rightCalendar.month.month(this.endDate.month()).year(this.endDate.year());
  546. this.updateCalendars();
  547. endDate.endOf('day');
  548. if (this.singleDatePicker)
  549. this.clickApply();
  550. },
  551. clickApply: function (e) {
  552. this.updateInputText();
  553. this.hide();
  554. this.element.trigger('apply.daterangepicker', this);
  555. },
  556. clickCancel: function (e) {
  557. this.startDate = this.oldStartDate;
  558. this.endDate = this.oldEndDate;
  559. this.chosenLabel = this.oldChosenLabel;
  560. this.updateView();
  561. this.updateCalendars();
  562. this.hide();
  563. this.element.trigger('cancel.daterangepicker', this);
  564. },
  565. updateMonthYear: function (e) {
  566. var isLeft = $(e.target).closest('.calendar').hasClass('left'),
  567. leftOrRight = isLeft ? 'left' : 'right',
  568. cal = this.container.find('.calendar.'+leftOrRight);
  569. // Month must be Number for new moment versions
  570. var month = parseInt(cal.find('.monthselect').val(), 10);
  571. var year = cal.find('.yearselect').val();
  572. this[leftOrRight+'Calendar'].month.month(month).year(year);
  573. this.updateCalendars();
  574. },
  575. updateTime: function(e) {
  576. var isLeft = $(e.target).closest('.calendar').hasClass('left'),
  577. leftOrRight = isLeft ? 'left' : 'right',
  578. cal = this.container.find('.calendar.'+leftOrRight);
  579. var hour = parseInt(cal.find('.hourselect').val(), 10);
  580. var minute = parseInt(cal.find('.minuteselect').val(), 10);
  581. if (this.timePicker12Hour) {
  582. var ampm = cal.find('.ampmselect').val();
  583. if (ampm === 'PM' && hour < 12)
  584. hour += 12;
  585. if (ampm === 'AM' && hour === 12)
  586. hour = 0;
  587. }
  588. if (isLeft) {
  589. var start = this.startDate.clone();
  590. start.hour(hour);
  591. start.minute(minute);
  592. this.startDate = start;
  593. this.leftCalendar.month.hour(hour).minute(minute);
  594. } else {
  595. var end = this.endDate.clone();
  596. end.hour(hour);
  597. end.minute(minute);
  598. this.endDate = end;
  599. this.rightCalendar.month.hour(hour).minute(minute);
  600. }
  601. this.updateCalendars();
  602. },
  603. updateCalendars: function () {
  604. this.leftCalendar.calendar = this.buildCalendar(this.leftCalendar.month.month(), this.leftCalendar.month.year(), this.leftCalendar.month.hour(), this.leftCalendar.month.minute(), 'left');
  605. this.rightCalendar.calendar = this.buildCalendar(this.rightCalendar.month.month(), this.rightCalendar.month.year(), this.rightCalendar.month.hour(), this.rightCalendar.month.minute(), 'right');
  606. this.container.find('.calendar.left').html(this.renderCalendar(this.leftCalendar.calendar, this.startDate, this.minDate, this.maxDate));
  607. this.container.find('.calendar.right').html(this.renderCalendar(this.rightCalendar.calendar, this.endDate, this.startDate, this.maxDate));
  608. this.container.find('.ranges li').removeClass('active');
  609. var customRange = true;
  610. var i = 0;
  611. for (var range in this.ranges) {
  612. if (this.timePicker) {
  613. if (this.startDate.isSame(this.ranges[range][0]) && this.endDate.isSame(this.ranges[range][1])) {
  614. customRange = false;
  615. this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')')
  616. .addClass('active').html();
  617. }
  618. } else {
  619. //ignore times when comparing dates if time picker is not enabled
  620. 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')) {
  621. customRange = false;
  622. this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')')
  623. .addClass('active').html();
  624. }
  625. }
  626. i++;
  627. }
  628. if (customRange) {
  629. this.chosenLabel = this.container.find('.ranges li:last')
  630. .addClass('active').html();
  631. }
  632. },
  633. buildCalendar: function (month, year, hour, minute, side) {
  634. var firstDay = moment([year, month, 1]);
  635. var lastMonth = moment(firstDay).subtract('month', 1).month();
  636. var lastYear = moment(firstDay).subtract('month', 1).year();
  637. var daysInLastMonth = moment([lastYear, lastMonth]).daysInMonth();
  638. var dayOfWeek = firstDay.day();
  639. var i;
  640. //initialize a 6 rows x 7 columns array for the calendar
  641. var calendar = [];
  642. for (i = 0; i < 6; i++) {
  643. calendar[i] = [];
  644. }
  645. //populate the calendar with date objects
  646. var startDay = daysInLastMonth - dayOfWeek + this.locale.firstDay + 1;
  647. if (startDay > daysInLastMonth)
  648. startDay -= 7;
  649. if (dayOfWeek == this.locale.firstDay)
  650. startDay = daysInLastMonth - 6;
  651. var curDate = moment([lastYear, lastMonth, startDay, 12, minute]);
  652. var col, row;
  653. for (i = 0, col = 0, row = 0; i < 42; i++, col++, curDate = moment(curDate).add('hour', 24)) {
  654. if (i > 0 && col % 7 === 0) {
  655. col = 0;
  656. row++;
  657. }
  658. calendar[row][col] = curDate.clone().hour(hour);
  659. curDate.hour(12);
  660. }
  661. return calendar;
  662. },
  663. renderDropdowns: function (selected, minDate, maxDate) {
  664. var currentMonth = selected.month();
  665. var monthHtml = '<select class="monthselect">';
  666. var inMinYear = false;
  667. var inMaxYear = false;
  668. for (var m = 0; m < 12; m++) {
  669. if ((!inMinYear || m >= minDate.month()) && (!inMaxYear || m <= maxDate.month())) {
  670. monthHtml += "<option value='" + m + "'" +
  671. (m === currentMonth ? " selected='selected'" : "") +
  672. ">" + this.locale.monthNames[m] + "</option>";
  673. }
  674. }
  675. monthHtml += "</select>";
  676. var currentYear = selected.year();
  677. var maxYear = (maxDate && maxDate.year()) || (currentYear + 5);
  678. var minYear = (minDate && minDate.year()) || (currentYear - 50);
  679. var yearHtml = '<select class="yearselect">';
  680. for (var y = minYear; y <= maxYear; y++) {
  681. yearHtml += '<option value="' + y + '"' +
  682. (y === currentYear ? ' selected="selected"' : '') +
  683. '>' + y + '</option>';
  684. }
  685. yearHtml += '</select>';
  686. return monthHtml + yearHtml;
  687. },
  688. renderCalendar: function (calendar, selected, minDate, maxDate) {
  689. var html = '<div class="calendar-date">';
  690. html += '<table class="table-condensed">';
  691. html += '<thead>';
  692. html += '<tr>';
  693. // add empty cell for week number
  694. if (this.showWeekNumbers)
  695. html += '<th></th>';
  696. if (!minDate || minDate.isBefore(calendar[1][1])) {
  697. html += '<th class="prev available"><i class="fa fa-arrow-left icon-arrow-left glyphicon glyphicon-arrow-left"></i></th>';
  698. } else {
  699. html += '<th></th>';
  700. }
  701. var dateHtml = this.locale.monthNames[calendar[1][1].month()] + calendar[1][1].format(" YYYY");
  702. if (this.showDropdowns) {
  703. dateHtml = this.renderDropdowns(calendar[1][1], minDate, maxDate);
  704. }
  705. html += '<th colspan="5" class="month">' + dateHtml + '</th>';
  706. if (!maxDate || maxDate.isAfter(calendar[1][1])) {
  707. html += '<th class="next available"><i class="fa fa-arrow-right icon-arrow-right glyphicon glyphicon-arrow-right"></i></th>';
  708. } else {
  709. html += '<th></th>';
  710. }
  711. html += '</tr>';
  712. html += '<tr>';
  713. // add week number label
  714. if (this.showWeekNumbers)
  715. html += '<th class="week">' + this.locale.weekLabel + '</th>';
  716. $.each(this.locale.daysOfWeek, function (index, dayOfWeek) {
  717. html += '<th>' + dayOfWeek + '</th>';
  718. });
  719. html += '</tr>';
  720. html += '</thead>';
  721. html += '<tbody>';
  722. for (var row = 0; row < 6; row++) {
  723. html += '<tr>';
  724. // add week number
  725. if (this.showWeekNumbers)
  726. html += '<td class="week">' + calendar[row][0].week() + '</td>';
  727. for (var col = 0; col < 7; col++) {
  728. var cname = 'available ';
  729. cname += (calendar[row][col].month() == calendar[1][1].month()) ? '' : 'off';
  730. if ((minDate && calendar[row][col].isBefore(minDate)) || (maxDate && calendar[row][col].isAfter(maxDate))) {
  731. cname = ' off disabled ';
  732. } else if (calendar[row][col].format('YYYY-MM-DD') == selected.format('YYYY-MM-DD')) {
  733. cname += ' active ';
  734. if (calendar[row][col].format('YYYY-MM-DD') == this.startDate.format('YYYY-MM-DD')) {
  735. cname += ' start-date ';
  736. }
  737. if (calendar[row][col].format('YYYY-MM-DD') == this.endDate.format('YYYY-MM-DD')) {
  738. cname += ' end-date ';
  739. }
  740. } else if (calendar[row][col] >= this.startDate && calendar[row][col] <= this.endDate) {
  741. cname += ' in-range ';
  742. if (calendar[row][col].isSame(this.startDate)) { cname += ' start-date '; }
  743. if (calendar[row][col].isSame(this.endDate)) { cname += ' end-date '; }
  744. }
  745. var title = 'r' + row + 'c' + col;
  746. html += '<td class="' + cname.replace(/\s+/g, ' ').replace(/^\s?(.*?)\s?$/, '$1') + '" data-title="' + title + '">' + calendar[row][col].date() + '</td>';
  747. }
  748. html += '</tr>';
  749. }
  750. html += '</tbody>';
  751. html += '</table>';
  752. html += '</div>';
  753. var i;
  754. if (this.timePicker) {
  755. html += '<div class="calendar-time">';
  756. html += '<select class="hourselect">';
  757. var start = 0;
  758. var end = 23;
  759. var selected_hour = selected.hour();
  760. if (this.timePicker12Hour) {
  761. start = 1;
  762. end = 12;
  763. if (selected_hour >= 12)
  764. selected_hour -= 12;
  765. if (selected_hour === 0)
  766. selected_hour = 12;
  767. }
  768. for (i = start; i <= end; i++) {
  769. if (i == selected_hour) {
  770. html += '<option value="' + i + '" selected="selected">' + i + '</option>';
  771. } else {
  772. html += '<option value="' + i + '">' + i + '</option>';
  773. }
  774. }
  775. html += '</select> : ';
  776. html += '<select class="minuteselect">';
  777. for (i = 0; i < 60; i += this.timePickerIncrement) {
  778. var num = i;
  779. if (num < 10)
  780. num = '0' + num;
  781. if (i == selected.minute()) {
  782. html += '<option value="' + i + '" selected="selected">' + num + '</option>';
  783. } else {
  784. html += '<option value="' + i + '">' + num + '</option>';
  785. }
  786. }
  787. html += '</select> ';
  788. if (this.timePicker12Hour) {
  789. html += '<select class="ampmselect">';
  790. if (selected.hour() >= 12) {
  791. html += '<option value="AM">AM</option><option value="PM" selected="selected">PM</option>';
  792. } else {
  793. html += '<option value="AM" selected="selected">AM</option><option value="PM">PM</option>';
  794. }
  795. html += '</select>';
  796. }
  797. html += '</div>';
  798. }
  799. return html;
  800. },
  801. remove: function() {
  802. this.container.remove();
  803. this.element.off('.daterangepicker');
  804. this.element.removeData('daterangepicker');
  805. }
  806. };
  807. $.fn.daterangepicker = function (options, cb) {
  808. this.each(function () {
  809. var el = $(this);
  810. if (el.data('daterangepicker'))
  811. el.data('daterangepicker').remove();
  812. el.data('daterangepicker', new DateRangePicker(el, options, cb));
  813. });
  814. return this;
  815. };
  816. }(window.jQuery, window.moment);