mirror of
https://github.com/OCA/web.git
synced 2025-02-22 13:21:25 +02:00
[IMP] web_pwa_oca: Port changes from 12.0
This commit is contained in:
committed by
sergio-teruel
parent
c1afeed3c1
commit
1194498f2e
@@ -1,43 +0,0 @@
|
||||
odoo.define("web_pwa_oca.systray.install", function(require) {
|
||||
"use strict";
|
||||
|
||||
var UserMenu = require("web.UserMenu");
|
||||
|
||||
if ("serviceWorker" in navigator) {
|
||||
window.addEventListener("load", function() {
|
||||
navigator.serviceWorker.register("/service-worker.js").then(function(reg) {
|
||||
console.log("Service worker registered.", reg);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
var deferredInstallPrompt = null;
|
||||
|
||||
UserMenu.include({
|
||||
start: function() {
|
||||
window.addEventListener(
|
||||
"beforeinstallprompt",
|
||||
this.saveBeforeInstallPromptEvent
|
||||
);
|
||||
return this._super.apply(this, arguments);
|
||||
},
|
||||
saveBeforeInstallPromptEvent: function(evt) {
|
||||
deferredInstallPrompt = evt;
|
||||
this.$.find("#pwa_install_button")[0].removeAttribute("hidden");
|
||||
},
|
||||
_onMenuInstallpwa: function() {
|
||||
deferredInstallPrompt.prompt();
|
||||
// Hide the install button, it can't be called twice.
|
||||
this.el.setAttribute("hidden", true);
|
||||
// Log user response to prompt.
|
||||
deferredInstallPrompt.userChoice.then(function(choice) {
|
||||
if (choice.outcome === "accepted") {
|
||||
console.log("User accepted the A2HS prompt", choice);
|
||||
} else {
|
||||
console.log("User dismissed the A2HS prompt", choice);
|
||||
}
|
||||
deferredInstallPrompt = null;
|
||||
});
|
||||
},
|
||||
});
|
||||
});
|
||||
55
web_pwa_oca/static/src/js/pwa_manager.js
Normal file
55
web_pwa_oca/static/src/js/pwa_manager.js
Normal file
@@ -0,0 +1,55 @@
|
||||
/* Copyright 2020 Tecnativa - Alexandre D. Díaz
|
||||
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
|
||||
|
||||
odoo.define("web_pwa_oca.PWAManager", function(require) {
|
||||
"use strict";
|
||||
|
||||
var core = require("web.core");
|
||||
var Widget = require("web.Widget");
|
||||
|
||||
var _t = core._t;
|
||||
|
||||
var PWAManager = Widget.extend({
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
init: function() {
|
||||
this._super.apply(this, arguments);
|
||||
if (!("serviceWorker" in navigator)) {
|
||||
console.error(
|
||||
_t(
|
||||
"Service workers are not supported! Maybe you are not using HTTPS or you work in private mode."
|
||||
)
|
||||
);
|
||||
} else {
|
||||
this._service_worker = navigator.serviceWorker;
|
||||
this.registerServiceWorker("/service-worker.js");
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {String} sw_script
|
||||
* @returns {Promise}
|
||||
*/
|
||||
registerServiceWorker: function(sw_script) {
|
||||
return this._service_worker
|
||||
.register(sw_script)
|
||||
.then(this._onRegisterServiceWorker)
|
||||
.catch(function(error) {
|
||||
console.log(_t("[ServiceWorker] Registration failed: "), error);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Need register some extra API? override this!
|
||||
*
|
||||
* @private
|
||||
* @param {ServiceWorkerRegistration} registration
|
||||
*/
|
||||
_onRegisterServiceWorker: function(registration) {
|
||||
console.log(_t("[ServiceWorker] Registered:"), registration);
|
||||
},
|
||||
});
|
||||
|
||||
return PWAManager;
|
||||
});
|
||||
19
web_pwa_oca/static/src/js/webclient.js
Normal file
19
web_pwa_oca/static/src/js/webclient.js
Normal file
@@ -0,0 +1,19 @@
|
||||
/* Copyright 2020 Tecnativa - Alexandre D. Díaz
|
||||
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
|
||||
|
||||
odoo.define("web_pwa_oca.webclient", function(require) {
|
||||
"use strict";
|
||||
|
||||
var WebClient = require("web.WebClient");
|
||||
var PWAManager = require("web_pwa_oca.PWAManager");
|
||||
|
||||
WebClient.include({
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
show_application: function() {
|
||||
this.pwa_manager = new PWAManager(this);
|
||||
return this._super.apply(this, arguments);
|
||||
},
|
||||
});
|
||||
});
|
||||
62
web_pwa_oca/static/src/js/worker/jquery-sw-compat.js
vendored
Normal file
62
web_pwa_oca/static/src/js/worker/jquery-sw-compat.js
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
/* 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 hack, not a complete implementation!
|
||||
// only expected to be used by boot.js
|
||||
|
||||
(function() {
|
||||
"use strict";
|
||||
function JQuery(selector, context) {
|
||||
return new JQuery.prototype.init(selector, context);
|
||||
}
|
||||
|
||||
JQuery.prototype = {
|
||||
init: function(selector) {
|
||||
if (typeof selector === "function") {
|
||||
selector();
|
||||
}
|
||||
},
|
||||
|
||||
deparam: function(data) {
|
||||
const params = data.split(",");
|
||||
const res = [];
|
||||
for (const param of params) {
|
||||
res.push(param.split("="));
|
||||
}
|
||||
return _.object(res);
|
||||
},
|
||||
|
||||
param: {
|
||||
querystring: function() {
|
||||
return "debug=1";
|
||||
},
|
||||
},
|
||||
|
||||
when: function(tasks) {
|
||||
return Promise.all(tasks instanceof Array ? tasks : [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;
|
||||
})();
|
||||
40
web_pwa_oca/static/src/js/worker/pwa.js
Normal file
40
web_pwa_oca/static/src/js/worker/pwa.js
Normal file
@@ -0,0 +1,40 @@
|
||||
/* Copyright 2020 Tecnativa - Alexandre D. Díaz
|
||||
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
|
||||
|
||||
/**
|
||||
* Services workers are a piece of software separated from the user page.
|
||||
* Here can't use 'Odoo Bootstrap', so we can't work with 'require' system.
|
||||
* When the service worker is called to be installed from the "pwa_manager"
|
||||
* this class is instantiated.
|
||||
*/
|
||||
|
||||
odoo.define("web_pwa_oca.PWA", function(require) {
|
||||
"use strict";
|
||||
|
||||
const OdooClass = require("web.Class");
|
||||
|
||||
const PWA = OdooClass.extend({
|
||||
// eslint-disable-next-line
|
||||
init: function(params) {
|
||||
// To be overridden
|
||||
},
|
||||
|
||||
/**
|
||||
* @returns {Promise}
|
||||
*/
|
||||
installWorker: function() {
|
||||
// To be overridden
|
||||
return Promise.resolve();
|
||||
},
|
||||
|
||||
/**
|
||||
* @returns {Promise}
|
||||
*/
|
||||
activateWorker: function() {
|
||||
// To be overridden
|
||||
return Promise.resolve();
|
||||
},
|
||||
});
|
||||
|
||||
return PWA;
|
||||
});
|
||||
@@ -1,18 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<templates>
|
||||
<t t-name="web_pwa_oca.systray.install">
|
||||
<a
|
||||
href="#"
|
||||
role="menuitem"
|
||||
id="pwa_install_button"
|
||||
data-menu="installpwa"
|
||||
class="dropdown-item"
|
||||
hidden="1"
|
||||
>Install PWA</a>
|
||||
</t>
|
||||
<t t-extend="UserMenu.Actions">
|
||||
<t t-jquery="a[data-menu='settings']" t-operation="after">
|
||||
<t t-call="web_pwa_oca.systray.install" />
|
||||
</t>
|
||||
</t>
|
||||
</templates>
|
||||
Reference in New Issue
Block a user