1
0

daterangepicker.js 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  1. /**
  2. * @version: 3.0.5
  3. * @author: Dan Grossman http://www.dangrossman.info/
  4. * @copyright: Copyright (c) 2012-2019 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.endOf('day');
  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. if (isNaN(minute)) {
  459. minute = parseInt(this.container.find('.left .minuteselect option:last').val(), 10);
  460. }
  461. second = this.timePickerSeconds ? parseInt(this.container.find('.left .secondselect').val(), 10) : 0;
  462. if (!this.timePicker24Hour) {
  463. var ampm = this.container.find('.left .ampmselect').val();
  464. if (ampm === 'PM' && hour < 12)
  465. hour += 12;
  466. if (ampm === 'AM' && hour === 12)
  467. hour = 0;
  468. }
  469. } else {
  470. hour = parseInt(this.container.find('.right .hourselect').val(), 10);
  471. minute = parseInt(this.container.find('.right .minuteselect').val(), 10);
  472. if (isNaN(minute)) {
  473. minute = parseInt(this.container.find('.right .minuteselect option:last').val(), 10);
  474. }
  475. second = this.timePickerSeconds ? parseInt(this.container.find('.right .secondselect').val(), 10) : 0;
  476. if (!this.timePicker24Hour) {
  477. var ampm = this.container.find('.right .ampmselect').val();
  478. if (ampm === 'PM' && hour < 12)
  479. hour += 12;
  480. if (ampm === 'AM' && hour === 12)
  481. hour = 0;
  482. }
  483. }
  484. this.leftCalendar.month.hour(hour).minute(minute).second(second);
  485. this.rightCalendar.month.hour(hour).minute(minute).second(second);
  486. }
  487. this.renderCalendar('left');
  488. this.renderCalendar('right');
  489. //highlight any predefined range matching the current start and end dates
  490. this.container.find('.ranges li').removeClass('active');
  491. if (this.endDate == null) return;
  492. this.calculateChosenLabel();
  493. },
  494. renderCalendar: function(side) {
  495. //
  496. // Build the matrix of dates that will populate the calendar
  497. //
  498. var calendar = side == 'left' ? this.leftCalendar : this.rightCalendar;
  499. var month = calendar.month.month();
  500. var year = calendar.month.year();
  501. var hour = calendar.month.hour();
  502. var minute = calendar.month.minute();
  503. var second = calendar.month.second();
  504. var daysInMonth = moment([year, month]).daysInMonth();
  505. var firstDay = moment([year, month, 1]);
  506. var lastDay = moment([year, month, daysInMonth]);
  507. var lastMonth = moment(firstDay).subtract(1, 'month').month();
  508. var lastYear = moment(firstDay).subtract(1, 'month').year();
  509. var daysInLastMonth = moment([lastYear, lastMonth]).daysInMonth();
  510. var dayOfWeek = firstDay.day();
  511. //initialize a 6 rows x 7 columns array for the calendar
  512. var calendar = [];
  513. calendar.firstDay = firstDay;
  514. calendar.lastDay = lastDay;
  515. for (var i = 0; i < 6; i++) {
  516. calendar[i] = [];
  517. }
  518. //populate the calendar with date objects
  519. var startDay = daysInLastMonth - dayOfWeek + this.locale.firstDay + 1;
  520. if (startDay > daysInLastMonth)
  521. startDay -= 7;
  522. if (dayOfWeek == this.locale.firstDay)
  523. startDay = daysInLastMonth - 6;
  524. var curDate = moment([lastYear, lastMonth, startDay, 12, minute, second]);
  525. var col, row;
  526. for (var i = 0, col = 0, row = 0; i < 42; i++, col++, curDate = moment(curDate).add(24, 'hour')) {
  527. if (i > 0 && col % 7 === 0) {
  528. col = 0;
  529. row++;
  530. }
  531. calendar[row][col] = curDate.clone().hour(hour).minute(minute).second(second);
  532. curDate.hour(12);
  533. if (this.minDate && calendar[row][col].format('YYYY-MM-DD') == this.minDate.format('YYYY-MM-DD') && calendar[row][col].isBefore(this.minDate) && side == 'left') {
  534. calendar[row][col] = this.minDate.clone();
  535. }
  536. if (this.maxDate && calendar[row][col].format('YYYY-MM-DD') == this.maxDate.format('YYYY-MM-DD') && calendar[row][col].isAfter(this.maxDate) && side == 'right') {
  537. calendar[row][col] = this.maxDate.clone();
  538. }
  539. }
  540. //make the calendar object available to hoverDate/clickDate
  541. if (side == 'left') {
  542. this.leftCalendar.calendar = calendar;
  543. } else {
  544. this.rightCalendar.calendar = calendar;
  545. }
  546. //
  547. // Display the calendar
  548. //
  549. var minDate = side == 'left' ? this.minDate : this.startDate;
  550. var maxDate = this.maxDate;
  551. var selected = side == 'left' ? this.startDate : this.endDate;
  552. var arrow = this.locale.direction == 'ltr' ? {left: 'chevron-left', right: 'chevron-right'} : {left: 'chevron-right', right: 'chevron-left'};
  553. var html = '<table class="table-condensed">';
  554. html += '<thead>';
  555. html += '<tr>';
  556. // add empty cell for week number
  557. if (this.showWeekNumbers || this.showISOWeekNumbers)
  558. html += '<th></th>';
  559. if ((!minDate || minDate.isBefore(calendar.firstDay)) && (!this.linkedCalendars || side == 'left')) {
  560. html += '<th class="prev available"><span></span></th>';
  561. } else {
  562. html += '<th></th>';
  563. }
  564. var dateHtml = this.locale.monthNames[calendar[1][1].month()] + calendar[1][1].format(" YYYY");
  565. if (this.showDropdowns) {
  566. var currentMonth = calendar[1][1].month();
  567. var currentYear = calendar[1][1].year();
  568. var maxYear = (maxDate && maxDate.year()) || (this.maxYear);
  569. var minYear = (minDate && minDate.year()) || (this.minYear);
  570. var inMinYear = currentYear == minYear;
  571. var inMaxYear = currentYear == maxYear;
  572. var monthHtml = '<select class="monthselect">';
  573. for (var m = 0; m < 12; m++) {
  574. if ((!inMinYear || (minDate && m >= minDate.month())) && (!inMaxYear || (maxDate && m <= maxDate.month()))) {
  575. monthHtml += "<option value='" + m + "'" +
  576. (m === currentMonth ? " selected='selected'" : "") +
  577. ">" + this.locale.monthNames[m] + "</option>";
  578. } else {
  579. monthHtml += "<option value='" + m + "'" +
  580. (m === currentMonth ? " selected='selected'" : "") +
  581. " disabled='disabled'>" + this.locale.monthNames[m] + "</option>";
  582. }
  583. }
  584. monthHtml += "</select>";
  585. var yearHtml = '<select class="yearselect">';
  586. for (var y = minYear; y <= maxYear; y++) {
  587. yearHtml += '<option value="' + y + '"' +
  588. (y === currentYear ? ' selected="selected"' : '') +
  589. '>' + y + '</option>';
  590. }
  591. yearHtml += '</select>';
  592. dateHtml = monthHtml + yearHtml;
  593. }
  594. html += '<th colspan="5" class="month">' + dateHtml + '</th>';
  595. if ((!maxDate || maxDate.isAfter(calendar.lastDay)) && (!this.linkedCalendars || side == 'right' || this.singleDatePicker)) {
  596. html += '<th class="next available"><span></span></th>';
  597. } else {
  598. html += '<th></th>';
  599. }
  600. html += '</tr>';
  601. html += '<tr>';
  602. // add week number label
  603. if (this.showWeekNumbers || this.showISOWeekNumbers)
  604. html += '<th class="week">' + this.locale.weekLabel + '</th>';
  605. $.each(this.locale.daysOfWeek, function(index, dayOfWeek) {
  606. html += '<th>' + dayOfWeek + '</th>';
  607. });
  608. html += '</tr>';
  609. html += '</thead>';
  610. html += '<tbody>';
  611. //adjust maxDate to reflect the maxSpan setting in order to
  612. //grey out end dates beyond the maxSpan
  613. if (this.endDate == null && this.maxSpan) {
  614. var maxLimit = this.startDate.clone().add(this.maxSpan).endOf('day');
  615. if (!maxDate || maxLimit.isBefore(maxDate)) {
  616. maxDate = maxLimit;
  617. }
  618. }
  619. for (var row = 0; row < 6; row++) {
  620. html += '<tr>';
  621. // add week number
  622. if (this.showWeekNumbers)
  623. html += '<td class="week">' + calendar[row][0].week() + '</td>';
  624. else if (this.showISOWeekNumbers)
  625. html += '<td class="week">' + calendar[row][0].isoWeek() + '</td>';
  626. for (var col = 0; col < 7; col++) {
  627. var classes = [];
  628. //highlight today's date
  629. if (calendar[row][col].isSame(new Date(), "day"))
  630. classes.push('today');
  631. //highlight weekends
  632. if (calendar[row][col].isoWeekday() > 5)
  633. classes.push('weekend');
  634. //grey out the dates in other months displayed at beginning and end of this calendar
  635. if (calendar[row][col].month() != calendar[1][1].month())
  636. classes.push('off', 'ends');
  637. //don't allow selection of dates before the minimum date
  638. if (this.minDate && calendar[row][col].isBefore(this.minDate, 'day'))
  639. classes.push('off', 'disabled');
  640. //don't allow selection of dates after the maximum date
  641. if (maxDate && calendar[row][col].isAfter(maxDate, 'day'))
  642. classes.push('off', 'disabled');
  643. //don't allow selection of date if a custom function decides it's invalid
  644. if (this.isInvalidDate(calendar[row][col]))
  645. classes.push('off', 'disabled');
  646. //highlight the currently selected start date
  647. if (calendar[row][col].format('YYYY-MM-DD') == this.startDate.format('YYYY-MM-DD'))
  648. classes.push('active', 'start-date');
  649. //highlight the currently selected end date
  650. if (this.endDate != null && calendar[row][col].format('YYYY-MM-DD') == this.endDate.format('YYYY-MM-DD'))
  651. classes.push('active', 'end-date');
  652. //highlight dates in-between the selected dates
  653. if (this.endDate != null && calendar[row][col] > this.startDate && calendar[row][col] < this.endDate)
  654. classes.push('in-range');
  655. //apply custom classes for this date
  656. var isCustom = this.isCustomDate(calendar[row][col]);
  657. if (isCustom !== false) {
  658. if (typeof isCustom === 'string')
  659. classes.push(isCustom);
  660. else
  661. Array.prototype.push.apply(classes, isCustom);
  662. }
  663. var cname = '', disabled = false;
  664. for (var i = 0; i < classes.length; i++) {
  665. cname += classes[i] + ' ';
  666. if (classes[i] == 'disabled')
  667. disabled = true;
  668. }
  669. if (!disabled)
  670. cname += 'available';
  671. html += '<td class="' + cname.replace(/^\s+|\s+$/g, '') + '" data-title="' + 'r' + row + 'c' + col + '">' + calendar[row][col].date() + '</td>';
  672. }
  673. html += '</tr>';
  674. }
  675. html += '</tbody>';
  676. html += '</table>';
  677. this.container.find('.drp-calendar.' + side + ' .calendar-table').html(html);
  678. },
  679. renderTimePicker: function(side) {
  680. // Don't bother updating the time picker if it's currently disabled
  681. // because an end date hasn't been clicked yet
  682. if (side == 'right' && !this.endDate) return;
  683. var html, selected, minDate, maxDate = this.maxDate;
  684. if (this.maxSpan && (!this.maxDate || this.startDate.clone().add(this.maxSpan).isBefore(this.maxDate)))
  685. maxDate = this.startDate.clone().add(this.maxSpan);
  686. if (side == 'left') {
  687. selected = this.startDate.clone();
  688. minDate = this.minDate;
  689. } else if (side == 'right') {
  690. selected = this.endDate.clone();
  691. minDate = this.startDate;
  692. //Preserve the time already selected
  693. var timeSelector = this.container.find('.drp-calendar.right .calendar-time');
  694. if (timeSelector.html() != '') {
  695. selected.hour(!isNaN(selected.hour()) ? selected.hour() : timeSelector.find('.hourselect option:selected').val());
  696. selected.minute(!isNaN(selected.minute()) ? selected.minute() : timeSelector.find('.minuteselect option:selected').val());
  697. selected.second(!isNaN(selected.second()) ? selected.second() : timeSelector.find('.secondselect option:selected').val());
  698. if (!this.timePicker24Hour) {
  699. var ampm = timeSelector.find('.ampmselect option:selected').val();
  700. if (ampm === 'PM' && selected.hour() < 12)
  701. selected.hour(selected.hour() + 12);
  702. if (ampm === 'AM' && selected.hour() === 12)
  703. selected.hour(0);
  704. }
  705. }
  706. if (selected.isBefore(this.startDate))
  707. selected = this.startDate.clone();
  708. if (maxDate && selected.isAfter(maxDate))
  709. selected = maxDate.clone();
  710. }
  711. //
  712. // hours
  713. //
  714. html = '<select class="hourselect">';
  715. var start = this.timePicker24Hour ? 0 : 1;
  716. var end = this.timePicker24Hour ? 23 : 12;
  717. for (var i = start; i <= end; i++) {
  718. var i_in_24 = i;
  719. if (!this.timePicker24Hour)
  720. i_in_24 = selected.hour() >= 12 ? (i == 12 ? 12 : i + 12) : (i == 12 ? 0 : i);
  721. var time = selected.clone().hour(i_in_24);
  722. var disabled = false;
  723. if (minDate && time.minute(59).isBefore(minDate))
  724. disabled = true;
  725. if (maxDate && time.minute(0).isAfter(maxDate))
  726. disabled = true;
  727. if (i_in_24 == selected.hour() && !disabled) {
  728. html += '<option value="' + i + '" selected="selected">' + i + '</option>';
  729. } else if (disabled) {
  730. html += '<option value="' + i + '" disabled="disabled" class="disabled">' + i + '</option>';
  731. } else {
  732. html += '<option value="' + i + '">' + i + '</option>';
  733. }
  734. }
  735. html += '</select> ';
  736. //
  737. // minutes
  738. //
  739. html += ': <select class="minuteselect">';
  740. for (var i = 0; i < 60; i += this.timePickerIncrement) {
  741. var padded = i < 10 ? '0' + i : i;
  742. var time = selected.clone().minute(i);
  743. var disabled = false;
  744. if (minDate && time.second(59).isBefore(minDate))
  745. disabled = true;
  746. if (maxDate && time.second(0).isAfter(maxDate))
  747. disabled = true;
  748. if (selected.minute() == i && !disabled) {
  749. html += '<option value="' + i + '" selected="selected">' + padded + '</option>';
  750. } else if (disabled) {
  751. html += '<option value="' + i + '" disabled="disabled" class="disabled">' + padded + '</option>';
  752. } else {
  753. html += '<option value="' + i + '">' + padded + '</option>';
  754. }
  755. }
  756. html += '</select> ';
  757. //
  758. // seconds
  759. //
  760. if (this.timePickerSeconds) {
  761. html += ': <select class="secondselect">';
  762. for (var i = 0; i < 60; i++) {
  763. var padded = i < 10 ? '0' + i : i;
  764. var time = selected.clone().second(i);
  765. var disabled = false;
  766. if (minDate && time.isBefore(minDate))
  767. disabled = true;
  768. if (maxDate && time.isAfter(maxDate))
  769. disabled = true;
  770. if (selected.second() == i && !disabled) {
  771. html += '<option value="' + i + '" selected="selected">' + padded + '</option>';
  772. } else if (disabled) {
  773. html += '<option value="' + i + '" disabled="disabled" class="disabled">' + padded + '</option>';
  774. } else {
  775. html += '<option value="' + i + '">' + padded + '</option>';
  776. }
  777. }
  778. html += '</select> ';
  779. }
  780. //
  781. // AM/PM
  782. //
  783. if (!this.timePicker24Hour) {
  784. html += '<select class="ampmselect">';
  785. var am_html = '';
  786. var pm_html = '';
  787. if (minDate && selected.clone().hour(12).minute(0).second(0).isBefore(minDate))
  788. am_html = ' disabled="disabled" class="disabled"';
  789. if (maxDate && selected.clone().hour(0).minute(0).second(0).isAfter(maxDate))
  790. pm_html = ' disabled="disabled" class="disabled"';
  791. if (selected.hour() >= 12) {
  792. html += '<option value="AM"' + am_html + '>AM</option><option value="PM" selected="selected"' + pm_html + '>PM</option>';
  793. } else {
  794. html += '<option value="AM" selected="selected"' + am_html + '>AM</option><option value="PM"' + pm_html + '>PM</option>';
  795. }
  796. html += '</select>';
  797. }
  798. this.container.find('.drp-calendar.' + side + ' .calendar-time').html(html);
  799. },
  800. updateFormInputs: function() {
  801. if (this.singleDatePicker || (this.endDate && (this.startDate.isBefore(this.endDate) || this.startDate.isSame(this.endDate)))) {
  802. this.container.find('button.applyBtn').removeAttr('disabled');
  803. } else {
  804. this.container.find('button.applyBtn').attr('disabled', 'disabled');
  805. }
  806. },
  807. move: function() {
  808. var parentOffset = { top: 0, left: 0 },
  809. containerTop;
  810. var parentRightEdge = $(window).width();
  811. if (!this.parentEl.is('body')) {
  812. parentOffset = {
  813. top: this.parentEl.offset().top - this.parentEl.scrollTop(),
  814. left: this.parentEl.offset().left - this.parentEl.scrollLeft()
  815. };
  816. parentRightEdge = this.parentEl[0].clientWidth + this.parentEl.offset().left;
  817. }
  818. if (this.drops == 'up')
  819. containerTop = this.element.offset().top - this.container.outerHeight() - parentOffset.top;
  820. else
  821. containerTop = this.element.offset().top + this.element.outerHeight() - parentOffset.top;
  822. // Force the container to it's actual width
  823. this.container.css({
  824. top: 0,
  825. left: 0,
  826. right: 'auto'
  827. });
  828. var containerWidth = this.container.outerWidth();
  829. this.container[this.drops == 'up' ? 'addClass' : 'removeClass']('drop-up');
  830. if (this.opens == 'left') {
  831. var containerRight = parentRightEdge - this.element.offset().left - this.element.outerWidth();
  832. if (containerWidth + containerRight > $(window).width()) {
  833. this.container.css({
  834. top: containerTop,
  835. right: 'auto',
  836. left: 9
  837. });
  838. } else {
  839. this.container.css({
  840. top: containerTop,
  841. right: containerRight,
  842. left: 'auto'
  843. });
  844. }
  845. } else if (this.opens == 'center') {
  846. var containerLeft = this.element.offset().left - parentOffset.left + this.element.outerWidth() / 2
  847. - containerWidth / 2;
  848. if (containerLeft < 0) {
  849. this.container.css({
  850. top: containerTop,
  851. right: 'auto',
  852. left: 9
  853. });
  854. } else if (containerLeft + containerWidth > $(window).width()) {
  855. this.container.css({
  856. top: containerTop,
  857. left: 'auto',
  858. right: 0
  859. });
  860. } else {
  861. this.container.css({
  862. top: containerTop,
  863. left: containerLeft,
  864. right: 'auto'
  865. });
  866. }
  867. } else {
  868. var containerLeft = this.element.offset().left - parentOffset.left;
  869. if (containerLeft + containerWidth > $(window).width()) {
  870. this.container.css({
  871. top: containerTop,
  872. left: 'auto',
  873. right: 0
  874. });
  875. } else {
  876. this.container.css({
  877. top: containerTop,
  878. left: containerLeft,
  879. right: 'auto'
  880. });
  881. }
  882. }
  883. },
  884. show: function(e) {
  885. if (this.isShowing) return;
  886. // Create a click proxy that is private to this instance of datepicker, for unbinding
  887. this._outsideClickProxy = $.proxy(function(e) { this.outsideClick(e); }, this);
  888. // Bind global datepicker mousedown for hiding and
  889. $(document)
  890. .on('mousedown.daterangepicker', this._outsideClickProxy)
  891. // also support mobile devices
  892. .on('touchend.daterangepicker', this._outsideClickProxy)
  893. // also explicitly play nice with Bootstrap dropdowns, which stopPropagation when clicking them
  894. .on('click.daterangepicker', '[data-toggle=dropdown]', this._outsideClickProxy)
  895. // and also close when focus changes to outside the picker (eg. tabbing between controls)
  896. .on('focusin.daterangepicker', this._outsideClickProxy);
  897. // Reposition the picker if the window is resized while it's open
  898. $(window).on('resize.daterangepicker', $.proxy(function(e) { this.move(e); }, this));
  899. this.oldStartDate = this.startDate.clone();
  900. this.oldEndDate = this.endDate.clone();
  901. this.previousRightTime = this.endDate.clone();
  902. this.updateView();
  903. this.container.show();
  904. this.move();
  905. this.element.trigger('show.daterangepicker', this);
  906. this.isShowing = true;
  907. },
  908. hide: function(e) {
  909. if (!this.isShowing) return;
  910. //incomplete date selection, revert to last values
  911. if (!this.endDate) {
  912. this.startDate = this.oldStartDate.clone();
  913. this.endDate = this.oldEndDate.clone();
  914. }
  915. //if a new date range was selected, invoke the user callback function
  916. if (!this.startDate.isSame(this.oldStartDate) || !this.endDate.isSame(this.oldEndDate))
  917. this.callback(this.startDate.clone(), this.endDate.clone(), this.chosenLabel);
  918. //if picker is attached to a text input, update it
  919. this.updateElement();
  920. $(document).off('.daterangepicker');
  921. $(window).off('.daterangepicker');
  922. this.container.hide();
  923. this.element.trigger('hide.daterangepicker', this);
  924. this.isShowing = false;
  925. },
  926. toggle: function(e) {
  927. if (this.isShowing) {
  928. this.hide();
  929. } else {
  930. this.show();
  931. }
  932. },
  933. outsideClick: function(e) {
  934. var target = $(e.target);
  935. // if the page is clicked anywhere except within the daterangerpicker/button
  936. // itself then call this.hide()
  937. if (
  938. // ie modal dialog fix
  939. e.type == "focusin" ||
  940. target.closest(this.element).length ||
  941. target.closest(this.container).length ||
  942. target.closest('.calendar-table').length
  943. ) return;
  944. this.hide();
  945. this.element.trigger('outsideClick.daterangepicker', this);
  946. },
  947. showCalendars: function() {
  948. this.container.addClass('show-calendar');
  949. this.move();
  950. this.element.trigger('showCalendar.daterangepicker', this);
  951. },
  952. hideCalendars: function() {
  953. this.container.removeClass('show-calendar');
  954. this.element.trigger('hideCalendar.daterangepicker', this);
  955. },
  956. clickRange: function(e) {
  957. var label = e.target.getAttribute('data-range-key');
  958. this.chosenLabel = label;
  959. if (label == this.locale.customRangeLabel) {
  960. this.showCalendars();
  961. } else {
  962. var dates = this.ranges[label];
  963. this.startDate = dates[0];
  964. this.endDate = dates[1];
  965. if (!this.timePicker) {
  966. this.startDate.startOf('day');
  967. this.endDate.endOf('day');
  968. }
  969. if (!this.alwaysShowCalendars)
  970. this.hideCalendars();
  971. this.clickApply();
  972. }
  973. },
  974. clickPrev: function(e) {
  975. var cal = $(e.target).parents('.drp-calendar');
  976. if (cal.hasClass('left')) {
  977. this.leftCalendar.month.subtract(1, 'month');
  978. if (this.linkedCalendars)
  979. this.rightCalendar.month.subtract(1, 'month');
  980. } else {
  981. this.rightCalendar.month.subtract(1, 'month');
  982. }
  983. this.updateCalendars();
  984. },
  985. clickNext: function(e) {
  986. var cal = $(e.target).parents('.drp-calendar');
  987. if (cal.hasClass('left')) {
  988. this.leftCalendar.month.add(1, 'month');
  989. } else {
  990. this.rightCalendar.month.add(1, 'month');
  991. if (this.linkedCalendars)
  992. this.leftCalendar.month.add(1, 'month');
  993. }
  994. this.updateCalendars();
  995. },
  996. hoverDate: function(e) {
  997. //ignore dates that can't be selected
  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. //highlight the dates between the start date and the date being hovered as a potential end date
  1005. var leftCalendar = this.leftCalendar;
  1006. var rightCalendar = this.rightCalendar;
  1007. var startDate = this.startDate;
  1008. if (!this.endDate) {
  1009. this.container.find('.drp-calendar tbody td').each(function(index, el) {
  1010. //skip week numbers, only look at dates
  1011. if ($(el).hasClass('week')) return;
  1012. var title = $(el).attr('data-title');
  1013. var row = title.substr(1, 1);
  1014. var col = title.substr(3, 1);
  1015. var cal = $(el).parents('.drp-calendar');
  1016. var dt = cal.hasClass('left') ? leftCalendar.calendar[row][col] : rightCalendar.calendar[row][col];
  1017. if ((dt.isAfter(startDate) && dt.isBefore(date)) || dt.isSame(date, 'day')) {
  1018. $(el).addClass('in-range');
  1019. } else {
  1020. $(el).removeClass('in-range');
  1021. }
  1022. });
  1023. }
  1024. },
  1025. clickDate: function(e) {
  1026. if (!$(e.target).hasClass('available')) return;
  1027. var title = $(e.target).attr('data-title');
  1028. var row = title.substr(1, 1);
  1029. var col = title.substr(3, 1);
  1030. var cal = $(e.target).parents('.drp-calendar');
  1031. var date = cal.hasClass('left') ? this.leftCalendar.calendar[row][col] : this.rightCalendar.calendar[row][col];
  1032. //
  1033. // this function needs to do a few things:
  1034. // * alternate between selecting a start and end date for the range,
  1035. // * if the time picker is enabled, apply the hour/minute/second from the select boxes to the clicked date
  1036. // * if autoapply is enabled, and an end date was chosen, apply the selection
  1037. // * if single date picker mode, and time picker isn't enabled, apply the selection immediately
  1038. // * if one of the inputs above the calendars was focused, cancel that manual input
  1039. //
  1040. if (this.endDate || date.isBefore(this.startDate, 'day')) { //picking start
  1041. if (this.timePicker) {
  1042. var hour = parseInt(this.container.find('.left .hourselect').val(), 10);
  1043. if (!this.timePicker24Hour) {
  1044. var ampm = this.container.find('.left .ampmselect').val();
  1045. if (ampm === 'PM' && hour < 12)
  1046. hour += 12;
  1047. if (ampm === 'AM' && hour === 12)
  1048. hour = 0;
  1049. }
  1050. var minute = parseInt(this.container.find('.left .minuteselect').val(), 10);
  1051. if (isNaN(minute)) {
  1052. minute = parseInt(this.container.find('.left .minuteselect option:last').val(), 10);
  1053. }
  1054. var second = this.timePickerSeconds ? parseInt(this.container.find('.left .secondselect').val(), 10) : 0;
  1055. date = date.clone().hour(hour).minute(minute).second(second);
  1056. }
  1057. this.endDate = null;
  1058. this.setStartDate(date.clone());
  1059. } else if (!this.endDate && date.isBefore(this.startDate)) {
  1060. //special case: clicking the same date for start/end,
  1061. //but the time of the end date is before the start date
  1062. this.setEndDate(this.startDate.clone());
  1063. } else { // picking end
  1064. if (this.timePicker) {
  1065. var hour = parseInt(this.container.find('.right .hourselect').val(), 10);
  1066. if (!this.timePicker24Hour) {
  1067. var ampm = this.container.find('.right .ampmselect').val();
  1068. if (ampm === 'PM' && hour < 12)
  1069. hour += 12;
  1070. if (ampm === 'AM' && hour === 12)
  1071. hour = 0;
  1072. }
  1073. var minute = parseInt(this.container.find('.right .minuteselect').val(), 10);
  1074. if (isNaN(minute)) {
  1075. minute = parseInt(this.container.find('.right .minuteselect option:last').val(), 10);
  1076. }
  1077. var second = this.timePickerSeconds ? parseInt(this.container.find('.right .secondselect').val(), 10) : 0;
  1078. date = date.clone().hour(hour).minute(minute).second(second);
  1079. }
  1080. this.setEndDate(date.clone());
  1081. if (this.autoApply) {
  1082. this.calculateChosenLabel();
  1083. this.clickApply();
  1084. }
  1085. }
  1086. if (this.singleDatePicker) {
  1087. this.setEndDate(this.startDate);
  1088. if (!this.timePicker)
  1089. this.clickApply();
  1090. }
  1091. this.updateView();
  1092. //This is to cancel the blur event handler if the mouse was in one of the inputs
  1093. e.stopPropagation();
  1094. },
  1095. calculateChosenLabel: function () {
  1096. var customRange = true;
  1097. var i = 0;
  1098. for (var range in this.ranges) {
  1099. if (this.timePicker) {
  1100. var format = this.timePickerSeconds ? "YYYY-MM-DD HH:mm:ss" : "YYYY-MM-DD HH:mm";
  1101. //ignore times when comparing dates if time picker seconds is not enabled
  1102. if (this.startDate.format(format) == this.ranges[range][0].format(format) && this.endDate.format(format) == this.ranges[range][1].format(format)) {
  1103. customRange = false;
  1104. this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').attr('data-range-key');
  1105. break;
  1106. }
  1107. } else {
  1108. //ignore times when comparing dates if time picker is not enabled
  1109. 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')) {
  1110. customRange = false;
  1111. this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')').addClass('active').attr('data-range-key');
  1112. break;
  1113. }
  1114. }
  1115. i++;
  1116. }
  1117. if (customRange) {
  1118. if (this.showCustomRangeLabel) {
  1119. this.chosenLabel = this.container.find('.ranges li:last').addClass('active').attr('data-range-key');
  1120. } else {
  1121. this.chosenLabel = null;
  1122. }
  1123. this.showCalendars();
  1124. }
  1125. },
  1126. clickApply: function(e) {
  1127. this.hide();
  1128. this.element.trigger('apply.daterangepicker', this);
  1129. },
  1130. clickCancel: function(e) {
  1131. this.startDate = this.oldStartDate;
  1132. this.endDate = this.oldEndDate;
  1133. this.hide();
  1134. this.element.trigger('cancel.daterangepicker', this);
  1135. },
  1136. monthOrYearChanged: function(e) {
  1137. var isLeft = $(e.target).closest('.drp-calendar').hasClass('left'),
  1138. leftOrRight = isLeft ? 'left' : 'right',
  1139. cal = this.container.find('.drp-calendar.'+leftOrRight);
  1140. // Month must be Number for new moment versions
  1141. var month = parseInt(cal.find('.monthselect').val(), 10);
  1142. var year = cal.find('.yearselect').val();
  1143. if (!isLeft) {
  1144. if (year < this.startDate.year() || (year == this.startDate.year() && month < this.startDate.month())) {
  1145. month = this.startDate.month();
  1146. year = this.startDate.year();
  1147. }
  1148. }
  1149. if (this.minDate) {
  1150. if (year < this.minDate.year() || (year == this.minDate.year() && month < this.minDate.month())) {
  1151. month = this.minDate.month();
  1152. year = this.minDate.year();
  1153. }
  1154. }
  1155. if (this.maxDate) {
  1156. if (year > this.maxDate.year() || (year == this.maxDate.year() && month > this.maxDate.month())) {
  1157. month = this.maxDate.month();
  1158. year = this.maxDate.year();
  1159. }
  1160. }
  1161. if (isLeft) {
  1162. this.leftCalendar.month.month(month).year(year);
  1163. if (this.linkedCalendars)
  1164. this.rightCalendar.month = this.leftCalendar.month.clone().add(1, 'month');
  1165. } else {
  1166. this.rightCalendar.month.month(month).year(year);
  1167. if (this.linkedCalendars)
  1168. this.leftCalendar.month = this.rightCalendar.month.clone().subtract(1, 'month');
  1169. }
  1170. this.updateCalendars();
  1171. },
  1172. timeChanged: function(e) {
  1173. var cal = $(e.target).closest('.drp-calendar'),
  1174. isLeft = cal.hasClass('left');
  1175. var hour = parseInt(cal.find('.hourselect').val(), 10);
  1176. var minute = parseInt(cal.find('.minuteselect').val(), 10);
  1177. if (isNaN(minute)) {
  1178. minute = parseInt(cal.find('.minuteselect option:last').val(), 10);
  1179. }
  1180. var second = this.timePickerSeconds ? parseInt(cal.find('.secondselect').val(), 10) : 0;
  1181. if (!this.timePicker24Hour) {
  1182. var ampm = cal.find('.ampmselect').val();
  1183. if (ampm === 'PM' && hour < 12)
  1184. hour += 12;
  1185. if (ampm === 'AM' && hour === 12)
  1186. hour = 0;
  1187. }
  1188. if (isLeft) {
  1189. var start = this.startDate.clone();
  1190. start.hour(hour);
  1191. start.minute(minute);
  1192. start.second(second);
  1193. this.setStartDate(start);
  1194. if (this.singleDatePicker) {
  1195. this.endDate = this.startDate.clone();
  1196. } else if (this.endDate && this.endDate.format('YYYY-MM-DD') == start.format('YYYY-MM-DD') && this.endDate.isBefore(start)) {
  1197. this.setEndDate(start.clone());
  1198. }
  1199. } else if (this.endDate) {
  1200. var end = this.endDate.clone();
  1201. end.hour(hour);
  1202. end.minute(minute);
  1203. end.second(second);
  1204. this.setEndDate(end);
  1205. }
  1206. //update the calendars so all clickable dates reflect the new time component
  1207. this.updateCalendars();
  1208. //update the form inputs above the calendars with the new time
  1209. this.updateFormInputs();
  1210. //re-render the time pickers because changing one selection can affect what's enabled in another
  1211. this.renderTimePicker('left');
  1212. this.renderTimePicker('right');
  1213. },
  1214. elementChanged: function() {
  1215. if (!this.element.is('input')) return;
  1216. if (!this.element.val().length) return;
  1217. var dateString = this.element.val().split(this.locale.separator),
  1218. start = null,
  1219. end = null;
  1220. if (dateString.length === 2) {
  1221. start = moment(dateString[0], this.locale.format);
  1222. end = moment(dateString[1], this.locale.format);
  1223. }
  1224. if (this.singleDatePicker || start === null || end === null) {
  1225. start = moment(this.element.val(), this.locale.format);
  1226. end = start;
  1227. }
  1228. if (!start.isValid() || !end.isValid()) return;
  1229. this.setStartDate(start);
  1230. this.setEndDate(end);
  1231. this.updateView();
  1232. },
  1233. keydown: function(e) {
  1234. //hide on tab or enter
  1235. if ((e.keyCode === 9) || (e.keyCode === 13)) {
  1236. this.hide();
  1237. }
  1238. //hide on esc and prevent propagation
  1239. if (e.keyCode === 27) {
  1240. e.preventDefault();
  1241. e.stopPropagation();
  1242. this.hide();
  1243. }
  1244. },
  1245. updateElement: function() {
  1246. if (this.element.is('input') && this.autoUpdateInput) {
  1247. var newValue = this.startDate.format(this.locale.format);
  1248. if (!this.singleDatePicker) {
  1249. newValue += this.locale.separator + this.endDate.format(this.locale.format);
  1250. }
  1251. if (newValue !== this.element.val()) {
  1252. this.element.val(newValue).trigger('change');
  1253. }
  1254. }
  1255. },
  1256. remove: function() {
  1257. this.container.remove();
  1258. this.element.off('.daterangepicker');
  1259. this.element.removeData();
  1260. }
  1261. };
  1262. $.fn.daterangepicker = function(options, callback) {
  1263. var implementOptions = $.extend(true, {}, $.fn.daterangepicker.defaultOptions, options);
  1264. this.each(function() {
  1265. var el = $(this);
  1266. if (el.data('daterangepicker'))
  1267. el.data('daterangepicker').remove();
  1268. el.data('daterangepicker', new DateRangePicker(el, implementOptions, callback));
  1269. });
  1270. return this;
  1271. };
  1272. return DateRangePicker;
  1273. }));