-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path_Widget.js
337 lines (301 loc) · 8.3 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
define([
"module",
"dojo/_base/declare",
"dijit/_Widget",
"dojo/_base/lang",
"dojo/_base/fx",
"dojo/dom-style",
"dojo/dom-class",
"dojo/aspect",
"dojo/promise/all",
"dojo/Deferred",
"./util/makeDOM",
"./util/async"
], function(module, declare, _Widget, lang, fx, style, domClass, aspect, allPromises, Deferred,
makeDOM, async) {
/**
* Base class widget class
*
* Child classes should define 'makeContentNodes' as a function
* returning an array of node definitions (first arg to geonef/jig/util/makeDOM)
*/
return declare([_Widget], { //--noindent--
/**
* CSS classes to be set on domNode (prefer 'extraClass' at construction-time)
*
* @type {string} class
*/
'class': 'jigWidget',
/**
* Other CSS classes to be added on domNode
*
* This proprty works the same as 'class', they are distinct for the
* convenience of inheritance.
*
* @type {string} extraClass
*/
extraClass: '',
/**
* Nom du noeud DOM domNode du widget
*
* @type {string} nodeName
*/
nodeName: 'div',
/**
* Static alternative to implementing makeContentNodes()
*
* @type {Array.<Array>} contentNodes
*/
contentNodes: [],
/**
* If true, content nodes are built upon rebuildDom() instead of initial buildRendering()
*
* In this case, the concrete class has the responsability to call
* rebuildDom() (typically, when some data used by makeContentNodes() were
* sucessfully loaded).
*
* @type {boolean} delayedContent
*/
delayedContent: false,
/**
* Promise, resolved when widget DOM content is built and ready
*
* @type {dojo/Deferred}
*/
whenDomReady: null,
/**
* Names of node properties to hide when a sub-widget is open
*
* @type {Array.<string>} subHides
*/
subHides: [],
/**
* Used to provide makeDOM (thruogh this.dom()) with the values of these args
*
* @type {Array.<string>}
*/
recursiveWidgetsProps: ["appView"],
/**
* @override
*/
postMixInProperties: function() {
this.domWidgets = [];
this.whenDomReady = new Deferred();
this.inherited(arguments);
this.domWidgetProps = {};
this.recursiveWidgetsProps.forEach(function(name) {
this.domWidgetProps[name] = this[name];
}, this);
},
/**
* @override
*/
buildRendering: function() {
if (!this.domNode) {
var attrs = { 'class': this['class']+' '+this.extraClass+' '+
(this.delayedContent ? "domLoading": "") };
if (this.srcNodeRef) {
if (this.srcNodeRef.hasAttribute('style')) {
attrs.style = this.srcNodeRef.getAttribute('style');
}
if (this.srcNodeRef.hasAttribute("class")) {
attrs["class"] += " " + this.srcNodeRef.getAttribute("class");
}
}
var nodes = this.delayedContent ? [this.makeSpinnerNode()] : this.makeContentNodes();
this.domNode = this.dom([this.nodeName, attrs, nodes]);
}
this.inherited(arguments);
},
/**
* Copy children nodes from this.srcNodeRef to this.containerNode
*
* this.containerNode has to be defined
*/
copySrcNodeChildren: function() {
var source = this.srcNodeRef;
var dest = this.containerNode;
if (source && dest){
while (source.hasChildNodes()){
dest.appendChild(source.firstChild);
}
}
},
/**
* Overriden by child class to define DOM content
*
* @return {Array} An array of arrays (see geonef/jig/util/makeDOM).
* It can contain DOM Elements or flat makeDOM representation.
* If it has promises, then this.delayedContent must be true
* (buildRendering() does not support asynchronous DOM).
*/
makeContentNodes: function() {
return this.contentNodes;
},
/**
* Make DOM definition for a spinner node
*
* @param {string} extraClass CSS class to apply to node, in addition to "jigLoading"
* @return Array
*/
makeSpinnerNode: function(extraClass) {
return ["div", {"class":"jigLoading "+(extraClass || "")}, "Chargement..."];
},
/**
* @override
*/
startup: function() {
this.inherited(arguments);
this.domWidgets.forEach(function(w) { if (!w._started) { w.startup(); }});
if (!this.delayedContent) {
this.whenDomReady.resolve();
}
},
/**
* @override
*/
destroyRendering: function() {
this.destroyDom();
if (this._supportingWidgets) {
this._supportingWidgets.forEach(function(w) { w.destroy(); });
delete this._supportingWidgets;
}
this.inherited(arguments);
},
/**
* @override
*/
destroyDom: function() {
this.destroySubWidget();
if (this.domWidgets) {
this.domWidgets.forEach(function(w) { w.destroy(); });
this.domWidgets = [];
}
if (this.domNode) {
this.domNode.innerHTML = '';
}
},
/**
* Rebuild the DOM
*
* this.domNode itself remain unchanged, only its descendants are destroyed
*
* @param arg Custom arg passed to makeContentNodes()
*/
rebuildDom: function(arg) {
if (this._destroyed) { return null; }
this.destroyDom();
var domNode = this.domNode;
var _this = this;
return allPromises(this.dom(this.makeContentNodes(arg)))
.then(function(nodes) {
if (_this._destroyed) {
throw new Error("rebuildDom(): widget was destroyed in the middle :(");
}
domClass.remove(domNode, "domLoading");
nodes.forEach(function(node) { if (node) { domNode.appendChild(node); } });
return async.bindArg(nodes, _this.afterRebuildDom());
// return nodes;
});
},
/**
* Hook
*/
afterRebuildDom: function() {
if (!(this.whenDomReady.fired >= 0)) {
this.whenDomReady.resolve();
}
if (this.resize) {
this.resize();
}
this.onResize();
},
/**
* Hook
*/
onResize: function() {
},
/**
* Kind of helper to util/makeDOM, using this as obj
*/
dom: function(struct, obj) {
return makeDOM(struct, obj || this);
},
/**
* Build DOM tree, returning clearing function
*/
tempDom: function(struct, objOrFunc) {
var obj = lang.mixin({ domWidgets: [], _started: true },
typeof objOrFunc === "function" ? objOrFunc.obj : objOrFunc);
makeDOM(struct, obj);
if (typeof objOrFunc === "function") {
// the objOrFunc function will destroy or widgets,
// which were added to objOrFunc.obj.domWidgets
return objOrFunc;
}
var _destroy = function() {
obj.domWidgets.forEach(function(w) { w.destroy(); });
};
_destroy.obj = obj;
return _destroy;
},
/**
* Add a widget within this.domNode and manage it's lifecycle
*
* @type {dijit/_WidgetBase} widget
* @type {Function} onDestroy
* @return {dijit/_WidgetBase} the given widget
*/
enableSubWidget: function(widget, onDestroy) {
this.destroySubWidget();
this.subHides.forEach(
function(name) {
var node = this[name];
if (node) {
style.set(node.domNode || node, 'display', 'none');
}
}, this);
widget.placeAt(this.opNode || this.domNode).startup();
this.subWidget = widget;
domClass.add(widget.domNode, "sub");
domClass.add(this.domNode, "hasSub "+(widget.parentClass || ""));
var _this = this;
aspect.before(widget, "uninitialize", function() {
_this.destroySubWidget(); // uninitialize won't be called again
if (onDestroy) {
onDestroy.call(_this, widget._argToDestroyHandler);
}
widget._argToDestroyHandler = null;
});
return widget;
},
/**
* Destroy a subwidget added with enablSubWidget
*
* @return {boolean} Whether the widget was not already closed
*/
destroySubWidget: function(argToDestroyHandler) {
var widget = this.subWidget;
if (widget) {
delete this.subWidget;
if (this.domNode) {
domClass.remove(this.domNode, "hasSub "+(widget.parentClass || ""));
}
if (!widget._beingDestroyed) {
widget._argToDestroyHandler = argToDestroyHandler;
widget.destroy();
}
this.subHides.forEach(
function(name) {
var node = this[name];
if (node) {
style.set(node.domNode || node, 'display', '');
}
}, this);
return true;
}
return false;
},
declaredClass: module.id
});
});