daterangepicker.js 62 KB

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