datepicker.esm.js 42 KB

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