daterangepicker.js 40 KB

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