daterangepicker.js 63 KB

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