Files
web/web_pwa_oca/static/src/js/worker/jquery-sw-compat.js
Alexandre D. Díaz dfe4e8ae6c [IMP] web_pwa_oca: Add support for require system
Service workers need to load "Odoo Bootstrap" to be able to use
"require". The problem is that "Odoo Bootstrap" does operations
on the DOM (by using jQuery) and to avoid errors a minimal
compatibility layer "jquery-sw-compat.js" has been created to
allow using "require" in the service worker.

Moved Service Worker (js) code from the xml to python.
2021-03-11 19:06:29 +01:00

63 lines
1.5 KiB
JavaScript

/* Copyright 2020 Tecnativa - Alexandre D. Díaz
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
// Compatibility layer to load some Odoo modules
// This is a very simple implementation!!!
function JQuery (selector, context) {
return new JQuery.prototype.init(selector, context);
};
JQuery.prototype = {
init: function (selector, context) {
if (typeof selector === "function") {
selector();
}
},
// This is a hack, not a complete implementation!
// only expected to be used by boot.js
deparam: function (data) {
const params = data.split(',');
const res = [];
for (let param of params) {
res.push(param.split('='));
}
return _.object(res);
},
param: {
querystring: function () {
return "debug=1";
}
},
when: function (tasks) {
if (!(tasks instanceof Array)) {
tasks = [tasks];
}
return Promise.all(tasks).then((results) => {
return results.length === 1 ? results[0] : results;
});
},
};
class Deferred {
constructor() {
this.promise = new Promise((resolve, reject)=> {
this.reject = reject;
this.resolve = resolve;
})
}
};
JQuery.prototype.Deferred = () => new Deferred();
self.$ = JQuery;
self.$.deparam = JQuery.prototype.deparam;
self.$.param = JQuery.prototype.param;
self.$.Deferred = JQuery.prototype.Deferred;
self.$.when = JQuery.prototype.when;
self.window = self;