From 4bacdb09d2bf71ca793212db4dfde380e70c69b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20D=2E=20D=C3=ADaz?= Date: Thu, 30 Jun 2022 12:37:46 +0200 Subject: [PATCH] [IMP] web_refresher: Work as element of control panel --- web_refresher/README.rst | 1 + web_refresher/__manifest__.py | 1 + web_refresher/readme/CONTRIBUTORS.rst | 1 + web_refresher/static/description/index.html | 1 + web_refresher/static/src/js/refresher.js | 128 ++++++++++++++++--- web_refresher/static/src/scss/refresher.scss | 7 + web_refresher/static/src/xml/refresher.xml | 27 ++++ web_refresher/templates/assets.xml | 7 +- 8 files changed, 154 insertions(+), 19 deletions(-) create mode 100644 web_refresher/static/src/scss/refresher.scss create mode 100644 web_refresher/static/src/xml/refresher.xml diff --git a/web_refresher/README.rst b/web_refresher/README.rst index 14142914d..7cb3e5338 100644 --- a/web_refresher/README.rst +++ b/web_refresher/README.rst @@ -59,6 +59,7 @@ Contributors * `Tecnativa `__: * João Marques + * Alexandre D. Díaz Maintainers ~~~~~~~~~~~ diff --git a/web_refresher/__manifest__.py b/web_refresher/__manifest__.py index 1e5144bea..9e9f4059f 100644 --- a/web_refresher/__manifest__.py +++ b/web_refresher/__manifest__.py @@ -5,6 +5,7 @@ "license": "AGPL-3", "website": "https://github.com/OCA/web", "data": ["templates/assets.xml"], + "qweb": ["static/src/xml/refresher.xml"], "depends": ["web"], "installable": True, "auto_install": False, diff --git a/web_refresher/readme/CONTRIBUTORS.rst b/web_refresher/readme/CONTRIBUTORS.rst index 6a3baeef8..dfd40a729 100644 --- a/web_refresher/readme/CONTRIBUTORS.rst +++ b/web_refresher/readme/CONTRIBUTORS.rst @@ -2,3 +2,4 @@ * `Tecnativa `__: * João Marques + * Alexandre D. Díaz diff --git a/web_refresher/static/description/index.html b/web_refresher/static/description/index.html index 04b60705b..2eeb0a7ab 100644 --- a/web_refresher/static/description/index.html +++ b/web_refresher/static/description/index.html @@ -404,6 +404,7 @@ If you spotted it first, help us smashing it by providing a detailed and welcome
  • Samuel Fringeli
  • Tecnativa:
    • João Marques
    • +
    • Alexandre D. Díaz
  • diff --git a/web_refresher/static/src/js/refresher.js b/web_refresher/static/src/js/refresher.js index 0a3ae407c..3445eb94a 100644 --- a/web_refresher/static/src/js/refresher.js +++ b/web_refresher/static/src/js/refresher.js @@ -1,25 +1,117 @@ -// Initial pager is located in source/addons/web/static/src/js/chrome/pager.js - -odoo.define("refresher.pager", function(require) { +/* Copyright 2022 Tecnativa - Alexandre D. Díaz + * License AGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). */ +odoo.define("refresher.Refresher", function(require) { "use strict"; - var pager = require("web.Pager"); - pager.include({ - start: function() { - var self = this; - var res = self._super(); + const Widget = require("web.Widget"); + const AbstractController = require("web.AbstractController"); + const BasicController = require("web.BasicController"); + const ControlPanelRenderer = require("web.ControlPanelRenderer"); + const FieldX2Many = require("web.relational_fields").FieldX2Many; - var $button = $("", { - class: "fa fa-refresh btn btn-icon o_pager_refresh", - css: {"margin-right": "8px"}, - "aria-label": "Refresh", - }); - $button.on("click", function() { - self._changeSelection(0); - }); + const Refresher = Widget.extend({ + template: "web_refresher.Button", + events: { + "click .oe_pager_refresh": "_onClickRefresher", + }, - self.$el.prepend($button); - return res; + _onClickRefresher: function() { + this.trigger("pager_refresh"); }, }); + + AbstractController.include({ + /** + * Hook + * + * @param {jQuery} $node + * @returns {Promise} + */ + // eslint-disable-next-line no-unused-vars + renderRefresher: function($node) { + return Promise.resolve(); + }, + + /** + * Adds the 'refresher' to the control panel + * + * @override + */ + _renderControlPanelElements: function() { + return this._super.apply(this, arguments).then(elements => { + elements.$refresher = $("
    "); + return this.renderRefresher(elements.$refresher).then(() => { + elements.$refresher = elements.$refresher.contents(); + return elements; + }); + }); + }, + }); + + BasicController.include({ + /** + * @param {jQuery} $node + * @returns {Promise} + */ + renderRefresher: function($node) { + this.refresher = new Refresher(this); + this.refresher.on("pager_refresh", this, () => { + if (this.pager) { + this.pager.trigger("pager_changed", this._getPagerParams()); + } + }); + return this.refresher.appendTo($node); + }, + }); + + FieldX2Many.include({ + /** + * @override + */ + _renderControlPanel: function() { + if (!this.view) { + return this._super.apply(this, arguments); + } + this.refresher = new Refresher(this); + this.refresher.on("pager_refresh", this, () => { + if (this.pager) { + this.pager.trigger("pager_changed", { + current_min: this.value.offset + 1, + limit: this.value.limit, + size: this.value.count, + }); + } + }); + return this._super + .apply(this, arguments) + .then(() => { + return this.refresher.appendTo($("
    ")); + }) + .then(() => { + this._controlPanel.updateContents( + { + cp_content: { + $refresher: this.refresher.$el, + }, + }, + { + clear: false, + } + ); + }); + }, + }); + + ControlPanelRenderer.include({ + /** + * @override + */ + start: function() { + return this._super.apply(this, arguments).then(() => { + this.nodes.$refresher = this.$(".oe_cp_refresher"); + }); + }, + }); + + return Refresher; }); diff --git a/web_refresher/static/src/scss/refresher.scss b/web_refresher/static/src/scss/refresher.scss new file mode 100644 index 000000000..464069d8a --- /dev/null +++ b/web_refresher/static/src/scss/refresher.scss @@ -0,0 +1,7 @@ +.oe_cp_refresher { + margin: auto 0 auto auto; + padding-left: 5px; + text-align: right; + user-select: none; + flex-grow: 1; +} diff --git a/web_refresher/static/src/xml/refresher.xml b/web_refresher/static/src/xml/refresher.xml new file mode 100644 index 000000000..a7ea07c0b --- /dev/null +++ b/web_refresher/static/src/xml/refresher.xml @@ -0,0 +1,27 @@ + + + diff --git a/web_refresher/templates/assets.xml b/web_refresher/templates/assets.xml index 34db7dfd1..c813b7fc1 100644 --- a/web_refresher/templates/assets.xml +++ b/web_refresher/templates/assets.xml @@ -1,7 +1,12 @@