MDL-79762 core: Add possibility to define modal type for modalform

If `moduleName` param is passed in config object for modalform, it will be
used to instantiate modal class, otherwise 'core/modal_save_cancel' is
used.

Co-authored-by: Marina Glancy <marina@moodle.com>
This commit is contained in:
Ruslan Kabalin 2023-10-19 11:28:51 +01:00
parent d76e211be6
commit afe7f76e14
4 changed files with 32 additions and 6 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -43,7 +43,6 @@ import ModalEvents from 'core/modal_events';
import Notification from 'core/notification'; import Notification from 'core/notification';
import Pending from 'core/pending'; import Pending from 'core/pending';
import {serialize} from './util'; import {serialize} from './util';
import SaveCancelModal from 'core/modal_save_cancel';
export default class ModalForm { export default class ModalForm {
@ -89,8 +88,9 @@ export default class ModalForm {
* *
* @param {Object} config parameters for the form and modal dialogue: * @param {Object} config parameters for the form and modal dialogue:
* @paramy {String} config.formClass PHP class name that handles the form (should extend \core_form\modal ) * @paramy {String} config.formClass PHP class name that handles the form (should extend \core_form\modal )
* @paramy {Object} config.modalConfig modal config - title, type, etc. * @paramy {String} config.moduleName module name to use if different to core/modal_save_cancel (optional)
* Default: {removeOnClose: true, type: ModalFactory.types.SAVE_CANCEL} * @paramy {Object} config.modalConfig modal config - title, header, footer, etc.
* Default: {removeOnClose: true, large: true}
* @paramy {Object} config.args Arguments for the initial form rendering (for example, id of the edited entity) * @paramy {Object} config.args Arguments for the initial form rendering (for example, id of the edited entity)
* @paramy {String} config.saveButtonText the text to display on the Modal "Save" button (optional) * @paramy {String} config.saveButtonText the text to display on the Modal "Save" button (optional)
* @paramy {String} config.saveButtonClasses additional CSS classes for the Modal "Save" button * @paramy {String} config.saveButtonClasses additional CSS classes for the Modal "Save" button
@ -108,6 +108,28 @@ export default class ModalForm {
this.futureListeners = []; this.futureListeners = [];
} }
/**
* Loads the modal module and creates an instance
*
* @returns {Promise}
*/
getModalModule() {
if (!this.config.moduleName && this.config.modalConfig.type && this.config.modalConfig.type !== 'SAVE_CANCEL') {
// Legacy loader for plugins that were not updated with Moodle 4.3 changes.
window.console.warn(
'Passing config.modalConfig.type to ModalForm has been deprecated since Moodle 4.3. ' +
'Please pass config.modalName instead with the full module name.',
);
return import('core/modal_factory')
.then((ModalFactory) => ModalFactory.create(this.config.modalConfig));
} else {
// New loader for Moodle 4.3 and above.
const moduleName = this.config.moduleName ?? 'core/modal_save_cancel';
return import(moduleName)
.then((module) => module.create(this.config.modalConfig));
}
}
/** /**
* Initialise the modal and shows it * Initialise the modal and shows it
* *
@ -116,7 +138,7 @@ export default class ModalForm {
show() { show() {
const pendingPromise = new Pending('core_form/modalform:init'); const pendingPromise = new Pending('core_form/modalform:init');
return SaveCancelModal.create(this.config.modalConfig) return this.getModalModule()
.then((modal) => { .then((modal) => {
this.modal = modal; this.modal = modal;

View File

@ -1,6 +1,10 @@
This files describes API changes in core libraries and APIs, This files describes API changes in core libraries and APIs,
information provided here is intended especially for developers. information provided here is intended especially for developers.
=== 4.3.1 ===
* Added modalform config object `moduleName` param that can be used to define alternative modal type for the modalform. By default 'core/modal_save_cancel' is used.
=== 4.3 === === 4.3 ===
* Unnecessary parameters of admin_apply_default_settings() function were removed; upgrade script lists * Unnecessary parameters of admin_apply_default_settings() function were removed; upgrade script lists