From ee5e972586795114f4adeead47746397a181c549 Mon Sep 17 00:00:00 2001 From: Yannick Tivisse Date: Mon, 1 Jul 2019 16:36:40 +0200 Subject: [PATCH] [IMP] web: Always display (un)archive buttons in Action on form views Purpose ======= While cleaning the stat buttons across all modules, we decided to move the (un)archiving from stat button to actions in the 'Action' dropdown. Specification ============= If the record has an 'active' field, display an action 'Archive' in the dropdown if the record is active, else display an action 'Unarchive'. The 'Archive' action should ask to the user if he's sure he wants to make this action. --- .../src/js/views/form/form_controller.js | 64 ++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/js/views/form/form_controller.js b/addons/web/static/src/js/views/form/form_controller.js index 89477f9c974..67d1a6aa130 100644 --- a/addons/web/static/src/js/views/form/form_controller.js +++ b/addons/web/static/src/js/views/form/form_controller.js @@ -183,6 +183,24 @@ var FormController = BasicController.extend({ var self = this; if (this.hasSidebar) { var otherItems = []; + if (this.archiveEnabled) { + var classname = "o_sidebar_item_archive" + (this.initialState.data.active ? "" : " o_hidden") + otherItems.push({ + label: _t("Archive"), + callback: function () { + Dialog.confirm(self, _t("Are you sure that you want to archive this record?"), { + confirm_callback: self._toggleArchiveState.bind(self, true), + }); + }, + classname: classname, + }); + classname = "o_sidebar_item_unarchive" + (this.initialState.data.active ? " o_hidden" : "") + otherItems.push({ + label: _t("Unarchive"), + callback: this._toggleArchiveState.bind(this, false), + classname: classname, + }); + } if (this.is_action_enabled('delete')) { otherItems.push({ label: _t('Delete'), @@ -263,6 +281,23 @@ var FormController = BasicController.extend({ // Private //-------------------------------------------------------------------------- + /** + * Archive the current selection + * + * @private + * @param {string[]} ids + * @param {boolean} archive + * @returns {Promise} + */ + _archive: function (ids, archive) { + if (ids.length === 0) { + return Promise.resolve(); + } + return this.model + .toggleActive(ids, !archive, this.handle) + .then(this.update.bind(this, {}, {reload: false})); + }, + /** * Assign on the buttons save and discard additionnal behavior to facilitate * the work of the users doing input only using the keyboard @@ -428,7 +463,6 @@ var FormController = BasicController.extend({ self._setTitle(title); self._updateButtons(); self._updateSidebar(); - self.autofocus(); }); }, @@ -457,6 +491,25 @@ var FormController = BasicController.extend({ _updateSidebar: function () { if (this.sidebar) { this.sidebar.do_toggle(this.mode === 'readonly'); + // Hide/Show Archive/Unarchive dropdown items + // We could have toggled the o_hidden class on the + // item theirselves, but the items are redrawed + // at each update, based on the initial definition + var archive_item = _.find(this.sidebar.items.other, function(item) { + return item.classname && item.classname.includes('o_sidebar_item_archive') + }) + var unarchive_item = _.find(this.sidebar.items.other, function(item) { + return item.classname && item.classname.includes('o_sidebar_item_unarchive') + }) + if (archive_item && unarchive_item) { + if (this.renderer.state.data.active) { + archive_item.classname = 'o_sidebar_item_archive'; + unarchive_item.classname = 'o_sidebar_item_unarchive o_hidden'; + } else { + archive_item.classname = 'o_sidebar_item_archive o_hidden'; + unarchive_item.classname = 'o_sidebar_item_unarchive'; + } + } } }, @@ -712,6 +765,15 @@ var FormController = BasicController.extend({ self.renderer.confirmChange(state, state.id, [field]); }); }, + /** + * Called when clicking on 'Archive' or 'Unarchive' in the sidebar. + * + * @private + * @param {boolean} archive + */ + _toggleArchiveState: function (archive) { + this._archive([this.handle], archive); + }, }); return FormController;