# HG changeset patch # User cin # Date 2020-08-26 10:49:03 # Node ID 8f4d5e2c719a20ae6d65f1f4b5e2141ed765e975 # Parent 8419ebe3b63d69635441736e9d1924d906983bb2 Added DjxFragment added nodeType type guard functions in tsx/traits removed reference to dijit from WidgetContext to minimize interface requirements diff --git a/src/main/ts/dom-inject.ts b/src/main/ts/dom-inject.ts --- a/src/main/ts/dom-inject.ts +++ b/src/main/ts/dom-inject.ts @@ -68,6 +68,7 @@ class DomInject { trace.log("done {0}", url); } catch (e) { trace.error("failed {0}: {1}", url, e); + throw e; } } @@ -87,6 +88,7 @@ class DomInject { trace.log("done {0}", url); } catch (e) { trace.error("failed {0}: {1}", url, e); + throw e; } } }; diff --git a/src/main/ts/tsx/BuildContextBase.ts b/src/main/ts/tsx/BuildContextBase.ts --- a/src/main/ts/tsx/BuildContextBase.ts +++ b/src/main/ts/tsx/BuildContextBase.ts @@ -1,4 +1,4 @@ -import { isNull, mixin, argumentNotNull } from "@implab/core-amd/safe"; +import { isNull, mixin } from "@implab/core-amd/safe"; import { isPlainObject, isNode, isBuildContext, DojoNodePosition, BuildContext } from "./traits"; import dom = require("dojo/dom-construct"); @@ -37,13 +37,13 @@ export abstract class BuildContextBase i } else if (isNode(v)) { return v; } else if (isBuildContext(v)) { - return v.getDomElement(); + return v.getDomNode(); } else { throw new Error("Invalid parameter"); } } - abstract _getDomElement(): HTMLElement; + abstract _getDomNode(): Node; ensureCreated() { if (!this._created) { @@ -54,13 +54,18 @@ export abstract class BuildContextBase i } } + /** @deprecated use getDomNode() */ getDomElement() { + return this.getDomNode(); + } + + getDomNode() { this.ensureCreated(); - return this._getDomElement(); + return this._getDomNode(); } placeAt(refNode: string | Node, position?: DojoNodePosition) { - dom.place(this.getDomElement(), refNode, position); + dom.place(this.getDomNode(), refNode, position); } abstract _addChild(child: any): void; diff --git a/src/main/ts/tsx/DjxFragment.ts b/src/main/ts/tsx/DjxFragment.ts new file mode 100644 --- /dev/null +++ b/src/main/ts/tsx/DjxFragment.ts @@ -0,0 +1,23 @@ +import { _Widget } from "./WidgetContext"; +import { MapOf } from "@implab/core-amd/interfaces"; + +/** Special widget used to create a document fragment */ +export class DjxFragment implements _Widget { + + domNode: Node; + + containerNode?: Node; + + constructor() { + this.domNode = this.containerNode = document.createDocumentFragment(); + } + + get(attr: string) { + return undefined; + } + set(attr: string, value: any): void; + set(attrs: MapOf): void; + set() { + /* do nothing */ + } +} \ No newline at end of file diff --git a/src/main/ts/tsx/DjxWidgetBase.ts b/src/main/ts/tsx/DjxWidgetBase.ts --- a/src/main/ts/tsx/DjxWidgetBase.ts +++ b/src/main/ts/tsx/DjxWidgetBase.ts @@ -11,7 +11,7 @@ import { Handle } from "dojo/interfaces" export abstract class DjxWidgetBase extends djbase(_WidgetBase, _AttachMixin) { buildRendering() { - this.domNode = this.render().getDomElement(); + this.domNode = this.render().getDomNode(); super.buildRendering(); } diff --git a/src/main/ts/tsx/HtmlElementContext.ts b/src/main/ts/tsx/HtmlElementContext.ts --- a/src/main/ts/tsx/HtmlElementContext.ts +++ b/src/main/ts/tsx/HtmlElementContext.ts @@ -35,7 +35,7 @@ export class HtmlElementContext extends children.forEach(v => this._addChild(v)); } - _getDomElement() { + _getDomNode() { if (!this._element) throw new Error("The HTML element isn't created"); diff --git a/src/main/ts/tsx/WidgetContext.ts b/src/main/ts/tsx/WidgetContext.ts --- a/src/main/ts/tsx/WidgetContext.ts +++ b/src/main/ts/tsx/WidgetContext.ts @@ -1,16 +1,28 @@ import dom = require("dojo/dom-construct"); import { argumentNotNull } from "@implab/core-amd/safe"; -import _WidgetBase = require("dijit/_WidgetBase"); import { BuildContextBase } from "./BuildContextBase"; +import { MapOf } from "@implab/core-amd/interfaces"; -type _WidgetBaseConstructor = typeof _WidgetBase; +// tslint:disable-next-line: class-name +export interface _Widget { + domNode: Node; + + get(attr: string): any; + + set(attr: string, value: any): void; + set(attrs: MapOf): void; + + containerNode?: Node +} + +export type _WidgetCtor = new (attrs: any, srcNode?: string | Node) => _Widget; export class WidgetContext extends BuildContextBase { - widgetClass: _WidgetBaseConstructor; + widgetClass: _WidgetCtor; - _instance: _WidgetBase | undefined; + _instance: _Widget | undefined; - constructor(widgetClass: _WidgetBaseConstructor) { + constructor(widgetClass: _WidgetCtor) { super(); argumentNotNull(widgetClass, "widgetClass"); @@ -34,7 +46,7 @@ export class WidgetContext extends Build children.forEach(x => this._addChild(x)); } - _getDomElement() { + _getDomNode() { if (!this._instance) throw new Error("The instance of the widget isn't created"); return this._instance.domNode; diff --git a/src/main/ts/tsx/traits.ts b/src/main/ts/tsx/traits.ts --- a/src/main/ts/tsx/traits.ts +++ b/src/main/ts/tsx/traits.ts @@ -5,15 +5,43 @@ type _WidgetBaseConstructor = typeof _Wi export type DojoNodePosition = "first" | "after" | "before" | "last" | "replace" | "only" | number; export interface BuildContext { - getDomElement(): HTMLElement; + getDomNode(): Node; placeAt(refNode: string | Node, position?: DojoNodePosition): void; } -export function isNode(el: any): el is HTMLElement { +export function isNode(el: any): el is Node { return el && el.nodeName && el.nodeType; } +export function isElementNode(el: any): el is Element { + return isNode(el) && el.nodeType === 1; +} + +export function isTextNode(el: any): el is Text { + return isNode(el) && el.nodeType === 3; +} + +export function isProcessingInstructionNode(el: any): el is ProcessingInstruction { + return isNode(el) && el.nodeType === 7; +} + +export function isCommentNode(el: any): el is Comment { + return isNode(el) && el.nodeType === 8; +} + +export function isDocumentNode(el: any): el is Document { + return isNode(el) && el.nodeType === 9; +} + +export function isDocumentTypeNode(el: any): el is DocumentType { + return isNode(el) && el.nodeType === 10; +} + +export function isDocumentFragmentNode(el: any): el is DocumentFragment { + return isNode(el) && el.nodeType === 11; +} + export function isWidget(v: any): v is _WidgetBase { return v && "domNode" in v; }