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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
odoo.define('web.Widget', function (require) {
"use strict";
var ajax = require('web.ajax');
var core = require('web.core');
var mixins = require('web.mixins');
var ServicesMixin = require('web.ServicesMixin');
/**
* Base class for all visual components. Provides a lot of functions helpful
* for the management of a part of the DOM.
*
* Widget handles:
*
* - Rendering with QWeb.
* - Life-cycle management and parenting (when a parent is destroyed, all its
* children are destroyed too).
* - Insertion in DOM.
*
* **Guide to create implementations of the Widget class**
*
* Here is a sample child class::
*
* var MyWidget = Widget.extend({
* // the name of the QWeb template to use for rendering
* template: "MyQWebTemplate",
*
* init: function (parent) {
* this._super(parent);
* // stuff that you want to init before the rendering
* },
* willStart: function () {
* // async work that need to be done before the widget is ready
* // this method should return a promise
* },
* start: function() {
* // stuff you want to make after the rendering, `this.$el` holds a correct value
* this.$(".my_button").click(/* an example of event binding * /);
*
* // if you have some asynchronous operations, it's a good idea to return
* // a promise in start(). Note that this is quite rare, and if you
* // need to fetch some data, this should probably be done in the
* // willStart method
* var promise = this._rpc(...);
* return promise;
* }
* });
*
* Now this class can simply be used with the following syntax::
*
* var myWidget = new MyWidget(this);
* myWidget.appendTo($(".some-div"));
*
* With these two lines, the MyWidget instance was initialized, rendered,
* inserted into the DOM inside the ``.some-div`` div and its events were
* bound.
*
* And of course, when you don't need that widget anymore, just do::
*
* myWidget.destroy();
*
* That will kill the widget in a clean way and erase its content from the dom.
*/
var Widget = core.Class.extend(mixins.PropertiesMixin, ServicesMixin, {
// Backbone-ish API
tagName: 'div',
id: null,
className: null,
attributes: {},
events: {},
/**
* The name of the QWeb template that will be used for rendering. Must be
* redefined in subclasses or the default render() method can not be used.
*
* @type {null|string}
*/
template: null,
/**
* List of paths to xml files that need to be loaded before the widget can
* be rendered. This will not induce loading anything that has already been
* loaded.
*
* @type {null|string[]}
*/
xmlDependencies: null,
/**
* List of paths to css files that need to be loaded before the widget can
* be rendered. This will not induce loading anything that has already been
* loaded.
*
* @type {null|string[]}
*/
cssLibs: null,
/**
* List of paths to js files that need to be loaded before the widget can
* be rendered. This will not induce loading anything that has already been
* loaded.
*
* @type {null|string[]}
*/
jsLibs: null,
/**
* List of xmlID that need to be loaded before the widget can be rendered.
* The content css (link file or style tag) and js (file or inline) of the
* assets are loaded.
* This will not induce loading anything that has already been
* loaded.
*
* @type {null|string[]}
*/
assetLibs: null,
/**
* Constructs the widget and sets its parent if a parent is given.
*
* @param {Widget|null} parent Binds the current instance to the given Widget
* instance. When that widget is destroyed by calling destroy(), the
* current instance will be destroyed too. Can be null.
*/
init: function (parent) {
mixins.PropertiesMixin.init.call(this);
this.setParent(parent);
// Bind on_/do_* methods to this
// We might remove this automatic binding in the future
for (var name in this) {
if(typeof(this[name]) === "function") {
if((/^on_|^do_/).test(name)) {
this[name] = this[name].bind(this);
}
}
}
},
/**
* Method called between @see init and @see start. Performs asynchronous
* calls required by the rendering and the start method.
*
* This method should return a Promose which is resolved when start can be
* executed.
*
* @returns {Promise}
*/
willStart: function () {
var proms = [];
if (this.xmlDependencies) {
proms.push.apply(proms, _.map(this.xmlDependencies, function (xmlPath) {
return ajax.loadXML(xmlPath, core.qweb);
}));
}
if (this.jsLibs || this.cssLibs || this.assetLibs) {
proms.push(this._loadLibs(this));
}
return Promise.all(proms);
},
/**
* Method called after rendering. Mostly used to bind actions, perform
* asynchronous calls, etc...
*
* By convention, this method should return an object that can be passed to
* Promise.resolve() to inform the caller when this widget has been initialized.
*
* Note that, for historic reasons, many widgets still do work in the start
* method that would be more suited to the willStart method.
*
* @returns {Promise}
*/
start: function () {
return Promise.resolve();
},
/**
* Destroys the current widget, also destroys all its children before
* destroying itself.
*/
destroy: function () {
mixins.PropertiesMixin.destroy.call(this);
if (this.$el) {
this.$el.remove();
}
},
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* Renders the current widget and appends it to the given jQuery object.
*
* @param {jQuery} target
* @returns {Promise}
*/
appendTo: function (target) {
var self = this;
return this._widgetRenderAndInsert(function (t) {
self.$el.appendTo(t);
}, target);
},
/**
* Attach the current widget to a dom element
*
* @param {jQuery} target
* @returns {Promise}
*/
attachTo: function (target) {
var self = this;
this.setElement(target.$el || target);
return this.willStart().then(function () {
if (self.__parentedDestroyed) {
return;
}
return self.start();
});
},
/**
* Hides the widget
*/
do_hide: function () {
if (this.$el) {
this.$el.addClass('o_hidden');
}
},
/**
* Displays the widget
*/
do_show: function () {
if (this.$el) {
this.$el.removeClass('o_hidden');
}
},
/**
* Displays or hides the widget
* @param {boolean} [display] use true to show the widget or false to hide it
*/
do_toggle: function (display) {
if (_.isBoolean(display)) {
display ? this.do_show() : this.do_hide();
} else if (this.$el) {
this.$el.hasClass('o_hidden') ? this.do_show() : this.do_hide();
}
},
/**
* Renders the current widget and inserts it after to the given jQuery
* object.
*
* @param {jQuery} target
* @returns {Promise}
*/
insertAfter: function (target) {
var self = this;
return this._widgetRenderAndInsert(function (t) {
self.$el.insertAfter(t);
}, target);
},
/**
* Renders the current widget and inserts it before to the given jQuery
* object.
*
* @param {jQuery} target
* @returns {Promise}
*/
insertBefore: function (target) {
var self = this;
return this._widgetRenderAndInsert(function (t) {
self.$el.insertBefore(t);
}, target);
},
/**
* Renders the current widget and prepends it to the given jQuery object.
*
* @param {jQuery} target
* @returns {Promise}
*/
prependTo: function (target) {
var self = this;
return this._widgetRenderAndInsert(function (t) {
self.$el.prependTo(t);
}, target);
},
/**
* Renders the element. The default implementation renders the widget using
* QWeb, `this.template` must be defined. The context given to QWeb contains
* the "widget" key that references `this`.
*/
renderElement: function () {
var $el;
if (this.template) {
$el = $(core.qweb.render(this.template, {widget: this}).trim());
} else {
$el = this._makeDescriptive();
}
this._replaceElement($el);
},
/**
* Renders the current widget and replaces the given jQuery object.
*
* @param target A jQuery object or a Widget instance.
* @returns {Promise}
*/
replace: function (target) {
return this._widgetRenderAndInsert(_.bind(function (t) {
this.$el.replaceAll(t);
}, this), target);
},
/**
* Re-sets the widget's root element (el/$el/$el).
*
* Includes:
*
* * re-delegating events
* * re-binding sub-elements
* * if the widget already had a root element, replacing the pre-existing
* element in the DOM
*
* @param {HTMLElement | jQuery} element new root element for the widget
* @return {Widget} this
*/
setElement: function (element) {
if (this.$el) {
this._undelegateEvents();
}
this.$el = (element instanceof $) ? element : $(element);
this.el = this.$el[0];
this._delegateEvents();
return this;
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Helper method, for ``this.$el.find(selector)``
*
* @private
* @param {string} selector CSS selector, rooted in $el
* @returns {jQuery} selector match
*/
$: function (selector) {
if (selector === undefined) {
return this.$el;
}
return this.$el.find(selector);
},
/**
* Attach event handlers for events described in the 'events' key
*
* @private
*/
_delegateEvents: function () {
var events = this.events;
if (_.isEmpty(events)) { return; }
for(var key in events) {
if (!events.hasOwnProperty(key)) { continue; }
var method = this.proxy(events[key]);
var match = /^(\S+)(\s+(.*))?$/.exec(key);
var event = match[1];
var selector = match[3];
event += '.widget_events';
if (!selector) {
this.$el.on(event, method);
} else {
this.$el.on(event, selector, method);
}
}
},
/**
* Makes a potential root element from the declarative builder of the
* widget
*
* @private
* @return {jQuery}
*/
_makeDescriptive: function () {
var attrs = _.extend({}, this.attributes || {});
if (this.id) {
attrs.id = this.id;
}
if (this.className) {
attrs['class'] = this.className;
}
var $el = $(document.createElement(this.tagName));
if (!_.isEmpty(attrs)) {
$el.attr(attrs);
}
return $el;
},
/**
* Re-sets the widget's root element and replaces the old root element
* (if any) by the new one in the DOM.
*
* @private
* @param {HTMLElement | jQuery} $el
* @returns {Widget} this instance, so it can be chained
*/
_replaceElement: function ($el) {
var $oldel = this.$el;
this.setElement($el);
if ($oldel && !$oldel.is(this.$el)) {
if ($oldel.length > 1) {
$oldel.wrapAll('<div/>');
$oldel.parent().replaceWith(this.$el);
} else {
$oldel.replaceWith(this.$el);
}
}
return this;
},
/**
* Remove all handlers registered on this.$el
*
* @private
*/
_undelegateEvents: function () {
this.$el.off('.widget_events');
},
/**
* Render the widget. This is a private method, and should really never be
* called by anyone (except this widget). It assumes that the widget was
* not willStarted yet.
*
* @private
* @param {function: jQuery -> any} insertion
* @param {jQuery} target
* @returns {Promise}
*/
_widgetRenderAndInsert: function (insertion, target) {
var self = this;
return this.willStart().then(function () {
if (self.__parentedDestroyed) {
return;
}
self.renderElement();
insertion(target);
return self.start();
});
},
});
return Widget;
});
|