1 // This file is part of Moodle - http://moodle.org/
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * @module core/modal_factory
20 * @class modal_factory
22 * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 define(['jquery', 'core/modal_events', 'core/modal_registry', 'core/modal',
26 'core/modal_save_cancel', 'core/modal_confirm', 'core/modal_cancel',
27 'core/templates', 'core/notification', 'core/custom_interaction_events'],
28 function($, ModalEvents, ModalRegistry, Modal, ModalSaveCancel, ModalConfirm,
29 ModalCancel, Templates, Notification, CustomEvents) {
31 // The templates for each type of modal.
33 DEFAULT: 'core/modal',
34 SAVE_CANCEL: 'core/modal_save_cancel',
35 CONFIRM: 'core/modal_confirm',
36 CANCEL: 'core/modal_cancel',
39 // The available types of modals.
42 SAVE_CANCEL: 'SAVE_CANCEL',
47 // Register the common set of modals.
48 ModalRegistry.register(TYPES.DEFAULT, Modal, TEMPLATES.DEFAULT);
49 ModalRegistry.register(TYPES.SAVE_CANCEL, ModalSaveCancel, TEMPLATES.SAVE_CANCEL);
50 ModalRegistry.register(TYPES.CONFIRM, ModalConfirm, TEMPLATES.CONFIRM);
51 ModalRegistry.register(TYPES.CANCEL, ModalCancel, TEMPLATES.CANCEL);
54 * Set up the events required to show the modal and return focus when the modal
57 * @method setUpTrigger
58 * @param {object} modal The modal instance
59 * @param {object} triggerElement The jQuery element to open the modal
61 var setUpTrigger = function(modal, triggerElement) {
62 if (typeof triggerElement != 'undefined') {
63 if (Array.isArray(triggerElement)) {
64 var selector = triggerElement[1];
65 triggerElement = triggerElement[0];
67 CustomEvents.define(triggerElement, [CustomEvents.events.activate]);
68 triggerElement.on(CustomEvents.events.activate, selector, function(e, data) {
70 data.originalEvent.preventDefault();
73 CustomEvents.define(triggerElement, [CustomEvents.events.activate]);
74 triggerElement.on(CustomEvents.events.activate, function(e, data) {
76 data.originalEvent.preventDefault();
80 modal.getRoot().on(ModalEvents.hidden, function() {
81 triggerElement.focus();
87 * Create the correct instance of a modal based on the givem type. Sets up
88 * the trigger between the modal and the trigger element.
90 * @method createFromElement
91 * @param {object} registryConf A config from the ModalRegistry
92 * @param {object} modalElement The modal HTML jQuery object
93 * @param {object} triggerElement The trigger HTML jQuery object
94 * @return {object} Modal instance
96 var createFromElement = function(registryConf, modalElement, triggerElement) {
97 modalElement = $(modalElement);
98 var module = registryConf.module;
99 var modal = new module(modalElement);
100 setUpTrigger(modal, triggerElement);
106 * Create the correct modal instance for the given type, including loading
107 * the correct template and setting up the trigger relationship with the
110 * @method createFromType
111 * @param {object} registryConf A config from the ModalRegistry
112 * @param {object} triggerElement The trigger HTML jQuery object
113 * @return {promise} Resolved with a Modal instance
115 var createFromType = function(registryConf, templateContext, triggerElement) {
116 var templateName = registryConf.template;
118 return Templates.render(templateName, templateContext)
119 .then(function(html) {
120 var modalElement = $(html);
121 return createFromElement(registryConf, modalElement, triggerElement);
123 .fail(Notification.exception);
127 * Create a Modal instance.
130 * @param {object} modalConfig The configuration to create the modal instance
131 * @param {object} triggerElement The trigger HTML jQuery object
132 * @return {promise} Resolved with a Modal instance
134 var create = function(modalConfig, triggerElement) {
135 var type = modalConfig.type || TYPES.DEFAULT;
136 var isLarge = modalConfig.large ? true : false;
137 var registryConf = null;
138 var templateContext = {};
140 registryConf = ModalRegistry.get(type);
143 Notification.exception({message: 'Unable to find modal of type: ' + type});
146 if (typeof modalConfig.templateContext != 'undefined') {
147 templateContext = modalConfig.templateContext;
150 return createFromType(registryConf, templateContext, triggerElement)
151 .then(function(modal) {
152 if (typeof modalConfig.title != 'undefined') {
153 modal.setTitle(modalConfig.title);
156 if (typeof modalConfig.body != 'undefined') {
157 modal.setBody(modalConfig.body);
160 if (typeof modalConfig.footer != 'undefined') {
161 modal.setFooter(modalConfig.footer);