datepicker.js 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. /*!
  2. * Datepicker v1.0.10
  3. * https://fengyuanchen.github.io/datepicker
  4. *
  5. * Copyright 2014-present Chen Fengyuan
  6. * Released under the MIT license
  7. *
  8. * Date: 2020-09-29T14:46:10.983Z
  9. */
  10. (function(global, factory) {
  11. typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('jquery')) :
  12. typeof define === 'function' && define.amd ? define(['jquery'], factory) :
  13. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.jQuery));
  14. }(this, (function($) {
  15. 'use strict';
  16. $ = $ && $.hasOwnProperty('default') ? $['default'] : $;
  17. function _classCallCheck(instance, Constructor) {
  18. if (!(instance instanceof Constructor)) {
  19. throw new TypeError("Cannot call a class as a function");
  20. }
  21. }
  22. function _defineProperties(target, props) {
  23. for (var i = 0; i < props.length; i++) {
  24. var descriptor = props[i];
  25. descriptor.enumerable = descriptor.enumerable || false;
  26. descriptor.configurable = true;
  27. if ("value" in descriptor) descriptor.writable = true;
  28. Object.defineProperty(target, descriptor.key, descriptor);
  29. }
  30. }
  31. function _createClass(Constructor, protoProps, staticProps) {
  32. if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  33. if (staticProps) _defineProperties(Constructor, staticProps);
  34. return Constructor;
  35. }
  36. var DEFAULTS = {
  37. // Show the datepicker automatically when initialized
  38. autoShow: false,
  39. // Hide the datepicker automatically when picked
  40. autoHide: false,
  41. // Pick the initial date automatically when initialized
  42. autoPick: false,
  43. // Enable inline mode
  44. inline: false,
  45. // A element (or selector) for putting the datepicker
  46. container: null,
  47. // A element (or selector) for triggering the datepicker
  48. trigger: null,
  49. // The ISO language code (built-in: en-US)
  50. language: '',
  51. // The date string format
  52. format: 'mm/dd/yyyy',
  53. // The initial date
  54. date: null,
  55. // The start view date
  56. startDate: null,
  57. // The end view date
  58. endDate: null,
  59. // The start view when initialized
  60. startView: 0,
  61. // 0 for days, 1 for months, 2 for years
  62. // The start day of the week
  63. // 0 for Sunday, 1 for Monday, 2 for Tuesday, 3 for Wednesday,
  64. // 4 for Thursday, 5 for Friday, 6 for Saturday
  65. weekStart: 0,
  66. // Show year before month on the datepicker header
  67. yearFirst: false,
  68. // A string suffix to the year number.
  69. yearSuffix: '',
  70. // Days' name of the week.
  71. days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  72. // Shorter days' name
  73. daysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
  74. // Shortest days' name
  75. daysMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
  76. // Months' name
  77. months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
  78. // Shorter months' name
  79. monthsShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  80. // A element tag for each item of years, months and days
  81. itemTag: 'li',
  82. // A class (CSS) for muted date item
  83. mutedClass: 'muted',
  84. // A class (CSS) for picked date item
  85. pickedClass: 'picked',
  86. // A class (CSS) for disabled date item
  87. disabledClass: 'disabled',
  88. // A class (CSS) for highlight date item
  89. highlightedClass: 'highlighted',
  90. // The template of the datepicker
  91. template: '<div class="datepicker-container">' + '<div class="datepicker-panel" data-view="years picker">' + '<ul>' + '<li data-view="years prev">&lsaquo;</li>' + '<li data-view="years current"></li>' + '<li data-view="years next">&rsaquo;</li>' + '</ul>' + '<ul data-view="years"></ul>' + '</div>' + '<div class="datepicker-panel" data-view="months picker">' + '<ul>' + '<li data-view="year prev">&lsaquo;</li>' + '<li data-view="year current"></li>' + '<li data-view="year next">&rsaquo;</li>' + '</ul>' + '<ul data-view="months"></ul>' + '</div>' + '<div class="datepicker-panel" data-view="days picker">' + '<ul>' + '<li data-view="month prev">&lsaquo;</li>' + '<li data-view="month current"></li>' + '<li data-view="month next">&rsaquo;</li>' + '</ul>' + '<ul data-view="week"></ul>' + '<ul data-view="days"></ul>' + '</div>' + '</div>',
  92. // The offset top or bottom of the datepicker from the element
  93. offset: 10,
  94. // The `z-index` of the datepicker
  95. zIndex: 1000000,
  96. // Filter each date item (return `false` to disable a date item)
  97. filter: null,
  98. // Event shortcuts
  99. show: null,
  100. hide: null,
  101. pick: null
  102. };
  103. var IS_BROWSER = typeof window !== 'undefined';
  104. var WINDOW = IS_BROWSER ? window : {};
  105. var IS_TOUCH_DEVICE = IS_BROWSER ? 'ontouchstart' in WINDOW.document.documentElement : false;
  106. var NAMESPACE = 'datepicker';
  107. var EVENT_CLICK = "click.".concat(NAMESPACE);
  108. var EVENT_FOCUS = "focus.".concat(NAMESPACE);
  109. var EVENT_HIDE = "hide.".concat(NAMESPACE);
  110. var EVENT_KEYUP = "keyup.".concat(NAMESPACE);
  111. var EVENT_PICK = "pick.".concat(NAMESPACE);
  112. var EVENT_RESIZE = "resize.".concat(NAMESPACE);
  113. var EVENT_SCROLL = "scroll.".concat(NAMESPACE);
  114. var EVENT_SHOW = "show.".concat(NAMESPACE);
  115. var EVENT_TOUCH_START = "touchstart.".concat(NAMESPACE);
  116. var CLASS_HIDE = "".concat(NAMESPACE, "-hide");
  117. var LANGUAGES = {};
  118. var VIEWS = {
  119. DAYS: 0,
  120. MONTHS: 1,
  121. YEARS: 2
  122. };
  123. var toString = Object.prototype.toString;
  124. function typeOf(obj) {
  125. return toString.call(obj).slice(8, -1).toLowerCase();
  126. }
  127. function isString(value) {
  128. return typeof value === 'string';
  129. }
  130. var isNaN = Number.isNaN || WINDOW.isNaN;
  131. function isNumber(value) {
  132. return typeof value === 'number' && !isNaN(value);
  133. }
  134. function isUndefined(value) {
  135. return typeof value === 'undefined';
  136. }
  137. function isDate(value) {
  138. return typeOf(value) === 'date' && !isNaN(value.getTime());
  139. }
  140. function proxy(fn, context) {
  141. for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
  142. args[_key - 2] = arguments[_key];
  143. }
  144. return function() {
  145. for (var _len2 = arguments.length, args2 = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  146. args2[_key2] = arguments[_key2];
  147. }
  148. return fn.apply(context, args.concat(args2));
  149. };
  150. }
  151. function selectorOf(view) {
  152. return "[data-view=\"".concat(view, "\"]");
  153. }
  154. function isLeapYear(year) {
  155. return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
  156. }
  157. function getDaysInMonth(year, month) {
  158. return [31, isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
  159. }
  160. function getMinDay(year, month, day) {
  161. return Math.min(day, getDaysInMonth(year, month));
  162. }
  163. var formatParts = /(y|m|d)+/g;
  164. function parseFormat(format) {
  165. var source = String(format).toLowerCase();
  166. var parts = source.match(formatParts);
  167. if (!parts || parts.length === 0) {
  168. throw new Error('Invalid date format.');
  169. }
  170. format = {
  171. source: source,
  172. parts: parts
  173. };
  174. $.each(parts, function(i, part) {
  175. switch (part) {
  176. case 'dd':
  177. case 'd':
  178. format.hasDay = true;
  179. break;
  180. case 'mm':
  181. case 'm':
  182. format.hasMonth = true;
  183. break;
  184. case 'yyyy':
  185. case 'yy':
  186. format.hasYear = true;
  187. break;
  188. }
  189. });
  190. return format;
  191. }
  192. function getScrollParent(element) {
  193. var includeHidden = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  194. var $element = $(element);
  195. var position = $element.css('position');
  196. var excludeStaticParent = position === 'absolute';
  197. var overflowRegex = includeHidden ? /auto|scroll|hidden/ : /auto|scroll/;
  198. var scrollParent = $element.parents().filter(function(index, parent) {
  199. var $parent = $(parent);
  200. if (excludeStaticParent && $parent.css('position') === 'static') {
  201. return false;
  202. }
  203. return overflowRegex.test($parent.css('overflow') + $parent.css('overflow-y') + $parent.css('overflow-x'));
  204. }).eq(0);
  205. return position === 'fixed' || !scrollParent.length ? $(element.ownerDocument || document) : scrollParent;
  206. }
  207. /**
  208. * Add leading zeroes to the given value
  209. * @param {number} value - The value to add.
  210. * @param {number} [length=1] - The expected value length.
  211. * @returns {string} Returns converted value.
  212. */
  213. function addLeadingZero(value) {
  214. var length = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  215. var str = String(Math.abs(value));
  216. var i = str.length;
  217. var result = '';
  218. if (value < 0) {
  219. result += '-';
  220. }
  221. while (i < length) {
  222. i += 1;
  223. result += '0';
  224. }
  225. return result + str;
  226. }
  227. var REGEXP_DIGITS = /\d+/g;
  228. var methods = {
  229. // Show the datepicker
  230. show: function show() {
  231. if (!this.built) {
  232. this.build();
  233. }
  234. if (this.shown) {
  235. return;
  236. }
  237. if (this.trigger(EVENT_SHOW).isDefaultPrevented()) {
  238. return;
  239. }
  240. this.shown = true;
  241. this.$picker.removeClass(CLASS_HIDE).on(EVENT_CLICK, $.proxy(this.click, this));
  242. this.showView(this.options.startView);
  243. if (!this.inline) {
  244. this.$scrollParent.on(EVENT_SCROLL, $.proxy(this.place, this));
  245. $(window).on(EVENT_RESIZE, this.onResize = proxy(this.place, this));
  246. $(document).on(EVENT_CLICK, this.onGlobalClick = proxy(this.globalClick, this));
  247. $(document).on(EVENT_KEYUP, this.onGlobalKeyup = proxy(this.globalKeyup, this));
  248. if (IS_TOUCH_DEVICE) {
  249. $(document).on(EVENT_TOUCH_START, this.onTouchStart = proxy(this.touchstart, this));
  250. }
  251. this.place();
  252. }
  253. },
  254. // Hide the datepicker
  255. hide: function hide() {
  256. if (!this.shown) {
  257. return;
  258. }
  259. if (this.trigger(EVENT_HIDE).isDefaultPrevented()) {
  260. return;
  261. }
  262. this.shown = false;
  263. this.$picker.addClass(CLASS_HIDE).off(EVENT_CLICK, this.click);
  264. if (!this.inline) {
  265. this.$scrollParent.off(EVENT_SCROLL, this.place);
  266. $(window).off(EVENT_RESIZE, this.onResize);
  267. $(document).off(EVENT_CLICK, this.onGlobalClick);
  268. $(document).off(EVENT_KEYUP, this.onGlobalKeyup);
  269. if (IS_TOUCH_DEVICE) {
  270. $(document).off(EVENT_TOUCH_START, this.onTouchStart);
  271. }
  272. }
  273. },
  274. toggle: function toggle() {
  275. if (this.shown) {
  276. this.hide();
  277. } else {
  278. this.show();
  279. }
  280. },
  281. // Update the datepicker with the current input value
  282. update: function update() {
  283. var value = this.getValue();
  284. if (value === this.oldValue) {
  285. return;
  286. }
  287. this.setDate(value, true);
  288. this.oldValue = value;
  289. },
  290. /**
  291. * Pick the current date to the element
  292. *
  293. * @param {String} _view (private)
  294. */
  295. pick: function pick(_view) {
  296. var $this = this.$element;
  297. var date = this.date;
  298. if (this.trigger(EVENT_PICK, {
  299. view: _view || '',
  300. date: date
  301. }).isDefaultPrevented()) {
  302. return;
  303. }
  304. date = this.formatDate(this.date);
  305. this.setValue(date);
  306. if (this.isInput) {
  307. $this.trigger('input');
  308. $this.trigger('change');
  309. }
  310. },
  311. // Reset the datepicker
  312. reset: function reset() {
  313. this.setDate(this.initialDate, true);
  314. this.setValue(this.initialValue);
  315. if (this.shown) {
  316. this.showView(this.options.startView);
  317. }
  318. },
  319. /**
  320. * Get the month name with given argument or the current date
  321. *
  322. * @param {Number} month (optional)
  323. * @param {Boolean} shortForm (optional)
  324. * @return {String} (month name)
  325. */
  326. getMonthName: function getMonthName(month, shortForm) {
  327. var options = this.options;
  328. var monthsShort = options.monthsShort;
  329. var months = options.months;
  330. if ($.isNumeric(month)) {
  331. month = Number(month);
  332. } else if (isUndefined(shortForm)) {
  333. shortForm = month;
  334. }
  335. if (shortForm === true) {
  336. months = monthsShort;
  337. }
  338. return months[isNumber(month) ? month : this.date.getMonth()];
  339. },
  340. /**
  341. * Get the day name with given argument or the current date
  342. *
  343. * @param {Number} day (optional)
  344. * @param {Boolean} shortForm (optional)
  345. * @param {Boolean} min (optional)
  346. * @return {String} (day name)
  347. */
  348. getDayName: function getDayName(day, shortForm, min) {
  349. var options = this.options;
  350. var days = options.days;
  351. if ($.isNumeric(day)) {
  352. day = Number(day);
  353. } else {
  354. if (isUndefined(min)) {
  355. min = shortForm;
  356. }
  357. if (isUndefined(shortForm)) {
  358. shortForm = day;
  359. }
  360. }
  361. if (min) {
  362. days = options.daysMin;
  363. } else if (shortForm) {
  364. days = options.daysShort;
  365. }
  366. return days[isNumber(day) ? day : this.date.getDay()];
  367. },
  368. /**
  369. * Get the current date
  370. *
  371. * @param {Boolean} formatted (optional)
  372. * @return {Date|String} (date)
  373. */
  374. getDate: function getDate(formatted) {
  375. var date = this.date;
  376. return formatted ? this.formatDate(date) : new Date(date);
  377. },
  378. /**
  379. * Set the current date with a new date
  380. *
  381. * @param {Date} date
  382. * @param {Boolean} _updated (private)
  383. */
  384. setDate: function setDate(date, _updated) {
  385. var filter = this.options.filter;
  386. if (isDate(date) || isString(date)) {
  387. date = this.parseDate(date);
  388. if ($.isFunction(filter) && filter.call(this.$element, date, 'day') === false) {
  389. return;
  390. }
  391. this.date = date;
  392. this.viewDate = new Date(date);
  393. if (!_updated) {
  394. this.pick();
  395. }
  396. if (this.built) {
  397. this.render();
  398. }
  399. }
  400. },
  401. /**
  402. * Set the start view date with a new date
  403. *
  404. * @param {Date|string|null} date
  405. */
  406. setStartDate: function setStartDate(date) {
  407. if (isDate(date) || isString(date)) {
  408. this.startDate = this.parseDate(date);
  409. } else {
  410. this.startDate = null;
  411. }
  412. if (this.built) {
  413. this.render();
  414. }
  415. },
  416. /**
  417. * Set the end view date with a new date
  418. *
  419. * @param {Date|string|null} date
  420. */
  421. setEndDate: function setEndDate(date) {
  422. if (isDate(date) || isString(date)) {
  423. this.endDate = this.parseDate(date);
  424. } else {
  425. this.endDate = null;
  426. }
  427. if (this.built) {
  428. this.render();
  429. }
  430. },
  431. /**
  432. * Parse a date string with the set date format
  433. *
  434. * @param {String} date
  435. * @return {Date} (parsed date)
  436. */
  437. parseDate: function parseDate(date) {
  438. var format = this.format;
  439. var parts = [];
  440. if (!isDate(date)) {
  441. if (isString(date)) {
  442. parts = date.match(REGEXP_DIGITS) || [];
  443. }
  444. date = date ? new Date(date) : new Date();
  445. if (!isDate(date)) {
  446. date = new Date();
  447. }
  448. if (parts.length === format.parts.length) {
  449. // Set year and month first
  450. $.each(parts, function(i, part) {
  451. var value = parseInt(part, 10);
  452. switch (format.parts[i]) {
  453. case 'yy':
  454. date.setFullYear(2000 + value);
  455. break;
  456. case 'yyyy':
  457. // Converts 2-digit year to 2000+
  458. date.setFullYear(part.length === 2 ? 2000 + value : value);
  459. break;
  460. case 'mm':
  461. case 'm':
  462. date.setMonth(value - 1);
  463. break;
  464. }
  465. }); // Set day in the last to avoid converting `31/10/2019` to `01/10/2019`
  466. $.each(parts, function(i, part) {
  467. var value = parseInt(part, 10);
  468. switch (format.parts[i]) {
  469. case 'dd':
  470. case 'd':
  471. date.setDate(value);
  472. break;
  473. }
  474. });
  475. }
  476. } // Ignore hours, minutes, seconds and milliseconds to avoid side effect (#192)
  477. return new Date(date.getFullYear(), date.getMonth(), date.getDate());
  478. },
  479. /**
  480. * Format a date object to a string with the set date format
  481. *
  482. * @param {Date} date
  483. * @return {String} (formatted date)
  484. */
  485. formatDate: function formatDate(date) {
  486. var format = this.format;
  487. var formatted = '';
  488. if (isDate(date)) {
  489. var year = date.getFullYear();
  490. var month = date.getMonth();
  491. var day = date.getDate();
  492. var values = {
  493. d: day,
  494. dd: addLeadingZero(day, 2),
  495. m: month + 1,
  496. mm: addLeadingZero(month + 1, 2),
  497. yy: String(year).substring(2),
  498. yyyy: addLeadingZero(year, 4)
  499. };
  500. formatted = format.source;
  501. $.each(format.parts, function(i, part) {
  502. formatted = formatted.replace(part, values[part]);
  503. });
  504. }
  505. return formatted;
  506. },
  507. // Destroy the datepicker and remove the instance from the target element
  508. destroy: function destroy() {
  509. this.unbind();
  510. this.unbuild();
  511. this.$element.removeData(NAMESPACE);
  512. }
  513. };
  514. var handlers = {
  515. click: function click(e) {
  516. var $target = $(e.target);
  517. var options = this.options,
  518. date = this.date,
  519. viewDate = this.viewDate,
  520. format = this.format;
  521. e.stopPropagation();
  522. e.preventDefault();
  523. if ($target.hasClass('disabled')) {
  524. return;
  525. }
  526. var view = $target.data('view');
  527. var viewYear = viewDate.getFullYear();
  528. var viewMonth = viewDate.getMonth();
  529. var viewDay = viewDate.getDate();
  530. switch (view) {
  531. case 'years prev':
  532. case 'years next':
  533. {
  534. viewYear = view === 'years prev' ? viewYear - 10 : viewYear + 10;
  535. viewDate.setFullYear(viewYear);
  536. viewDate.setDate(getMinDay(viewYear, viewMonth, viewDay));
  537. this.renderYears();
  538. break;
  539. }
  540. case 'year prev':
  541. case 'year next':
  542. viewYear = view === 'year prev' ? viewYear - 1 : viewYear + 1;
  543. viewDate.setFullYear(viewYear);
  544. viewDate.setDate(getMinDay(viewYear, viewMonth, viewDay));
  545. this.renderMonths();
  546. break;
  547. case 'year current':
  548. if (format.hasYear) {
  549. this.showView(VIEWS.YEARS);
  550. }
  551. break;
  552. case 'year picked':
  553. if (format.hasMonth) {
  554. this.showView(VIEWS.MONTHS);
  555. } else {
  556. $target.siblings(".".concat(options.pickedClass)).removeClass(options.pickedClass).data('view', 'year');
  557. this.hideView();
  558. }
  559. this.pick('year');
  560. break;
  561. case 'year':
  562. viewYear = parseInt($target.text(), 10); // Set date first to avoid month changing (#195)
  563. date.setDate(getMinDay(viewYear, viewMonth, viewDay));
  564. date.setFullYear(viewYear);
  565. viewDate.setDate(getMinDay(viewYear, viewMonth, viewDay));
  566. viewDate.setFullYear(viewYear);
  567. if (format.hasMonth) {
  568. this.showView(VIEWS.MONTHS);
  569. } else {
  570. $target.addClass(options.pickedClass).data('view', 'year picked').siblings(".".concat(options.pickedClass)).removeClass(options.pickedClass).data('view', 'year');
  571. this.hideView();
  572. }
  573. this.pick('year');
  574. break;
  575. case 'month prev':
  576. case 'month next':
  577. viewMonth = view === 'month prev' ? viewMonth - 1 : viewMonth + 1;
  578. if (viewMonth < 0) {
  579. viewYear -= 1;
  580. viewMonth += 12;
  581. } else if (viewMonth > 11) {
  582. viewYear += 1;
  583. viewMonth -= 12;
  584. }
  585. viewDate.setFullYear(viewYear);
  586. viewDate.setDate(getMinDay(viewYear, viewMonth, viewDay));
  587. viewDate.setMonth(viewMonth);
  588. this.renderDays();
  589. break;
  590. case 'month current':
  591. if (format.hasMonth) {
  592. this.showView(VIEWS.MONTHS);
  593. }
  594. break;
  595. case 'month picked':
  596. if (format.hasDay) {
  597. this.showView(VIEWS.DAYS);
  598. } else {
  599. $target.siblings(".".concat(options.pickedClass)).removeClass(options.pickedClass).data('view', 'month');
  600. this.hideView();
  601. }
  602. this.pick('month');
  603. break;
  604. case 'month':
  605. viewMonth = $.inArray($target.text(), options.monthsShort);
  606. date.setFullYear(viewYear); // Set date before month to avoid month changing (#195)
  607. date.setDate(getMinDay(viewYear, viewMonth, viewDay));
  608. date.setMonth(viewMonth);
  609. viewDate.setFullYear(viewYear);
  610. viewDate.setDate(getMinDay(viewYear, viewMonth, viewDay));
  611. viewDate.setMonth(viewMonth);
  612. if (format.hasDay) {
  613. this.showView(VIEWS.DAYS);
  614. } else {
  615. $target.addClass(options.pickedClass).data('view', 'month picked').siblings(".".concat(options.pickedClass)).removeClass(options.pickedClass).data('view', 'month');
  616. this.hideView();
  617. }
  618. this.pick('month');
  619. break;
  620. case 'day prev':
  621. case 'day next':
  622. case 'day':
  623. if (view === 'day prev') {
  624. viewMonth -= 1;
  625. } else if (view === 'day next') {
  626. viewMonth += 1;
  627. }
  628. viewDay = parseInt($target.text(), 10); // Set date to 1 to avoid month changing (#195)
  629. date.setDate(1);
  630. date.setFullYear(viewYear);
  631. date.setMonth(viewMonth);
  632. date.setDate(viewDay);
  633. viewDate.setDate(1);
  634. viewDate.setFullYear(viewYear);
  635. viewDate.setMonth(viewMonth);
  636. viewDate.setDate(viewDay);
  637. this.renderDays();
  638. if (view === 'day') {
  639. this.hideView();
  640. }
  641. this.pick('day');
  642. break;
  643. case 'day picked':
  644. this.hideView();
  645. this.pick('day');
  646. break;
  647. }
  648. },
  649. globalClick: function globalClick(_ref) {
  650. var target = _ref.target;
  651. var element = this.element,
  652. $trigger = this.$trigger;
  653. var trigger = $trigger[0];
  654. var hidden = true;
  655. while (target !== document) {
  656. if (target === trigger || target === element) {
  657. hidden = false;
  658. break;
  659. }
  660. target = target.parentNode;
  661. }
  662. if (hidden) {
  663. this.hide();
  664. }
  665. },
  666. keyup: function keyup() {
  667. this.update();
  668. },
  669. globalKeyup: function globalKeyup(_ref2) {
  670. var target = _ref2.target,
  671. key = _ref2.key,
  672. keyCode = _ref2.keyCode;
  673. if (this.isInput && target !== this.element && this.shown && (key === 'Tab' || keyCode === 9)) {
  674. this.hide();
  675. }
  676. },
  677. touchstart: function touchstart(_ref3) {
  678. var target = _ref3.target;
  679. // Emulate click in touch devices to support hiding the picker automatically (#197).
  680. if (this.isInput && target !== this.element && !$.contains(this.$picker[0], target)) {
  681. this.hide();
  682. this.element.blur();
  683. }
  684. }
  685. };
  686. var render = {
  687. render: function render() {
  688. this.renderYears();
  689. this.renderMonths();
  690. this.renderDays();
  691. },
  692. renderWeek: function renderWeek() {
  693. var _this = this;
  694. var items = [];
  695. var _this$options = this.options,
  696. weekStart = _this$options.weekStart,
  697. daysMin = _this$options.daysMin;
  698. weekStart = parseInt(weekStart, 10) % 7;
  699. daysMin = daysMin.slice(weekStart).concat(daysMin.slice(0, weekStart));
  700. $.each(daysMin, function(i, day) {
  701. items.push(_this.createItem({
  702. text: day
  703. }));
  704. });
  705. this.$week.html(items.join(''));
  706. },
  707. renderYears: function renderYears() {
  708. var options = this.options,
  709. startDate = this.startDate,
  710. endDate = this.endDate;
  711. var disabledClass = options.disabledClass,
  712. filter = options.filter,
  713. yearSuffix = options.yearSuffix;
  714. var viewYear = this.viewDate.getFullYear();
  715. var now = new Date();
  716. var thisYear = now.getFullYear();
  717. var year = this.date.getFullYear();
  718. var start = -5;
  719. var end = 6;
  720. var items = [];
  721. var prevDisabled = false;
  722. var nextDisabled = false;
  723. var i;
  724. for (i = start; i <= end; i += 1) {
  725. var date = new Date(viewYear + i, 1, 1);
  726. var disabled = false;
  727. if (startDate) {
  728. disabled = date.getFullYear() < startDate.getFullYear();
  729. if (i === start) {
  730. prevDisabled = disabled;
  731. }
  732. }
  733. if (!disabled && endDate) {
  734. disabled = date.getFullYear() > endDate.getFullYear();
  735. if (i === end) {
  736. nextDisabled = disabled;
  737. }
  738. }
  739. if (!disabled && filter) {
  740. disabled = filter.call(this.$element, date, 'year') === false;
  741. }
  742. var picked = viewYear + i === year;
  743. var view = picked ? 'year picked' : 'year';
  744. items.push(this.createItem({
  745. picked: picked,
  746. disabled: disabled,
  747. text: viewYear + i,
  748. view: disabled ? 'year disabled' : view,
  749. highlighted: date.getFullYear() === thisYear
  750. }));
  751. }
  752. this.$yearsPrev.toggleClass(disabledClass, prevDisabled);
  753. this.$yearsNext.toggleClass(disabledClass, nextDisabled);
  754. this.$yearsCurrent.toggleClass(disabledClass, true).html("".concat(viewYear + start + yearSuffix, " - ").concat(viewYear + end).concat(yearSuffix));
  755. this.$years.html(items.join(''));
  756. },
  757. renderMonths: function renderMonths() {
  758. var options = this.options,
  759. startDate = this.startDate,
  760. endDate = this.endDate,
  761. viewDate = this.viewDate;
  762. var disabledClass = options.disabledClass || '';
  763. var months = options.monthsShort;
  764. var filter = $.isFunction(options.filter) && options.filter;
  765. var viewYear = viewDate.getFullYear();
  766. var now = new Date();
  767. var thisYear = now.getFullYear();
  768. var thisMonth = now.getMonth();
  769. var year = this.date.getFullYear();
  770. var month = this.date.getMonth();
  771. var items = [];
  772. var prevDisabled = false;
  773. var nextDisabled = false;
  774. var i;
  775. for (i = 0; i <= 11; i += 1) {
  776. var date = new Date(viewYear, i, 1);
  777. var disabled = false;
  778. if (startDate) {
  779. prevDisabled = date.getFullYear() === startDate.getFullYear();
  780. disabled = prevDisabled && date.getMonth() < startDate.getMonth();
  781. }
  782. if (!disabled && endDate) {
  783. nextDisabled = date.getFullYear() === endDate.getFullYear();
  784. disabled = nextDisabled && date.getMonth() > endDate.getMonth();
  785. }
  786. if (!disabled && filter) {
  787. disabled = filter.call(this.$element, date, 'month') === false;
  788. }
  789. var picked = viewYear === year && i === month;
  790. var view = picked ? 'month picked' : 'month';
  791. items.push(this.createItem({
  792. disabled: disabled,
  793. picked: picked,
  794. highlighted: viewYear === thisYear && date.getMonth() === thisMonth,
  795. index: i,
  796. text: months[i],
  797. view: disabled ? 'month disabled' : view
  798. }));
  799. }
  800. this.$yearPrev.toggleClass(disabledClass, prevDisabled);
  801. this.$yearNext.toggleClass(disabledClass, nextDisabled);
  802. this.$yearCurrent.toggleClass(disabledClass, prevDisabled && nextDisabled).html(viewYear + options.yearSuffix || '');
  803. this.$months.html(items.join(''));
  804. },
  805. renderDays: function renderDays() {
  806. var $element = this.$element,
  807. options = this.options,
  808. startDate = this.startDate,
  809. endDate = this.endDate,
  810. viewDate = this.viewDate,
  811. currentDate = this.date;
  812. var disabledClass = options.disabledClass,
  813. filter = options.filter,
  814. months = options.months,
  815. weekStart = options.weekStart,
  816. yearSuffix = options.yearSuffix;
  817. var viewYear = viewDate.getFullYear();
  818. var viewMonth = viewDate.getMonth();
  819. var now = new Date();
  820. var thisYear = now.getFullYear();
  821. var thisMonth = now.getMonth();
  822. var thisDay = now.getDate();
  823. var year = currentDate.getFullYear();
  824. var month = currentDate.getMonth();
  825. var day = currentDate.getDate();
  826. var length;
  827. var i;
  828. var n; // Days of prev month
  829. // -----------------------------------------------------------------------
  830. var prevItems = [];
  831. var prevViewYear = viewYear;
  832. var prevViewMonth = viewMonth;
  833. var prevDisabled = false;
  834. if (viewMonth === 0) {
  835. prevViewYear -= 1;
  836. prevViewMonth = 11;
  837. } else {
  838. prevViewMonth -= 1;
  839. } // The length of the days of prev month
  840. length = getDaysInMonth(prevViewYear, prevViewMonth); // The first day of current month
  841. var firstDay = new Date(viewYear, viewMonth, 1); // The visible length of the days of prev month
  842. // [0,1,2,3,4,5,6] - [0,1,2,3,4,5,6] => [-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6]
  843. n = firstDay.getDay() - parseInt(weekStart, 10) % 7; // [-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6] => [1,2,3,4,5,6,7]
  844. if (n <= 0) {
  845. n += 7;
  846. }
  847. if (startDate) {
  848. prevDisabled = firstDay.getTime() <= startDate.getTime();
  849. }
  850. for (i = length - (n - 1); i <= length; i += 1) {
  851. var prevViewDate = new Date(prevViewYear, prevViewMonth, i);
  852. var disabled = false;
  853. if (startDate) {
  854. disabled = prevViewDate.getTime() < startDate.getTime();
  855. }
  856. if (!disabled && filter) {
  857. disabled = filter.call($element, prevViewDate, 'day') === false;
  858. }
  859. prevItems.push(this.createItem({
  860. disabled: disabled,
  861. highlighted: prevViewYear === thisYear && prevViewMonth === thisMonth && prevViewDate.getDate() === thisDay,
  862. muted: true,
  863. picked: prevViewYear === year && prevViewMonth === month && i === day,
  864. text: i,
  865. view: 'day prev'
  866. }));
  867. } // Days of next month
  868. // -----------------------------------------------------------------------
  869. var nextItems = [];
  870. var nextViewYear = viewYear;
  871. var nextViewMonth = viewMonth;
  872. var nextDisabled = false;
  873. if (viewMonth === 11) {
  874. nextViewYear += 1;
  875. nextViewMonth = 0;
  876. } else {
  877. nextViewMonth += 1;
  878. } // The length of the days of current month
  879. length = getDaysInMonth(viewYear, viewMonth); // The visible length of next month (42 means 6 rows and 7 columns)
  880. n = 42 - (prevItems.length + length); // The last day of current month
  881. var lastDate = new Date(viewYear, viewMonth, length);
  882. if (endDate) {
  883. nextDisabled = lastDate.getTime() >= endDate.getTime();
  884. }
  885. for (i = 1; i <= n; i += 1) {
  886. var date = new Date(nextViewYear, nextViewMonth, i);
  887. var picked = nextViewYear === year && nextViewMonth === month && i === day;
  888. var _disabled = false;
  889. if (endDate) {
  890. _disabled = date.getTime() > endDate.getTime();
  891. }
  892. if (!_disabled && filter) {
  893. _disabled = filter.call($element, date, 'day') === false;
  894. }
  895. nextItems.push(this.createItem({
  896. disabled: _disabled,
  897. picked: picked,
  898. highlighted: nextViewYear === thisYear && nextViewMonth === thisMonth && date.getDate() === thisDay,
  899. muted: true,
  900. text: i,
  901. view: 'day next'
  902. }));
  903. } // Days of current month
  904. // -----------------------------------------------------------------------
  905. var items = [];
  906. for (i = 1; i <= length; i += 1) {
  907. var _date = new Date(viewYear, viewMonth, i);
  908. var _disabled2 = false;
  909. if (startDate) {
  910. _disabled2 = _date.getTime() < startDate.getTime();
  911. }
  912. if (!_disabled2 && endDate) {
  913. _disabled2 = _date.getTime() > endDate.getTime();
  914. }
  915. if (!_disabled2 && filter) {
  916. _disabled2 = filter.call($element, _date, 'day') === false;
  917. }
  918. var _picked = viewYear === year && viewMonth === month && i === day;
  919. var view = _picked ? 'day picked' : 'day';
  920. items.push(this.createItem({
  921. disabled: _disabled2,
  922. picked: _picked,
  923. highlighted: viewYear === thisYear && viewMonth === thisMonth && _date.getDate() === thisDay,
  924. text: i,
  925. view: _disabled2 ? 'day disabled' : view
  926. }));
  927. } // Render days picker
  928. // -----------------------------------------------------------------------
  929. this.$monthPrev.toggleClass(disabledClass, prevDisabled);
  930. this.$monthNext.toggleClass(disabledClass, nextDisabled);
  931. this.$monthCurrent.toggleClass(disabledClass, prevDisabled && nextDisabled).html(options.yearFirst ? "".concat(viewYear + yearSuffix, " ").concat(months[viewMonth]) : "".concat(months[viewMonth], " ").concat(viewYear).concat(yearSuffix));
  932. this.$days.html(prevItems.join('') + items.join('') + nextItems.join(''));
  933. }
  934. };
  935. var CLASS_TOP_LEFT = "".concat(NAMESPACE, "-top-left");
  936. var CLASS_TOP_RIGHT = "".concat(NAMESPACE, "-top-right");
  937. var CLASS_BOTTOM_LEFT = "".concat(NAMESPACE, "-bottom-left");
  938. var CLASS_BOTTOM_RIGHT = "".concat(NAMESPACE, "-bottom-right");
  939. var CLASS_PLACEMENTS = [CLASS_TOP_LEFT, CLASS_TOP_RIGHT, CLASS_BOTTOM_LEFT, CLASS_BOTTOM_RIGHT].join(' ');
  940. var Datepicker = /*#__PURE__*/ function() {
  941. function Datepicker(element) {
  942. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  943. _classCallCheck(this, Datepicker);
  944. this.$element = $(element);
  945. this.element = element;
  946. this.options = $.extend({}, DEFAULTS, LANGUAGES[options.language], $.isPlainObject(options) && options);
  947. this.$scrollParent = getScrollParent(element, true);
  948. this.built = false;
  949. this.shown = false;
  950. this.isInput = false;
  951. this.inline = false;
  952. this.initialValue = '';
  953. this.initialDate = null;
  954. this.startDate = null;
  955. this.endDate = null;
  956. this.init();
  957. }
  958. _createClass(Datepicker, [{
  959. key: "init",
  960. value: function init() {
  961. var $this = this.$element,
  962. options = this.options;
  963. var startDate = options.startDate,
  964. endDate = options.endDate,
  965. date = options.date;
  966. this.$trigger = $(options.trigger);
  967. this.isInput = $this.is('input') || $this.is('textarea');
  968. this.inline = options.inline && (options.container || !this.isInput);
  969. this.format = parseFormat(options.format);
  970. var initialValue = this.getValue();
  971. this.initialValue = initialValue;
  972. this.oldValue = initialValue;
  973. date = this.parseDate(date || initialValue);
  974. if (startDate) {
  975. startDate = this.parseDate(startDate);
  976. if (date.getTime() < startDate.getTime()) {
  977. date = new Date(startDate);
  978. }
  979. this.startDate = startDate;
  980. }
  981. if (endDate) {
  982. endDate = this.parseDate(endDate);
  983. if (startDate && endDate.getTime() < startDate.getTime()) {
  984. endDate = new Date(startDate);
  985. }
  986. if (date.getTime() > endDate.getTime()) {
  987. date = new Date(endDate);
  988. }
  989. this.endDate = endDate;
  990. }
  991. this.date = date;
  992. this.viewDate = new Date(date);
  993. this.initialDate = new Date(this.date);
  994. this.bind();
  995. if (options.autoShow || this.inline) {
  996. this.show();
  997. }
  998. if (options.autoPick) {
  999. this.pick();
  1000. }
  1001. }
  1002. }, {
  1003. key: "build",
  1004. value: function build() {
  1005. if (this.built) {
  1006. return;
  1007. }
  1008. this.built = true;
  1009. var $this = this.$element,
  1010. options = this.options;
  1011. var $picker = $(options.template);
  1012. this.$picker = $picker;
  1013. this.$week = $picker.find(selectorOf('week')); // Years view
  1014. this.$yearsPicker = $picker.find(selectorOf('years picker'));
  1015. this.$yearsPrev = $picker.find(selectorOf('years prev'));
  1016. this.$yearsNext = $picker.find(selectorOf('years next'));
  1017. this.$yearsCurrent = $picker.find(selectorOf('years current'));
  1018. this.$years = $picker.find(selectorOf('years')); // Months view
  1019. this.$monthsPicker = $picker.find(selectorOf('months picker'));
  1020. this.$yearPrev = $picker.find(selectorOf('year prev'));
  1021. this.$yearNext = $picker.find(selectorOf('year next'));
  1022. this.$yearCurrent = $picker.find(selectorOf('year current'));
  1023. this.$months = $picker.find(selectorOf('months')); // Days view
  1024. this.$daysPicker = $picker.find(selectorOf('days picker'));
  1025. this.$monthPrev = $picker.find(selectorOf('month prev'));
  1026. this.$monthNext = $picker.find(selectorOf('month next'));
  1027. this.$monthCurrent = $picker.find(selectorOf('month current'));
  1028. this.$days = $picker.find(selectorOf('days'));
  1029. if (this.inline) {
  1030. $(options.container || $this).append($picker.addClass("".concat(NAMESPACE, "-inline")));
  1031. } else {
  1032. $(document.body).append($picker.addClass("".concat(NAMESPACE, "-dropdown")));
  1033. $picker.addClass(CLASS_HIDE).css({
  1034. zIndex: parseInt(options.zIndex, 10)
  1035. });
  1036. }
  1037. this.renderWeek();
  1038. }
  1039. }, {
  1040. key: "unbuild",
  1041. value: function unbuild() {
  1042. if (!this.built) {
  1043. return;
  1044. }
  1045. this.built = false;
  1046. this.$picker.remove();
  1047. }
  1048. }, {
  1049. key: "bind",
  1050. value: function bind() {
  1051. var options = this.options,
  1052. $this = this.$element;
  1053. if ($.isFunction(options.show)) {
  1054. $this.on(EVENT_SHOW, options.show);
  1055. }
  1056. if ($.isFunction(options.hide)) {
  1057. $this.on(EVENT_HIDE, options.hide);
  1058. }
  1059. if ($.isFunction(options.pick)) {
  1060. $this.on(EVENT_PICK, options.pick);
  1061. }
  1062. if (this.isInput) {
  1063. $this.on(EVENT_KEYUP, $.proxy(this.keyup, this));
  1064. }
  1065. if (!this.inline) {
  1066. if (options.trigger) {
  1067. this.$trigger.on(EVENT_CLICK, $.proxy(this.toggle, this));
  1068. } else if (this.isInput) {
  1069. $this.on(EVENT_FOCUS, $.proxy(this.show, this));
  1070. } else {
  1071. $this.on(EVENT_CLICK, $.proxy(this.show, this));
  1072. }
  1073. }
  1074. }
  1075. }, {
  1076. key: "unbind",
  1077. value: function unbind() {
  1078. var $this = this.$element,
  1079. options = this.options;
  1080. if ($.isFunction(options.show)) {
  1081. $this.off(EVENT_SHOW, options.show);
  1082. }
  1083. if ($.isFunction(options.hide)) {
  1084. $this.off(EVENT_HIDE, options.hide);
  1085. }
  1086. if ($.isFunction(options.pick)) {
  1087. $this.off(EVENT_PICK, options.pick);
  1088. }
  1089. if (this.isInput) {
  1090. $this.off(EVENT_KEYUP, this.keyup);
  1091. }
  1092. if (!this.inline) {
  1093. if (options.trigger) {
  1094. this.$trigger.off(EVENT_CLICK, this.toggle);
  1095. } else if (this.isInput) {
  1096. $this.off(EVENT_FOCUS, this.show);
  1097. } else {
  1098. $this.off(EVENT_CLICK, this.show);
  1099. }
  1100. }
  1101. }
  1102. }, {
  1103. key: "showView",
  1104. value: function showView(view) {
  1105. var $yearsPicker = this.$yearsPicker,
  1106. $monthsPicker = this.$monthsPicker,
  1107. $daysPicker = this.$daysPicker,
  1108. format = this.format;
  1109. if (format.hasYear || format.hasMonth || format.hasDay) {
  1110. switch (Number(view)) {
  1111. case VIEWS.YEARS:
  1112. $monthsPicker.addClass(CLASS_HIDE);
  1113. $daysPicker.addClass(CLASS_HIDE);
  1114. if (format.hasYear) {
  1115. this.renderYears();
  1116. $yearsPicker.removeClass(CLASS_HIDE);
  1117. this.place();
  1118. } else {
  1119. this.showView(VIEWS.DAYS);
  1120. }
  1121. break;
  1122. case VIEWS.MONTHS:
  1123. $yearsPicker.addClass(CLASS_HIDE);
  1124. $daysPicker.addClass(CLASS_HIDE);
  1125. if (format.hasMonth) {
  1126. this.renderMonths();
  1127. $monthsPicker.removeClass(CLASS_HIDE);
  1128. this.place();
  1129. } else {
  1130. this.showView(VIEWS.YEARS);
  1131. }
  1132. break;
  1133. // case VIEWS.DAYS:
  1134. default:
  1135. $yearsPicker.addClass(CLASS_HIDE);
  1136. $monthsPicker.addClass(CLASS_HIDE);
  1137. if (format.hasDay) {
  1138. this.renderDays();
  1139. $daysPicker.removeClass(CLASS_HIDE);
  1140. this.place();
  1141. } else {
  1142. this.showView(VIEWS.MONTHS);
  1143. }
  1144. }
  1145. }
  1146. }
  1147. }, {
  1148. key: "hideView",
  1149. value: function hideView() {
  1150. if (!this.inline && this.options.autoHide) {
  1151. this.hide();
  1152. }
  1153. }
  1154. }, {
  1155. key: "place",
  1156. value: function place() {
  1157. if (this.inline) {
  1158. return;
  1159. }
  1160. var $this = this.$element,
  1161. options = this.options,
  1162. $picker = this.$picker;
  1163. var containerWidth = $(document).outerWidth();
  1164. var containerHeight = $(document).outerHeight();
  1165. var elementWidth = $this.outerWidth();
  1166. var elementHeight = $this.outerHeight();
  1167. var width = $picker.width();
  1168. var height = $picker.height();
  1169. var _$this$offset = $this.offset(),
  1170. left = _$this$offset.left,
  1171. top = _$this$offset.top;
  1172. var offset = parseFloat(options.offset);
  1173. var placement = CLASS_TOP_LEFT;
  1174. if (isNaN(offset)) {
  1175. offset = 10;
  1176. }
  1177. if (top > height && top + elementHeight + height > containerHeight) {
  1178. top -= height + offset;
  1179. placement = CLASS_BOTTOM_LEFT;
  1180. } else {
  1181. top += elementHeight + offset;
  1182. }
  1183. if (left + width > containerWidth) {
  1184. left += elementWidth - width;
  1185. placement = placement.replace('left', 'right');
  1186. }
  1187. $picker.removeClass(CLASS_PLACEMENTS).addClass(placement).css({
  1188. top: top,
  1189. left: left
  1190. });
  1191. } // A shortcut for triggering custom events
  1192. }, {
  1193. key: "trigger",
  1194. value: function trigger(type, data) {
  1195. var e = $.Event(type, data);
  1196. this.$element.trigger(e);
  1197. return e;
  1198. }
  1199. }, {
  1200. key: "createItem",
  1201. value: function createItem(data) {
  1202. var options = this.options;
  1203. var itemTag = options.itemTag;
  1204. var item = {
  1205. text: '',
  1206. view: '',
  1207. muted: false,
  1208. picked: false,
  1209. disabled: false,
  1210. highlighted: false
  1211. };
  1212. var classes = [];
  1213. $.extend(item, data);
  1214. if (item.muted) {
  1215. classes.push(options.mutedClass);
  1216. }
  1217. if (item.highlighted) {
  1218. classes.push(options.highlightedClass);
  1219. }
  1220. if (item.picked) {
  1221. classes.push(options.pickedClass);
  1222. }
  1223. if (item.disabled) {
  1224. classes.push(options.disabledClass);
  1225. }
  1226. return "<".concat(itemTag, " class=\"").concat(classes.join(' '), "\" data-view=\"").concat(item.view, "\">").concat(item.text, "</").concat(itemTag, ">");
  1227. }
  1228. }, {
  1229. key: "getValue",
  1230. value: function getValue() {
  1231. var $this = this.$element;
  1232. return this.isInput ? $this.val() : $this.text();
  1233. }
  1234. }, {
  1235. key: "setValue",
  1236. value: function setValue() {
  1237. var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  1238. var $this = this.$element;
  1239. if (this.isInput) {
  1240. $this.val(value);
  1241. } else if (!this.inline || this.options.container) {
  1242. $this.text(value);
  1243. }
  1244. }
  1245. }], [{
  1246. key: "setDefaults",
  1247. value: function setDefaults() {
  1248. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  1249. $.extend(DEFAULTS, LANGUAGES[options.language], $.isPlainObject(options) && options);
  1250. }
  1251. }]);
  1252. return Datepicker;
  1253. }();
  1254. if ($.extend) {
  1255. $.extend(Datepicker.prototype, render, handlers, methods);
  1256. }
  1257. if ($.fn) {
  1258. var AnotherDatepicker = $.fn.datepicker;
  1259. $.fn.datepicker = function jQueryDatepicker(option) {
  1260. for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  1261. args[_key - 1] = arguments[_key];
  1262. }
  1263. var result;
  1264. this.each(function(i, element) {
  1265. var $element = $(element);
  1266. var isDestroy = option === 'destroy';
  1267. var datepicker = $element.data(NAMESPACE);
  1268. if (!datepicker) {
  1269. if (isDestroy) {
  1270. return;
  1271. }
  1272. var options = $.extend({}, $element.data(), $.isPlainObject(option) && option);
  1273. datepicker = new Datepicker(element, options);
  1274. $element.data(NAMESPACE, datepicker);
  1275. }
  1276. if (isString(option)) {
  1277. var fn = datepicker[option];
  1278. if ($.isFunction(fn)) {
  1279. result = fn.apply(datepicker, args);
  1280. if (isDestroy) {
  1281. $element.removeData(NAMESPACE);
  1282. }
  1283. }
  1284. }
  1285. });
  1286. return !isUndefined(result) ? result : this;
  1287. };
  1288. $.fn.datepicker.Constructor = Datepicker;
  1289. $.fn.datepicker.languages = LANGUAGES;
  1290. $.fn.datepicker.setDefaults = Datepicker.setDefaults;
  1291. $.fn.datepicker.noConflict = function noConflict() {
  1292. $.fn.datepicker = AnotherDatepicker;
  1293. return this;
  1294. };
  1295. }
  1296. })));