@forcecalendar/interface 1.5.0 → 1.6.0

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.
@@ -8,7 +8,7 @@ var n = class extends HTMLElement {
8
8
  this._initialized ||= (this.initialize(), !0), this.mount();
9
9
  }
10
10
  disconnectedCallback() {
11
- this.unmount(), this.cleanup(), this._styleEl = null, this._contentWrapper = null;
11
+ this.unmount(), this.cleanup(), this._styleEl && this._styleEl.remove(), this._contentWrapper && this._contentWrapper.remove(), this._styleEl = null, this._contentWrapper = null;
12
12
  }
13
13
  initialize() {}
14
14
  mount() {
@@ -236,7 +236,7 @@ var n = class extends HTMLElement {
236
236
  loading: !1,
237
237
  error: null,
238
238
  config: { ...t }
239
- }, this.subscribers = /* @__PURE__ */ new Set(), this.subscribe = this.subscribe.bind(this), this.unsubscribe = this.unsubscribe.bind(this), this.setState = this.setState.bind(this), this._syncEventsFromCore({ silent: !0 });
239
+ }, this.subscribers = /* @__PURE__ */ new Set(), this.subscribe = this.subscribe.bind(this), this.unsubscribe = this.unsubscribe.bind(this), this.setState = this.setState.bind(this), this._syncEventsFromCore({ silent: !0 }), this._visibleRangeKey = this._rangeKey(this.getVisibleRange());
240
240
  }
241
241
  _syncEventsFromCore(e = {}) {
242
242
  let { force: t = !1 } = e, n = this.calendar.getEvents() || [];
@@ -299,28 +299,28 @@ var n = class extends HTMLElement {
299
299
  });
300
300
  }
301
301
  setView(e) {
302
- this.calendar.setView(e), this.setState({ view: e }), this.eventBus.emit("view:changed", { view: e });
302
+ this.calendar.setView(e), this.setState({ view: e }), this.eventBus.emit("view:changed", { view: e }), this._syncVisibleRange();
303
303
  }
304
304
  getView() {
305
305
  return this.state.view;
306
306
  }
307
307
  setDate(e) {
308
- this.calendar.goToDate(e), this.setState({ currentDate: this.calendar.getCurrentDate() }), this.eventBus.emit("date:changed", { date: this.state.currentDate });
308
+ this.calendar.goToDate(e), this.setState({ currentDate: this.calendar.getCurrentDate() }), this.eventBus.emit("date:changed", { date: this.state.currentDate }), this._syncVisibleRange();
309
309
  }
310
310
  getCurrentDate() {
311
311
  return this.state.currentDate;
312
312
  }
313
313
  next() {
314
- this.calendar.next(), this.setState({ currentDate: this.calendar.getCurrentDate() }), this.eventBus.emit("navigation:next", { date: this.state.currentDate });
314
+ this.calendar.next(), this.setState({ currentDate: this.calendar.getCurrentDate() }), this.eventBus.emit("navigation:next", { date: this.state.currentDate }), this._syncVisibleRange();
315
315
  }
316
316
  previous() {
317
- this.calendar.previous(), this.setState({ currentDate: this.calendar.getCurrentDate() }), this.eventBus.emit("navigation:previous", { date: this.state.currentDate });
317
+ this.calendar.previous(), this.setState({ currentDate: this.calendar.getCurrentDate() }), this.eventBus.emit("navigation:previous", { date: this.state.currentDate }), this._syncVisibleRange();
318
318
  }
319
319
  today() {
320
- this.calendar.today(), this.setState({ currentDate: this.calendar.getCurrentDate() }), this.eventBus.emit("navigation:today", { date: this.state.currentDate });
320
+ this.calendar.today(), this.setState({ currentDate: this.calendar.getCurrentDate() }), this.eventBus.emit("navigation:today", { date: this.state.currentDate }), this._syncVisibleRange();
321
321
  }
322
322
  goToDate(e) {
323
- this.calendar.goToDate(e), this.setState({ currentDate: this.calendar.getCurrentDate() }), this.eventBus.emit("navigation:goto", { date: this.state.currentDate });
323
+ this.calendar.goToDate(e), this.setState({ currentDate: this.calendar.getCurrentDate() }), this.eventBus.emit("navigation:goto", { date: this.state.currentDate }), this._syncVisibleRange();
324
324
  }
325
325
  addEvent(e) {
326
326
  let t = this.calendar.addEvent(e);
@@ -350,6 +350,69 @@ var n = class extends HTMLElement {
350
350
  getEvents() {
351
351
  return this.calendar.getEvents() || [];
352
352
  }
353
+ setEvents(e, t = {}) {
354
+ let { removeMissing: n = !0 } = t, r = e ? Array.from(e) : [], i;
355
+ try {
356
+ i = typeof this.calendar.reconcileEvents == "function" ? this.calendar.setEvents(r, {
357
+ reconcile: !0,
358
+ removeMissing: n
359
+ }) : this._reconcileFallback(r, n);
360
+ } catch (e) {
361
+ throw this.eventBus.emit("event:error", {
362
+ action: "set",
363
+ events: r,
364
+ error: e
365
+ }), e;
366
+ }
367
+ let { added: a, updated: o, removed: s, unchanged: c } = i, l = {
368
+ events: this.getEvents(),
369
+ added: a,
370
+ updated: o,
371
+ removed: s,
372
+ unchanged: c
373
+ };
374
+ return (a.length > 0 || o.length > 0 || s.length > 0) && this.setState({ events: [...l.events] }), this.eventBus.emit("events:set", l), l;
375
+ }
376
+ _reconcileFallback(e, t) {
377
+ let n = /* @__PURE__ */ new Map();
378
+ e.forEach((e) => {
379
+ let t = e && e.id;
380
+ if (t == null || t === "") throw Error("Every event in a snapshot must have an id");
381
+ if (n.has(t)) throw Error(`Duplicate event id in snapshot: ${t}`);
382
+ n.set(t, e);
383
+ });
384
+ let r = {
385
+ added: [],
386
+ updated: [],
387
+ removed: [],
388
+ unchanged: []
389
+ }, i = new Map(this.getEvents().map((e) => [e.id, e]));
390
+ return t && i.forEach((e, t) => {
391
+ !n.has(t) && this.calendar.removeEvent(t) && r.removed.push(e);
392
+ }), n.forEach((e, t) => {
393
+ let n = i.get(t), a = typeof e.toObject == "function" ? e.toObject() : e;
394
+ if (!n) {
395
+ let n = this.calendar.addEvent(e);
396
+ if (!n) throw Error(`Failed to add event: ${t}`);
397
+ r.added.push(n);
398
+ } else if (n === e || this._isEquivalentFallback(n, a)) r.unchanged.push(n);
399
+ else {
400
+ let e = this.calendar.updateEvent(t, a);
401
+ if (!e) throw Error(`Failed to update event: ${t}`);
402
+ r.updated.push({
403
+ event: e,
404
+ oldEvent: n
405
+ });
406
+ }
407
+ }), r;
408
+ }
409
+ _isEquivalentFallback(e, t) {
410
+ let n = (e) => e instanceof Date ? e.getTime() : e == null ? NaN : new Date(e).getTime();
411
+ return Object.keys(t).every((r) => {
412
+ let i = e[r], a = t[r];
413
+ return r === "start" || r === "end" ? n(i) === n(a) : i === a ? !0 : i == null || a == null ? !1 : JSON.stringify(i) === JSON.stringify(a);
414
+ });
415
+ }
353
416
  syncEvents() {
354
417
  return this._syncEventsFromCore();
355
418
  }
@@ -388,6 +451,41 @@ var n = class extends HTMLElement {
388
451
  }
389
452
  return t;
390
453
  }
454
+ getVisibleRange() {
455
+ let e = this.calendar.getView(), t = this.calendar.getCurrentDate();
456
+ if (e !== "day") {
457
+ let e = this.calendar.getViewData() || {};
458
+ if (e.startDate instanceof Date && e.endDate instanceof Date) {
459
+ let t = new Date(e.startDate), n = new Date(e.endDate);
460
+ return e.type === "list" && (n = /* @__PURE__ */ new Date(n.getTime() - 1)), {
461
+ start: t,
462
+ end: n
463
+ };
464
+ }
465
+ }
466
+ let n = new Date(t);
467
+ n.setHours(0, 0, 0, 0);
468
+ let r = new Date(n);
469
+ return r.setDate(r.getDate() + 1), r.setMilliseconds(-1), {
470
+ start: n,
471
+ end: r
472
+ };
473
+ }
474
+ _rangeKey(e) {
475
+ return `${e.start.getTime()}:${e.end.getTime()}`;
476
+ }
477
+ _syncVisibleRange() {
478
+ if (!this.calendar) return;
479
+ let e = this.getVisibleRange(), t = this._rangeKey(e);
480
+ if (t === this._visibleRangeKey) return;
481
+ this._visibleRangeKey = t;
482
+ let n = {
483
+ ...e,
484
+ view: this.state.view,
485
+ date: this.state.currentDate
486
+ };
487
+ this.eventBus.emit("range:changed", n);
488
+ }
391
489
  selectEvent(e) {
392
490
  this.setState({ selectedEvent: e }), this.eventBus.emit("event:selected", { event: e });
393
491
  }
@@ -428,7 +526,7 @@ var n = class extends HTMLElement {
428
526
  this.setState({ config: {
429
527
  ...this.state.config,
430
528
  ...e
431
- } }), e.weekStartsOn !== void 0 && this.calendar.setWeekStartsOn(e.weekStartsOn), e.locale !== void 0 && this.calendar.setLocale(e.locale), e.timeZone !== void 0 && this.calendar.setTimezone(e.timeZone);
529
+ } }), e.weekStartsOn !== void 0 && (this.calendar.setWeekStartsOn(e.weekStartsOn), this._syncVisibleRange()), e.locale !== void 0 && this.calendar.setLocale(e.locale), e.timeZone !== void 0 && this.calendar.setTimezone(e.timeZone);
432
530
  }
433
531
  destroy() {
434
532
  this.subscribers.clear(), this._subscriberIds &&= (this._subscriberIds.clear(), null), this.eventBus &&= (this.eventBus.clear(), null), this.state = null, this.calendar = null;
@@ -2084,7 +2182,7 @@ var S = class e extends n {
2084
2182
  ];
2085
2183
  }
2086
2184
  constructor() {
2087
- super(), this.stateManager = null, this.currentView = null, this._hasRendered = !1, this._busUnsubscribers = [];
2185
+ super(), this.stateManager = null, this.currentView = null, this._hasRendered = !1, this._busUnsubscribers = [], this._pendingEvents = null;
2088
2186
  }
2089
2187
  propChanged(e, t, n) {
2090
2188
  if (!(!this.stateManager || t === n)) switch (e) {
@@ -2117,7 +2215,16 @@ var S = class e extends n {
2117
2215
  timeZone: this.getAttribute("timezone") || Intl.DateTimeFormat().resolvedOptions().timeZone,
2118
2216
  weekStartsOn: parseInt(this.getAttribute("week-starts-on") || "0")
2119
2217
  };
2120
- this.stateManager = new a(e), this._stateUnsubscribe = this.stateManager.subscribe(this.handleStateChange.bind(this)), this.setupEventListeners();
2218
+ if (this.stateManager = new a(e), this._stateUnsubscribe = this.stateManager.subscribe(this.handleStateChange.bind(this)), this.setupEventListeners(), this._upgradeProperty("events"), this._pendingEvents) {
2219
+ let e = this._pendingEvents;
2220
+ this._pendingEvents = null, this.stateManager.setEvents(e.events, e.options);
2221
+ }
2222
+ }
2223
+ _upgradeProperty(e) {
2224
+ if (Object.prototype.hasOwnProperty.call(this, e)) {
2225
+ let t = this[e];
2226
+ delete this[e], this[e] = t;
2227
+ }
2121
2228
  }
2122
2229
  setupEventListeners() {
2123
2230
  this._busUnsubscribers.forEach((e) => e()), this._busUnsubscribers = [];
@@ -2145,6 +2252,10 @@ var S = class e extends n {
2145
2252
  this.emit("calendar-event-updated", e);
2146
2253
  })), this._busUnsubscribers.push(e.on("event:deleted", (e) => {
2147
2254
  this.emit("calendar-event-deleted", e);
2255
+ })), this._busUnsubscribers.push(e.on("events:set", (e) => {
2256
+ this.emit("calendar-events-set", e);
2257
+ })), this._busUnsubscribers.push(e.on("range:changed", (e) => {
2258
+ this.emit("calendar-range-change", e);
2148
2259
  })), this._busUnsubscribers.push(e.on("date:selected", (e) => {
2149
2260
  this.emit("calendar-date-select", e);
2150
2261
  }));
@@ -2196,7 +2307,7 @@ var S = class e extends n {
2196
2307
  t && (t.style.display = e ? "flex" : "none"), n && (n.style.display = e ? "none" : "flex");
2197
2308
  }
2198
2309
  mount() {
2199
- this.currentView = this.stateManager.getView(), super.mount();
2310
+ (!this.stateManager || !this.stateManager.state) && this.initialize(), this._stateUnsubscribe || (this._stateUnsubscribe = this.stateManager.subscribe(this.handleStateChange.bind(this)), this.setupEventListeners()), this.currentView = this.stateManager.getView(), super.mount();
2200
2311
  }
2201
2312
  loadView(e) {
2202
2313
  !e || this.currentView === e || (this.currentView = e, this._switchView(), this._updateViewButtons(), this._updateTitle());
@@ -2737,7 +2848,16 @@ var S = class e extends n {
2737
2848
  id: n,
2738
2849
  ...t
2739
2850
  });
2740
- }), this._hasRendered = !0;
2851
+ });
2852
+ let i = !this._hasRendered;
2853
+ if (this._hasRendered = !0, i && this.stateManager) {
2854
+ let e = this.stateManager.getState();
2855
+ this.emit("calendar-range-change", {
2856
+ ...this.stateManager.getVisibleRange(),
2857
+ view: e.view,
2858
+ date: e.currentDate
2859
+ });
2860
+ }
2741
2861
  }
2742
2862
  handleNavigation(e) {
2743
2863
  switch (e.currentTarget.dataset.action) {
@@ -2787,6 +2907,21 @@ var S = class e extends n {
2787
2907
  getEvents() {
2788
2908
  return this.stateManager.getEvents();
2789
2909
  }
2910
+ setEvents(e, t = {}) {
2911
+ return this.stateManager ? this.stateManager.setEvents(e, t) : (this._pendingEvents = {
2912
+ events: e ? Array.from(e) : [],
2913
+ options: t
2914
+ }, null);
2915
+ }
2916
+ get events() {
2917
+ return this.stateManager ? this.stateManager.getEvents() : this._pendingEvents ? this._pendingEvents.events : [];
2918
+ }
2919
+ set events(e) {
2920
+ this.setEvents(e);
2921
+ }
2922
+ getVisibleRange() {
2923
+ return this.stateManager ? this.stateManager.getVisibleRange() : null;
2924
+ }
2790
2925
  setView(e) {
2791
2926
  this.stateManager.setView(e);
2792
2927
  }
@@ -2803,14 +2938,17 @@ var S = class e extends n {
2803
2938
  this.stateManager.today();
2804
2939
  }
2805
2940
  unmount() {
2806
- this.destroy();
2941
+ this._releaseBindings();
2807
2942
  }
2808
2943
  destroy() {
2809
- this._busUnsubscribers.forEach((e) => e()), this._busUnsubscribers = [], this._stateUnsubscribe &&= (this._stateUnsubscribe(), null), this._currentViewInstance && this._currentViewInstance.cleanup && (this._currentViewInstance.cleanup(), this._currentViewInstance = null), this.stateManager && this.stateManager.destroy(), super.cleanup();
2944
+ this._releaseBindings(), this.stateManager && this.stateManager.destroy();
2945
+ }
2946
+ _releaseBindings() {
2947
+ this._busUnsubscribers.forEach((e) => e()), this._busUnsubscribers = [], this._stateUnsubscribe &&= (this._stateUnsubscribe(), null), this._currentViewInstance && this._currentViewInstance.cleanup && this._currentViewInstance.cleanup(), this._currentViewInstance = null, this._hasRendered = !1, super.cleanup();
2810
2948
  }
2811
2949
  };
2812
2950
  customElements.get("forcecal-main") || customElements.define("forcecal-main", S);
2813
2951
  //#endregion
2814
- export { n as BaseComponent, u as BaseViewRenderer, s as DOMUtils, o as DateUtils, b as DayViewRenderer, r as EventBus, S as ForceCalendar, v as MonthViewRenderer, a as StateManager, l as StyleUtils, y as WeekViewRenderer, i as eventBus };
2952
+ export { n as BaseComponent, u as BaseViewRenderer, s as DOMUtils, o as DateUtils, b as DayViewRenderer, r as EventBus, x as EventForm, S as ForceCalendar, v as MonthViewRenderer, a as StateManager, l as StyleUtils, y as WeekViewRenderer, i as eventBus };
2815
2953
 
2816
2954
  //# sourceMappingURL=force-calendar-interface.esm.js.map