| 1 |
define([ |
| 2 |
"dojo/_base/array", |
| 3 |
"dojo/_base/window", |
| 4 |
"./main" |
| 5 |
], function(array, win, dijit){ |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
var _widgetTypeCtr = {}, hash = {}; |
| 11 |
|
| 12 |
var registry = { |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
length: 0, |
| 19 |
|
| 20 |
add: function(widget){ |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
if(hash[widget.id]){ |
| 26 |
throw new Error("Tried to register widget with id==" + widget.id + " but that id is already registered"); |
| 27 |
} |
| 28 |
hash[widget.id] = widget; |
| 29 |
this.length++; |
| 30 |
}, |
| 31 |
|
| 32 |
remove: function( id){ |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
if(hash[id]){ |
| 37 |
delete hash[id]; |
| 38 |
this.length--; |
| 39 |
} |
| 40 |
}, |
| 41 |
|
| 42 |
byId: function( id){ |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
return typeof id == "string" ? hash[id] : id; |
| 47 |
}, |
| 48 |
|
| 49 |
byNode: function( node){ |
| 50 |
|
| 51 |
|
| 52 |
return hash[node.getAttribute("widgetId")]; |
| 53 |
}, |
| 54 |
|
| 55 |
toArray: function(){ |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
var ar = []; |
| 64 |
for(var id in hash){ |
| 65 |
ar.push(hash[id]); |
| 66 |
} |
| 67 |
return ar; |
| 68 |
}, |
| 69 |
|
| 70 |
getUniqueId: function(widgetType){ |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
var id; |
| 75 |
do{ |
| 76 |
id = widgetType + "_" + |
| 77 |
(widgetType in _widgetTypeCtr ? |
| 78 |
++_widgetTypeCtr[widgetType] : _widgetTypeCtr[widgetType] = 0); |
| 79 |
}while(hash[id]); |
| 80 |
return dijit._scopeName == "dijit" ? id : dijit._scopeName + "_" + id; |
| 81 |
}, |
| 82 |
|
| 83 |
findWidgets: function(root, skipNode){ |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
var outAry = []; |
| 93 |
|
| 94 |
function getChildrenHelper(root){ |
| 95 |
for(var node = root.firstChild; node; node = node.nextSibling){ |
| 96 |
if(node.nodeType == 1){ |
| 97 |
var widgetId = node.getAttribute("widgetId"); |
| 98 |
if(widgetId){ |
| 99 |
var widget = hash[widgetId]; |
| 100 |
if(widget){ |
| 101 |
outAry.push(widget); |
| 102 |
} |
| 103 |
}else if(node !== skipNode){ |
| 104 |
getChildrenHelper(node); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
getChildrenHelper(root); |
| 111 |
return outAry; |
| 112 |
}, |
| 113 |
|
| 114 |
_destroyAll: function(){ |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
dijit._curFocus = null; |
| 120 |
dijit._prevFocus = null; |
| 121 |
dijit._activeStack = []; |
| 122 |
|
| 123 |
|
| 124 |
array.forEach(registry.findWidgets(win.body()), function(widget){ |
| 125 |
|
| 126 |
|
| 127 |
if(!widget._destroyed){ |
| 128 |
if(widget.destroyRecursive){ |
| 129 |
widget.destroyRecursive(); |
| 130 |
}else if(widget.destroy){ |
| 131 |
widget.destroy(); |
| 132 |
} |
| 133 |
} |
| 134 |
}); |
| 135 |
}, |
| 136 |
|
| 137 |
getEnclosingWidget: function( node){ |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
while(node){ |
| 142 |
var id = node.nodeType == 1 && node.getAttribute("widgetId"); |
| 143 |
if(id){ |
| 144 |
return hash[id]; |
| 145 |
} |
| 146 |
node = node.parentNode; |
| 147 |
} |
| 148 |
return null; |
| 149 |
}, |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
_hash: hash |
| 154 |
}; |
| 155 |
|
| 156 |
dijit.registry = registry; |
| 157 |
|
| 158 |
return registry; |
| 159 |
}); |