datepicker.js 45 KB

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