| @@ -1,74 +1,74 | |||||
| 1 | plugins { | 
             | 
        1 | plugins { | |
| 2 | id "org.implab.gradle-typescript" version "1.3.3" | 
             | 
        2 | id "org.implab.gradle-typescript" version "1.3.3" | |
| 3 | id "ivy-publish" | 
             | 
        3 | id "ivy-publish" | |
| 4 | } | 
             | 
        4 | } | |
| 5 | 
             | 
        5 | |||
| 6 | typescript { | 
             | 
        6 | typescript { | |
| 7 | compilerOptions { | 
             | 
        7 | compilerOptions { | |
| 8 | lib = ["es5", "dom", "scripthost", "es2015.promise", "es2015.symbol", "es2015.iterable"] | 
             | 
        8 | lib = ["es5", "dom", "scripthost", "es2015.promise", "es2015.symbol", "es2015.iterable"] | |
| 9 | //listFiles = true | 
             | 
        9 | //listFiles = true | |
| 10 | declaration = true | 
             | 
        10 | declaration = true | |
| 11 | strict = true | 
             | 
        11 | strict = true | |
| 12 | types = [] | 
             | 
        12 | types = [] | |
| 13 | module = "amd" | 
             | 
        13 | module = "amd" | |
| 14 | it.target = "es5" | 
             | 
        14 | it.target = "es5" | |
| 15 | experimentalDecorators = true | 
             | 
        15 | experimentalDecorators = true | |
| 16 | jsx = "react" | 
             | 
        16 | jsx = "react" | |
| 17 | jsxFactory = "createElement" | 
             | 
        17 | jsxFactory = "createElement" | |
| 18 | moduleResolution = "node" | 
             | 
        18 | moduleResolution = "node" | |
| 19 | // dojo-typings are sick | 
             | 
        19 | // dojo-typings are sick | |
| 20 | skipLibCheck = true | 
             | 
        20 | skipLibCheck = true | |
| 21 | // traceResolution = true | 
             | 
        21 | // traceResolution = true | |
| 22 | // baseUrl = "./" | 
             | 
        22 | // baseUrl = "./" | |
| 23 | // paths = [ "*": [ "$projectDir/src/typings/*" ] ] | 
             | 
        23 | // paths = [ "*": [ "$projectDir/src/typings/*" ] ] | |
| 24 | // baseUrl = "$projectDir/src/typings" | 
             | 
        24 | // baseUrl = "$projectDir/src/typings" | |
| 25 | // typeRoots = ["$projectDir/src/typings"] | 
             | 
        25 | // typeRoots = ["$projectDir/src/typings"] | |
| 26 | } | 
             | 
        26 | } | |
| 27 | 
             | 
        27 | |||
| 28 | tscCmd = "$projectDir/node_modules/.bin/tsc" | 
             | 
        28 | tscCmd = "$projectDir/node_modules/.bin/tsc" | |
| 29 | tsLintCmd = "$projectDir/node_modules/.bin/tslint" | 
             | 
        29 | tsLintCmd = "$projectDir/node_modules/.bin/tslint" | |
| 30 | esLintCmd = "$projectDir/node_modules/.bin/eslint" | 
             | 
        30 | esLintCmd = "$projectDir/node_modules/.bin/eslint" | |
| 31 | } | 
             | 
        31 | } | |
| 32 | 
             | 
        32 | |||
| 33 | configureTsMain { | 
             | 
        33 | configureTsMain { | |
| 34 | compilerOptions { | 
             | 
        34 | compilerOptions { | |
| 35 | 
            
                    / | 
        
             | 
        35 | //baseUrl = "$projectDir/src" | |
| 36 | paths = [ | 
             | 
        36 | /*paths = [ | |
| 37 | "dojo/*" : [ "typings/dojo/*" ], | 
             | 
        37 | "dojo/*" : [ "typings/dojo/*" ], | |
| 38 | "dijit/*" : [ "typings/dijit/*" ] | 
             | 
        38 | "dijit/*" : [ "typings/dijit/*" ] | |
| 39 | ]*/ | 
             | 
        39 | ]*/ | |
| 40 | types = ["requirejs", "dojo-typings"] | 
             | 
        40 | types = ["requirejs", "dojo-typings", "$projectDir/src/main/typings"] | |
| 41 | } | 
             | 
        41 | } | |
| 42 | } | 
             | 
        42 | } | |
| 43 | 
             | 
        43 | |||
| 44 | configureTsTest { | 
             | 
        44 | configureTsTest { | |
| 45 | compilerOptions { | 
             | 
        45 | compilerOptions { | |
| 46 | typeRoots = [] | 
             | 
        46 | typeRoots = [] | |
| 47 | types = ["requirejs", sources.main.output.typingsDir.get().toString() ] | 
             | 
        47 | types = ["requirejs", sources.main.output.typingsDir.get().toString() ] | |
| 48 | } | 
             | 
        48 | } | |
| 49 | } | 
             | 
        49 | } | |
| 50 | 
             | 
        50 | |||
| 51 | npmPackMeta { | 
             | 
        51 | npmPackMeta { | |
| 52 | meta { | 
             | 
        52 | meta { | |
| 53 | name = "@$npmScope/$project.name" | 
             | 
        53 | name = "@$npmScope/$project.name" | |
| 54 | } | 
             | 
        54 | } | |
| 55 | } | 
             | 
        55 | } | |
| 56 | 
             | 
        56 | |||
| 57 | task npmPackTypings(type: Copy) { | 
             | 
        57 | task npmPackTypings(type: Copy) { | |
| 58 | dependsOn typings | 
             | 
        58 | dependsOn typings | |
| 59 | 
             | 
        59 | |||
| 60 | npmPackContents.dependsOn it | 
             | 
        60 | npmPackContents.dependsOn it | |
| 61 | 
             | 
        61 | |||
| 62 | from typescript.typingsDir | 
             | 
        62 | from typescript.typingsDir | |
| 63 | into npm.packageDir | 
             | 
        63 | into npm.packageDir | |
| 64 | } | 
             | 
        64 | } | |
| 65 | 
             | 
        65 | |||
| 66 | task printVersion { | 
             | 
        66 | task printVersion { | |
| 67 | doLast { | 
             | 
        67 | doLast { | |
| 68 | println "packageName: ${npmPackMeta.metadata.get().name}"; | 
             | 
        68 | println "packageName: ${npmPackMeta.metadata.get().name}"; | |
| 69 | println "version: $version"; | 
             | 
        69 | println "version: $version"; | |
| 70 | println "target: $typescript.compilerOptions.target"; | 
             | 
        70 | println "target: $typescript.compilerOptions.target"; | |
| 71 | println "module: $typescript.compilerOptions.module"; | 
             | 
        71 | println "module: $typescript.compilerOptions.module"; | |
| 72 | println "symbols: $symbols"; | 
             | 
        72 | println "symbols: $symbols"; | |
| 73 | } | 
             | 
        73 | } | |
| 74 | } No newline at end of file | 
             | 
        74 | } | |
| @@ -1,81 +1,89 | |||||
| 1 | import { isNull, mixin } from "@implab/core-amd/safe"; | 
             | 
        1 | import { isNull, mixin } from "@implab/core-amd/safe"; | |
| 2 | import { isPlainObject, isNode, isBuildContext, DojoNodePosition, BuildContext } from "./traits"; | 
             | 
        2 | import { isPlainObject, isNode, isBuildContext, DojoNodePosition, BuildContext, isInPage } from "./traits"; | |
| 3 | 
             | 
        3 | |||
| 4 | import dom = require("dojo/dom-construct"); | 
             | 
        4 | import dom = require("dojo/dom-construct"); | |
| 5 | import registry = require("dijit/registry"); | 
             | 
        5 | import registry = require("dijit/registry"); | |
| 6 | 
             | 
        6 | |||
| 
             | 
        7 | ||||
| 7 | export abstract class BuildContextBase<TNode extends Node> implements BuildContext<TNode> { | 
             | 
        8 | export abstract class BuildContextBase<TNode extends Node> implements BuildContext<TNode> { | |
| 8 | private _attrs = {}; | 
             | 
        9 | private _attrs = {}; | |
| 9 | 
             | 
        10 | |||
| 10 | private _children = new Array(); | 
             | 
        11 | private _children = new Array(); | |
| 11 | 
             | 
        12 | |||
| 12 | private _created: boolean = false; | 
             | 
        13 | private _created: boolean = false; | |
| 13 | 
             | 
        14 | |||
| 14 | visitNext(v: any) { | 
             | 
        15 | visitNext(v: any) { | |
| 15 | if (this._created) | 
             | 
        16 | if (this._created) | |
| 16 | throw new Error("The Element is already created"); | 
             | 
        17 | throw new Error("The Element is already created"); | |
| 17 | 
             | 
        18 | |||
| 18 | if (isNull(v) || typeof v === "boolean") | 
             | 
        19 | if (isNull(v) || typeof v === "boolean") | |
| 19 | // skip null, undefined, booleans ( this will work: {value && <span>{value}</span>} ) | 
             | 
        20 | // skip null, undefined, booleans ( this will work: {value && <span>{value}</span>} ) | |
| 20 | return; | 
             | 
        21 | return; | |
| 21 | 
             | 
        22 | |||
| 22 | if (isPlainObject(v)) { | 
             | 
        23 | if (isPlainObject(v)) { | |
| 23 | mixin(this._attrs, v); | 
             | 
        24 | mixin(this._attrs, v); | |
| 24 | } else if (v instanceof Array) { | 
             | 
        25 | } else if (v instanceof Array) { | |
| 25 | v.forEach(x => this.visitNext(x)); | 
             | 
        26 | v.forEach(x => this.visitNext(x)); | |
| 26 | } else { | 
             | 
        27 | } else { | |
| 27 | this._children.push(v); | 
             | 
        28 | this._children.push(v); | |
| 28 | } | 
             | 
        29 | } | |
| 29 | } | 
             | 
        30 | } | |
| 30 | 
             | 
        31 | |||
| 31 | getChildDom(v: any) { | 
             | 
        32 | getChildDom(v: any) { | |
| 32 | const tv = typeof v; | 
             | 
        33 | const tv = typeof v; | |
| 33 | if (tv === "string" || tv === "number" || v instanceof RegExp || v instanceof Date) { | 
             | 
        34 | if (tv === "string" || tv === "number" || v instanceof RegExp || v instanceof Date) { | |
| 34 | return document.createTextNode(v.toString()); | 
             | 
        35 | return document.createTextNode(v.toString()); | |
| 35 | } else if (isNode(v)) { | 
             | 
        36 | } else if (isNode(v)) { | |
| 36 | return v; | 
             | 
        37 | return v; | |
| 37 | } else if (isBuildContext(v)) { | 
             | 
        38 | } else if (isBuildContext(v)) { | |
| 38 | return v.getDomNode(); | 
             | 
        39 | return v.getDomNode(); | |
| 39 | } else { | 
             | 
        40 | } else { | |
| 40 | throw new Error("Invalid parameter"); | 
             | 
        41 | throw new Error("Invalid parameter"); | |
| 41 | } | 
             | 
        42 | } | |
| 42 | } | 
             | 
        43 | } | |
| 43 | 
             | 
        44 | |||
| 44 | ensureCreated() { | 
             | 
        45 | ensureCreated() { | |
| 45 | if (!this._created) { | 
             | 
        46 | if (!this._created) { | |
| 46 | this._create(this._attrs, this._children); | 
             | 
        47 | this._create(this._attrs, this._children); | |
| 47 | this._children = []; | 
             | 
        48 | this._children = []; | |
| 48 | this._attrs = {}; | 
             | 
        49 | this._attrs = {}; | |
| 49 | this._created = true; | 
             | 
        50 | this._created = true; | |
| 50 | } | 
             | 
        51 | } | |
| 51 | } | 
             | 
        52 | } | |
| 52 | 
             | 
        53 | |||
| 53 | /** @deprecated will be removed in 1.0.0, use getDomNode() */ | 
             | 
        54 | /** @deprecated will be removed in 1.0.0, use getDomNode() */ | |
| 54 | getDomElement() { | 
             | 
        55 | getDomElement() { | |
| 55 | return this.getDomNode(); | 
             | 
        56 | return this.getDomNode(); | |
| 56 | } | 
             | 
        57 | } | |
| 57 | 
             | 
        58 | |||
| 58 | /** Creates DOM node if not created. No additional actions are taken. */ | 
             | 
        59 | /** Creates DOM node if not created. No additional actions are taken. */ | |
| 59 | getDomNode() { | 
             | 
        60 | getDomNode() { | |
| 60 | this.ensureCreated(); | 
             | 
        61 | this.ensureCreated(); | |
| 61 | return this._getDomNode(); | 
             | 
        62 | return this._getDomNode(); | |
| 62 | } | 
             | 
        63 | } | |
| 63 | 
             | 
        64 | |||
| 64 | /** Creates DOM node if not created, places it to the specified position | 
             | 
        65 | /** Creates DOM node if not created, places it to the specified position | |
| 65 | * and calls startup() method for all widgets contained by this node. | 
             | 
        66 | * and calls startup() method for all widgets contained by this node. | |
| 66 | * | 
             | 
        67 | * | |
| 67 | * @param {string | Node} refNode The reference node where the created | 
             | 
        68 | * @param {string | Node} refNode The reference node where the created | |
| 68 | * DOM should be placed. | 
             | 
        69 | * DOM should be placed. | |
| 69 | * @param {DojoNodePosition} position Optional parameter, specifies the | 
             | 
        70 | * @param {DojoNodePosition} position Optional parameter, specifies the | |
| 70 | * position relative to refNode. Default is "last" (i.e. last child). | 
             | 
        71 | * position relative to refNode. Default is "last" (i.e. last child). | |
| 71 | */ | 
             | 
        72 | */ | |
| 72 | placeAt(refNode: string | Node, position?: DojoNodePosition) { | 
             | 
        73 | placeAt(refNode: string | Node, position?: DojoNodePosition) { | |
| 73 | const domNode = this.getDomNode(); | 
             | 
        74 | const domNode = this.getDomNode(); | |
| 74 | dom.place(domNode, refNode, position); | 
             | 
        75 | dom.place(domNode, refNode, position); | |
| 75 | registry.findWidgets(domNode).forEach(w => w.startup()); | 
             | 
        76 | const parentWidget = domNode.parentNode ? registry.getEnclosingWidget(domNode.parentNode) : null; | |
| 
             | 
        77 | ||||
| 
             | 
        78 | if ((parentWidget && parentWidget._started) || isInPage(domNode)) | |||
| 
             | 
        79 | this._startup(); | |||
| 
             | 
        80 | } | |||
| 
             | 
        81 | ||||
| 
             | 
        82 | _startup () { | |||
| 
             | 
        83 | registry.findWidgets(this._getDomNode()).forEach(w => w.startup()); | |||
| 76 | } | 
             | 
        84 | } | |
| 77 | 
             | 
        85 | |||
| 78 | abstract _create(attrs: object, children: any[]): void; | 
             | 
        86 | abstract _create(attrs: object, children: any[]): void; | |
| 79 | 
             | 
        87 | |||
| 80 | abstract _getDomNode(): TNode; | 
             | 
        88 | abstract _getDomNode(): TNode; | |
| 81 | } | 
             | 
        89 | } | |
| @@ -1,32 +1,33 | |||||
| 1 | import dom = require("dojo/dom-construct"); | 
             | 
        1 | import dom = require("dojo/dom-construct"); | |
| 2 | import attr = require("dojo/dom-attr"); | 
             | 
        2 | import attr = require("dojo/dom-attr"); | |
| 3 | import { argumentNotNull } from "@implab/core-amd/safe"; | 
             | 
        3 | import { argumentNotNull } from "@implab/core-amd/safe"; | |
| 4 | import { BuildContextBase } from "./BuildContextBase"; | 
             | 
        4 | import { BuildContextBase } from "./BuildContextBase"; | |
| 
             | 
        5 | import registry = require("dijit/registry"); | |||
| 5 | 
             | 
        6 | |||
| 6 | 
             | 
        7 | |||
| 7 | 
            
            export class FunctionComponentContext extends BuildContextBase< | 
        
             | 
        8 | export class FunctionComponentContext extends BuildContextBase<Node> { | |
| 8 | 
            
                private _component: (props: any) =>  | 
        
             | 
        9 | private _component: (props: any) => any; | |
| 9 | 
             | 
        10 | |||
| 10 | 
            
                private _ | 
        
             | 
        11 | private _node: Node | undefined; | |
| 11 | 
             | 
        12 | |||
| 12 | 
            
                constructor(component: (props: any) =>  | 
        
             | 
        13 | constructor(component: (props: any) => any) { | |
| 13 | super(); | 
             | 
        14 | super(); | |
| 14 | argumentNotNull(component, "component"); | 
             | 
        15 | argumentNotNull(component, "component"); | |
| 15 | 
             | 
        16 | |||
| 16 | this._component = component; | 
             | 
        17 | this._component = component; | |
| 17 | } | 
             | 
        18 | } | |
| 18 | 
             | 
        19 | |||
| 19 | _create(attrs: object, children: any[]) { | 
             | 
        20 | _create(attrs: object, children: any[]) { | |
| 20 | const _attrs: any = attrs || {}; | 
             | 
        21 | const _attrs: any = attrs || {}; | |
| 21 | _attrs.children = children.map(x => this.getChildDom(x)); | 
             | 
        22 | _attrs.children = children.map(x => this.getChildDom(x)); | |
| 22 | 
             | 
        23 | |||
| 23 | 
            
                    this._ | 
        
             | 
        24 | this._node = this.getChildDom(this._component.call(null, _attrs)); | |
| 24 | } | 
             | 
        25 | } | |
| 25 | 
             | 
        26 | |||
| 26 | _getDomNode() { | 
             | 
        27 | _getDomNode() { | |
| 27 | 
            
                    if (!this._ | 
        
             | 
        28 | if (!this._node) | |
| 28 | throw new Error("The instance of the widget isn't created"); | 
             | 
        29 | throw new Error("The instance of the widget isn't created"); | |
| 29 | 
            
                    return this._ | 
        
             | 
        30 | return this._node; | |
| 30 | } | 
             | 
        31 | } | |
| 31 | 
             | 
        32 | |||
| 32 | } | 
             | 
        33 | } | |
| @@ -1,38 +1,39 | |||||
| 1 | import dom = require("dojo/dom-construct"); | 
             | 
        1 | import dom = require("dojo/dom-construct"); | |
| 2 | import attr = require("dojo/dom-attr"); | 
             | 
        2 | import attr = require("dojo/dom-attr"); | |
| 3 | import { argumentNotEmptyString } from "@implab/core-amd/safe"; | 
             | 
        3 | import { argumentNotEmptyString } from "@implab/core-amd/safe"; | |
| 4 | import { BuildContextBase } from "./BuildContextBase"; | 
             | 
        4 | import { BuildContextBase } from "./BuildContextBase"; | |
| 
             | 
        5 | import registry = require("dijit/registry"); | |||
| 5 | 
             | 
        6 | |||
| 6 | export class HtmlElementContext extends BuildContextBase<HTMLElement> { | 
             | 
        7 | export class HtmlElementContext extends BuildContextBase<HTMLElement> { | |
| 7 | elementType: string; | 
             | 
        8 | elementType: string; | |
| 8 | 
             | 
        9 | |||
| 9 | _element: HTMLElement | undefined; | 
             | 
        10 | _element: HTMLElement | undefined; | |
| 10 | 
             | 
        11 | |||
| 11 | constructor(elementType: string) { | 
             | 
        12 | constructor(elementType: string) { | |
| 12 | argumentNotEmptyString(elementType, "elementType"); | 
             | 
        13 | argumentNotEmptyString(elementType, "elementType"); | |
| 13 | super(); | 
             | 
        14 | super(); | |
| 14 | 
             | 
        15 | |||
| 15 | this.elementType = elementType; | 
             | 
        16 | this.elementType = elementType; | |
| 16 | } | 
             | 
        17 | } | |
| 17 | 
             | 
        18 | |||
| 18 | _addChild(child: any): void { | 
             | 
        19 | _addChild(child: any): void { | |
| 19 | if (!this._element) | 
             | 
        20 | if (!this._element) | |
| 20 | throw new Error("The HTML element isn't created"); | 
             | 
        21 | throw new Error("The HTML element isn't created"); | |
| 21 | dom.place(this.getChildDom(child), this._element); | 
             | 
        22 | dom.place(this.getChildDom(child), this._element); | |
| 22 | } | 
             | 
        23 | } | |
| 23 | 
             | 
        24 | |||
| 24 | _create(attrs: object, children: any[]) { | 
             | 
        25 | _create(attrs: object, children: any[]) { | |
| 25 | this._element = dom.create(this.elementType, attrs); | 
             | 
        26 | this._element = dom.create(this.elementType, attrs); | |
| 26 | 
             | 
        27 | |||
| 27 | if (children) | 
             | 
        28 | if (children) | |
| 28 | children.forEach(v => this._addChild(v)); | 
             | 
        29 | children.forEach(v => this._addChild(v)); | |
| 29 | } | 
             | 
        30 | } | |
| 30 | 
             | 
        31 | |||
| 31 | _getDomNode() { | 
             | 
        32 | _getDomNode() { | |
| 32 | if (!this._element) | 
             | 
        33 | if (!this._element) | |
| 33 | throw new Error("The HTML element isn't created"); | 
             | 
        34 | throw new Error("The HTML element isn't created"); | |
| 34 | 
             | 
        35 | |||
| 35 | return this._element; | 
             | 
        36 | return this._element; | |
| 36 | } | 
             | 
        37 | } | |
| 37 | 
             | 
        38 | |||
| 38 | } | 
             | 
        39 | } | |
| @@ -1,95 +1,128 | |||||
| 1 | import dom = require("dojo/dom-construct"); | 
             | 
        1 | import dom = require("dojo/dom-construct"); | |
| 2 | import { argumentNotNull } from "@implab/core-amd/safe"; | 
             | 
        2 | import { argumentNotNull } from "@implab/core-amd/safe"; | |
| 3 | import { BuildContextBase } from "./BuildContextBase"; | 
             | 
        3 | import { BuildContextBase } from "./BuildContextBase"; | |
| 4 | import { DojoNodePosition, isWidget } from "./traits"; | 
             | 
        4 | import { DojoNodePosition, isInPage, isWidget } from "./traits"; | |
| 
             | 
        5 | import registry = require("dijit/registry"); | |||
| 
             | 
        6 | import ContentPane = require("dijit/layout/ContentPane"); | |||
| 5 | 
             | 
        7 | |||
| 6 | // tslint:disable-next-line: class-name | 
             | 
        8 | // tslint:disable-next-line: class-name | |
| 7 | export interface _Widget { | 
             | 
        9 | export interface _Widget { | |
| 8 | domNode: Node; | 
             | 
        10 | domNode: Node; | |
| 9 | 
             | 
        11 | |||
| 10 | containerNode?: Node; | 
             | 
        12 | containerNode?: Node; | |
| 11 | 
             | 
        13 | |||
| 12 | placeAt?(refNode: string | Node, position?: DojoNodePosition): void; | 
             | 
        14 | placeAt?(refNode: string | Node, position?: DojoNodePosition): void; | |
| 13 | startup?(): void; | 
             | 
        15 | startup?(): void; | |
| 14 | 
             | 
        16 | |||
| 15 | addChild?(widget: any, index?: number): void; | 
             | 
        17 | addChild?(widget: any, index?: number): void; | |
| 16 | } | 
             | 
        18 | } | |
| 17 | 
             | 
        19 | |||
| 18 | export type _WidgetCtor = new (attrs: any, srcNode?: string | Node) => _Widget; | 
             | 
        20 | export type _WidgetCtor = new (attrs: any, srcNode?: string | Node) => _Widget; | |
| 19 | 
             | 
        21 | |||
| 20 | export class WidgetContext extends BuildContextBase<Node> { | 
             | 
        22 | export class WidgetContext extends BuildContextBase<Node> { | |
| 21 | readonly widgetClass: _WidgetCtor; | 
             | 
        23 | readonly widgetClass: _WidgetCtor; | |
| 22 | 
             | 
        24 | |||
| 23 | _instance: _Widget | undefined; | 
             | 
        25 | _instance: _Widget | undefined; | |
| 24 | 
             | 
        26 | |||
| 25 | constructor(widgetClass: _WidgetCtor) { | 
             | 
        27 | constructor(widgetClass: _WidgetCtor) { | |
| 26 | super(); | 
             | 
        28 | super(); | |
| 27 | argumentNotNull(widgetClass, "widgetClass"); | 
             | 
        29 | argumentNotNull(widgetClass, "widgetClass"); | |
| 28 | 
             | 
        30 | |||
| 29 | this.widgetClass = widgetClass; | 
             | 
        31 | this.widgetClass = widgetClass; | |
| 30 | } | 
             | 
        32 | } | |
| 31 | 
             | 
        33 | |||
| 32 | _addChild(child: any): void { | 
             | 
        34 | _addChild(child: any): void { | |
| 33 | const instance = this._getInstance(); | 
             | 
        35 | const instance = this._getInstance(); | |
| 34 | 
             | 
        36 | |||
| 35 | if (instance.addChild) { | 
             | 
        37 | if (instance.addChild) { | |
| 36 | if (child instanceof WidgetContext) | 
             | 
        38 | if (child instanceof WidgetContext) { | |
| 37 | // layout containers add custom logic to addChild methods | 
             | 
        39 | // layout containers add custom logic to addChild methods | |
| 38 | 
            
             | 
        
             | 
        40 | instance.addChild(child.getWidgetInstance()); | |
| 39 | else if (isWidget(child)) | 
             | 
        41 | } else if (isWidget(child)) { | |
| 40 | instance.addChild(child); | 
             | 
        42 | instance.addChild(child); | |
| 41 | else | 
             | 
        43 | } else { | |
| 42 | instance.addChild(this.getChildDom(child)); | 
             | 
        44 | if (!instance.containerNode) | |
| 
             | 
        45 | throw new Error("The widget doesn't have neither addChild nor containerNode"); | |||
| 
             | 
        46 | ||||
| 
             | 
        47 | // the current widget isn't started, it's children shouldn't start too | |||
| 
             | 
        48 | dom.place(this.getChildDom(child), instance.containerNode); | |||
| 
             | 
        49 | } | |||
| 43 | } else { | 
             | 
        50 | } else { | |
| 44 | if (!instance.containerNode) | 
             | 
        51 | if (!instance.containerNode) | |
| 45 | throw new Error("The widget doesn't have neither addChild nor containerNode"); | 
             | 
        52 | throw new Error("The widget doesn't have neither addChild nor containerNode"); | |
| 46 | 
             | 
        53 | |||
| 47 | // the current widget isn't started, it's children shouldn't start too | 
             | 
        54 | // the current widget isn't started, it's children shouldn't start too | |
| 48 | dom.place(this.getChildDom(child), instance.containerNode); | 
             | 
        55 | dom.place(this.getChildDom(child), instance.containerNode); | |
| 49 | } | 
             | 
        56 | } | |
| 50 | } | 
             | 
        57 | } | |
| 51 | 
             | 
        58 | |||
| 52 | 
            
                _create(attrs:  | 
        
             | 
        59 | _create(attrs: any, children: any[]) { | |
| 53 | this._instance = new this.widgetClass(attrs); | 
             | 
        60 | if (this.widgetClass.prototype instanceof ContentPane) { | |
| 54 | if (children) | 
             | 
        61 | // a special case for the ContentPane this is for | |
| 
             | 
        62 | // the compatibility with this heavy widget, all | |||
| 
             | 
        63 | // regular containers could be easily manipulated | |||
| 
             | 
        64 | // through `containerNode` property or `addChild` method. | |||
| 
             | 
        65 | ||||
| 
             | 
        66 | // render children to the DocumentFragment | |||
| 
             | 
        67 | const content = document.createDocumentFragment(); | |||
| 
             | 
        68 | children.forEach(child => content.appendChild(this.getChildDom(child))); | |||
| 
             | 
        69 | ||||
| 
             | 
        70 | // set the content property to the parameters of the widget | |||
| 
             | 
        71 | const _attrs = { ...attrs, content }; | |||
| 
             | 
        72 | this._instance = new this.widgetClass(_attrs); | |||
| 
             | 
        73 | } else { | |||
| 
             | 
        74 | this._instance = new this.widgetClass(attrs); | |||
| 55 | children.forEach(x => this._addChild(x)); | 
             | 
        75 | children.forEach(x => this._addChild(x)); | |
| 
             | 
        76 | } | |||
| 
             | 
        77 | ||||
| 56 | } | 
             | 
        78 | } | |
| 57 | 
             | 
        79 | |||
| 58 | private _getInstance() { | 
             | 
        80 | private _getInstance() { | |
| 59 | if (!this._instance) | 
             | 
        81 | if (!this._instance) | |
| 60 | throw new Error("The instance of the widget isn't created"); | 
             | 
        82 | throw new Error("The instance of the widget isn't created"); | |
| 61 | return this._instance; | 
             | 
        83 | return this._instance; | |
| 62 | } | 
             | 
        84 | } | |
| 63 | 
             | 
        85 | |||
| 64 | _getDomNode() { | 
             | 
        86 | _getDomNode() { | |
| 65 | if (!this._instance) | 
             | 
        87 | if (!this._instance) | |
| 66 | throw new Error("The instance of the widget isn't created"); | 
             | 
        88 | throw new Error("The instance of the widget isn't created"); | |
| 67 | return this._instance.domNode; | 
             | 
        89 | return this._instance.domNode; | |
| 68 | } | 
             | 
        90 | } | |
| 69 | 
             | 
        91 | |||
| 70 | /** Overrides default placeAt implementation. Calls placeAt of the | 
             | 
        92 | /** Overrides default placeAt implementation. Calls placeAt of the | |
| 71 | * widget and then starts it. | 
             | 
        93 | * widget and then starts it. | |
| 72 | * | 
             | 
        94 | * | |
| 73 | * @param refNode A node or id of the node where the widget should be placed. | 
             | 
        95 | * @param refNode A node or id of the node where the widget should be placed. | |
| 74 | * @param position A position relative to refNode. | 
             | 
        96 | * @param position A position relative to refNode. | |
| 75 | */ | 
             | 
        97 | */ | |
| 76 | placeAt(refNode: string | Node, position?: DojoNodePosition) { | 
             | 
        98 | placeAt(refNode: string | Node, position?: DojoNodePosition) { | |
| 77 | this.ensureCreated(); | 
             | 
        99 | this.ensureCreated(); | |
| 78 | const instance = this._getInstance(); | 
             | 
        100 | const instance = this._getInstance(); | |
| 79 | if (typeof instance.placeAt === "function") { | 
             | 
        101 | if (typeof instance.placeAt === "function") { | |
| 80 | instance.placeAt(refNode, position); | 
             | 
        102 | instance.placeAt(refNode, position); | |
| 81 | 
             | 
        103 | |||
| 82 | // do we need to force widget startup? | 
             | 
        104 | // fix the dojo startup behavior when the widget is placed | |
| 83 | if (typeof instance.startup === "function") | 
             | 
        105 | // directly to the document and doesn't have any enclosing widgets | |
| 84 | instance.startup(); | 
             | 
        106 | const parentWidget = instance.domNode.parentNode ? | |
| 
             | 
        107 | registry.getEnclosingWidget(instance.domNode.parentNode) : null | |||
| 
             | 
        108 | if (!parentWidget && isInPage(instance.domNode)) | |||
| 
             | 
        109 | this._startup(); | |||
| 85 | } else { | 
             | 
        110 | } else { | |
| 
             | 
        111 | // the widget doesn't have a placeAt method, strange but whatever | |||
| 86 | super.placeAt(refNode, position); | 
             | 
        112 | super.placeAt(refNode, position); | |
| 87 | } | 
             | 
        113 | } | |
| 88 | } | 
             | 
        114 | } | |
| 89 | 
             | 
        115 | |||
| 
             | 
        116 | _startup() { | |||
| 
             | 
        117 | const instance = this._getInstance(); | |||
| 
             | 
        118 | ||||
| 
             | 
        119 | if (typeof instance.startup === "function") | |||
| 
             | 
        120 | instance.startup(); | |||
| 
             | 
        121 | } | |||
| 
             | 
        122 | ||||
| 90 | getWidgetInstance() { | 
             | 
        123 | getWidgetInstance() { | |
| 91 | this.ensureCreated(); | 
             | 
        124 | this.ensureCreated(); | |
| 92 | return this._getInstance(); | 
             | 
        125 | return this._getInstance(); | |
| 93 | } | 
             | 
        126 | } | |
| 94 | 
             | 
        127 | |||
| 95 | } | 
             | 
        128 | } | |
| @@ -1,66 +1,73 | |||||
| 1 | import _WidgetBase = require("dijit/_WidgetBase"); | 
             | 
        1 | import _WidgetBase = require("dijit/_WidgetBase"); | |
| 2 | 
             | 
        2 | |||
| 3 | type _WidgetBaseConstructor = typeof _WidgetBase; | 
             | 
        3 | type _WidgetBaseConstructor = typeof _WidgetBase; | |
| 4 | 
             | 
        4 | |||
| 5 | export type DojoNodePosition = "first" | "after" | "before" | "last" | "replace" | "only" | number; | 
             | 
        5 | export type DojoNodePosition = "first" | "after" | "before" | "last" | "replace" | "only" | number; | |
| 6 | 
             | 
        6 | |||
| 7 | export interface BuildContext<TNode extends Node = Node> { | 
             | 
        7 | export interface BuildContext<TNode extends Node = Node> { | |
| 8 | getDomNode(): TNode; | 
             | 
        8 | getDomNode(): TNode; | |
| 9 | 
             | 
        9 | |||
| 10 | placeAt(refNode: string | Node, position?: DojoNodePosition): void; | 
             | 
        10 | placeAt(refNode: string | Node, position?: DojoNodePosition): void; | |
| 11 | } | 
             | 
        11 | } | |
| 12 | 
             | 
        12 | |||
| 13 | export function isNode(el: any): el is Node { | 
             | 
        13 | export function isNode(el: any): el is Node { | |
| 14 | return el && el.nodeName && el.nodeType; | 
             | 
        14 | return el && el.nodeName && el.nodeType; | |
| 15 | } | 
             | 
        15 | } | |
| 16 | 
             | 
        16 | |||
| 17 | export function isElementNode(el: any): el is Element { | 
             | 
        17 | export function isElementNode(el: any): el is Element { | |
| 18 | return isNode(el) && el.nodeType === 1; | 
             | 
        18 | return isNode(el) && el.nodeType === 1; | |
| 19 | } | 
             | 
        19 | } | |
| 20 | 
             | 
        20 | |||
| 21 | export function isTextNode(el: any): el is Text { | 
             | 
        21 | export function isTextNode(el: any): el is Text { | |
| 22 | return isNode(el) && el.nodeType === 3; | 
             | 
        22 | return isNode(el) && el.nodeType === 3; | |
| 23 | } | 
             | 
        23 | } | |
| 24 | 
             | 
        24 | |||
| 25 | export function isProcessingInstructionNode(el: any): el is ProcessingInstruction { | 
             | 
        25 | export function isProcessingInstructionNode(el: any): el is ProcessingInstruction { | |
| 26 | return isNode(el) && el.nodeType === 7; | 
             | 
        26 | return isNode(el) && el.nodeType === 7; | |
| 27 | } | 
             | 
        27 | } | |
| 28 | 
             | 
        28 | |||
| 29 | export function isCommentNode(el: any): el is Comment { | 
             | 
        29 | export function isCommentNode(el: any): el is Comment { | |
| 30 | return isNode(el) && el.nodeType === 8; | 
             | 
        30 | return isNode(el) && el.nodeType === 8; | |
| 31 | } | 
             | 
        31 | } | |
| 32 | 
             | 
        32 | |||
| 33 | export function isDocumentNode(el: any): el is Document { | 
             | 
        33 | export function isDocumentNode(el: any): el is Document { | |
| 34 | return isNode(el) && el.nodeType === 9; | 
             | 
        34 | return isNode(el) && el.nodeType === 9; | |
| 35 | } | 
             | 
        35 | } | |
| 36 | 
             | 
        36 | |||
| 37 | export function isDocumentTypeNode(el: any): el is DocumentType { | 
             | 
        37 | export function isDocumentTypeNode(el: any): el is DocumentType { | |
| 38 | return isNode(el) && el.nodeType === 10; | 
             | 
        38 | return isNode(el) && el.nodeType === 10; | |
| 39 | } | 
             | 
        39 | } | |
| 40 | 
             | 
        40 | |||
| 41 | export function isDocumentFragmentNode(el: any): el is DocumentFragment { | 
             | 
        41 | export function isDocumentFragmentNode(el: any): el is DocumentFragment { | |
| 42 | return isNode(el) && el.nodeType === 11; | 
             | 
        42 | return isNode(el) && el.nodeType === 11; | |
| 43 | } | 
             | 
        43 | } | |
| 44 | 
             | 
        44 | |||
| 45 | export function isWidget(v: any): v is _WidgetBase { | 
             | 
        45 | export function isWidget(v: any): v is _WidgetBase { | |
| 46 | return v && "domNode" in v; | 
             | 
        46 | return v && "domNode" in v; | |
| 47 | } | 
             | 
        47 | } | |
| 48 | 
             | 
        48 | |||
| 49 | export function isBuildContext(v: any): v is BuildContext { | 
             | 
        49 | export function isBuildContext(v: any): v is BuildContext { | |
| 50 | return typeof v === "object" && typeof v.getDomElement === "function"; | 
             | 
        50 | return typeof v === "object" && typeof v.getDomElement === "function"; | |
| 51 | } | 
             | 
        51 | } | |
| 52 | 
             | 
        52 | |||
| 53 | export function isPlainObject(v: object) { | 
             | 
        53 | export function isPlainObject(v: object) { | |
| 54 | if (typeof v !== "object") | 
             | 
        54 | if (typeof v !== "object") | |
| 55 | return false; | 
             | 
        55 | return false; | |
| 56 | 
             | 
        56 | |||
| 57 | const vp = Object.getPrototypeOf(v); | 
             | 
        57 | const vp = Object.getPrototypeOf(v); | |
| 58 | return !vp || vp === Object.prototype; | 
             | 
        58 | return !vp || vp === Object.prototype; | |
| 59 | } | 
             | 
        59 | } | |
| 60 | 
             | 
        60 | |||
| 61 | export function isWidgetConstructor(v: any): v is _WidgetBaseConstructor { | 
             | 
        61 | export function isWidgetConstructor(v: any): v is _WidgetBaseConstructor { | |
| 62 | return typeof v === "function" && v.prototype && ( | 
             | 
        62 | return typeof v === "function" && v.prototype && ( | |
| 63 | "domNode" in v.prototype || | 
             | 
        63 | "domNode" in v.prototype || | |
| 64 | "buildRendering" in v.prototype | 
             | 
        64 | "buildRendering" in v.prototype | |
| 65 | ); | 
             | 
        65 | ); | |
| 66 | } | 
             | 
        66 | } | |
| 
             | 
        67 | ||||
| 
             | 
        68 | /** Tests whether the specified node is placed in visible dom. | |||
| 
             | 
        69 | * @param {Node} node The node to test | |||
| 
             | 
        70 | */ | |||
| 
             | 
        71 | export function isInPage(node: Node) { | |||
| 
             | 
        72 | return (node === document.body) ? false : document.body.contains(node); | |||
| 
             | 
        73 | } | |||
        
        General Comments 0
    
    
  
  
                      You need to be logged in to leave comments.
                      Login now
                    
                