Built files from Bizgaze WebServer
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

daterangepicker.js 67KB

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