From f9e3953f6867727913696daa682f68c8ff5495bc Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 24 Aug 2022 16:39:59 +0300 Subject: [PATCH] record modal refactor --- client/src/helpers/record-modal.js | 98 +++++++++++++++++++ .../email/fields/from-address-varchar.js | 44 ++------- client/src/views/fields/link-multiple.js | 27 +---- client/src/views/fields/link-parent.js | 27 +---- client/src/views/fields/link.js | 27 +---- client/src/views/record/list.js | 67 +++++-------- 6 files changed, 140 insertions(+), 150 deletions(-) create mode 100644 client/src/helpers/record-modal.js diff --git a/client/src/helpers/record-modal.js b/client/src/helpers/record-modal.js new file mode 100644 index 0000000000..8d5ff90a48 --- /dev/null +++ b/client/src/helpers/record-modal.js @@ -0,0 +1,98 @@ +/************************************************************************ + * This file is part of EspoCRM. + * + * EspoCRM - Open Source CRM application. + * Copyright (C) 2014-2022 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko + * Website: https://www.espocrm.com + * + * EspoCRM is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EspoCRM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EspoCRM. If not, see http://www.gnu.org/licenses/. + * + * The interactive user interfaces in modified source and object code versions + * of this program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU General Public License version 3. + * + * In accordance with Section 7(b) of the GNU General Public License version 3, + * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. + ************************************************************************/ + +/** + * @module helpers/record-modal + */ +define(() => { + + /** + * @memberOf module:helpers/record-modal + */ + class Class { + /** + * @param {module:metadata.Class} metadata + * @param {module:acl-manager.Class} acl + */ + constructor(metadata, acl) { + this.metadata = metadata; + this.acl = acl; + } + + /** + * @param {module:view.Class} view + * @param {{ + * id: string, + * scope: string, + * model?: module:model.Class, + * editDisabled?: boolean, + * rootUrl?: string, + * }} params + * @return {Promise} + */ + showDetail(view, params) { + let id = params.id; + let scope = params.scope; + let model = params.model; + + if (!id || !scope) { + console.error("Bad data."); + + return Promise.reject(); + } + + if (!this.acl.checkScope(scope, 'read')) { + return Promise.reject(); + } + + let viewName = this.metadata.get(['clientDefs', scope, 'modalViews', 'detail']) || + 'views/modals/detail'; + + Espo.Ui.notify(view.translate('loading', 'messages')); + + let options = { + scope: scope, + model: model, + id: id, + quickEditDisabled: params.editDisabled, + rootUrl: params.rootUrl, + }; + + return view.createView('modal', viewName, options, modalView => { + modalView.render() + .then(() => Espo.Ui.notify(false)); + + view.listenToOnce(modalView, 'remove', () => { + view.clearView('modal'); + }); + }); + } + } + + return Class; +}); diff --git a/client/src/views/email/fields/from-address-varchar.js b/client/src/views/email/fields/from-address-varchar.js index 80f2d05316..9eb3c5e619 100644 --- a/client/src/views/email/fields/from-address-varchar.js +++ b/client/src/views/email/fields/from-address-varchar.js @@ -28,8 +28,8 @@ define( 'views/email/fields/from-address-varchar', - ['views/fields/base', 'views/email/fields/email-address'], - function (Dep, EmailAddress) { + ['views/fields/base', 'views/email/fields/email-address', 'helpers/record-modal'], + function (Dep, EmailAddress, RecordModal) { return Dep.extend({ @@ -515,43 +515,11 @@ define( }, quickView: function (data) { - data = data || {}; + let helper = new RecordModal(this.getMetadata(), this.getAcl()); - let id = data.id; - let scope = data.scope; - - if (!id) { - console.error("No id."); - - return; - } - - if (!scope) { - console.error("No scope."); - - return; - } - - let viewName = this.getMetadata().get(['clientDefs', scope, 'modalViews', 'detail']) || - 'views/modals/detail'; - - Espo.Ui.notify(this.translate('loading', 'messages')); - - let options = { - scope: scope, - id: id, - }; - - this.createView('modal', viewName, options, view => { - this.listenToOnce(view, 'after:render', () => { - Espo.Ui.notify(false); - }); - - view.render(); - - this.listenToOnce(view, 'remove', () => { - this.clearView('modal'); - }); + helper.showDetail(this, { + id: data.id, + scope: data.scope, }); }, }); diff --git a/client/src/views/fields/link-multiple.js b/client/src/views/fields/link-multiple.js index dccbb94b47..a43286322f 100644 --- a/client/src/views/fields/link-multiple.js +++ b/client/src/views/fields/link-multiple.js @@ -26,7 +26,7 @@ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ -define('views/fields/link-multiple', ['views/fields/base'], function (Dep) { +define('views/fields/link-multiple', ['views/fields/base', 'helpers/record-modal'], function (Dep, RecordModal) { /** * A link-multiple field (has-many relation). @@ -894,30 +894,11 @@ define('views/fields/link-multiple', ['views/fields/base'], function (Dep) { quickView: function (id) { let entityType = this.foreignScope; - if (!this.getAcl().checkScope(entityType, 'read')) { - return; - } + let helper = new RecordModal(this.getMetadata(), this.getAcl()); - let viewName = this.getMetadata().get(['clientDefs', entityType, 'modalViews', 'detail']) || - 'views/modals/detail'; - - Espo.Ui.notify(this.translate('loading', 'messages')); - - let options = { - scope: entityType, + helper.showDetail(this, { id: id, - }; - - this.createView('dialog', viewName, options, view => { - this.listenToOnce(view, 'after:render', () => { - Espo.Ui.notify(false); - }); - - view.render(); - - this.listenToOnce(view, 'remove', () => { - this.clearView('dialog'); - }); + scope: entityType, }); }, }); diff --git a/client/src/views/fields/link-parent.js b/client/src/views/fields/link-parent.js index d9fc498a31..77f5b41025 100644 --- a/client/src/views/fields/link-parent.js +++ b/client/src/views/fields/link-parent.js @@ -26,7 +26,7 @@ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ -define('views/fields/link-parent', ['views/fields/base'], function (Dep) { +define('views/fields/link-parent', ['views/fields/base', 'helpers/record-modal'], function (Dep, RecordModal) { /** * A link-parent field (belongs-to-parent relation). @@ -722,30 +722,11 @@ define('views/fields/link-parent', ['views/fields/base'], function (Dep) { return; } - if (!this.getAcl().checkScope(entityType, 'read')) { - return; - } + let helper = new RecordModal(this.getMetadata(), this.getAcl()); - let viewName = this.getMetadata().get(['clientDefs', entityType, 'modalViews', 'detail']) || - 'views/modals/detail'; - - Espo.Ui.notify(this.translate('loading', 'messages')); - - let options = { - scope: entityType, + helper.showDetail(this, { id: id, - }; - - this.createView('dialog', viewName, options, view => { - this.listenToOnce(view, 'after:render', () => { - Espo.Ui.notify(false); - }); - - view.render(); - - this.listenToOnce(view, 'remove', () => { - this.clearView('dialog'); - }); + scope: entityType, }); }, }); diff --git a/client/src/views/fields/link.js b/client/src/views/fields/link.js index 655ee4e61f..184837cf08 100644 --- a/client/src/views/fields/link.js +++ b/client/src/views/fields/link.js @@ -26,7 +26,7 @@ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ -define('views/fields/link', ['views/fields/base'], function (Dep) { +define('views/fields/link', ['views/fields/base', 'helpers/record-modal'], function (Dep, RecordModal) { /** * A link field (belongs-to relation). @@ -978,30 +978,11 @@ define('views/fields/link', ['views/fields/base'], function (Dep) { let entityType = this.foreignScope; - if (!this.getAcl().checkScope(entityType, 'read')) { - return; - } + let helper = new RecordModal(this.getMetadata(), this.getAcl()); - let viewName = this.getMetadata().get(['clientDefs', entityType, 'modalViews', 'detail']) || - 'views/modals/detail'; - - Espo.Ui.notify(this.translate('loading', 'messages')); - - let options = { - scope: entityType, + helper.showDetail(this, { id: id, - }; - - this.createView('dialog', viewName, options, view => { - this.listenToOnce(view, 'after:render', () => { - Espo.Ui.notify(false); - }); - - view.render(); - - this.listenToOnce(view, 'remove', () => { - this.clearView('dialog'); - }); + scope: entityType, }); }, }); diff --git a/client/src/views/record/list.js b/client/src/views/record/list.js index b5fe92cfb9..1e5939d73f 100644 --- a/client/src/views/record/list.js +++ b/client/src/views/record/list.js @@ -26,8 +26,8 @@ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ -define('views/record/list', ['view', 'helpers/mass-action', 'helpers/export'], -function (Dep, MassActionHelper, ExportHelper) { +define('views/record/list', ['view', 'helpers/mass-action', 'helpers/export', 'helpers/record-modal'], +function (Dep, MassActionHelper, ExportHelper, RecordModal) { /** * A record-list view. Renders and processes list items, actions. @@ -2879,51 +2879,32 @@ function (Dep, MassActionHelper, ExportHelper) { return; } - let viewName = this.getMetadata().get(['clientDefs', scope, 'modalViews', 'detail']) || - 'views/modals/detail'; - - if (!this.quickDetailDisabled) { - Espo.Ui.notify(this.translate('loading', 'messages')); - - let options = { - scope: scope, - model: model, - id: id, - quickEditDisabled: this.quickEditDisabled, - }; - - if (this.options.keepCurrentRootUrl) { - options.rootUrl = this.getRouter().getCurrentUrl(); - } - - this.createView('modal', viewName, options, view => { - this.listenToOnce(view, 'after:render', () => { - Espo.Ui.notify(false); - }); - - view.render(); - - this.listenToOnce(view, 'remove', () => { - this.clearView('modal'); - }); - - if (!model) { - return; - } - - /*this.listenToOnce(view, 'after:edit-cancel', () => { - this.actionQuickView({id: view.model.id, scope: view.model.name}); - });*/ - - this.listenToOnce(view, 'after:save', (model) => { - this.trigger('after:save', model); - }); - }); + if (this.quickDetailDisabled) { + this.getRouter().navigate('#' + scope + '/view/' + id, {trigger: true}); return; } - this.getRouter().navigate('#' + scope + '/view/' + id, {trigger: true}); + let helper = new RecordModal(this.getMetadata(), this.getAcl()); + + helper + .showDetail(this, { + id: id, + scope: scope, + model: model, + rootUrl: this.options.keepCurrentRootUrl ? + this.getRouter().getCurrentUrl() : null, + editDisabled: this.quickEditDisabled, + }) + .then(view => { + if (!model) { + return; + } + + this.listenTo(view, 'after:save', model => { + this.trigger('after:save', model); + }); + }); }, actionQuickEdit: function (data) {