daterangepicker.js 68 KB

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