-
Notifications
You must be signed in to change notification settings - Fork 1
/
Widget.js
112 lines (96 loc) · 2.94 KB
/
Widget.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
define([
'dojo/_base/declare',
'dijit/registry',
'dijit/_WidgetBase',
'dijit/_TemplatedMixin',
'dx-alias/dom',
'dx-alias/string',
'dx-alias/on'
],function(declare, registry, _WidgetBase, _TemplatedMixin, dom, string, on){
// summary:
// Widget is not very complicated, it's a shortcut for the most common
// Dijit creation modules, _WidgetBase, and _TemplatedMixin.
// Widget also mixes in some useful methods, like show and hide.
//
return declare('dx-alias.Widget', [_WidgetBase, _TemplatedMixin], {
templateString:'<div></div>', //to be overwritten
showing: true,
show: function(){
if(this.showing) return;
this.showing = true;
dom.show(this.domNode);
},
hide: function(){
if(!this.showing) return;
this.showing = false;
dom.hide(this.domNode);
},
getName: function(){
return string.last(this.declaredClass, '.');
},
getParent: function(){
// summary:
// Returns the parent widget of this widget
//console.log('getParent', this.domNode)
if(!this.domNode.parentNode){
console.warn('NO PARENT', this)
return null;
}
return this._parent || registry.getEnclosingWidget(this.domNode.parentNode) || registry.getEnclosingWidget(this.domNode.parentNode.parentNode);
},
addChildren: function(widgets, node){
//console.log('addChildren', widgets)
widgets.forEach(function(w){
//console.log('addChild', w);
this.addChild(w, node);
}, this)
},
addChild: function(widget, node){
//console.log('addChild')
widget._parent = this;
node = node || this.containerNode || this.domNode;
node.appendChild(widget.domNode);
if(this._started && !widget._started) widget.startup();
return widget;
},
removeChild: function(widget){
if(widget){
var node = widget.domNode;
if(node && node.parentNode){
node.parentNode.removeChild(node); // detach but don't destroy
}
}
return widget;
},
connectEvents: function(){
this._connections.forEach(function(handle){ handle.resume(); }, this);
},
disconnectEvents: function(){
this._connections.forEach(function(handle){ handle.pause(); }, this);
},
getObject: function(obj){
return typeof obj == 'string' ? typeof this[obj] == 'object' ? this[obj] : registry.byId(obj) : obj;
},
on: function(obj, event, ctx, method){
this._connections = this._connections || [];
var h = on(obj, event, ctx, method);
this._connections.push(h);
return h;
},
sub: function(/*String*/channel, /*Object|Function*/ctx, /*String|Function*/method, /*String?*/group){
this._connections = this._connections || [];
var h = on.sub(channel, ctx, method, group);
this._connections.push(h);
return h;
},
// add sub() ?
destroy: function(){
if(this._connections){
this._connections.forEach(function(h){
h.remove();
});
}
this.inherited(arguments);
}
});
});