daterangepicker.js 68 KB

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