@progressive-development/pd-calendar 0.2.12 → 0.2.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "progressive development calendar web component",
4
4
  "license": "SEE LICENSE IN LICENSE",
5
5
  "author": "PD Progressive Development",
6
- "version": "0.2.12",
6
+ "version": "0.2.13",
7
7
  "main": "index.js",
8
8
  "module": "index.js",
9
9
  "scripts": {
package/src/PdCalendar.js CHANGED
@@ -1,3 +1,4 @@
1
+ /* eslint-disable lit-a11y/click-events-have-key-events */
1
2
  /**
2
3
  * @license
3
4
  * Copyright (c) 2021 PD Progressive Development UG. All rights reserved.
@@ -12,6 +13,9 @@ import '@progressive-development/pd-icon/pd-icon.js';
12
13
  import { PDColorStyles, PDFontStyles } from '@progressive-development/pd-shared-styles';
13
14
 
14
15
  import './PdCalendarCell.js';
16
+ import { PdYearPopup } from './PdYearPopup.js';
17
+
18
+ const TOUCH_MIN_MOVE = 70;
15
19
 
16
20
  const loadLocales = () =>
17
21
  ({
@@ -121,6 +125,7 @@ export class PdCalendar extends LitElement {
121
125
  return {
122
126
  refDate: { type: Object },
123
127
  selectableDates: { type: Boolean},
128
+ withYearPopup: { type: Boolean},
124
129
  showSelection: { type: Boolean},
125
130
  hideWeekend: { type: Boolean, reflect: true },
126
131
  prevMonthConstraint: { type: Number },
@@ -133,7 +138,8 @@ export class PdCalendar extends LitElement {
133
138
  numberClass: { type: String },
134
139
 
135
140
  _currentDate: { type: Object },
136
- _viewType: { type: Number }
141
+ _viewType: { type: Number },
142
+ _wheelDelta: { type: Number, state: true },
137
143
  };
138
144
  }
139
145
 
@@ -221,6 +227,7 @@ export class PdCalendar extends LitElement {
221
227
  }
222
228
 
223
229
  .content-title {
230
+ position: relative;
224
231
  font-size: var(--pd-calendar-title-font-size, 1.2em);
225
232
  font-weight: bold;
226
233
  }
@@ -237,6 +244,16 @@ export class PdCalendar extends LitElement {
237
244
  --pd-icon-size: var(--pd-calendar-title-icon-size, 1.2em);
238
245
  --pd-icon-bg-col-hover: lightgrey;
239
246
  }
247
+
248
+ .year-popup-link {
249
+
250
+ }
251
+
252
+ .year-popup-link:hover {
253
+ cursor: pointer;
254
+ color: var(--pd-default-hover-col);
255
+ }
256
+
240
257
  `];
241
258
  }
242
259
 
@@ -250,7 +267,7 @@ export class PdCalendar extends LitElement {
250
267
  this.hideWeekend = false;
251
268
  this.selectableDates = false;
252
269
  this.showSelection = false;
253
-
270
+ this._wheelDelta = 0;
254
271
  this._currentMonthNavNr = 0;
255
272
  }
256
273
 
@@ -367,15 +384,21 @@ export class PdCalendar extends LitElement {
367
384
  ></pd-icon>
368
385
  </div>
369
386
 
370
- <div class="content-title">
371
- ${this._monthName} ${this._year}
387
+ <div id="titleContentId" class="content-title">
388
+ ${this._monthName} ${this.withYearPopup ? html`
389
+ <span class="year-popup-link" @click="${this._openYearPopup}">${this._year}</span>
390
+ ` : html`${this._year}`}
372
391
  </div>
373
392
 
374
393
  </div>
375
394
 
376
395
  </div>
377
396
 
378
- <div class="content grid-container ${sizeVar}">
397
+ <div
398
+ @wheel="${this._wheelEvent}"
399
+ @touchstart="${this._touchStartEvent}"
400
+ @touchend="${this._touchEndEvent}"
401
+ class="content grid-container ${sizeVar}">
379
402
  ${this._getWeekDays().map(
380
403
  shortName => html` <div class="title-week-day">${shortName}</div>`
381
404
  )}
@@ -388,6 +411,39 @@ export class PdCalendar extends LitElement {
388
411
  `;
389
412
  }
390
413
 
414
+ // eslint-disable-next-line class-methods-use-this
415
+ _openYearPopup() {
416
+
417
+ const popup = new PdYearPopup();
418
+ popup.yearSelection = ["2024", "2025"];
419
+ this.shadowRoot.getElementById("titleContentId").appendChild(popup);
420
+
421
+ popup.addEventListener("abort-year-selection", () => {
422
+ this.shadowRoot.getElementById("titleContentId").removeChild(popup);
423
+ });
424
+
425
+ popup.addEventListener("change-year-selection", (e) => {
426
+ const newYear = e.detail.year;
427
+ if (newYear) {
428
+ const newDate = new Date(
429
+ this._currentDate.getFullYear(),
430
+ this._currentDate.getMonth(),
431
+ 1
432
+ );
433
+ newDate.setFullYear(newYear);
434
+ this.dispatchEvent(
435
+ new CustomEvent('change-month', {
436
+ detail: {
437
+ newDate,
438
+ },
439
+ })
440
+ );
441
+ this._initFromDate(newDate);
442
+ }
443
+ this.shadowRoot.getElementById("titleContentId").removeChild(popup);
444
+ });
445
+ }
446
+
391
447
  // eslint-disable-next-line class-methods-use-this
392
448
  _getWeekDays() {
393
449
  return this.hideWeekend ? [
@@ -435,6 +491,47 @@ export class PdCalendar extends LitElement {
435
491
  }
436
492
  }
437
493
 
494
+ // eslint-disable-next-line class-methods-use-this
495
+ _wheelEvent(e) {
496
+
497
+ this._wheelDelta += e.deltaY;
498
+
499
+ if (this._wheelDelta > 360) {
500
+ this._wheelDelta = 0;
501
+ this._nextMonth();
502
+ }
503
+
504
+ if (this._wheelDelta < -360) {
505
+ this._wheelDelta = 0;
506
+ this._previousMonth();
507
+ }
508
+
509
+ e.preventDefault();
510
+ e.stopPropagation();
511
+
512
+ }
513
+
514
+ // eslint-disable-next-line class-methods-use-this
515
+ _touchStartEvent(e) {
516
+ this._wheelDelta = e.changedTouches[0].screenX;
517
+ e.preventDefault();
518
+ e.stopPropagation();
519
+ }
520
+
521
+ _touchEndEvent(e) {
522
+ const touchEndX = e.changedTouches[0].screenX;
523
+
524
+ const result = this._wheelDelta - touchEndX;
525
+ if (result < (TOUCH_MIN_MOVE * -1)) {
526
+ this._previousMonth();
527
+ } else if (result > TOUCH_MIN_MOVE) {
528
+ this._nextMonth();
529
+ }
530
+
531
+ e.preventDefault();
532
+ e.stopPropagation();
533
+ }
534
+
438
535
  _initFromDate(date) {
439
536
  this._monthName = date.toLocaleString('default', { month: 'long' });
440
537
  this._year = date.getFullYear();
@@ -0,0 +1,91 @@
1
+ /* eslint-disable lit-a11y/click-events-have-key-events */
2
+ import { LitElement, html, css } from 'lit';
3
+
4
+ export class PdYearPopup extends LitElement {
5
+ /**
6
+ * @event select-date - fires when user clicks on a selectable cell.
7
+ */
8
+
9
+ static get properties() {
10
+ return {
11
+ yearSelection: { type: Array },
12
+ currentYear: { type: String },
13
+ };
14
+ }
15
+
16
+ static get styles() {
17
+ return [
18
+ css`
19
+ :host {
20
+ position: absolute;
21
+ z-index: 1;
22
+ top: 25px;
23
+ right: 0;
24
+ }
25
+
26
+ ul {
27
+ list-style: none;
28
+ max-width: 200px;
29
+ margin: 0;
30
+ padding: 0;
31
+ background: var(--pd-default-light-col, #cdeaf844);
32
+ box-shadow: 3px 3px 5px 0 #ccc;
33
+ border: 1px solid var(--pd-default-col);
34
+
35
+ display: flex;
36
+ flex-direction: column;
37
+
38
+ }
39
+
40
+ li {
41
+ text-align: center;
42
+ padding: 10px;
43
+ color: var(--pd-default-dark-col)
44
+ }
45
+
46
+ .current {
47
+ }
48
+
49
+ .selectable:hover {
50
+ cursor: pointer;
51
+ }
52
+
53
+ `];
54
+ }
55
+
56
+ constructor() {
57
+ super();
58
+ this.yearSelection = [];
59
+
60
+ }
61
+
62
+ render() {
63
+ return html`
64
+ <ul>
65
+ ${this.yearSelection.map(year => html`
66
+ <li class="${this.currentYear === year ? 'current' : ''}" data-year="${year}" @click="${this._yearSelection}">${year}</li>
67
+ `)}
68
+ </ul>
69
+ `;
70
+ }
71
+
72
+ _yearSelection(event) {
73
+ if (event.target.dataset.year) {
74
+ this.dispatchEvent(new CustomEvent(
75
+ "change-year-selection", {
76
+ detail: {
77
+ year: event.target.dataset.year
78
+ }
79
+ }));
80
+ } else {
81
+ this._closeSelection();
82
+ }
83
+ }
84
+
85
+ _closeSelection() {
86
+ this.dispatchEvent(new CustomEvent("abort-year-selection"));
87
+ }
88
+
89
+ }
90
+
91
+ window.customElements.define('pd-year-popup', PdYearPopup);
@@ -43,7 +43,7 @@ function CalendarSmallTemplate({ refDate, hideWeekend, width, cellHeight, prevMo
43
43
 
44
44
  }
45
45
  </style>
46
- <pd-calendar .refDate="${refDate}" selectableDates showSelection ?hideWeekend="${hideWeekend}" class="calendar-small" prevMonthConstraint="${prevMonthConstraint}"
46
+ <pd-calendar withYearPopup .refDate="${refDate}" selectableDates showSelection ?hideWeekend="${hideWeekend}" class="calendar-small" prevMonthConstraint="${prevMonthConstraint}"
47
47
  nextMonthConstraint="${nextMonthConstraint}" numberClass="center"
48
48
  @mouse-enter-date="${(e) => {
49
49
  console.log("Mouse Enter Event: ", e);