daterangepicker.js 53 KB

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