daterangepicker.js 63 KB

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