Update CellEditor to use FragmentManager
</div>
<div id="cell-editor-container">
- <div class="box hflex">
- <div class="vflex flex-auto">
- <h1>Cell Editor</h1>
- <h2>Title</h2>
- <input name="title">
- <h2>Description</h2>
- <textarea name="description" class="flex-auto"></textarea>
- <div class="hflex">
- <button id="cell-editor-close">Close</button>
- </div>
- </div>
- <div class="events">
- <h2>Events</h2>
- <div id="event-list"></div>
- <button id="add-event">Add Event</button>
- </div>
- </div>
</div>
</body>
export type CellEditorCallback = (cell: Cell) => void;
export default class CellEditor {
- container: HTMLDivElement
+ container: FragmentManager
titleRef: HTMLInputElement
descriptionRef: HTMLTextAreaElement
events: FragmentManager[]
onClose: (cell: Cell) => void
constructor() {
- this.container = <HTMLDivElement>document.getElementById('cell-editor-container');
- this.container.addEventListener('keydown', function(e: KeyboardEvent) {
+ this.container = new FragmentManager(`
+ <div class="box hflex">
+ <div class="vflex flex-auto">
+ <h1>Cell Editor</h1>
+ <h2>Title</h2>
+ <input name="title">
+ <h2>Description</h2>
+ <textarea name="description" class="flex-auto"></textarea>
+ <div class="hflex">
+ <button id="cell-editor-close">Close</button>
+ </div>
+ </div>
+ <div class="events">
+ <h2>Events</h2>
+ <div id="event-list"></div>
+ <button id="add-event">Add Event</button>
+ </div>
+ </div>
+ `);
+ this.container.mountInto('cell-editor-container');
+
+ this.container.elem.addEventListener('keydown', function(e: KeyboardEvent) {
e.cancelBubble = true;
});
- this.titleRef = this.container.querySelector('[name=title]');
- this.descriptionRef = this.container.querySelector('[name=description]');
+ this.titleRef = <HTMLInputElement>this.container.q('[name=title]');
+ this.descriptionRef = <HTMLTextAreaElement>this.container.q('[name=description]');
- const addEventButton = document.getElementById('add-event');
+ const addEventButton = this.container.q('#add-event');
addEventButton.addEventListener('click', this.addEventHandler.bind(this));
this.events = []
- const closeButton = document.getElementById('cell-editor-close');
+ const closeButton = this.container.q('#cell-editor-close');
closeButton.addEventListener('click', this.closeHandler.bind(this));
}
for (let [k, e] of cell.events) {
this.addEvent(e);
}
- this.container.classList.add('visible');
+ this.container.elem.classList.add('visible');
this.titleRef.focus();
}
this.onClose(this.cell);
this.onClose = null;
}
- this.container.classList.remove('visible');
+ this.container.elem.classList.remove('visible');
}
}
[key: string]: string
}
+function getNode(node: string | HTMLElement) {
+ if (typeof node == 'string') {
+ return document.getElementById(node);
+ if (!node) {
+ throw new Error('Could not find destination node ' + node);
+ }
+ }
+
+ return node;
+}
+
export default class FragmentManager {
elem: HTMLElement
}
mount(destNode: string | HTMLElement) {
- if (typeof destNode == 'string') {
- destNode = document.getElementById(destNode);
- if (!destNode) {
- throw new Error('Could not find destination node ' + destNode);
- }
- }
+ getNode(destNode).appendChild(this.elem);
+ }
- destNode.appendChild(this.elem);
+ /* mountInto puts the children into the specified node. Because this
+ * adopts an existing node, be careful when unmounting. */
+ mountInto(destNode: string | HTMLElement) {
+ destNode = getNode(destNode);
+ while (this.elem.firstChild) {
+ destNode.appendChild(this.elem.firstChild);
+ }
+ this.elem = destNode;
}
unmount() {