date-picker-month.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.82
  5. */
  6. import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client';
  7. import { s as sameDate, b as dateFromRange, i as inRange } from './date.js';
  8. import { d as defineCustomElement$1 } from './date-picker-day.js';
  9. const datePickerMonthCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}.calender{margin-bottom:0.25rem}.week-headers{display:-ms-flexbox;display:flex;border-width:0px;border-top-width:1px;border-style:solid;border-color:var(--calcite-ui-border-3);padding-top:0px;padding-bottom:0px;padding-left:0.25rem;padding-right:0.25rem}.week-header{text-align:center;font-weight:var(--calcite-font-weight-bold);color:var(--calcite-ui-text-3);width:14.2857142857%}:host([scale=s]) .week-header{padding-left:0px;padding-right:0px;padding-top:0.5rem;padding-bottom:0.75rem;font-size:var(--calcite-font-size--2);line-height:1rem}:host([scale=m]) .week-header{padding-left:0px;padding-right:0px;padding-top:0.75rem;padding-bottom:1rem;font-size:var(--calcite-font-size--2);line-height:1rem}:host([scale=l]) .week-header{padding-left:0px;padding-right:0px;padding-top:1rem;padding-bottom:1.25rem;font-size:var(--calcite-font-size--1);line-height:1rem}.week-days{display:-ms-flexbox;display:flex;-ms-flex-direction:row;flex-direction:row;padding-top:0px;padding-bottom:0px;padding-left:6px;padding-right:6px}.week-days:focus{outline:2px solid transparent;outline-offset:2px}";
  10. const DatePickerMonth = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  11. constructor() {
  12. super();
  13. this.__registerHost();
  14. this.__attachShadow();
  15. this.calciteDatePickerSelect = createEvent(this, "calciteDatePickerSelect", 7);
  16. this.calciteDatePickerHover = createEvent(this, "calciteDatePickerHover", 7);
  17. this.calciteDatePickerActiveDateChange = createEvent(this, "calciteDatePickerActiveDateChange", 7);
  18. this.calciteDatePickerMouseOut = createEvent(this, "calciteDatePickerMouseOut", 7);
  19. /** Date currently active.*/
  20. this.activeDate = new Date();
  21. //--------------------------------------------------------------------------
  22. //
  23. // Event Listeners
  24. //
  25. //--------------------------------------------------------------------------
  26. this.keyDownHandler = (e) => {
  27. const isRTL = this.el.dir === "rtl";
  28. switch (e.key) {
  29. case "ArrowUp":
  30. e.preventDefault();
  31. this.addDays(-7);
  32. break;
  33. case "ArrowRight":
  34. e.preventDefault();
  35. this.addDays(isRTL ? -1 : 1);
  36. break;
  37. case "ArrowDown":
  38. e.preventDefault();
  39. this.addDays(7);
  40. break;
  41. case "ArrowLeft":
  42. e.preventDefault();
  43. this.addDays(isRTL ? 1 : -1);
  44. break;
  45. case "PageUp":
  46. e.preventDefault();
  47. this.addMonths(-1);
  48. break;
  49. case "PageDown":
  50. e.preventDefault();
  51. this.addMonths(1);
  52. break;
  53. case "Home":
  54. e.preventDefault();
  55. this.activeDate.setDate(1);
  56. this.addDays();
  57. break;
  58. case "End":
  59. e.preventDefault();
  60. this.activeDate.setDate(new Date(this.activeDate.getFullYear(), this.activeDate.getMonth() + 1, 0).getDate());
  61. this.addDays();
  62. break;
  63. case "Enter":
  64. case " ":
  65. e.preventDefault();
  66. break;
  67. case "Tab":
  68. this.activeFocus = false;
  69. }
  70. };
  71. /**
  72. * Once user is not interacting via keyboard,
  73. * disable auto focusing of active date
  74. */
  75. this.disableActiveFocus = () => {
  76. this.activeFocus = false;
  77. };
  78. this.dayHover = (e) => {
  79. const target = e.target;
  80. if (e.detail.disabled) {
  81. this.calciteDatePickerMouseOut.emit();
  82. }
  83. else {
  84. this.calciteDatePickerHover.emit(target.value);
  85. }
  86. };
  87. this.daySelect = (e) => {
  88. const target = e.target;
  89. this.calciteDatePickerSelect.emit(target.value);
  90. };
  91. }
  92. mouseoutHandler() {
  93. this.calciteDatePickerMouseOut.emit();
  94. }
  95. //--------------------------------------------------------------------------
  96. //
  97. // Lifecycle
  98. //
  99. //--------------------------------------------------------------------------
  100. render() {
  101. const month = this.activeDate.getMonth();
  102. const year = this.activeDate.getFullYear();
  103. const startOfWeek = this.localeData.weekStart % 7;
  104. const { abbreviated, short, narrow } = this.localeData.days;
  105. const weekDays = this.scale === "s" ? narrow || short || abbreviated : short || abbreviated || narrow;
  106. const adjustedWeekDays = [...weekDays.slice(startOfWeek, 7), ...weekDays.slice(0, startOfWeek)];
  107. const curMonDays = this.getCurrentMonthDays(month, year);
  108. const prevMonDays = this.getPrevMonthdays(month, year, startOfWeek);
  109. const nextMonDays = this.getNextMonthDays(month, year, startOfWeek);
  110. const days = [
  111. ...prevMonDays.map((day) => {
  112. const date = new Date(year, month - 1, day);
  113. return this.renderDateDay(false, day, date);
  114. }),
  115. ...curMonDays.map((day) => {
  116. const date = new Date(year, month, day);
  117. const active = sameDate(date, this.activeDate);
  118. return this.renderDateDay(active, day, date, true, true);
  119. }),
  120. ...nextMonDays.map((day) => {
  121. const date = new Date(year, month + 1, day);
  122. return this.renderDateDay(false, day, date);
  123. })
  124. ];
  125. const weeks = [];
  126. for (let i = 0; i < days.length; i += 7) {
  127. weeks.push(days.slice(i, i + 7));
  128. }
  129. return (h(Host, { onFocusOut: this.disableActiveFocus, onKeyDown: this.keyDownHandler }, h("div", { class: "calender", role: "grid" }, h("div", { class: "week-headers", role: "row" }, adjustedWeekDays.map((weekday) => (h("span", { class: "week-header", role: "columnheader" }, weekday)))), weeks.map((days) => (h("div", { class: "week-days", role: "row" }, days))))));
  130. }
  131. //--------------------------------------------------------------------------
  132. //
  133. // Private Methods
  134. //
  135. //--------------------------------------------------------------------------
  136. /**
  137. * Add n months to the current month
  138. */
  139. addMonths(step) {
  140. const nextDate = new Date(this.activeDate);
  141. nextDate.setMonth(this.activeDate.getMonth() + step);
  142. this.calciteDatePickerActiveDateChange.emit(dateFromRange(nextDate, this.min, this.max));
  143. this.activeFocus = true;
  144. }
  145. /**
  146. * Add n days to the current date
  147. */
  148. addDays(step = 0) {
  149. const nextDate = new Date(this.activeDate);
  150. nextDate.setDate(this.activeDate.getDate() + step);
  151. this.calciteDatePickerActiveDateChange.emit(dateFromRange(nextDate, this.min, this.max));
  152. this.activeFocus = true;
  153. }
  154. /**
  155. * Get dates for last days of the previous month
  156. */
  157. getPrevMonthdays(month, year, startOfWeek) {
  158. const lastDate = new Date(year, month, 0);
  159. const date = lastDate.getDate();
  160. const day = lastDate.getDay();
  161. const days = [];
  162. if (day - 6 === startOfWeek) {
  163. return days;
  164. }
  165. for (let i = Math.abs(lastDate.getDay() - startOfWeek); i >= 0; i--) {
  166. days.push(date - i);
  167. }
  168. return days;
  169. }
  170. /**
  171. * Get dates for the current month
  172. */
  173. getCurrentMonthDays(month, year) {
  174. const num = new Date(year, month + 1, 0).getDate();
  175. const days = [];
  176. for (let i = 0; i < num; i++) {
  177. days.push(i + 1);
  178. }
  179. return days;
  180. }
  181. /**
  182. * Get dates for first days of the next month
  183. */
  184. getNextMonthDays(month, year, startOfWeek) {
  185. const endDay = new Date(year, month + 1, 0).getDay();
  186. const days = [];
  187. if (endDay === (startOfWeek + 6) % 7) {
  188. return days;
  189. }
  190. for (let i = 0; i < (6 - (endDay - startOfWeek)) % 7; i++) {
  191. days.push(i + 1);
  192. }
  193. return days;
  194. }
  195. /**
  196. * Determine if the date is in between the start and end dates
  197. */
  198. betweenSelectedRange(date) {
  199. return !!(this.startDate &&
  200. this.endDate &&
  201. date > this.startDate &&
  202. date < this.endDate &&
  203. !this.isRangeHover(date));
  204. }
  205. /**
  206. * Determine if the date should be in selected state
  207. */
  208. isSelected(date) {
  209. return !!(sameDate(date, this.selectedDate) ||
  210. (this.startDate && sameDate(date, this.startDate)) ||
  211. (this.endDate && sameDate(date, this.endDate)));
  212. }
  213. /**
  214. * Determine if the date is the start of the date range
  215. */
  216. isStartOfRange(date) {
  217. return !!(this.startDate &&
  218. !sameDate(this.startDate, this.endDate) &&
  219. sameDate(this.startDate, date) &&
  220. !this.isEndOfRange(date));
  221. }
  222. isEndOfRange(date) {
  223. return !!((this.endDate && !sameDate(this.startDate, this.endDate) && sameDate(this.endDate, date)) ||
  224. (!this.endDate &&
  225. this.hoverRange &&
  226. sameDate(this.startDate, this.hoverRange.end) &&
  227. sameDate(date, this.hoverRange.end)));
  228. }
  229. /**
  230. * Render calcite-date-picker-day
  231. */
  232. renderDateDay(active, day, date, currentMonth, ref) {
  233. var _a;
  234. const isFocusedOnStart = this.isFocusedOnStart();
  235. const isHoverInRange = this.isHoverInRange() ||
  236. (!this.endDate && this.hoverRange && sameDate((_a = this.hoverRange) === null || _a === void 0 ? void 0 : _a.end, this.startDate));
  237. return (h("calcite-date-picker-day", { active: active, class: {
  238. "hover--inside-range": this.startDate && isHoverInRange,
  239. "hover--outside-range": this.startDate && !isHoverInRange,
  240. "focused--start": isFocusedOnStart,
  241. "focused--end": !isFocusedOnStart
  242. }, currentMonth: currentMonth, day: day, disabled: !inRange(date, this.min, this.max), endOfRange: this.isEndOfRange(date), highlighted: this.betweenSelectedRange(date), key: date.toDateString(), localeData: this.localeData, onCalciteDayHover: this.dayHover, onCalciteDaySelect: this.daySelect, range: !!this.startDate && !!this.endDate && !sameDate(this.startDate, this.endDate), rangeHover: this.isRangeHover(date), ref: (el) => {
  243. // when moving via keyboard, focus must be updated on active date
  244. if (ref && active && this.activeFocus) {
  245. el === null || el === void 0 ? void 0 : el.focus();
  246. }
  247. }, scale: this.scale, selected: this.isSelected(date), startOfRange: this.isStartOfRange(date), value: date }));
  248. }
  249. isFocusedOnStart() {
  250. var _a;
  251. return ((_a = this.hoverRange) === null || _a === void 0 ? void 0 : _a.focused) === "start";
  252. }
  253. isHoverInRange() {
  254. if (!this.hoverRange) {
  255. return false;
  256. }
  257. const { start, end } = this.hoverRange;
  258. return !!((!this.isFocusedOnStart() && this.startDate && (!this.endDate || end < this.endDate)) ||
  259. (this.isFocusedOnStart() && this.startDate && start > this.startDate));
  260. }
  261. isRangeHover(date) {
  262. if (!this.hoverRange) {
  263. return false;
  264. }
  265. const { start, end } = this.hoverRange;
  266. const isStart = this.isFocusedOnStart();
  267. const insideRange = this.isHoverInRange();
  268. const cond1 = insideRange &&
  269. ((!isStart && date > this.startDate && (date < end || sameDate(date, end))) ||
  270. (isStart && date < this.endDate && (date > start || sameDate(date, start))));
  271. const cond2 = !insideRange &&
  272. ((!isStart && date >= this.endDate && (date < end || sameDate(date, end))) ||
  273. (isStart &&
  274. (date < this.startDate || (this.endDate && sameDate(date, this.startDate))) &&
  275. (date > start || sameDate(date, start))));
  276. return cond1 || cond2;
  277. }
  278. get el() { return this; }
  279. static get style() { return datePickerMonthCss; }
  280. }, [1, "calcite-date-picker-month", {
  281. "selectedDate": [16],
  282. "activeDate": [16],
  283. "startDate": [16],
  284. "endDate": [16],
  285. "min": [16],
  286. "max": [16],
  287. "scale": [513],
  288. "localeData": [16],
  289. "hoverRange": [16]
  290. }, [[1, "mouseout", "mouseoutHandler"]]]);
  291. function defineCustomElement() {
  292. if (typeof customElements === "undefined") {
  293. return;
  294. }
  295. const components = ["calcite-date-picker-month", "calcite-date-picker-day"];
  296. components.forEach(tagName => { switch (tagName) {
  297. case "calcite-date-picker-month":
  298. if (!customElements.get(tagName)) {
  299. customElements.define(tagName, DatePickerMonth);
  300. }
  301. break;
  302. case "calcite-date-picker-day":
  303. if (!customElements.get(tagName)) {
  304. defineCustomElement$1();
  305. }
  306. break;
  307. } });
  308. }
  309. defineCustomElement();
  310. export { DatePickerMonth as D, defineCustomElement as d };