date-picker-month.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 { Host, h } from "@stencil/core";
  7. import { inRange, sameDate, dateFromRange } from "../../utils/date";
  8. export class DatePickerMonth {
  9. constructor() {
  10. /** Date currently active.*/
  11. this.activeDate = new Date();
  12. //--------------------------------------------------------------------------
  13. //
  14. // Event Listeners
  15. //
  16. //--------------------------------------------------------------------------
  17. this.keyDownHandler = (event) => {
  18. if (event.defaultPrevented) {
  19. return;
  20. }
  21. const isRTL = this.el.dir === "rtl";
  22. switch (event.key) {
  23. case "ArrowUp":
  24. event.preventDefault();
  25. this.addDays(-7);
  26. break;
  27. case "ArrowRight":
  28. event.preventDefault();
  29. this.addDays(isRTL ? -1 : 1);
  30. break;
  31. case "ArrowDown":
  32. event.preventDefault();
  33. this.addDays(7);
  34. break;
  35. case "ArrowLeft":
  36. event.preventDefault();
  37. this.addDays(isRTL ? 1 : -1);
  38. break;
  39. case "PageUp":
  40. event.preventDefault();
  41. this.addMonths(-1);
  42. break;
  43. case "PageDown":
  44. event.preventDefault();
  45. this.addMonths(1);
  46. break;
  47. case "Home":
  48. event.preventDefault();
  49. this.activeDate.setDate(1);
  50. this.addDays();
  51. break;
  52. case "End":
  53. event.preventDefault();
  54. this.activeDate.setDate(new Date(this.activeDate.getFullYear(), this.activeDate.getMonth() + 1, 0).getDate());
  55. this.addDays();
  56. break;
  57. case "Enter":
  58. case " ":
  59. event.preventDefault();
  60. break;
  61. case "Tab":
  62. this.activeFocus = false;
  63. }
  64. };
  65. /**
  66. * Once user is not interacting via keyboard,
  67. * disable auto focusing of active date
  68. */
  69. this.disableActiveFocus = () => {
  70. this.activeFocus = false;
  71. };
  72. this.dayHover = (event) => {
  73. const target = event.target;
  74. if (target.disabled) {
  75. this.calciteInternalDatePickerMouseOut.emit();
  76. }
  77. else {
  78. this.calciteInternalDatePickerHover.emit(target.value);
  79. }
  80. event.stopPropagation();
  81. };
  82. this.daySelect = (event) => {
  83. const target = event.target;
  84. this.calciteDatePickerSelect.emit(target.value);
  85. };
  86. }
  87. mouseoutHandler() {
  88. this.calciteInternalDatePickerMouseOut.emit();
  89. }
  90. //--------------------------------------------------------------------------
  91. //
  92. // Lifecycle
  93. //
  94. //--------------------------------------------------------------------------
  95. render() {
  96. const month = this.activeDate.getMonth();
  97. const year = this.activeDate.getFullYear();
  98. const startOfWeek = this.localeData.weekStart % 7;
  99. const { abbreviated, short, narrow } = this.localeData.days;
  100. const weekDays = this.scale === "s" ? narrow || short || abbreviated : short || abbreviated || narrow;
  101. const adjustedWeekDays = [...weekDays.slice(startOfWeek, 7), ...weekDays.slice(0, startOfWeek)];
  102. const curMonDays = this.getCurrentMonthDays(month, year);
  103. const prevMonDays = this.getPrevMonthdays(month, year, startOfWeek);
  104. const nextMonDays = this.getNextMonthDays(month, year, startOfWeek);
  105. const days = [
  106. ...prevMonDays.map((day) => {
  107. const date = new Date(year, month - 1, day);
  108. return this.renderDateDay(false, day, date);
  109. }),
  110. ...curMonDays.map((day) => {
  111. const date = new Date(year, month, day);
  112. const active = sameDate(date, this.activeDate);
  113. return this.renderDateDay(active, day, date, true, true);
  114. }),
  115. ...nextMonDays.map((day) => {
  116. const date = new Date(year, month + 1, day);
  117. return this.renderDateDay(false, day, date);
  118. })
  119. ];
  120. const weeks = [];
  121. for (let i = 0; i < days.length; i += 7) {
  122. weeks.push(days.slice(i, i + 7));
  123. }
  124. 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))))));
  125. }
  126. //--------------------------------------------------------------------------
  127. //
  128. // Private Methods
  129. //
  130. //--------------------------------------------------------------------------
  131. /**
  132. * Add n months to the current month
  133. *
  134. * @param step
  135. */
  136. addMonths(step) {
  137. const nextDate = new Date(this.activeDate);
  138. nextDate.setMonth(this.activeDate.getMonth() + step);
  139. this.calciteDatePickerActiveDateChange.emit(dateFromRange(nextDate, this.min, this.max));
  140. this.activeFocus = true;
  141. }
  142. /**
  143. * Add n days to the current date
  144. *
  145. * @param step
  146. */
  147. addDays(step = 0) {
  148. const nextDate = new Date(this.activeDate);
  149. nextDate.setDate(this.activeDate.getDate() + step);
  150. this.calciteDatePickerActiveDateChange.emit(dateFromRange(nextDate, this.min, this.max));
  151. this.activeFocus = true;
  152. }
  153. /**
  154. * Get dates for last days of the previous month
  155. *
  156. * @param month
  157. * @param year
  158. * @param startOfWeek
  159. */
  160. getPrevMonthdays(month, year, startOfWeek) {
  161. const lastDate = new Date(year, month, 0);
  162. const date = lastDate.getDate();
  163. const day = lastDate.getDay();
  164. const days = [];
  165. if (day - 6 === startOfWeek) {
  166. return days;
  167. }
  168. for (let i = lastDate.getDay() - startOfWeek; i >= 0; i--) {
  169. days.push(date - i);
  170. }
  171. return days;
  172. }
  173. /**
  174. * Get dates for the current month
  175. *
  176. * @param month
  177. * @param year
  178. */
  179. getCurrentMonthDays(month, year) {
  180. const num = new Date(year, month + 1, 0).getDate();
  181. const days = [];
  182. for (let i = 0; i < num; i++) {
  183. days.push(i + 1);
  184. }
  185. return days;
  186. }
  187. /**
  188. * Get dates for first days of the next month
  189. *
  190. * @param month
  191. * @param year
  192. * @param startOfWeek
  193. */
  194. getNextMonthDays(month, year, startOfWeek) {
  195. const endDay = new Date(year, month + 1, 0).getDay();
  196. const days = [];
  197. if (endDay === (startOfWeek + 6) % 7) {
  198. return days;
  199. }
  200. for (let i = 0; i < (6 - (endDay - startOfWeek)) % 7; i++) {
  201. days.push(i + 1);
  202. }
  203. return days;
  204. }
  205. /**
  206. * Determine if the date is in between the start and end dates
  207. *
  208. * @param date
  209. */
  210. betweenSelectedRange(date) {
  211. return !!(this.startDate &&
  212. this.endDate &&
  213. date > this.startDate &&
  214. date < this.endDate &&
  215. !this.isRangeHover(date));
  216. }
  217. /**
  218. * Determine if the date should be in selected state
  219. *
  220. * @param date
  221. */
  222. isSelected(date) {
  223. return !!(sameDate(date, this.selectedDate) ||
  224. (this.startDate && sameDate(date, this.startDate)) ||
  225. (this.endDate && sameDate(date, this.endDate)));
  226. }
  227. /**
  228. * Determine if the date is the start of the date range
  229. *
  230. * @param date
  231. */
  232. isStartOfRange(date) {
  233. return !!(this.startDate &&
  234. !sameDate(this.startDate, this.endDate) &&
  235. sameDate(this.startDate, date) &&
  236. !this.isEndOfRange(date));
  237. }
  238. isEndOfRange(date) {
  239. return !!((this.endDate && !sameDate(this.startDate, this.endDate) && sameDate(this.endDate, date)) ||
  240. (!this.endDate &&
  241. this.hoverRange &&
  242. sameDate(this.startDate, this.hoverRange.end) &&
  243. sameDate(date, this.hoverRange.end)));
  244. }
  245. /**
  246. * Render calcite-date-picker-day
  247. *
  248. * @param active
  249. * @param day
  250. * @param date
  251. * @param currentMonth
  252. * @param ref
  253. */
  254. renderDateDay(active, day, date, currentMonth, ref) {
  255. var _a;
  256. const isFocusedOnStart = this.isFocusedOnStart();
  257. const isHoverInRange = this.isHoverInRange() ||
  258. (!this.endDate && this.hoverRange && sameDate((_a = this.hoverRange) === null || _a === void 0 ? void 0 : _a.end, this.startDate));
  259. return (h("calcite-date-picker-day", { active: active, class: {
  260. "hover--inside-range": this.startDate && isHoverInRange,
  261. "hover--outside-range": this.startDate && !isHoverInRange,
  262. "focused--start": isFocusedOnStart,
  263. "focused--end": !isFocusedOnStart
  264. }, 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) => {
  265. // when moving via keyboard, focus must be updated on active date
  266. if (ref && active && this.activeFocus) {
  267. el === null || el === void 0 ? void 0 : el.focus();
  268. }
  269. }, scale: this.scale, selected: this.isSelected(date), startOfRange: this.isStartOfRange(date), value: date }));
  270. }
  271. isFocusedOnStart() {
  272. var _a;
  273. return ((_a = this.hoverRange) === null || _a === void 0 ? void 0 : _a.focused) === "start";
  274. }
  275. isHoverInRange() {
  276. if (!this.hoverRange) {
  277. return false;
  278. }
  279. const { start, end } = this.hoverRange;
  280. return !!((!this.isFocusedOnStart() && this.startDate && (!this.endDate || end < this.endDate)) ||
  281. (this.isFocusedOnStart() && this.startDate && start > this.startDate));
  282. }
  283. isRangeHover(date) {
  284. if (!this.hoverRange) {
  285. return false;
  286. }
  287. const { start, end } = this.hoverRange;
  288. const isStart = this.isFocusedOnStart();
  289. const insideRange = this.isHoverInRange();
  290. const cond1 = insideRange &&
  291. ((!isStart && date > this.startDate && (date < end || sameDate(date, end))) ||
  292. (isStart && date < this.endDate && (date > start || sameDate(date, start))));
  293. const cond2 = !insideRange &&
  294. ((!isStart && date >= this.endDate && (date < end || sameDate(date, end))) ||
  295. (isStart &&
  296. (date < this.startDate || (this.endDate && sameDate(date, this.startDate))) &&
  297. (date > start || sameDate(date, start))));
  298. return cond1 || cond2;
  299. }
  300. static get is() { return "calcite-date-picker-month"; }
  301. static get encapsulation() { return "shadow"; }
  302. static get originalStyleUrls() {
  303. return {
  304. "$": ["date-picker-month.scss"]
  305. };
  306. }
  307. static get styleUrls() {
  308. return {
  309. "$": ["date-picker-month.css"]
  310. };
  311. }
  312. static get properties() {
  313. return {
  314. "selectedDate": {
  315. "type": "unknown",
  316. "mutable": false,
  317. "complexType": {
  318. "original": "Date",
  319. "resolved": "Date",
  320. "references": {
  321. "Date": {
  322. "location": "global"
  323. }
  324. }
  325. },
  326. "required": false,
  327. "optional": false,
  328. "docs": {
  329. "tags": [],
  330. "text": "Already selected date."
  331. }
  332. },
  333. "activeDate": {
  334. "type": "unknown",
  335. "mutable": false,
  336. "complexType": {
  337. "original": "Date",
  338. "resolved": "Date",
  339. "references": {
  340. "Date": {
  341. "location": "global"
  342. }
  343. }
  344. },
  345. "required": false,
  346. "optional": false,
  347. "docs": {
  348. "tags": [],
  349. "text": "Date currently active."
  350. },
  351. "defaultValue": "new Date()"
  352. },
  353. "startDate": {
  354. "type": "unknown",
  355. "mutable": false,
  356. "complexType": {
  357. "original": "Date",
  358. "resolved": "Date",
  359. "references": {
  360. "Date": {
  361. "location": "global"
  362. }
  363. }
  364. },
  365. "required": false,
  366. "optional": true,
  367. "docs": {
  368. "tags": [],
  369. "text": "Start date currently active."
  370. }
  371. },
  372. "endDate": {
  373. "type": "unknown",
  374. "mutable": false,
  375. "complexType": {
  376. "original": "Date",
  377. "resolved": "Date",
  378. "references": {
  379. "Date": {
  380. "location": "global"
  381. }
  382. }
  383. },
  384. "required": false,
  385. "optional": true,
  386. "docs": {
  387. "tags": [],
  388. "text": "End date currently active"
  389. }
  390. },
  391. "min": {
  392. "type": "unknown",
  393. "mutable": false,
  394. "complexType": {
  395. "original": "Date",
  396. "resolved": "Date",
  397. "references": {
  398. "Date": {
  399. "location": "global"
  400. }
  401. }
  402. },
  403. "required": false,
  404. "optional": false,
  405. "docs": {
  406. "tags": [],
  407. "text": "Minimum date of the calendar below which is disabled."
  408. }
  409. },
  410. "max": {
  411. "type": "unknown",
  412. "mutable": false,
  413. "complexType": {
  414. "original": "Date",
  415. "resolved": "Date",
  416. "references": {
  417. "Date": {
  418. "location": "global"
  419. }
  420. }
  421. },
  422. "required": false,
  423. "optional": false,
  424. "docs": {
  425. "tags": [],
  426. "text": "Maximum date of the calendar above which is disabled."
  427. }
  428. },
  429. "scale": {
  430. "type": "string",
  431. "mutable": false,
  432. "complexType": {
  433. "original": "Scale",
  434. "resolved": "\"l\" | \"m\" | \"s\"",
  435. "references": {
  436. "Scale": {
  437. "location": "import",
  438. "path": "../interfaces"
  439. }
  440. }
  441. },
  442. "required": false,
  443. "optional": false,
  444. "docs": {
  445. "tags": [],
  446. "text": "specify the scale of the date picker"
  447. },
  448. "attribute": "scale",
  449. "reflect": true
  450. },
  451. "localeData": {
  452. "type": "unknown",
  453. "mutable": false,
  454. "complexType": {
  455. "original": "DateLocaleData",
  456. "resolved": "DateLocaleData",
  457. "references": {
  458. "DateLocaleData": {
  459. "location": "import",
  460. "path": "../date-picker/utils"
  461. }
  462. }
  463. },
  464. "required": false,
  465. "optional": false,
  466. "docs": {
  467. "tags": [{
  468. "name": "internal",
  469. "text": undefined
  470. }],
  471. "text": "CLDR locale data for current locale"
  472. }
  473. },
  474. "hoverRange": {
  475. "type": "unknown",
  476. "mutable": false,
  477. "complexType": {
  478. "original": "HoverRange",
  479. "resolved": "HoverRange",
  480. "references": {
  481. "HoverRange": {
  482. "location": "import",
  483. "path": "../../utils/date"
  484. }
  485. }
  486. },
  487. "required": false,
  488. "optional": false,
  489. "docs": {
  490. "tags": [],
  491. "text": "The range of dates currently being hovered"
  492. }
  493. }
  494. };
  495. }
  496. static get events() {
  497. return [{
  498. "method": "calciteDatePickerSelect",
  499. "name": "calciteDatePickerSelect",
  500. "bubbles": true,
  501. "cancelable": false,
  502. "composed": true,
  503. "docs": {
  504. "tags": [],
  505. "text": "Event emitted when user selects the date."
  506. },
  507. "complexType": {
  508. "original": "Date",
  509. "resolved": "Date",
  510. "references": {
  511. "Date": {
  512. "location": "global"
  513. }
  514. }
  515. }
  516. }, {
  517. "method": "calciteInternalDatePickerHover",
  518. "name": "calciteInternalDatePickerHover",
  519. "bubbles": true,
  520. "cancelable": false,
  521. "composed": true,
  522. "docs": {
  523. "tags": [{
  524. "name": "internal",
  525. "text": undefined
  526. }],
  527. "text": "Event emitted when user hovers the date."
  528. },
  529. "complexType": {
  530. "original": "Date",
  531. "resolved": "Date",
  532. "references": {
  533. "Date": {
  534. "location": "global"
  535. }
  536. }
  537. }
  538. }, {
  539. "method": "calciteDatePickerActiveDateChange",
  540. "name": "calciteDatePickerActiveDateChange",
  541. "bubbles": true,
  542. "cancelable": false,
  543. "composed": true,
  544. "docs": {
  545. "tags": [],
  546. "text": "Active date for the user keyboard access."
  547. },
  548. "complexType": {
  549. "original": "Date",
  550. "resolved": "Date",
  551. "references": {
  552. "Date": {
  553. "location": "global"
  554. }
  555. }
  556. }
  557. }, {
  558. "method": "calciteInternalDatePickerMouseOut",
  559. "name": "calciteInternalDatePickerMouseOut",
  560. "bubbles": true,
  561. "cancelable": false,
  562. "composed": true,
  563. "docs": {
  564. "tags": [{
  565. "name": "internal",
  566. "text": undefined
  567. }],
  568. "text": ""
  569. },
  570. "complexType": {
  571. "original": "void",
  572. "resolved": "void",
  573. "references": {}
  574. }
  575. }];
  576. }
  577. static get elementRef() { return "el"; }
  578. static get listeners() {
  579. return [{
  580. "name": "pointerout",
  581. "method": "mouseoutHandler",
  582. "target": undefined,
  583. "capture": false,
  584. "passive": true
  585. }];
  586. }
  587. }