From 6171f34c6cf779433f0bef8c3b2192b800525500 Mon Sep 17 00:00:00 2001 From: sergio-teruel Date: Mon, 3 Oct 2022 21:29:56 +0200 Subject: [PATCH] [MIG] web_pwa_oca: Migration to v15.0 --- setup/web_pwa_oca/odoo/addons/web_pwa_oca | 1 + setup/web_pwa_oca/setup.py | 6 +++ web_pwa_oca/README.rst | 11 ++-- web_pwa_oca/__manifest__.py | 8 ++- web_pwa_oca/controllers/main.py | 7 +-- web_pwa_oca/controllers/service_worker.py | 10 +++- web_pwa_oca/readme/CONTRIBUTORS.rst | 1 + web_pwa_oca/static/description/index.html | 9 ++-- web_pwa_oca/static/src/js/pwa_manager.js | 39 ++++++++++++--- web_pwa_oca/static/src/js/webclient.js | 18 +++---- web_pwa_oca/static/src/js/worker/pwa.js | 19 ++++++- web_pwa_oca/templates/assets.xml | 17 +------ .../views/res_config_settings_views.xml | 50 +++++++++---------- 13 files changed, 121 insertions(+), 75 deletions(-) create mode 120000 setup/web_pwa_oca/odoo/addons/web_pwa_oca create mode 100644 setup/web_pwa_oca/setup.py diff --git a/setup/web_pwa_oca/odoo/addons/web_pwa_oca b/setup/web_pwa_oca/odoo/addons/web_pwa_oca new file mode 120000 index 000000000..1b7095260 --- /dev/null +++ b/setup/web_pwa_oca/odoo/addons/web_pwa_oca @@ -0,0 +1 @@ +../../../../web_pwa_oca \ No newline at end of file diff --git a/setup/web_pwa_oca/setup.py b/setup/web_pwa_oca/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/web_pwa_oca/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/web_pwa_oca/README.rst b/web_pwa_oca/README.rst index e92ab481c..86d33d74a 100644 --- a/web_pwa_oca/README.rst +++ b/web_pwa_oca/README.rst @@ -14,13 +14,13 @@ Progressive web application :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html :alt: License: LGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github - :target: https://github.com/OCA/web/tree/14.0/web_pwa_oca + :target: https://github.com/OCA/web/tree/15.0/web_pwa_oca :alt: OCA/web .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png - :target: https://translation.odoo-community.org/projects/web-14-0/web-14-0-web_pwa_oca + :target: https://translation.odoo-community.org/projects/web-15-0/web-15-0-web_pwa_oca :alt: Translate me on Weblate .. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png - :target: https://runbot.odoo-community.org/runbot/162/14.0 + :target: https://runbot.odoo-community.org/runbot/162/15.0 :alt: Try me on Runbot |badge1| |badge2| |badge3| |badge4| |badge5| @@ -140,7 +140,7 @@ Bug Tracker Bugs are tracked on `GitHub Issues `_. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us smashing it by providing a detailed and welcomed -`feedback `_. +`feedback `_. Do not contact contributors directly about support or help with technical issues. @@ -164,6 +164,7 @@ Contributors * Alexandre D. Díaz * João Marques + * Sergio Teruel Maintainers ~~~~~~~~~~~ @@ -186,6 +187,6 @@ Current `maintainer `__: |maintainer-eLBati| -This module is part of the `OCA/web `_ project on GitHub. +This module is part of the `OCA/web `_ project on GitHub. You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/web_pwa_oca/__manifest__.py b/web_pwa_oca/__manifest__.py index 9bae0c3cb..5cd68293a 100644 --- a/web_pwa_oca/__manifest__.py +++ b/web_pwa_oca/__manifest__.py @@ -6,7 +6,7 @@ { "name": "Progressive web application", "summary": "Make Odoo a PWA", - "version": "14.0.1.0.0", + "version": "15.0.1.0.0", "development_status": "Beta", "category": "Website", "website": "https://github.com/OCA/web", @@ -17,5 +17,11 @@ "installable": True, "depends": ["web", "mail"], "data": ["templates/assets.xml", "views/res_config_settings_views.xml"], + "assets": { + "web.assets_backend": [ + "/web_pwa_oca/static/src/js/pwa_manager.js", + "/web_pwa_oca/static/src/js/webclient.js", + ] + }, "images": ["static/description/pwa.png"], } diff --git a/web_pwa_oca/controllers/main.py b/web_pwa_oca/controllers/main.py index fdaad56c1..bb3250e24 100644 --- a/web_pwa_oca/controllers/main.py +++ b/web_pwa_oca/controllers/main.py @@ -13,9 +13,9 @@ class PWA(Controller): return [ "/web/static/lib/underscore/underscore.js", "/web_pwa_oca/static/src/js/worker/jquery-sw-compat.js", - "/web/static/src/js/promise_extension.js", - "/web/static/src/js/boot.js", - "/web/static/src/js/core/class.js", + "/web/static/src/legacy/js/promise_extension.js", + "/web/static/src/boot.js", + "/web/static/src/legacy/js/core/class.js", "/web_pwa_oca/static/src/js/worker/pwa.js", ] @@ -52,6 +52,7 @@ class PWA(Controller): % (str(size[0]), str(size[1])), "sizes": "{}x{}".format(str(size[0]), str(size[1])), "type": "image/png", + "purpose": "any maskable", } ) elif not pwa_icon.mimetype.startswith("image/svg"): diff --git a/web_pwa_oca/controllers/service_worker.py b/web_pwa_oca/controllers/service_worker.py index ffd8fd737..1ec3e6e38 100644 --- a/web_pwa_oca/controllers/service_worker.py +++ b/web_pwa_oca/controllers/service_worker.py @@ -48,7 +48,15 @@ class ServiceWorker(PWA): def _get_js_pwa_init(self): return """ - const oca_pwa = new PWA({}); + let promise_start = Promise.resolve(); + if (typeof self.oca_pwa === "undefined") {{ + self.oca_pwa = new PWA({}); + promise_start = self.oca_pwa.start(); + if (self.serviceWorker.state === "activated") {{ + promise_start = promise_start.then( + () => self.oca_pwa.activateWorker(true)); + }} + }} """.format( self._get_pwa_params() ) diff --git a/web_pwa_oca/readme/CONTRIBUTORS.rst b/web_pwa_oca/readme/CONTRIBUTORS.rst index 200dad8dc..1484b47b2 100644 --- a/web_pwa_oca/readme/CONTRIBUTORS.rst +++ b/web_pwa_oca/readme/CONTRIBUTORS.rst @@ -6,3 +6,4 @@ * Alexandre D. Díaz * João Marques + * Sergio Teruel diff --git a/web_pwa_oca/static/description/index.html b/web_pwa_oca/static/description/index.html index 3c697997a..7f8ff10b0 100644 --- a/web_pwa_oca/static/description/index.html +++ b/web_pwa_oca/static/description/index.html @@ -367,7 +367,7 @@ ul.auto-toc { !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> -

Beta License: LGPL-3 OCA/web Translate me on Weblate Try me on Runbot

+

Beta License: LGPL-3 OCA/web Translate me on Weblate Try me on Runbot

Make Odoo an installable Progressive Web Application.

Progressive Web Apps provide an installable, app-like experience on desktop and mobile that are built and delivered directly via the web. They’re web apps that are fast and reliable. And most importantly, they’re web apps that work in any browser. @@ -464,7 +464,7 @@ need to use the following workaround:

 var MyClass = OdooClass.extend({
     myFunc: function() {
-        return new Promise(async (resolve, reject) => {
+        return new Promise(async (resolve, reject) => {
             const mydata = await ...do await stuff...
             return resolve(mydata);
         });
@@ -488,7 +488,7 @@ doesn’t send the cookie and web manifest returns 404.

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us smashing it by providing a detailed and welcomed -feedback.

+feedback.

Do not contact contributors directly about support or help with technical issues.

@@ -510,6 +510,7 @@ If you spotted it first, help us smashing it by providing a detailed and welcome
  • Tecnativa:
    • Alexandre D. Díaz
    • João Marques
    • +
    • Sergio Teruel
  • @@ -523,7 +524,7 @@ mission is to support the collaborative development of Odoo features and promote its widespread use.

    Current maintainer:

    eLBati

    -

    This module is part of the OCA/web project on GitHub.

    +

    This module is part of the OCA/web project on GitHub.

    You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

    diff --git a/web_pwa_oca/static/src/js/pwa_manager.js b/web_pwa_oca/static/src/js/pwa_manager.js index c4bd9de4b..d6c56ab13 100644 --- a/web_pwa_oca/static/src/js/pwa_manager.js +++ b/web_pwa_oca/static/src/js/pwa_manager.js @@ -5,26 +5,44 @@ odoo.define("web_pwa_oca.PWAManager", function (require) { "use strict"; var core = require("web.core"); + var config = require("web.config"); var Widget = require("web.Widget"); var _t = core._t; + /** + * @returns {Boolean} + */ + function isPWAStandalone() { + return ( + window.navigator.standalone || + document.referrer.includes("android-app://") || + window.matchMedia("(display-mode: standalone)").matches + ); + } + + if (isPWAStandalone()) { + config.device.isMobile = true; + } + var PWAManager = Widget.extend({ /** * @override */ init: function () { this._super.apply(this, arguments); - if (!("serviceWorker" in navigator)) { + this._isServiceWorkerSupported = "serviceWorker" in navigator; + if (!this._isServiceWorkerSupported) { console.error( _t( "Service workers are not supported! Maybe you are not using HTTPS or you work in private mode." ) ); - } - if ("serviceWorker" in navigator) { + } else { this._service_worker = navigator.serviceWorker; - this.registerServiceWorker("/service-worker.js"); + this.registerServiceWorker("/service-worker.js", { + updateViaCache: "none", + }); } }, @@ -32,15 +50,22 @@ odoo.define("web_pwa_oca.PWAManager", function (require) { * @param {String} sw_script * @returns {Promise} */ - registerServiceWorker: function (sw_script) { + registerServiceWorker: function (sw_script, options) { return this._service_worker - .register(sw_script) - .then(this._onRegisterServiceWorker) + .register(sw_script, options) + .then(this._onRegisterServiceWorker.bind(this)) .catch(function (error) { console.log(_t("[ServiceWorker] Registration failed: "), error); }); }, + /** + * @returns {Boolean} + */ + isPWAStandalone: function () { + return isPWAStandalone(); + }, + /** * Need register some extra API? override this! * diff --git a/web_pwa_oca/static/src/js/webclient.js b/web_pwa_oca/static/src/js/webclient.js index 5206251af..7d93d5a33 100644 --- a/web_pwa_oca/static/src/js/webclient.js +++ b/web_pwa_oca/static/src/js/webclient.js @@ -1,19 +1,15 @@ /* Copyright 2020 Tecnativa - Alexandre D. Díaz +/* Copyright 2022 Tecnativa - Sergio Teruel * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ -odoo.define("web_pwa_oca.webclient", function (require) { +odoo.define("web_pwa_oca.pwa_launch", function (require) { "use strict"; - - var WebClient = require("web.WebClient"); + var core = require("web.core"); 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); - }, + core.bus.on("web_client_ready", null, function () { + this.pwa_manager = new PWAManager(this); + const def = this.pwa_manager.start(); + return Promise.all([def]); }); }); diff --git a/web_pwa_oca/static/src/js/worker/pwa.js b/web_pwa_oca/static/src/js/worker/pwa.js index 629a4b576..645f3aa16 100644 --- a/web_pwa_oca/static/src/js/worker/pwa.js +++ b/web_pwa_oca/static/src/js/worker/pwa.js @@ -17,6 +17,14 @@ odoo.define("web_pwa_oca.PWA", function (require) { // eslint-disable-next-line init: function (params) { // To be overridden + this._sw_version = params.sw_version; + }, + + /** + * @returns {Promise} + */ + start: function () { + return Promise.resolve(); }, /** @@ -30,10 +38,19 @@ odoo.define("web_pwa_oca.PWA", function (require) { /** * @returns {Promise} */ - activateWorker: function () { + /* eslint-disable no-unused-vars */ + activateWorker: function (forced) { // To be overridden return Promise.resolve(); }, + + /** + * @returns {Promise} + */ + processRequest: function (request) { + // To be overridden + return fetch(request); + }, }); return PWA; diff --git a/web_pwa_oca/templates/assets.xml b/web_pwa_oca/templates/assets.xml index 37554c6ba..f616d2d44 100644 --- a/web_pwa_oca/templates/assets.xml +++ b/web_pwa_oca/templates/assets.xml @@ -28,20 +28,5 @@ -