-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent-manager.ts
62 lines (54 loc) · 2.29 KB
/
event-manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { LitElementWw } from "@webwriter/lit";
import { customElement, property } from "lit/decorators.js";
import "@shoelace-style/shoelace/dist/themes/light.css";
import { EventContainer } from "./event-container.component";
import { TimelineDialog } from "./tl-dialog";
import { WebWriterTimeline } from "./widgets/webwriter-timeline";
import { TlEventData } from "./tl-event-data";
@customElement("event-manager")
export class EventManager extends LitElementWw {
@property({ type: Number, attribute: true, reflect: true }) accessor tabIndex = -1;
static get scopedElements() {
return {
"event-container": EventContainer,
"timeline-dialog": TimelineDialog,
"webwriter-timeline": WebWriterTimeline,
};
}
// adding event to webwriter-timeline slot by creating event-container, sorting all appended events
addEvent(event: CustomEvent<TlEventData>, timeline) {
const tldialog = timeline?.shadowRoot?.querySelector( "timeline-dialog" ) as TimelineDialog;
const { title, startDate, endDate } = event.detail;
// const timeline_event = new EventContainer();
const timeline_event = document.createElement("event-container");
timeline_event.setAttribute("event_title", title);
timeline_event.setAttribute("event_startDate", JSON.stringify(startDate));
if (endDate !== undefined) {
timeline_event.setAttribute("event_endDate", JSON.stringify(endDate));
}
timeline_event.setAttribute("slot", "event-slot");
timeline.appendChild(timeline_event);
this.sortEvents(timeline);
tldialog.hideDialog();
}
// dispatch remove request of event to timeline
removeEvent(event) {
const eventToRemove = event.detail.id;
const timeline = document.querySelector( "webwriter-timeline" ) as WebWriterTimeline;
const tlslot = timeline.shadowRoot.querySelector('slot[name="event-slot"]');
const eventContainer = timeline.querySelector(
`event-container[id="${eventToRemove}"]`
);
if (eventContainer) {
timeline.removeChild(eventContainer);
}
}
// sort timeline events ascending
sortEvents(timeline) {
[...timeline.children]
.sort((a: EventContainer, b: EventContainer) => {
return a.getStartDate().isAfter(b.getStartDate()) ? 1 : -1;
})
.forEach((node) => timeline.appendChild(node));
}
}