@progressive-development/pd-calendar 0.2.21 → 0.5.2

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.
@@ -1,153 +1,108 @@
1
- /* eslint-disable lit-a11y/click-events-have-key-events */
1
+ import { LitElement, css, html } from "lit";
2
+ import { msg, updateWhenLocaleChanges } from "@lit/localize";
3
+ import { format } from "fecha";
4
+ import "@progressive-development/pd-icon/pd-icon";
5
+ import { PDColorStyles, PDFontStyles } from "@progressive-development/pd-shared-styles";
6
+ import "./PdCalendarCell.js";
7
+ import { PdYearPopup } from "./PdYearPopup.js";
2
8
  /**
3
9
  * @license
4
10
  * Copyright (c) 2021 PD Progressive Development UG. All rights reserved.
5
11
  */
6
-
7
- import { LitElement, html, css } from 'lit';
8
- import { msg, updateWhenLocaleChanges } from '@lit/localize';
9
-
10
- import { format } from 'fecha';
11
- import '@progressive-development/pd-icon/pd-icon.js';
12
-
13
- import { PDColorStyles, PDFontStyles } from '@progressive-development/pd-shared-styles';
14
-
15
- import './PdCalendarCell.js';
16
- import { PdYearPopup } from './PdYearPopup.js';
17
-
18
12
  const TOUCH_MIN_MOVE = 70;
19
-
20
- const loadLocales = () =>
21
- ({
22
- monthNames: [
23
- msg("Januar", { id: 'pd.datepicker.month.jan'}),
24
- msg("Februar", { id: 'pd.datepicker.month.feb'}),
25
- msg("März", { id: 'pd.datepicker.month.mar'}),
26
- msg("April", { id: 'pd.datepicker.month.apr'}),
27
- msg("Mai", { id: 'pd.datepicker.month.may'}),
28
- msg("Juni", { id: 'pd.datepicker.month.jun'}),
29
- msg("Juli", { id: 'pd.datepicker.month.jul'}),
30
- msg("August", { id: 'pd.datepicker.month.aug'}),
31
- msg("September", { id: 'pd.datepicker.month.sep'}),
32
- msg("Oktober", { id: 'pd.datepicker.month.oct'}),
33
- msg("November", { id: 'pd.datepicker.month.nov'}),
34
- msg("Dezember", { id: 'pd.datepicker.month.dec'}),
35
- ],
36
- weekdays: [
37
- msg("Sonntag", { id: 'pd.datepicker.day.sun'}),
38
- msg("Montag", { id: 'pd.datepicker.day.mon'}),
39
- msg("Dienstag", { id: 'pd.datepicker.day.tue'}),
40
- msg("Mittwoch", { id: 'pd.datepicker.day.wed'}),
41
- msg("Donnerstag", { id: 'pd.datepicker.day.thi'}),
42
- msg("Freitag", { id: 'pd.datepicker.day.fri'}),
43
- msg("Samstag", { id: 'pd.datepicker.day.sat'}),
44
- ],
45
- weekdaysShort: [
46
- msg("Mo", { id: 'pd.datepicker.shortday.mon'}),
47
- msg("Di", { id: 'pd.datepicker.shortday.tue'}),
48
- msg("Mi", { id: 'pd.datepicker.shortday.wed'}),
49
- msg("Do", { id: 'pd.datepicker.shortday.thi'}),
50
- msg("Fr", { id: 'pd.datepicker.shortday.fri'}),
51
- msg("Sa", { id: 'pd.datepicker.shortday.sat'}),
52
- msg("So", { id: 'pd.datepicker.shortday.sun'}),
53
- ],
54
- });
55
-
13
+ const loadLocales = () => ({
14
+ monthNames: [
15
+ msg("Januar", { id: "pd.datepicker.month.jan" }),
16
+ msg("Februar", { id: "pd.datepicker.month.feb" }),
17
+ msg("März", { id: "pd.datepicker.month.mar" }),
18
+ msg("April", { id: "pd.datepicker.month.apr" }),
19
+ msg("Mai", { id: "pd.datepicker.month.may" }),
20
+ msg("Juni", { id: "pd.datepicker.month.jun" }),
21
+ msg("Juli", { id: "pd.datepicker.month.jul" }),
22
+ msg("August", { id: "pd.datepicker.month.aug" }),
23
+ msg("September", { id: "pd.datepicker.month.sep" }),
24
+ msg("Oktober", { id: "pd.datepicker.month.oct" }),
25
+ msg("November", { id: "pd.datepicker.month.nov" }),
26
+ msg("Dezember", { id: "pd.datepicker.month.dec" })
27
+ ],
28
+ weekdays: [
29
+ msg("Sonntag", { id: "pd.datepicker.day.sun" }),
30
+ msg("Montag", { id: "pd.datepicker.day.mon" }),
31
+ msg("Dienstag", { id: "pd.datepicker.day.tue" }),
32
+ msg("Mittwoch", { id: "pd.datepicker.day.wed" }),
33
+ msg("Donnerstag", { id: "pd.datepicker.day.thi" }),
34
+ msg("Freitag", { id: "pd.datepicker.day.fri" }),
35
+ msg("Samstag", { id: "pd.datepicker.day.sat" })
36
+ ],
37
+ weekdaysShort: [
38
+ msg("Mo", { id: "pd.datepicker.shortday.mon" }),
39
+ msg("Di", { id: "pd.datepicker.shortday.tue" }),
40
+ msg("Mi", { id: "pd.datepicker.shortday.wed" }),
41
+ msg("Do", { id: "pd.datepicker.shortday.thi" }),
42
+ msg("Fr", { id: "pd.datepicker.shortday.fri" }),
43
+ msg("Sa", { id: "pd.datepicker.shortday.sat" }),
44
+ msg("So", { id: "pd.datepicker.shortday.sun" })
45
+ ]
46
+ });
56
47
  const LOCALES = loadLocales();
57
-
58
- // list views (list, month, week)
59
48
  const VIEW_LIST = 1;
60
49
  const VIEW_MONTH = 2;
61
50
  const VIEW_WEEK = 3;
62
-
63
51
  const isToday = (someDate) => {
64
52
  if (!someDate) {
65
53
  return false;
66
54
  }
67
- const today = new Date()
68
- return someDate.getDate() === today.getDate() &&
69
- someDate.getMonth() === today.getMonth() &&
70
- someDate.getFullYear() === today.getFullYear()
71
- }
72
-
55
+ const today = /* @__PURE__ */ new Date();
56
+ return someDate.getDate() === today.getDate() && someDate.getMonth() === today.getMonth() && someDate.getFullYear() === today.getFullYear();
57
+ };
73
58
  const isSelected = (someDate1, someDate2) => {
74
59
  if (!someDate1 || !someDate2) {
75
60
  return false;
76
- }
77
- return someDate1.getDate() === someDate2.getDate() &&
78
- someDate1.getMonth() === someDate2.getMonth() &&
79
- someDate1.getFullYear() === someDate2.getFullYear()
80
- }
81
-
82
- /**
83
- * PdCalendar displays montly view with information (selectable day cell).
84
- *
85
- * refDate: Date => if set use the day for initial month view
86
- * prevMonthConstraint: number => if set, allows only back steps until the constraint is reached
87
- * nextMonthConstraint: number => if set, allows only forward steps until the constraint is reached
88
- * data: Object => input calendar Data.
89
- * numberClass: string => display day in cell (top-left and center available)
90
- *
91
- * Data:
92
- * monthSelection: number => number wich indicates, the month with data, if the origin month contains no valid data.
93
- * starts the calendar view initial with the month from monthSelection
94
- * Input Data as followed
95
- * "2023-11-04": [
96
- * {
97
- * info: "some txt", => Text for day cell
98
- * special?: true|false => Highlight cell
99
- * }
100
- * ]
101
- */
102
- export class PdCalendar extends LitElement {
103
-
61
+ }
62
+ return someDate1.getDate() === someDate2.getDate() && someDate1.getMonth() === someDate2.getMonth() && someDate1.getFullYear() === someDate2.getFullYear();
63
+ };
64
+ class PdCalendar extends LitElement {
104
65
  /**
105
66
  * Fired when next or prev month clicked
106
67
  * @event change-month
107
68
  */
108
-
109
69
  /**
110
70
  * Fired when date with info element clicked => At the moment only for those ones
111
71
  * @event select-date
112
72
  */
113
-
114
73
  /**
115
74
  * Fired when mouse entered date cell, detail contains date (Date) and dateKey(string).
116
75
  * @event mouse-enter-date
117
76
  */
118
-
119
77
  /**
120
78
  * Fired when date leaves date cell
121
79
  * @event mouse-leave-date
122
80
  */
123
-
124
- static get properties() {
81
+ static get properties() {
125
82
  return {
126
83
  refDate: { type: Object },
127
- selectableDates: { type: Boolean},
128
- withYearPopup: { type: Array},
129
- withWheelNavigation: { type: Boolean},
130
- withTouchNavigation: { type: Boolean},
131
- showSelection: { type: Boolean},
84
+ selectableDates: { type: Boolean },
85
+ withYearPopup: { type: Array },
86
+ withWheelNavigation: { type: Boolean },
87
+ withTouchNavigation: { type: Boolean },
88
+ showSelection: { type: Boolean },
132
89
  hideWeekend: { type: Boolean, reflect: true },
133
90
  prevMonthConstraint: { type: Number },
134
91
  nextMonthConstraint: { type: Number },
135
92
  data: { type: Object },
136
-
137
93
  /**
138
94
  * CSS class for day number inside the cell, top-left and center available
139
95
  */
140
- numberClass: { type: String },
141
-
96
+ numberClass: { type: String },
142
97
  _currentDate: { type: Object },
143
98
  _viewType: { type: Number },
144
- _wheelDelta: { type: Number, state: true },
99
+ _wheelDelta: { type: Number, state: true }
145
100
  };
146
101
  }
147
-
148
102
  static get styles() {
149
103
  return [
150
- PDColorStyles, PDFontStyles,
104
+ PDColorStyles,
105
+ PDFontStyles,
151
106
  css`
152
107
  :host {
153
108
 
@@ -256,60 +211,48 @@ export class PdCalendar extends LitElement {
256
211
  color: var(--pd-default-hover-col);
257
212
  }
258
213
 
259
- `];
214
+ `
215
+ ];
260
216
  }
261
-
262
217
  constructor() {
263
218
  super();
264
219
  updateWhenLocaleChanges(this);
265
-
266
220
  this.data = {};
267
221
  this._viewType = VIEW_MONTH;
268
- this.numberClass = "top-left";
222
+ this.numberClass = "top-left";
269
223
  this.hideWeekend = false;
270
224
  this.selectableDates = false;
271
225
  this.showSelection = false;
272
- this._wheelDelta = 0;
226
+ this._wheelDelta = 0;
273
227
  this._currentMonthNavNr = 0;
274
- }
275
-
228
+ }
276
229
  connectedCallback() {
277
230
  super.connectedCallback();
278
-
279
- // get latest possible date
280
- const ref = this.refDate || new Date();
281
-
282
- // initialize date values with current date
231
+ const ref = this.refDate || /* @__PURE__ */ new Date();
283
232
  this._initFromDate(ref);
284
233
  }
285
-
286
234
  update(changedProperties) {
287
- // check for month selection (set by remote server if no result available for the first call)
288
- if (changedProperties.has("data") && this.data && this.data.monthSelection) {
235
+ if (changedProperties.has("data") && this.data && this.data.monthSelection) {
289
236
  const now = new Date(Date.now());
290
237
  const startDateNextMonth = new Date(now.getFullYear(), this.data.monthSelection, 1);
291
238
  this._currentMonthNavNr += 1;
292
239
  this._initFromDate(startDateNextMonth);
293
240
  }
294
-
295
241
  if (changedProperties.has("refDate") && this.refDate) {
296
242
  this._initFromDate(this.refDate);
297
243
  }
298
244
  super.update(changedProperties);
299
245
  }
300
-
301
246
  render() {
302
247
  return html`
303
248
  ${this.renderCalendarByViewType()}
304
249
  <slot name="calFooter"></slot>
305
250
  `;
306
251
  }
307
-
308
252
  renderCalendarByViewType() {
309
253
  switch (this._viewType) {
310
254
  case VIEW_LIST:
311
- // return this.renderListCalendar();
312
- return '';
255
+ return "";
313
256
  case VIEW_MONTH:
314
257
  return this.renderMonthCalendar();
315
258
  case VIEW_WEEK:
@@ -319,39 +262,27 @@ export class PdCalendar extends LitElement {
319
262
  }
320
263
  }
321
264
  /*
322
- renderListCalendar() {
323
- // TODO
324
- }
325
- */
326
-
265
+ renderListCalendar() {
266
+ // TODO
267
+ }
268
+ */
327
269
  renderMonthCalendar() {
328
- // generate divs for each day
329
270
  const itemTemplates = [];
330
-
331
- const sizeVar =
332
- this._numberOfDays >= 31 && this._daysFromPreviousMonth.length >= 5
333
- ? 'max'
334
- : 'normal';
271
+ const sizeVar = this._numberOfDays >= 31 && this._daysFromPreviousMonth.length >= 5 ? "max" : "normal";
335
272
  for (let i = 1; i <= this._numberOfDays; i += 1) {
336
273
  const tmpDate = new Date(this._year, this._currentDate.getMonth(), i);
337
274
  const day = tmpDate.getDay();
338
- if (!this.hideWeekend || ((day !== 6) && (day !== 0))) {
339
- const key = format(tmpDate, 'YYYY-MM-DD');
340
- // TODO => Array / Unit Thema... hier fest auf 0 zugegriffen zunächst
341
-
342
- const infoTxt = this.data && this.data[key] ? this.data[key][0].info : undefined;
343
- const special = this.data && this.data[key]
344
- ? this.data[key][0].special || false
345
- : false;
346
-
347
-
348
- itemTemplates.push(html`
275
+ if (!this.hideWeekend || day !== 6 && day !== 0) {
276
+ const key = format(tmpDate, "YYYY-MM-DD");
277
+ const infoTxt = this.data && this.data[key] ? this.data[key][0].info : void 0;
278
+ const special = this.data && this.data[key] ? this.data[key][0].special || false : false;
279
+ itemTemplates.push(html`
349
280
  <pd-calendar-cell
350
281
  key="${key}"
351
282
  dayNumber="${i}"
352
283
  weekDayNumber="${tmpDate.getDay()}"
353
284
  infoTxt="${infoTxt}"
354
- ?selectEnabled="${this.selectableDates || infoTxt !== undefined}"
285
+ ?selectEnabled="${this.selectableDates || infoTxt !== void 0}"
355
286
  ?special="${special}"
356
287
  numberClass="${this.numberClass}"
357
288
  @select-date="${this._forwardEvent}"
@@ -360,10 +291,9 @@ export class PdCalendar extends LitElement {
360
291
  ?today="${this.selectableDates && isToday(tmpDate)}"
361
292
  ?selected="${this.showSelection && isSelected(this.refDate, tmpDate)}"
362
293
  ></pd-calendar-cell>
363
- `);
364
- }
294
+ `);
295
+ }
365
296
  }
366
-
367
297
  return html`
368
298
  <div class="layout-container">
369
299
  <div class="header">
@@ -402,30 +332,26 @@ export class PdCalendar extends LitElement {
402
332
  @touchend="${this._touchEndEvent}"
403
333
  class="content grid-container ${sizeVar}">
404
334
  ${this._getWeekDays().map(
405
- shortName => html` <div class="title-week-day">${shortName}</div>`
406
- )}
335
+ (shortName) => html` <div class="title-week-day">${shortName}</div>`
336
+ )}
407
337
  ${this._daysFromPreviousMonth.map(
408
- () => html` <div class="cell cell-empty"></div>`
409
- )}
338
+ () => html` <div class="cell cell-empty"></div>`
339
+ )}
410
340
  ${itemTemplates}
411
341
  </div>
412
342
  </div>
413
343
  `;
414
344
  }
415
-
416
345
  // eslint-disable-next-line class-methods-use-this
417
346
  _openYearPopup() {
418
-
419
347
  const popup = new PdYearPopup();
420
348
  popup.yearSelection = this.withYearPopup;
421
349
  this.shadowRoot.getElementById("titleContentId").appendChild(popup);
422
-
423
350
  popup.addEventListener("abort-year-selection", () => {
424
351
  this.shadowRoot.getElementById("titleContentId").removeChild(popup);
425
352
  });
426
-
427
353
  popup.addEventListener("change-year-selection", (e) => {
428
- const newYear = e.detail.year;
354
+ const newYear = e.detail.year;
429
355
  if (newYear) {
430
356
  const newDate = new Date(
431
357
  this._currentDate.getFullYear(),
@@ -434,25 +360,23 @@ export class PdCalendar extends LitElement {
434
360
  );
435
361
  newDate.setFullYear(newYear);
436
362
  this.dispatchEvent(
437
- new CustomEvent('change-month', {
363
+ new CustomEvent("change-month", {
438
364
  detail: {
439
- newDate,
440
- },
365
+ newDate
366
+ }
441
367
  })
442
- );
368
+ );
443
369
  this._initFromDate(newDate);
444
370
  }
445
371
  this.shadowRoot.getElementById("titleContentId").removeChild(popup);
446
372
  });
447
373
  }
448
-
449
374
  // eslint-disable-next-line class-methods-use-this
450
375
  _getWeekDays() {
451
376
  return this.hideWeekend ? [
452
377
  ...LOCALES.weekdaysShort
453
378
  ].splice(0, 5) : LOCALES.weekdaysShort;
454
379
  }
455
-
456
380
  _nextMonth() {
457
381
  if (this._checkNextMonthConstraint()) {
458
382
  const newDate = new Date(
@@ -462,19 +386,18 @@ export class PdCalendar extends LitElement {
462
386
  );
463
387
  newDate.setMonth(newDate.getMonth() + 1);
464
388
  this.dispatchEvent(
465
- new CustomEvent('change-month', {
389
+ new CustomEvent("change-month", {
466
390
  detail: {
467
391
  newDate,
468
- next: true,
469
- },
392
+ next: true
393
+ }
470
394
  })
471
395
  );
472
396
  this._currentMonthNavNr += 1;
473
397
  this._initFromDate(newDate);
474
398
  }
475
399
  }
476
-
477
- _previousMonth() {
400
+ _previousMonth() {
478
401
  if (this._checkPrevMonthConstraint()) {
479
402
  const newDate = new Date(
480
403
  this._currentDate.getFullYear(),
@@ -483,54 +406,45 @@ export class PdCalendar extends LitElement {
483
406
  );
484
407
  newDate.setMonth(newDate.getMonth() - 1);
485
408
  this.dispatchEvent(
486
- new CustomEvent('change-month', {
409
+ new CustomEvent("change-month", {
487
410
  detail: {
488
411
  newDate,
489
- prev: true,
490
- },
412
+ prev: true
413
+ }
491
414
  })
492
415
  );
493
416
  this._currentMonthNavNr -= 1;
494
417
  this._initFromDate(newDate);
495
418
  }
496
419
  }
497
-
498
420
  // eslint-disable-next-line class-methods-use-this
499
- _wheelEvent(e) {
500
-
421
+ _wheelEvent(e) {
501
422
  if (this.withWheelNavigation) {
502
-
503
423
  this._wheelDelta += e.deltaY;
504
-
505
- if (this._wheelDelta > 360) {
424
+ if (this._wheelDelta > 360) {
506
425
  this._wheelDelta = 0;
507
426
  this._nextMonth();
508
427
  }
509
-
510
- if (this._wheelDelta < -360) {
428
+ if (this._wheelDelta < -360) {
511
429
  this._wheelDelta = 0;
512
430
  this._previousMonth();
513
431
  }
514
- }
515
-
432
+ }
516
433
  e.preventDefault();
517
434
  e.stopPropagation();
518
435
  }
519
-
520
436
  // eslint-disable-next-line class-methods-use-this
521
- _touchStartEvent(e) {
437
+ _touchStartEvent(e) {
522
438
  if (this.withTouchNavigation) {
523
- this._wheelDelta = e.changedTouches[0].screenX;
439
+ this._wheelDelta = e.changedTouches[0].screenX;
524
440
  }
525
- e.stopPropagation();
441
+ e.stopPropagation();
526
442
  }
527
-
528
- _touchEndEvent(e) {
443
+ _touchEndEvent(e) {
529
444
  if (this.withTouchNavigation) {
530
- const touchEndX = e.changedTouches[0].screenX;
531
-
445
+ const touchEndX = e.changedTouches[0].screenX;
532
446
  const result = this._wheelDelta - touchEndX;
533
- if (result < (TOUCH_MIN_MOVE * -1)) {
447
+ if (result < TOUCH_MIN_MOVE * -1) {
534
448
  this._nextMonth();
535
449
  } else if (result > TOUCH_MIN_MOVE) {
536
450
  this._previousMonth();
@@ -538,23 +452,17 @@ export class PdCalendar extends LitElement {
538
452
  }
539
453
  e.stopPropagation();
540
454
  }
541
-
542
455
  _initFromDate(date) {
543
- this._monthName = date.toLocaleString('default', { month: 'long' });
456
+ this._monthName = date.toLocaleString("default", { month: "long" });
544
457
  this._year = date.getFullYear();
545
458
  this._daysFromPreviousMonth = PdCalendar._getPreviousMonthDays(date);
546
- // last month day
547
459
  const newDate2 = new Date(date.getFullYear(), date.getMonth() + 1, 0);
548
460
  this._numberOfDays = newDate2.getDate();
549
461
  this._currentDate = date;
550
462
  }
551
-
552
463
  static _getPreviousMonthDays(date) {
553
- // get first day of month
554
464
  const date1 = new Date(date.getFullYear(), date.getMonth(), 1);
555
- // get missing days to monday
556
465
  const missingDays = date1.getDay() > 0 ? Math.abs(1 - date1.getDay()) : 6;
557
- // create array with missing days (dates)
558
466
  const missDayArray = [];
559
467
  for (let i = 1; i <= missingDays; i += 1) {
560
468
  date1.setDate(date1.getDate() - 1);
@@ -564,45 +472,42 @@ export class PdCalendar extends LitElement {
564
472
  }
565
473
  return missDayArray;
566
474
  }
567
-
568
- _checkNextMonthConstraint() {
475
+ _checkNextMonthConstraint() {
569
476
  if (this.nextMonthConstraint > 0) {
570
477
  return this.nextMonthConstraint > this._currentMonthNavNr;
571
- }
478
+ }
572
479
  if (this.nextMonthConstraint === -1) {
573
480
  return true;
574
481
  }
575
482
  return false;
576
483
  }
577
-
578
- _checkPrevMonthConstraint() {
484
+ _checkPrevMonthConstraint() {
579
485
  if (this._currentMonthNavNr > 0 || this.prevMonthConstraint === -1) {
580
486
  return true;
581
487
  }
582
- return this.prevMonthConstraint > (this._currentMonthNavNr * -1);
583
-
488
+ return this.prevMonthConstraint > this._currentMonthNavNr * -1;
584
489
  }
585
-
586
490
  // Forward Event from Calendar Cell, should be refactored (events not bubbles up here)
587
491
  _forwardEvent(e) {
588
492
  this.dispatchEvent(
589
- new CustomEvent('select-date', {
493
+ new CustomEvent("select-date", {
590
494
  detail: e.detail
591
495
  })
592
496
  );
593
497
  }
594
-
595
498
  _forwardEnterEvent(e) {
596
499
  this.dispatchEvent(
597
- new CustomEvent('mouse-enter-date', {
500
+ new CustomEvent("mouse-enter-date", {
598
501
  detail: e.detail
599
502
  })
600
503
  );
601
504
  }
602
-
603
505
  _forwardLeaveEvent() {
604
506
  this.dispatchEvent(
605
- new CustomEvent('mouse-leave-date')
507
+ new CustomEvent("mouse-leave-date")
606
508
  );
607
509
  }
608
510
  }
511
+ export {
512
+ PdCalendar
513
+ };