Theme Inspinia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

daterangepicker.js 52KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. /**
  2. * @version: 1.3.21
  3. * @author: Dan Grossman http://www.dangrossman.info/
  4. * @copyright: Copyright (c) 2012-2015 Dan Grossman. All rights reserved.
  5. * @license: Licensed under the MIT license. See http://www.opensource.org/licenses/mit-license.php
  6. * @website: https://www.improvely.com/
  7. */
  8. (function(root, factory) {
  9. if (typeof define === 'function' && define.amd) {
  10. define(['moment', 'jquery', 'exports'], function(momentjs, $, exports) {
  11. root.daterangepicker = factory(root, exports, momentjs, $);
  12. });
  13. } else if (typeof exports !== 'undefined') {
  14. var momentjs = require('moment');
  15. var jQuery;
  16. try {
  17. jQuery = require('jquery');
  18. } catch (err) {
  19. jQuery = window.jQuery;
  20. if (!jQuery) throw new Error('jQuery dependency not found');
  21. }
  22. factory(root, exports, momentjs, jQuery);
  23. // Finally, as a browser global.
  24. } else {
  25. root.daterangepicker = factory(root, {}, root.moment, (root.jQuery || root.Zepto || root.ender || root.$));
  26. }
  27. }(this, function(root, daterangepicker, moment, $) {
  28. var DateRangePicker = function (element, options, cb) {
  29. // by default, the daterangepicker element is placed at the bottom of HTML body
  30. this.parentEl = 'body';
  31. //element that triggered the date range picker
  32. this.element = $(element);
  33. //tracks visible state
  34. this.isShowing = false;
  35. //create the picker HTML object
  36. var DRPTemplate = '<div class="daterangepicker dropdown-menu">' +
  37. '<div class="calendar first left"></div>' +
  38. '<div class="calendar second right"></div>' +
  39. '<div class="ranges">' +
  40. '<div class="range_inputs">' +
  41. '<div class="daterangepicker_start_input">' +
  42. '<label for="daterangepicker_start"></label>' +
  43. '<input class="input-mini" type="text" name="daterangepicker_start" value="" />' +
  44. '</div>' +
  45. '<div class="daterangepicker_end_input">' +
  46. '<label for="daterangepicker_end"></label>' +
  47. '<input class="input-mini" type="text" name="daterangepicker_end" value="" />' +
  48. '</div>' +
  49. '<button class="applyBtn" disabled="disabled"></button>&nbsp;' +
  50. '<button class="cancelBtn"></button>' +
  51. '</div>' +
  52. '</div>' +
  53. '</div>';
  54. //custom options
  55. if (typeof options !== 'object' || options === null)
  56. options = {};
  57. this.parentEl = (typeof options === 'object' && options.parentEl && $(options.parentEl).length) ? $(options.parentEl) : $(this.parentEl);
  58. this.container = $(DRPTemplate).appendTo(this.parentEl);
  59. this.setOptions(options, cb);
  60. //event listeners
  61. this.container.find('.calendar')
  62. .on('click.daterangepicker', '.prev', $.proxy(this.clickPrev, this))
  63. .on('click.daterangepicker', '.next', $.proxy(this.clickNext, this))
  64. .on('click.daterangepicker', 'td.available', $.proxy(this.clickDate, this))
  65. .on('mouseenter.daterangepicker', 'td.available', $.proxy(this.hoverDate, this))
  66. .on('mouseleave.daterangepicker', 'td.available', $.proxy(this.updateFormInputs, this))
  67. .on('change.daterangepicker', 'select.yearselect', $.proxy(this.updateMonthYear, this))
  68. .on('change.daterangepicker', 'select.monthselect', $.proxy(this.updateMonthYear, this))
  69. .on('change.daterangepicker', 'select.hourselect,select.minuteselect,select.secondselect,select.ampmselect', $.proxy(this.updateTime, this));
  70. this.container.find('.ranges')
  71. .on('click.daterangepicker', 'button.applyBtn', $.proxy(this.clickApply, this))
  72. .on('click.daterangepicker', 'button.cancelBtn', $.proxy(this.clickCancel, this))
  73. .on('click.daterangepicker', '.daterangepicker_start_input,.daterangepicker_end_input', $.proxy(this.showCalendars, this))
  74. .on('change.daterangepicker', '.daterangepicker_start_input,.daterangepicker_end_input', $.proxy(this.inputsChanged, this))
  75. .on('keydown.daterangepicker', '.daterangepicker_start_input,.daterangepicker_end_input', $.proxy(this.inputsKeydown, this))
  76. .on('click.daterangepicker', 'li', $.proxy(this.clickRange, this))
  77. .on('mouseenter.daterangepicker', 'li', $.proxy(this.enterRange, this))
  78. .on('mouseleave.daterangepicker', 'li', $.proxy(this.updateFormInputs, this));
  79. if (this.element.is('input')) {
  80. this.element.on({
  81. 'click.daterangepicker': $.proxy(this.show, this),
  82. 'focus.daterangepicker': $.proxy(this.show, this),
  83. 'keyup.daterangepicker': $.proxy(this.updateFromControl, this),
  84. 'keydown.daterangepicker': $.proxy(this.keydown, this)
  85. });
  86. } else {
  87. this.element.on('click.daterangepicker', $.proxy(this.toggle, this));
  88. }
  89. };
  90. DateRangePicker.prototype = {
  91. constructor: DateRangePicker,
  92. setOptions: function(options, callback) {
  93. this.startDate = moment().startOf('day');
  94. this.endDate = moment().endOf('day');
  95. this.timeZone = moment().utcOffset();
  96. this.minDate = false;
  97. this.maxDate = false;
  98. this.dateLimit = false;
  99. this.showDropdowns = false;
  100. this.showWeekNumbers = false;
  101. this.timePicker = false;
  102. this.timePickerSeconds = false;
  103. this.timePickerIncrement = 30;
  104. this.timePicker12Hour = true;
  105. this.singleDatePicker = false;
  106. this.ranges = {};
  107. this.opens = 'right';
  108. if (this.element.hasClass('pull-right'))
  109. this.opens = 'left';
  110. this.drops = 'down';
  111. if (this.element.hasClass('dropup'))
  112. this.drops = 'up';
  113. this.buttonClasses = ['btn', 'btn-small btn-sm'];
  114. this.applyClass = 'btn-success';
  115. this.cancelClass = 'btn-default';
  116. this.format = 'MM/DD/YYYY';
  117. this.separator = ' - ';
  118. this.locale = {
  119. applyLabel: 'Apply',
  120. cancelLabel: 'Cancel',
  121. fromLabel: 'From',
  122. toLabel: 'To',
  123. weekLabel: 'W',
  124. customRangeLabel: 'Custom Range',
  125. daysOfWeek: moment.weekdaysMin(),
  126. monthNames: moment.monthsShort(),
  127. firstDay: moment.localeData()._week.dow
  128. };
  129. this.cb = function () { };
  130. if (typeof options.format === 'string')
  131. this.format = options.format;
  132. if (typeof options.separator === 'string')
  133. this.separator = options.separator;
  134. if (typeof options.startDate === 'string')
  135. this.startDate = moment(options.startDate, this.format);
  136. if (typeof options.endDate === 'string')
  137. this.endDate = moment(options.endDate, this.format);
  138. if (typeof options.minDate === 'string')
  139. this.minDate = moment(options.minDate, this.format);
  140. if (typeof options.maxDate === 'string')
  141. this.maxDate = moment(options.maxDate, this.format);
  142. if (typeof options.startDate === 'object')
  143. this.startDate = moment(options.startDate);
  144. if (typeof options.endDate === 'object')
  145. this.endDate = moment(options.endDate);
  146. if (typeof options.minDate === 'object')
  147. this.minDate = moment(options.minDate);
  148. if (typeof options.maxDate === 'object')
  149. this.maxDate = moment(options.maxDate);
  150. if (typeof options.applyClass === 'string')
  151. this.applyClass = options.applyClass;
  152. if (typeof options.cancelClass === 'string')
  153. this.cancelClass = options.cancelClass;
  154. if (typeof options.dateLimit === 'object')
  155. this.dateLimit = options.dateLimit;
  156. if (typeof options.locale === 'object') {
  157. if (typeof options.locale.daysOfWeek === 'object') {
  158. // Create a copy of daysOfWeek to avoid modification of original
  159. // options object for reusability in multiple daterangepicker instances
  160. this.locale.daysOfWeek = options.locale.daysOfWeek.slice();
  161. }
  162. if (typeof options.locale.monthNames === 'object') {
  163. this.locale.monthNames = options.locale.monthNames.slice();
  164. }
  165. if (typeof options.locale.firstDay === 'number') {
  166. this.locale.firstDay = options.locale.firstDay;
  167. }
  168. if (typeof options.locale.applyLabel === 'string') {
  169. this.locale.applyLabel = options.locale.applyLabel;
  170. }
  171. if (typeof options.locale.cancelLabel === 'string') {
  172. this.locale.cancelLabel = options.locale.cancelLabel;
  173. }
  174. if (typeof options.locale.fromLabel === 'string') {
  175. this.locale.fromLabel = options.locale.fromLabel;
  176. }
  177. if (typeof options.locale.toLabel === 'string') {
  178. this.locale.toLabel = options.locale.toLabel;
  179. }
  180. if (typeof options.locale.weekLabel === 'string') {
  181. this.locale.weekLabel = options.locale.weekLabel;
  182. }
  183. if (typeof options.locale.customRangeLabel === 'string') {
  184. this.locale.customRangeLabel = options.locale.customRangeLabel;
  185. }
  186. }
  187. if (typeof options.opens === 'string')
  188. this.opens = options.opens;
  189. if (typeof options.drops === 'string')
  190. this.drops = options.drops;
  191. if (typeof options.showWeekNumbers === 'boolean') {
  192. this.showWeekNumbers = options.showWeekNumbers;
  193. }
  194. if (typeof options.buttonClasses === 'string') {
  195. this.buttonClasses = [options.buttonClasses];
  196. }
  197. if (typeof options.buttonClasses === 'object') {
  198. this.buttonClasses = options.buttonClasses;
  199. }
  200. if (typeof options.showDropdowns === 'boolean') {
  201. this.showDropdowns = options.showDropdowns;
  202. }
  203. if (typeof options.singleDatePicker === 'boolean') {
  204. this.singleDatePicker = options.singleDatePicker;
  205. if (this.singleDatePicker) {
  206. this.endDate = this.startDate.clone();
  207. }
  208. }
  209. if (typeof options.timePicker === 'boolean') {
  210. this.timePicker = options.timePicker;
  211. }
  212. if (typeof options.timePickerSeconds === 'boolean') {
  213. this.timePickerSeconds = options.timePickerSeconds;
  214. }
  215. if (typeof options.timePickerIncrement === 'number') {
  216. this.timePickerIncrement = options.timePickerIncrement;
  217. }
  218. if (typeof options.timePicker12Hour === 'boolean') {
  219. this.timePicker12Hour = options.timePicker12Hour;
  220. }
  221. // update day names order to firstDay
  222. if (this.locale.firstDay != 0) {
  223. var iterator = this.locale.firstDay;
  224. while (iterator > 0) {
  225. this.locale.daysOfWeek.push(this.locale.daysOfWeek.shift());
  226. iterator--;
  227. }
  228. }
  229. var start, end, range;
  230. //if no start/end dates set, check if an input element contains initial values
  231. if (typeof options.startDate === 'undefined' && typeof options.endDate === 'undefined') {
  232. if ($(this.element).is('input[type=text]')) {
  233. var val = $(this.element).val(),
  234. split = val.split(this.separator);
  235. start = end = null;
  236. if (split.length == 2) {
  237. start = moment(split[0], this.format);
  238. end = moment(split[1], this.format);
  239. } else if (this.singleDatePicker && val !== "") {
  240. start = moment(val, this.format);
  241. end = moment(val, this.format);
  242. }
  243. if (start !== null && end !== null) {
  244. this.startDate = start;
  245. this.endDate = end;
  246. }
  247. }
  248. }
  249. // bind the time zone used to build the calendar to either the timeZone passed in through the options or the zone of the startDate (which will be the local time zone by default)
  250. if (typeof options.timeZone === 'string' || typeof options.timeZone === 'number') {
  251. if (typeof options.timeZone === 'string' && typeof moment.tz !== 'undefined') {
  252. this.timeZone = moment.tz.zone(options.timeZone).parse(new Date) * -1; // Offset is positive if the timezone is behind UTC and negative if it is ahead.
  253. } else {
  254. this.timeZone = options.timeZone;
  255. }
  256. this.startDate.utcOffset(this.timeZone);
  257. this.endDate.utcOffset(this.timeZone);
  258. } else {
  259. this.timeZone = moment(this.startDate).utcOffset();
  260. }
  261. if (typeof options.ranges === 'object') {
  262. for (range in options.ranges) {
  263. if (typeof options.ranges[range][0] === 'string')
  264. start = moment(options.ranges[range][0], this.format);
  265. else
  266. start = moment(options.ranges[range][0]);
  267. if (typeof options.ranges[range][1] === 'string')
  268. end = moment(options.ranges[range][1], this.format);
  269. else
  270. end = moment(options.ranges[range][1]);
  271. // If we have a min/max date set, bound this range
  272. // to it, but only if it would otherwise fall
  273. // outside of the min/max.
  274. if (this.minDate && start.isBefore(this.minDate))
  275. start = moment(this.minDate);
  276. if (this.maxDate && end.isAfter(this.maxDate))
  277. end = moment(this.maxDate);
  278. // If the end of the range is before the minimum (if min is set) OR
  279. // the start of the range is after the max (also if set) don't display this
  280. // range option.
  281. if ((this.minDate && end.isBefore(this.minDate)) || (this.maxDate && start.isAfter(this.maxDate))) {
  282. continue;
  283. }
  284. this.ranges[range] = [start, end];
  285. }
  286. var list = '<ul>';
  287. for (range in this.ranges) {
  288. list += '<li>' + range + '</li>';
  289. }
  290. list += '<li>' + this.locale.customRangeLabel + '</li>';
  291. list += '</ul>';
  292. this.container.find('.ranges ul').remove();
  293. this.container.find('.ranges').prepend(list);
  294. }
  295. if (typeof callback === 'function') {
  296. this.cb = callback;
  297. }
  298. if (!this.timePicker) {
  299. this.startDate = this.startDate.startOf('day');
  300. this.endDate = this.endDate.endOf('day');
  301. }
  302. if (this.singleDatePicker) {
  303. this.opens = 'right';
  304. this.container.addClass('single');
  305. this.container.find('.calendar.right').show();
  306. this.container.find('.calendar.left').hide();
  307. if (!this.timePicker) {
  308. this.container.find('.ranges').hide();
  309. } else {
  310. this.container.find('.ranges .daterangepicker_start_input, .ranges .daterangepicker_end_input').hide();
  311. }
  312. if (!this.container.find('.calendar.right').hasClass('single'))
  313. this.container.find('.calendar.right').addClass('single');
  314. } else {
  315. this.container.removeClass('single');
  316. this.container.find('.calendar.right').removeClass('single');
  317. this.container.find('.ranges').show();
  318. }
  319. this.oldStartDate = this.startDate.clone();
  320. this.oldEndDate = this.endDate.clone();
  321. this.oldChosenLabel = this.chosenLabel;
  322. this.leftCalendar = {
  323. month: moment([this.startDate.year(), this.startDate.month(), 1, this.startDate.hour(), this.startDate.minute(), this.startDate.second()]),
  324. calendar: []
  325. };
  326. this.rightCalendar = {
  327. month: moment([this.endDate.year(), this.endDate.month(), 1, this.endDate.hour(), this.endDate.minute(), this.endDate.second()]),
  328. calendar: []
  329. };
  330. if (this.opens == 'right' || this.opens == 'center') {
  331. //swap calendar positions
  332. var first = this.container.find('.calendar.first');
  333. var second = this.container.find('.calendar.second');
  334. if (second.hasClass('single')) {
  335. second.removeClass('single');
  336. first.addClass('single');
  337. }
  338. first.removeClass('left').addClass('right');
  339. second.removeClass('right').addClass('left');
  340. if (this.singleDatePicker) {
  341. first.show();
  342. second.hide();
  343. }
  344. }
  345. if (typeof options.ranges === 'undefined' && !this.singleDatePicker) {
  346. this.container.addClass('show-calendar');
  347. }
  348. this.container.removeClass('opensleft opensright').addClass('opens' + this.opens);
  349. this.updateView();
  350. this.updateCalendars();
  351. //apply CSS classes and labels to buttons
  352. var c = this.container;
  353. $.each(this.buttonClasses, function (idx, val) {
  354. c.find('button').addClass(val);
  355. });
  356. this.container.find('.daterangepicker_start_input label').html(this.locale.fromLabel);
  357. this.container.find('.daterangepicker_end_input label').html(this.locale.toLabel);
  358. if (this.applyClass.length)
  359. this.container.find('.applyBtn').addClass(this.applyClass);
  360. if (this.cancelClass.length)
  361. this.container.find('.cancelBtn').addClass(this.cancelClass);
  362. this.container.find('.applyBtn').html(this.locale.applyLabel);
  363. this.container.find('.cancelBtn').html(this.locale.cancelLabel);
  364. },
  365. setStartDate: function(startDate) {
  366. if (typeof startDate === 'string')
  367. this.startDate = moment(startDate, this.format).utcOffset(this.timeZone);
  368. if (typeof startDate === 'object')
  369. this.startDate = moment(startDate);
  370. if (!this.timePicker)
  371. this.startDate = this.startDate.startOf('day');
  372. this.oldStartDate = this.startDate.clone();
  373. this.updateView();
  374. this.updateCalendars();
  375. this.updateInputText();
  376. },
  377. setEndDate: function(endDate) {
  378. if (typeof endDate === 'string')
  379. this.endDate = moment(endDate, this.format).utcOffset(this.timeZone);
  380. if (typeof endDate === 'object')
  381. this.endDate = moment(endDate);
  382. if (!this.timePicker)
  383. this.endDate = this.endDate.endOf('day');
  384. this.oldEndDate = this.endDate.clone();
  385. this.updateView();
  386. this.updateCalendars();
  387. this.updateInputText();
  388. },
  389. updateView: function () {
  390. this.leftCalendar.month.month(this.startDate.month()).year(this.startDate.year()).hour(this.startDate.hour()).minute(this.startDate.minute());
  391. this.rightCalendar.month.month(this.endDate.month()).year(this.endDate.year()).hour(this.endDate.hour()).minute(this.endDate.minute());
  392. this.updateFormInputs();
  393. },
  394. updateFormInputs: function () {
  395. this.container.find('input[name=daterangepicker_start]').val(this.startDate.format(this.format));
  396. this.container.find('input[name=daterangepicker_end]').val(this.endDate.format(this.format));
  397. if (this.startDate.isSame(this.endDate) || this.startDate.isBefore(this.endDate)) {
  398. this.container.find('button.applyBtn').removeAttr('disabled');
  399. } else {
  400. this.container.find('button.applyBtn').attr('disabled', 'disabled');
  401. }
  402. },
  403. updateFromControl: function () {
  404. if (!this.element.is('input')) return;
  405. if (!this.element.val().length) return;
  406. var dateString = this.element.val().split(this.separator),
  407. start = null,
  408. end = null;
  409. if(dateString.length === 2) {
  410. start = moment(dateString[0], this.format).utcOffset(this.timeZone);
  411. end = moment(dateString[1], this.format).utcOffset(this.timeZone);
  412. }
  413. if (this.singleDatePicker || start === null || end === null) {
  414. start = moment(this.element.val(), this.format).utcOffset(this.timeZone);
  415. end = start;
  416. }
  417. if (end.isBefore(start)) return;
  418. this.oldStartDate = this.startDate.clone();
  419. this.oldEndDate = this.endDate.clone();
  420. this.startDate = start;
  421. this.endDate = end;
  422. if (!this.startDate.isSame(this.oldStartDate) || !this.endDate.isSame(this.oldEndDate))
  423. this.notify();
  424. this.updateCalendars();
  425. },
  426. keydown: function (e) {
  427. //hide on tab or enter
  428. if ((e.keyCode === 9) || (e.keyCode === 13)) {
  429. this.hide();
  430. }
  431. },
  432. notify: function () {
  433. this.updateView();
  434. this.cb(this.startDate, this.endDate, this.chosenLabel);
  435. },
  436. move: function () {
  437. var parentOffset = { top: 0, left: 0 },
  438. containerTop;
  439. var parentRightEdge = $(window).width();
  440. if (!this.parentEl.is('body')) {
  441. parentOffset = {
  442. top: this.parentEl.offset().top - this.parentEl.scrollTop(),
  443. left: this.parentEl.offset().left - this.parentEl.scrollLeft()
  444. };
  445. parentRightEdge = this.parentEl[0].clientWidth + this.parentEl.offset().left;
  446. }
  447. if (this.drops == 'up')
  448. containerTop = this.element.offset().top - this.container.outerHeight() - parentOffset.top;
  449. else
  450. containerTop = this.element.offset().top + this.element.outerHeight() - parentOffset.top;
  451. this.container[this.drops == 'up' ? 'addClass' : 'removeClass']('dropup');
  452. if (this.opens == 'left') {
  453. this.container.css({
  454. top: containerTop,
  455. right: parentRightEdge - this.element.offset().left - this.element.outerWidth(),
  456. left: 'auto'
  457. });
  458. if (this.container.offset().left < 0) {
  459. this.container.css({
  460. right: 'auto',
  461. left: 9
  462. });
  463. }
  464. } else if (this.opens == 'center') {
  465. this.container.css({
  466. top: containerTop,
  467. left: this.element.offset().left - parentOffset.left + this.element.outerWidth() / 2
  468. - this.container.outerWidth() / 2,
  469. right: 'auto'
  470. });
  471. if (this.container.offset().left < 0) {
  472. this.container.css({
  473. right: 'auto',
  474. left: 9
  475. });
  476. }
  477. } else {
  478. this.container.css({
  479. top: containerTop,
  480. left: this.element.offset().left - parentOffset.left,
  481. right: 'auto'
  482. });
  483. if (this.container.offset().left + this.container.outerWidth() > $(window).width()) {
  484. this.container.css({
  485. left: 'auto',
  486. right: 0
  487. });
  488. }
  489. }
  490. },
  491. toggle: function (e) {
  492. if (this.element.hasClass('active')) {
  493. this.hide();
  494. } else {
  495. this.show();
  496. }
  497. },
  498. show: function (e) {
  499. if (this.isShowing) return;
  500. this.element.addClass('active');
  501. this.container.show();
  502. this.move();
  503. // Create a click proxy that is private to this instance of datepicker, for unbinding
  504. this._outsideClickProxy = $.proxy(function (e) { this.outsideClick(e); }, this);
  505. // Bind global datepicker mousedown for hiding and
  506. $(document)
  507. .on('mousedown.daterangepicker', this._outsideClickProxy)
  508. // also support mobile devices
  509. .on('touchend.daterangepicker', this._outsideClickProxy)
  510. // also explicitly play nice with Bootstrap dropdowns, which stopPropagation when clicking them
  511. .on('click.daterangepicker', '[data-toggle=dropdown]', this._outsideClickProxy)
  512. // and also close when focus changes to outside the picker (eg. tabbing between controls)
  513. .on('focusin.daterangepicker', this._outsideClickProxy);
  514. this.isShowing = true;
  515. this.element.trigger('show.daterangepicker', this);
  516. },
  517. outsideClick: function (e) {
  518. var target = $(e.target);
  519. // if the page is clicked anywhere except within the daterangerpicker/button
  520. // itself then call this.hide()
  521. if (
  522. // ie modal dialog fix
  523. e.type == "focusin" ||
  524. target.closest(this.element).length ||
  525. target.closest(this.container).length ||
  526. target.closest('.calendar-date').length
  527. ) return;
  528. this.hide();
  529. },
  530. hide: function (e) {
  531. if (!this.isShowing) return;
  532. $(document)
  533. .off('.daterangepicker');
  534. this.element.removeClass('active');
  535. this.container.hide();
  536. if (!this.startDate.isSame(this.oldStartDate) || !this.endDate.isSame(this.oldEndDate))
  537. this.notify();
  538. this.oldStartDate = this.startDate.clone();
  539. this.oldEndDate = this.endDate.clone();
  540. this.isShowing = false;
  541. this.element.trigger('hide.daterangepicker', this);
  542. },
  543. enterRange: function (e) {
  544. // mouse pointer has entered a range label
  545. var label = e.target.innerHTML;
  546. if (label == this.locale.customRangeLabel) {
  547. this.updateView();
  548. } else {
  549. var dates = this.ranges[label];
  550. this.container.find('input[name=daterangepicker_start]').val(dates[0].format(this.format));
  551. this.container.find('input[name=daterangepicker_end]').val(dates[1].format(this.format));
  552. }
  553. },
  554. showCalendars: function() {
  555. this.container.addClass('show-calendar');
  556. this.move();
  557. this.element.trigger('showCalendar.daterangepicker', this);
  558. },
  559. hideCalendars: function() {
  560. this.container.removeClass('show-calendar');
  561. this.element.trigger('hideCalendar.daterangepicker', this);
  562. },
  563. // when a date is typed into the start to end date textboxes
  564. inputsChanged: function (e) {
  565. var el = $(e.target);
  566. var date = moment(el.val(), this.format);
  567. if (!date.isValid()) return;
  568. var startDate, endDate;
  569. if (el.attr('name') === 'daterangepicker_start') {
  570. startDate = (false !== this.minDate && date.isBefore(this.minDate)) ? this.minDate : date;
  571. endDate = this.endDate;
  572. } else {
  573. startDate = this.startDate;
  574. endDate = (false !== this.maxDate && date.isAfter(this.maxDate)) ? this.maxDate : date;
  575. }
  576. this.setCustomDates(startDate, endDate);
  577. },
  578. inputsKeydown: function(e) {
  579. if (e.keyCode === 13) {
  580. this.inputsChanged(e);
  581. this.notify();
  582. }
  583. },
  584. updateInputText: function() {
  585. if (this.element.is('input') && !this.singleDatePicker) {
  586. this.element.val(this.startDate.format(this.format) + this.separator + this.endDate.format(this.format));
  587. this.element.trigger('change');
  588. } else if (this.element.is('input')) {
  589. this.element.val(this.endDate.format(this.format));
  590. this.element.trigger('change');
  591. }
  592. },
  593. clickRange: function (e) {
  594. var label = e.target.innerHTML;
  595. this.chosenLabel = label;
  596. if (label == this.locale.customRangeLabel) {
  597. this.showCalendars();
  598. } else {
  599. var dates = this.ranges[label];
  600. this.startDate = dates[0];
  601. this.endDate = dates[1];
  602. if (!this.timePicker) {
  603. this.startDate.startOf('day');
  604. this.endDate.endOf('day');
  605. }
  606. this.leftCalendar.month.month(this.startDate.month()).year(this.startDate.year()).hour(this.startDate.hour()).minute(this.startDate.minute());
  607. this.rightCalendar.month.month(this.endDate.month()).year(this.endDate.year()).hour(this.endDate.hour()).minute(this.endDate.minute());
  608. this.updateCalendars();
  609. this.updateInputText();
  610. this.hideCalendars();
  611. this.hide();
  612. this.element.trigger('apply.daterangepicker', this);
  613. }
  614. },
  615. clickPrev: function (e) {
  616. var cal = $(e.target).parents('.calendar');
  617. if (cal.hasClass('left')) {
  618. this.leftCalendar.month.subtract(1, 'month');
  619. } else {
  620. this.rightCalendar.month.subtract(1, 'month');
  621. }
  622. this.updateCalendars();
  623. },
  624. clickNext: function (e) {
  625. var cal = $(e.target).parents('.calendar');
  626. if (cal.hasClass('left')) {
  627. this.leftCalendar.month.add(1, 'month');
  628. } else {
  629. this.rightCalendar.month.add(1, 'month');
  630. }
  631. this.updateCalendars();
  632. },
  633. hoverDate: function (e) {
  634. var title = $(e.target).attr('data-title');
  635. var row = title.substr(1, 1);
  636. var col = title.substr(3, 1);
  637. var cal = $(e.target).parents('.calendar');
  638. if (cal.hasClass('left')) {
  639. this.container.find('input[name=daterangepicker_start]').val(this.leftCalendar.calendar[row][col].format(this.format));
  640. } else {
  641. this.container.find('input[name=daterangepicker_end]').val(this.rightCalendar.calendar[row][col].format(this.format));
  642. }
  643. },
  644. setCustomDates: function(startDate, endDate) {
  645. this.chosenLabel = this.locale.customRangeLabel;
  646. if (startDate.isAfter(endDate)) {
  647. var difference = this.endDate.diff(this.startDate);
  648. endDate = moment(startDate).add(difference, 'ms');
  649. if (this.maxDate && endDate.isAfter(this.maxDate)) {
  650. endDate = this.maxDate.clone();
  651. }
  652. }
  653. this.startDate = startDate;
  654. this.endDate = endDate;
  655. this.updateView();
  656. this.updateCalendars();
  657. },
  658. clickDate: function (e) {
  659. var title = $(e.target).attr('data-title');
  660. var row = title.substr(1, 1);
  661. var col = title.substr(3, 1);
  662. var cal = $(e.target).parents('.calendar');
  663. var startDate, endDate;
  664. if (cal.hasClass('left')) {
  665. startDate = this.leftCalendar.calendar[row][col];
  666. endDate = this.endDate;
  667. if (typeof this.dateLimit === 'object') {
  668. var maxDate = moment(startDate).add(this.dateLimit).startOf('day');
  669. if (endDate.isAfter(maxDate)) {
  670. endDate = maxDate;
  671. }
  672. }
  673. } else {
  674. startDate = this.startDate;
  675. endDate = this.rightCalendar.calendar[row][col];
  676. if (typeof this.dateLimit === 'object') {
  677. var minDate = moment(endDate).subtract(this.dateLimit).startOf('day');
  678. if (startDate.isBefore(minDate)) {
  679. startDate = minDate;
  680. }
  681. }
  682. }
  683. if (this.singleDatePicker && cal.hasClass('left')) {
  684. endDate = startDate.clone();
  685. } else if (this.singleDatePicker && cal.hasClass('right')) {
  686. startDate = endDate.clone();
  687. }
  688. cal.find('td').removeClass('active');
  689. $(e.target).addClass('active');
  690. this.setCustomDates(startDate, endDate);
  691. if (!this.timePicker)
  692. endDate.endOf('day');
  693. if (this.singleDatePicker && !this.timePicker)
  694. this.clickApply();
  695. },
  696. clickApply: function (e) {
  697. this.updateInputText();
  698. this.hide();
  699. this.element.trigger('apply.daterangepicker', this);
  700. },
  701. clickCancel: function (e) {
  702. this.startDate = this.oldStartDate;
  703. this.endDate = this.oldEndDate;
  704. this.chosenLabel = this.oldChosenLabel;
  705. this.updateView();
  706. this.updateCalendars();
  707. this.hide();
  708. this.element.trigger('cancel.daterangepicker', this);
  709. },
  710. updateMonthYear: function (e) {
  711. var isLeft = $(e.target).closest('.calendar').hasClass('left'),
  712. leftOrRight = isLeft ? 'left' : 'right',
  713. cal = this.container.find('.calendar.'+leftOrRight);
  714. // Month must be Number for new moment versions
  715. var month = parseInt(cal.find('.monthselect').val(), 10);
  716. var year = cal.find('.yearselect').val();
  717. if (!isLeft && !this.singleDatePicker) {
  718. if (year < this.startDate.year() || (year == this.startDate.year() && month < this.startDate.month())) {
  719. month = this.startDate.month();
  720. year = this.startDate.year();
  721. }
  722. }
  723. if (this.minDate) {
  724. if (year < this.minDate.year() || (year == this.minDate.year() && month < this.minDate.month())) {
  725. month = this.minDate.month();
  726. year = this.minDate.year();
  727. }
  728. }
  729. if (this.maxDate) {
  730. if (year > this.maxDate.year() || (year == this.maxDate.year() && month > this.maxDate.month())) {
  731. month = this.maxDate.month();
  732. year = this.maxDate.year();
  733. }
  734. }
  735. this[leftOrRight+'Calendar'].month.month(month).year(year);
  736. this.updateCalendars();
  737. },
  738. updateTime: function(e) {
  739. var cal = $(e.target).closest('.calendar'),
  740. isLeft = cal.hasClass('left');
  741. var hour = parseInt(cal.find('.hourselect').val(), 10);
  742. var minute = parseInt(cal.find('.minuteselect').val(), 10);
  743. var second = 0;
  744. if (this.timePickerSeconds) {
  745. second = parseInt(cal.find('.secondselect').val(), 10);
  746. }
  747. if (this.timePicker12Hour) {
  748. var ampm = cal.find('.ampmselect').val();
  749. if (ampm === 'PM' && hour < 12)
  750. hour += 12;
  751. if (ampm === 'AM' && hour === 12)
  752. hour = 0;
  753. }
  754. if (isLeft) {
  755. var start = this.startDate.clone();
  756. start.hour(hour);
  757. start.minute(minute);
  758. start.second(second);
  759. this.startDate = start;
  760. this.leftCalendar.month.hour(hour).minute(minute).second(second);
  761. if (this.singleDatePicker)
  762. this.endDate = start.clone();
  763. } else {
  764. var end = this.endDate.clone();
  765. end.hour(hour);
  766. end.minute(minute);
  767. end.second(second);
  768. this.endDate = end;
  769. if (this.singleDatePicker)
  770. this.startDate = end.clone();
  771. this.rightCalendar.month.hour(hour).minute(minute).second(second);
  772. }
  773. this.updateView();
  774. this.updateCalendars();
  775. },
  776. updateCalendars: function () {
  777. this.leftCalendar.calendar = this.buildCalendar(this.leftCalendar.month.month(), this.leftCalendar.month.year(), this.leftCalendar.month.hour(), this.leftCalendar.month.minute(), this.leftCalendar.month.second(), 'left');
  778. this.rightCalendar.calendar = this.buildCalendar(this.rightCalendar.month.month(), this.rightCalendar.month.year(), this.rightCalendar.month.hour(), this.rightCalendar.month.minute(), this.rightCalendar.month.second(), 'right');
  779. this.container.find('.calendar.left').empty().html(this.renderCalendar(this.leftCalendar.calendar, this.startDate, this.minDate, this.maxDate, 'left'));
  780. this.container.find('.calendar.right').empty().html(this.renderCalendar(this.rightCalendar.calendar, this.endDate, this.singleDatePicker ? this.minDate : this.startDate, this.maxDate, 'right'));
  781. this.container.find('.ranges li').removeClass('active');
  782. var customRange = true;
  783. var i = 0;
  784. for (var range in this.ranges) {
  785. if (this.timePicker) {
  786. if (this.startDate.isSame(this.ranges[range][0]) && this.endDate.isSame(this.ranges[range][1])) {
  787. customRange = false;
  788. this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')')
  789. .addClass('active').html();
  790. }
  791. } else {
  792. //ignore times when comparing dates if time picker is not enabled
  793. 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')) {
  794. customRange = false;
  795. this.chosenLabel = this.container.find('.ranges li:eq(' + i + ')')
  796. .addClass('active').html();
  797. }
  798. }
  799. i++;
  800. }
  801. if (customRange) {
  802. this.chosenLabel = this.container.find('.ranges li:last').addClass('active').html();
  803. this.showCalendars();
  804. }
  805. },
  806. buildCalendar: function (month, year, hour, minute, second, side) {
  807. var daysInMonth = moment([year, month]).daysInMonth();
  808. var firstDay = moment([year, month, 1]);
  809. var lastDay = moment([year, month, daysInMonth]);
  810. var lastMonth = moment(firstDay).subtract(1, 'month').month();
  811. var lastYear = moment(firstDay).subtract(1, 'month').year();
  812. var daysInLastMonth = moment([lastYear, lastMonth]).daysInMonth();
  813. var dayOfWeek = firstDay.day();
  814. var i;
  815. //initialize a 6 rows x 7 columns array for the calendar
  816. var calendar = [];
  817. calendar.firstDay = firstDay;
  818. calendar.lastDay = lastDay;
  819. for (i = 0; i < 6; i++) {
  820. calendar[i] = [];
  821. }
  822. //populate the calendar with date objects
  823. var startDay = daysInLastMonth - dayOfWeek + this.locale.firstDay + 1;
  824. if (startDay > daysInLastMonth)
  825. startDay -= 7;
  826. if (dayOfWeek == this.locale.firstDay)
  827. startDay = daysInLastMonth - 6;
  828. var curDate = moment([lastYear, lastMonth, startDay, 12, minute, second]).utcOffset(this.timeZone);
  829. var col, row;
  830. for (i = 0, col = 0, row = 0; i < 42; i++, col++, curDate = moment(curDate).add(24, 'hour')) {
  831. if (i > 0 && col % 7 === 0) {
  832. col = 0;
  833. row++;
  834. }
  835. calendar[row][col] = curDate.clone().hour(hour);
  836. curDate.hour(12);
  837. if (this.minDate && calendar[row][col].format('YYYY-MM-DD') == this.minDate.format('YYYY-MM-DD') && calendar[row][col].isBefore(this.minDate) && side == 'left') {
  838. calendar[row][col] = this.minDate.clone();
  839. }
  840. if (this.maxDate && calendar[row][col].format('YYYY-MM-DD') == this.maxDate.format('YYYY-MM-DD') && calendar[row][col].isAfter(this.maxDate) && side == 'right') {
  841. calendar[row][col] = this.maxDate.clone();
  842. }
  843. }
  844. return calendar;
  845. },
  846. renderDropdowns: function (selected, minDate, maxDate) {
  847. var currentMonth = selected.month();
  848. var currentYear = selected.year();
  849. var maxYear = (maxDate && maxDate.year()) || (currentYear + 5);
  850. var minYear = (minDate && minDate.year()) || (currentYear - 50);
  851. var monthHtml = '<select class="monthselect">';
  852. var inMinYear = currentYear == minYear;
  853. var inMaxYear = currentYear == maxYear;
  854. for (var m = 0; m < 12; m++) {
  855. if ((!inMinYear || m >= minDate.month()) && (!inMaxYear || m <= maxDate.month())) {
  856. monthHtml += "<option value='" + m + "'" +
  857. (m === currentMonth ? " selected='selected'" : "") +
  858. ">" + this.locale.monthNames[m] + "</option>";
  859. }
  860. }
  861. monthHtml += "</select>";
  862. var yearHtml = '<select class="yearselect">';
  863. for (var y = minYear; y <= maxYear; y++) {
  864. yearHtml += '<option value="' + y + '"' +
  865. (y === currentYear ? ' selected="selected"' : '') +
  866. '>' + y + '</option>';
  867. }
  868. yearHtml += '</select>';
  869. return monthHtml + yearHtml;
  870. },
  871. renderCalendar: function (calendar, selected, minDate, maxDate, side) {
  872. var html = '<div class="calendar-date">';
  873. html += '<table class="table-condensed">';
  874. html += '<thead>';
  875. html += '<tr>';
  876. // add empty cell for week number
  877. if (this.showWeekNumbers)
  878. html += '<th></th>';
  879. if (!minDate || minDate.isBefore(calendar.firstDay)) {
  880. html += '<th class="prev available"><i class="fa fa-arrow-left icon icon-arrow-left glyphicon glyphicon-arrow-left"></i></th>';
  881. } else {
  882. html += '<th></th>';
  883. }
  884. var dateHtml = this.locale.monthNames[calendar[1][1].month()] + calendar[1][1].format(" YYYY");
  885. if (this.showDropdowns) {
  886. dateHtml = this.renderDropdowns(calendar[1][1], minDate, maxDate);
  887. }
  888. html += '<th colspan="5" class="month">' + dateHtml + '</th>';
  889. if (!maxDate || maxDate.isAfter(calendar.lastDay)) {
  890. html += '<th class="next available"><i class="fa fa-arrow-right icon icon-arrow-right glyphicon glyphicon-arrow-right"></i></th>';
  891. } else {
  892. html += '<th></th>';
  893. }
  894. html += '</tr>';
  895. html += '<tr>';
  896. // add week number label
  897. if (this.showWeekNumbers)
  898. html += '<th class="week">' + this.locale.weekLabel + '</th>';
  899. $.each(this.locale.daysOfWeek, function (index, dayOfWeek) {
  900. html += '<th>' + dayOfWeek + '</th>';
  901. });
  902. html += '</tr>';
  903. html += '</thead>';
  904. html += '<tbody>';
  905. for (var row = 0; row < 6; row++) {
  906. html += '<tr>';
  907. // add week number
  908. if (this.showWeekNumbers)
  909. html += '<td class="week">' + calendar[row][0].week() + '</td>';
  910. for (var col = 0; col < 7; col++) {
  911. var cname = 'available ';
  912. cname += (calendar[row][col].month() == calendar[1][1].month()) ? '' : 'off';
  913. if ((minDate && calendar[row][col].isBefore(minDate, 'day')) || (maxDate && calendar[row][col].isAfter(maxDate, 'day'))) {
  914. cname = ' off disabled ';
  915. } else if (calendar[row][col].format('YYYY-MM-DD') == selected.format('YYYY-MM-DD')) {
  916. cname += ' active ';
  917. if (calendar[row][col].format('YYYY-MM-DD') == this.startDate.format('YYYY-MM-DD')) {
  918. cname += ' start-date ';
  919. }
  920. if (calendar[row][col].format('YYYY-MM-DD') == this.endDate.format('YYYY-MM-DD')) {
  921. cname += ' end-date ';
  922. }
  923. } else if (calendar[row][col] >= this.startDate && calendar[row][col] <= this.endDate) {
  924. cname += ' in-range ';
  925. if (calendar[row][col].isSame(this.startDate)) { cname += ' start-date '; }
  926. if (calendar[row][col].isSame(this.endDate)) { cname += ' end-date '; }
  927. }
  928. var title = 'r' + row + 'c' + col;
  929. html += '<td class="' + cname.replace(/\s+/g, ' ').replace(/^\s?(.*?)\s?$/, '$1') + '" data-title="' + title + '">' + calendar[row][col].date() + '</td>';
  930. }
  931. html += '</tr>';
  932. }
  933. html += '</tbody>';
  934. html += '</table>';
  935. html += '</div>';
  936. var i;
  937. if (this.timePicker) {
  938. html += '<div class="calendar-time">';
  939. html += '<select class="hourselect">';
  940. // Disallow selections before the minDate or after the maxDate
  941. var min_hour = 0;
  942. var max_hour = 23;
  943. if (minDate && (side == 'left' || this.singleDatePicker) && selected.format('YYYY-MM-DD') == minDate.format('YYYY-MM-DD')) {
  944. min_hour = minDate.hour();
  945. if (selected.hour() < min_hour)
  946. selected.hour(min_hour);
  947. if (this.timePicker12Hour && min_hour >= 12 && selected.hour() >= 12)
  948. min_hour -= 12;
  949. if (this.timePicker12Hour && min_hour == 12)
  950. min_hour = 1;
  951. }
  952. if (maxDate && (side == 'right' || this.singleDatePicker) && selected.format('YYYY-MM-DD') == maxDate.format('YYYY-MM-DD')) {
  953. max_hour = maxDate.hour();
  954. if (selected.hour() > max_hour)
  955. selected.hour(max_hour);
  956. if (this.timePicker12Hour && max_hour >= 12 && selected.hour() >= 12)
  957. max_hour -= 12;
  958. }
  959. var start = 0;
  960. var end = 23;
  961. var selected_hour = selected.hour();
  962. if (this.timePicker12Hour) {
  963. start = 1;
  964. end = 12;
  965. if (selected_hour >= 12)
  966. selected_hour -= 12;
  967. if (selected_hour === 0)
  968. selected_hour = 12;
  969. }
  970. for (i = start; i <= end; i++) {
  971. if (i == selected_hour) {
  972. html += '<option value="' + i + '" selected="selected">' + i + '</option>';
  973. } else if (i < min_hour || i > max_hour) {
  974. html += '<option value="' + i + '" disabled="disabled" class="disabled">' + i + '</option>';
  975. } else {
  976. html += '<option value="' + i + '">' + i + '</option>';
  977. }
  978. }
  979. html += '</select> : ';
  980. html += '<select class="minuteselect">';
  981. // Disallow selections before the minDate or after the maxDate
  982. var min_minute = 0;
  983. var max_minute = 59;
  984. if (minDate && (side == 'left' || this.singleDatePicker) && selected.format('YYYY-MM-DD h A') == minDate.format('YYYY-MM-DD h A')) {
  985. min_minute = minDate.minute();
  986. if (selected.minute() < min_minute)
  987. selected.minute(min_minute);
  988. }
  989. if (maxDate && (side == 'right' || this.singleDatePicker) && selected.format('YYYY-MM-DD h A') == maxDate.format('YYYY-MM-DD h A')) {
  990. max_minute = maxDate.minute();
  991. if (selected.minute() > max_minute)
  992. selected.minute(max_minute);
  993. }
  994. for (i = 0; i < 60; i += this.timePickerIncrement) {
  995. var num = i;
  996. if (num < 10)
  997. num = '0' + num;
  998. if (i == selected.minute()) {
  999. html += '<option value="' + i + '" selected="selected">' + num + '</option>';
  1000. } else if (i < min_minute || i > max_minute) {
  1001. html += '<option value="' + i + '" disabled="disabled" class="disabled">' + num + '</option>';
  1002. } else {
  1003. html += '<option value="' + i + '">' + num + '</option>';
  1004. }
  1005. }
  1006. html += '</select> ';
  1007. if (this.timePickerSeconds) {
  1008. html += ': <select class="secondselect">';
  1009. for (i = 0; i < 60; i += this.timePickerIncrement) {
  1010. var num = i;
  1011. if (num < 10)
  1012. num = '0' + num;
  1013. if (i == selected.second()) {
  1014. html += '<option value="' + i + '" selected="selected">' + num + '</option>';
  1015. } else {
  1016. html += '<option value="' + i + '">' + num + '</option>';
  1017. }
  1018. }
  1019. html += '</select>';
  1020. }
  1021. if (this.timePicker12Hour) {
  1022. html += '<select class="ampmselect">';
  1023. // Disallow selection before the minDate or after the maxDate
  1024. var am_html = '';
  1025. var pm_html = '';
  1026. if (minDate && (side == 'left' || this.singleDatePicker) && selected.format('YYYY-MM-DD') == minDate.format('YYYY-MM-DD') && minDate.hour() >= 12) {
  1027. am_html = ' disabled="disabled" class="disabled"';
  1028. }
  1029. if (maxDate && (side == 'right' || this.singleDatePicker) && selected.format('YYYY-MM-DD') == maxDate.format('YYYY-MM-DD') && maxDate.hour() < 12) {
  1030. pm_html = ' disabled="disabled" class="disabled"';
  1031. }
  1032. if (selected.hour() >= 12) {
  1033. html += '<option value="AM"' + am_html + '>AM</option><option value="PM" selected="selected"' + pm_html + '>PM</option>';
  1034. } else {
  1035. html += '<option value="AM" selected="selected"' + am_html + '>AM</option><option value="PM"' + pm_html + '>PM</option>';
  1036. }
  1037. html += '</select>';
  1038. }
  1039. html += '</div>';
  1040. }
  1041. return html;
  1042. },
  1043. remove: function() {
  1044. this.container.remove();
  1045. this.element.off('.daterangepicker');
  1046. this.element.removeData('daterangepicker');
  1047. }
  1048. };
  1049. $.fn.daterangepicker = function (options, cb) {
  1050. this.each(function () {
  1051. var el = $(this);
  1052. if (el.data('daterangepicker'))
  1053. el.data('daterangepicker').remove();
  1054. el.data('daterangepicker', new DateRangePicker(el, options, cb));
  1055. });
  1056. return this;
  1057. };
  1058. }));