| @@ -1,146 +1,148 | |||
| 
             | 
        1 | 1 | import { IDestroyable } from "@implab/core-amd/interfaces"; | 
| 
             | 
        2 | 2 | import { isDestroyable } from "@implab/core-amd/safe"; | 
| 
             | 
        3 | 3 | import _WidgetBase = require("dijit/_WidgetBase"); | 
| 
             | 
        4 | 4 | import registry = require("dijit/registry"); | 
| 
             | 
        5 | 5 | import dom = require("dojo/dom-construct"); | 
| 
             | 
        6 | 6 | |
| 
             | 
        7 | 7 | type _WidgetBaseConstructor = typeof _WidgetBase; | 
| 
             | 
        8 | 8 | |
| 
             | 
        9 | 9 | export type DojoNodePosition = "first" | "after" | "before" | "last" | "replace" | "only" | number; | 
| 
             | 
        10 | 10 | |
| 
             | 
        11 | 11 | export interface Rendition<TNode extends Node = Node> { | 
| 
             | 
        12 | 12 | getDomNode(): TNode; | 
| 
             | 
        13 | 13 | |
| 
             | 
        14 | 14 | placeAt(refNode: string | Node, position?: DojoNodePosition): void; | 
| 
             | 
        15 | 15 | } | 
| 
             | 
        16 | 16 | |
| 
             | 
        17 | 17 | /** | 
| 
             | 
        18 | 18 | * @deprecated use Rendition | 
| 
             | 
        19 | 19 | */ | 
| 
             | 
        20 | 20 | export type BuildContext<TNode extends Node = Node> = Rendition<TNode>; | 
| 
             | 
        21 | 21 | |
| 
             | 
        22 | 22 | export interface IRecursivelyDestroyable { | 
| 
             | 
        23 | 23 | destroyRecursive(): void; | 
| 
             | 
        24 | 24 | } | 
| 
             | 
        25 | 25 | |
| 
             | 
        26 | 26 | export function isNode(el: any): el is Node { | 
| 
             | 
        27 | 27 | return el && el.nodeName && el.nodeType; | 
| 
             | 
        28 | 28 | } | 
| 
             | 
        29 | 29 | |
| 
             | 
        30 | 30 | export function isElementNode(el: any): el is Element { | 
| 
             | 
        31 | 31 | return isNode(el) && el.nodeType === 1; | 
| 
             | 
        32 | 32 | } | 
| 
             | 
        33 | 33 | |
| 
             | 
        34 | 34 | export function isTextNode(el: any): el is Text { | 
| 
             | 
        35 | 35 | return isNode(el) && el.nodeType === 3; | 
| 
             | 
        36 | 36 | } | 
| 
             | 
        37 | 37 | |
| 
             | 
        38 | 38 | export function isProcessingInstructionNode(el: any): el is ProcessingInstruction { | 
| 
             | 
        39 | 39 | return isNode(el) && el.nodeType === 7; | 
| 
             | 
        40 | 40 | } | 
| 
             | 
        41 | 41 | |
| 
             | 
        42 | 42 | export function isCommentNode(el: any): el is Comment { | 
| 
             | 
        43 | 43 | return isNode(el) && el.nodeType === 8; | 
| 
             | 
        44 | 44 | } | 
| 
             | 
        45 | 45 | |
| 
             | 
        46 | 46 | export function isDocumentNode(el: any): el is Document { | 
| 
             | 
        47 | 47 | return isNode(el) && el.nodeType === 9; | 
| 
             | 
        48 | 48 | } | 
| 
             | 
        49 | 49 | |
| 
             | 
        50 | 50 | export function isDocumentTypeNode(el: any): el is DocumentType { | 
| 
             | 
        51 | 51 | return isNode(el) && el.nodeType === 10; | 
| 
             | 
        52 | 52 | } | 
| 
             | 
        53 | 53 | |
| 
             | 
        54 | 54 | export function isDocumentFragmentNode(el: any): el is DocumentFragment { | 
| 
             | 
        55 | 55 | return isNode(el) && el.nodeType === 11; | 
| 
             | 
        56 | 56 | } | 
| 
             | 
        57 | 57 | |
| 
             | 
        58 | 58 | export function isWidget(v: any): v is _WidgetBase { | 
| 
             | 
        59 | 59 | return v && "domNode" in v; | 
| 
             | 
        60 | 60 | } | 
| 
             | 
        61 | 61 | |
| 
             | 
        62 | 62 | export function isRendition(v: any): v is Rendition { | 
| 
             | 
        63 | 63 | return typeof v === "object" && typeof v.getDomElement === "function"; | 
| 
             | 
        64 | 64 | } | 
| 
             | 
        65 | 65 | |
| 
             | 
        66 | 66 | /** | 
| 
             | 
        67 | 67 | * @deprecated use isRendition | 
| 
             | 
        68 | 68 | */ | 
| 
             | 
        69 | 69 | export const isBuildContext = isRendition; | 
| 
             | 
        70 | 70 | |
| 
             | 
        71 | 71 | export function isPlainObject(v: object) { | 
| 
             | 
        72 | 72 | if (typeof v !== "object") | 
| 
             | 
        73 | 73 | return false; | 
| 
             | 
        74 | 74 | |
| 
             | 
        75 | 75 | const vp = Object.getPrototypeOf(v); | 
| 
             | 
        76 | 76 | return !vp || vp === Object.prototype; | 
| 
             | 
        77 | 77 | } | 
| 
             | 
        78 | 78 | |
| 
             | 
        79 | 79 | export function isWidgetConstructor(v: any): v is _WidgetBaseConstructor { | 
| 
             | 
        80 | 80 | return typeof v === "function" && v.prototype && ( | 
| 
             | 
        81 | 81 | "domNode" in v.prototype || | 
| 
             | 
        82 | 82 | "buildRendering" in v.prototype | 
| 
             | 
        83 | 83 | ); | 
| 
             | 
        84 | 84 | } | 
| 
             | 
        85 | 85 | |
| 
             | 
        86 | 86 | /** Tests whether the specified node is placed in visible dom. | 
| 
             | 
        87 | 87 | * @param {Node} node The node to test | 
| 
             | 
        88 | 88 | */ | 
| 
             | 
        89 | 89 | export function isInPage(node: Node) { | 
| 
             | 
        90 | 90 | return (node === document.body) ? false : document.body.contains(node); | 
| 
             | 
        91 | 91 | } | 
| 
             | 
        92 | 92 | |
| 
             | 
        93 | 93 | export function isRecursivelyDestroyable(target: any): target is IRecursivelyDestroyable { | 
| 
             | 
        94 | 94 | return target && typeof target.destroyRecursive === "function"; | 
| 
             | 
        95 | 95 | } | 
| 
             | 
        96 | 96 | |
| 
             | 
        97 | 97 | |
| 
             | 
        98 | 98 | /** Destroys DOM Node with all contained widgets. | 
| 
             | 
        99 | 99 | * If the specified node is the root node of a widget, then the | 
| 
             | 
        100 | 100 | * widget will be destroyed. | 
| 
             | 
        101 | 101 | * | 
| 
             | 
        102 | 102 | * @param target DOM Node or widget to destroy | 
| 
             | 
        103 | 103 | */ | 
| 
             | 
        104 | 104 | export function destroy(target: Node | IDestroyable | IRecursivelyDestroyable) { | 
| 
             | 
        105 | 105 | if (isRecursivelyDestroyable(target)) { | 
| 
             | 
        106 | 106 | target.destroyRecursive(); | 
| 
             | 
        107 | 107 | } else if (isDestroyable(target)) { | 
| 
             | 
        108 | 108 | target.destroy(); | 
| 
             | 
        109 | 109 | } else if (isNode(target)) { | 
| 
             | 
        110 | 110 | const self = registry.byNode(target); | 
| 
             | 
        111 | 111 | if (self) { | 
| 
             | 
        112 | 112 | self.destroyRecursive(); | 
| 
             | 
        113 | 113 | } else { | 
| 
             | 
        114 | 114 | registry.findWidgets(target).forEach(destroy); | 
| 
             | 
        115 | 115 | dom.destroy(target); | 
| 
             | 
        116 | 116 | } | 
| 
             | 
        117 | 117 | } | 
| 
             | 
        118 | 118 | } | 
| 
             | 
        119 | 119 | |
| 
             | 
        120 | 120 | /** Empties a content of the specified node and destroys all contained widgets. | 
| 
             | 
        121 | 121 | * | 
| 
             | 
        122 | 122 | * @param target DOM node to . | 
| 
             | 
        123 | 123 | */ | 
| 
             | 
        124 | 124 | export function emptyNode(target: Node) { | 
| 
             | 
        125 | 125 | registry.findWidgets(target).forEach(destroy); | 
| 
             | 
        126 | 126 | dom.empty(target); | 
| 
             | 
        127 | 127 | } | 
| 
             | 
        128 | 128 | |
| 
             | 
        129 | 129 | /** This function starts all widgets inside the DOM node if the target is a node | 
| 
             | 
        130 | 130 | * or starts widget itself if the target is the widget. If the specified node | 
| 
             | 
        131 | 131 | * associated with the widget that widget will be started. | 
| 
             | 
        132 | 132 | * | 
| 
             | 
        133 | 133 | * @param target DOM node to find and start widgets or the widget itself. | 
| 
             | 
        134 | 134 | */ | 
| 
             | 
        135 | 135 | export function startupWidgets(target: Node | _WidgetBase, skipNode?: Node) { | 
| 
             | 
        136 | 136 | if (isNode(target)) { | 
| 
             | 
        137 | const w = registry.byNode(target); | |
| 
             | 
        137 | const w = isElementNode(target) ? registry.byNode(target) : undefined; | |
| 
             | 
        138 | 138 | if (w) { | 
| 
             | 
        139 | 
            
                         w.startup | 
    |
| 
             | 
        139 | if (w.startup) | |
| 
             | 
        140 | w.startup(); | |
| 
             | 
        140 | 141 | } else { | 
| 
             | 
        141 | 
            
                         registry.findWidgets(target, skipNode).forEach( | 
    |
| 
             | 
        142 | registry.findWidgets(target, skipNode).forEach(x => x.startup()); | |
| 
             | 
        142 | 143 | } | 
| 
             | 
        143 | 144 | } else { | 
| 
             | 
        144 | 
            
                     target.startup | 
    |
| 
             | 
        145 | if(target.startup) | |
| 
             | 
        146 | target.startup(); | |
| 
             | 
        145 | 147 | } | 
| 
             | 
        146 | 148 | } No newline at end of file | 
        
        General Comments 0
    
    
  
  
                      You need to be logged in to leave comments.
                      Login now
                    
                