daterangepicker.js 62 KB

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