date-picker-month.js 14 KB

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