diff options
| author | Géry Debongnie <ged@odoo.com> | 2019-08-23 13:19:24 +0000 |
|---|---|---|
| committer | Vincent Schippefilt <vsc@odoo.com> | 2019-12-09 11:09:10 +0000 |
| commit | 29726e8fd000cb09075f9b6b2a6a8d2ddaf4ef58 (patch) | |
| tree | 91718f6edc90cc069ee5ff3b44c4beb823d31104 /addons/web/static/src/js/core | |
| parent | 3b4592acfcd67f2b3e467c5e3f82d7b524b96843 (diff) | |
[ADD] web: add owl 1.0.0-beta1 to odoo
With this commit, we properly introduce the Owl library into our beloved
codebase. More information on the Owl library can be found in the project
repository: https://github.com/odoo/owl/
Co-authored-by: Aaron Bohy <aab@odoo.com>
Co-authored-by: Julien Mougenot <jum@odoo.com>
Co-authored-by: Mathieu Duckerts-Antoine <dam@odoo.com>
Diffstat (limited to 'addons/web/static/src/js/core')
| -rw-r--r-- | addons/web/static/src/js/core/session.js | 8 | ||||
| -rw-r--r-- | addons/web/static/src/js/core/utils.js | 75 |
2 files changed, 82 insertions, 1 deletions
diff --git a/addons/web/static/src/js/core/session.js b/addons/web/static/src/js/core/session.js index 4c8a63dbdb5..a8159469145 100644 --- a/addons/web/static/src/js/core/session.js +++ b/addons/web/static/src/js/core/session.js @@ -4,7 +4,6 @@ odoo.define('web.Session', function (require) { var ajax = require('web.ajax'); var concurrency = require('web.concurrency'); var core = require('web.core'); -var local_storage = require('web.local_storage'); var mixins = require('web.mixins'); var utils = require('web.utils'); @@ -239,7 +238,14 @@ var Session = core.Class.extend(mixins.EventDispatcherMixin, { var route = '/web/webclient/qweb/' + (cacheId ? cacheId : Date.now()) + '?mods=' + mods; return $.get(route).then(function (doc) { if (!doc) { return; } + const owlTemplates = []; + for (let child of doc.querySelectorAll("templates > [owl]")) { + child.removeAttribute('owl'); + owlTemplates.push(child.outerHTML); + child.remove(); + } qweb.add_template(doc); + self.owlTemplates = `<templates> ${owlTemplates.join('\n')} </templates>`; }); }); return lock; diff --git a/addons/web/static/src/js/core/utils.js b/addons/web/static/src/js/core/utils.js index 29c2ff04c40..4fe1740d6eb 100644 --- a/addons/web/static/src/js/core/utils.js +++ b/addons/web/static/src/js/core/utils.js @@ -203,7 +203,10 @@ var diacriticsMap = { '\u0225': 'z','\u0240': 'z','\u2C6C': 'z','\uA763': 'z', }; +const patchMap = new WeakMap(); + var utils = { + /** * Throws an error if the given condition is not true * @@ -511,6 +514,51 @@ var utils = { return new Array(size - str.length + 1).join('0') + str; }, /** + * Patch a class and return a function that remove the patch + * when called. + * + * @param {Class} C Class to patch + * @param {string} patchName + * @param {Object} patch + * @returns {Function} + */ + patch: function(C, patchName, patch) { + let metadata = patchMap.get(C.prototype); + if (!metadata) { + metadata = { + origMethods: {}, + patches: {}, + current: [] + }; + patchMap.set(C.prototype, metadata); + } + const proto = C.prototype; + if (metadata.patches[patchName]) { + throw new Error(`Patch [${patchName}] already exists`); + } + metadata.patches[patchName] = patch; + applyPatch(proto, patch); + metadata.current.push(patchName); + + function applyPatch(proto, patch) { + Object.keys(patch).forEach(function (methodName) { + const method = patch[methodName]; + if (typeof method === "function") { + const original = proto[methodName]; + if (!(methodName in metadata.origMethods)) { + metadata.origMethods[methodName] = original; + } + proto[methodName] = function (...args) { + this._super = original; + return method.call(this, ...args); + }; + } + }); + } + + return this.unpatch.bind(null, C, patchName); + }, + /** * performs a half up rounding with a fixed amount of decimals, correcting for float loss of precision * See the corresponding float_round() in server/tools/float_utils.py for more info * @param {Number} value the value to be rounded @@ -662,6 +710,33 @@ var utils = { return casesensetive ? str : str.toLowerCase(); }, /** + * We define here an unpatch function. This is mostly useful if we want to + * remove a patch. For example, for testing purposes + * + * @param {Class} C + * @param {string} patchName + */ + unpatch: function(C, patchName) { + const proto = C.prototype; + let metadata = patchMap.get(proto); + if (!metdata) { + return; + } + patchMap.delete(proto); + + // reset to original + for (let k in metadata.origMethods) { + proto[k] = metadata.origMethods[k]; + } + + // apply other patches + for (let name of metadata.current) { + if (name !== patchName) { + this.patch(C, name, metadata.patches[name]); + } + } + }, + /** * @param {any} node * @param {any} strip_whitespace * @returns |
