diff --git a/addons/web_editor/static/src/scss/wysiwyg_snippets.scss b/addons/web_editor/static/src/scss/wysiwyg_snippets.scss index 52be5b79c15..3bc87f18f83 100644 --- a/addons/web_editor/static/src/scss/wysiwyg_snippets.scss +++ b/addons/web_editor/static/src/scss/wysiwyg_snippets.scss @@ -883,6 +883,11 @@ body.editor_enable.editor_has_snippets { } } + // Buttons with an icon but no text, so no possible overflow + we-button.o_we_button_icon { + flex: 0 0 auto; + } + // Checkboxes we-button.o_we_checkbox_wrapper.o_we_user_value_widget { min-width: $o-we-sidebar-content-field-toggle-width; diff --git a/addons/website/static/src/snippets/s_website_form/options.js b/addons/website/static/src/snippets/s_website_form/options.js index 81949c1ec78..1106892db7f 100644 --- a/addons/website/static/src/snippets/s_website_form/options.js +++ b/addons/website/static/src/snippets/s_website_form/options.js @@ -4,6 +4,8 @@ odoo.define('website.form_editor', function (require) { const core = require('web.core'); const FormEditorRegistry = require('website.form_editor_registry'); const options = require('web_editor.snippets.options'); +const Dialog = require('web.Dialog'); +const dom = require('web.dom'); require('website.editor.snippets.options'); const qweb = core.qweb; @@ -450,6 +452,55 @@ options.registry.WebsiteFormEditor = FormEditor.extend({ } this._addHiddenField(value, fieldName); }, + /** + * Prompts the user to save changes before being redirected + * towards an action specified in value. + * + * @see this.selectClass for parameters + */ + promptSaveRedirect: function (name, value, widgetValue) { + return new Promise((resolve, reject) => { + const message = _t("Would you like to save before being redirected? Unsaved changes will be discarded."); + Dialog.confirm(this, message, { + cancel_callback: () => resolve(), + buttons: [ + { + text: _t("Save"), + classes: 'btn-primary', + click: (ev) => { + const restore = dom.addButtonLoadingEffect(ev.currentTarget); + this.trigger_up('request_save', { + reload: false, + onSuccess: () => { + this._redirectToAction(value); + }, + onFailure: () => { + restore(); + this.displayNotification({ + message: _t("Something went wrong."), + type: 'danger', + sticky: true, + }); + reject(); + }, + }); + resolve(); + }, + }, { + text: _t("Discard"), + click: (ev) => { + dom.addButtonLoadingEffect(ev.currentTarget); + this._redirectToAction(value); + }, + }, { + text: _t("Cancel"), + close: true, + click: () => resolve(), + }, + ], + }); + }); + }, /** * Changes the onSuccess event. */ @@ -636,8 +687,34 @@ options.registry.WebsiteFormEditor = FormEditor.extend({ button.dataset.addActionField = el.id; selectEl.append(button); }); + if (field.createAction) { + return this._addCreateButton(selectEl, field.createAction); + } return selectEl; }, + /** + * Wraps an HTML element in a we-row element, and adds a + * we-button linking to the given action. + * + * @private + * @param {HTMLElement} element + * @param {String} action + * @returns {HTMLElement} + */ + _addCreateButton: function (element, action) { + const iconEl = document.createElement('i'); + iconEl.classList.add('fa', 'fa-fw', 'fa-plus'); + const linkButtonEl = document.createElement('we-button'); + linkButtonEl.title = _t("Create new"); + linkButtonEl.dataset.noPreview = 'true'; + linkButtonEl.dataset.promptSaveRedirect = action; + linkButtonEl.classList.add('o_we_button_icon'); + linkButtonEl.append(iconEl); + const projectRowEl = document.createElement('we-row'); + projectRowEl.append(element); + projectRowEl.append(linkButtonEl); + return projectRowEl; + }, /** * Apply the model on the form changing it's fields * @@ -706,6 +783,15 @@ options.registry.WebsiteFormEditor = FormEditor.extend({ field.querySelector('.s_website_form_label').appendChild(span); }); }, + /** + * Redirects the user to the page of a specified action. + * + * @private + * @param {string} action + */ + _redirectToAction: function (action) { + window.location.replace(`/web#action=${action}`); + }, //-------------------------------------------------------------------------- // Handlers diff --git a/addons/website_form_project/static/src/js/website_form_project_editor.js b/addons/website_form_project/static/src/js/website_form_project_editor.js index 92b523251f4..df3f9514558 100644 --- a/addons/website_form_project/static/src/js/website_form_project_editor.js +++ b/addons/website_form_project/static/src/js/website_form_project_editor.js @@ -28,6 +28,7 @@ FormEditorRegistry.add('create_task', { type: 'many2one', relation: 'project.project', string: _t('Project'), + createAction: 'project.open_view_project_all', }], });