Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
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.
7
//
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.
12
//
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/>.
15
 
16
/**
17
 * Create a modal.
18
 *
19
 * @module     core/modal_factory
20
 * @copyright  2016 Ryan Wyllie <ryan@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @deprecated since Moodle 4.3
23
 * @todo       Final deprecation in Moodle 4.7/5.2. See MDL-79128/
24
 */
25
 
26
import $ from 'jquery';
27
import ModalEvents from 'core/modal_events';
28
import * as ModalRegistry from 'core/modal_registry';
29
import Modal from 'core/modal';
30
import ModalSaveCancel from 'core/modal_save_cancel';
31
import ModalDeleteCancel from 'core/modal_delete_cancel';
32
import ModalCancel from 'core/modal_cancel';
33
import ModalAlert from 'core/local/modal/alert';
34
import * as Notification from 'core/notification';
35
import * as CustomEvents from 'core/custom_interaction_events';
36
import Pending from 'core/pending';
37
 
38
/**
39
 * The available standard modals.
40
 *
41
 * @property {String} DEFAULT The default modal
42
 * @property {String} SAVE_CANCEL A modal which can be used to either save, or cancel.
43
 * @property {String} DELETE_CANCEL A modal which can be used to either delete, or cancel.
44
 * @property {String} CANCEL A modal which displayed a cancel button
45
 * @property {String} ALERT An information modal which only displays information.
46
 */
47
export const types = {
48
    DEFAULT: 'DEFAULT',
49
    SAVE_CANCEL: ModalSaveCancel.TYPE,
50
    DELETE_CANCEL: ModalDeleteCancel.TYPE,
51
    CANCEL: ModalCancel.TYPE,
52
    ALERT: ModalAlert.TYPE,
53
};
54
 
55
// Most modals are self-registering.
56
// We do not self-register the base Modal because we do not want to define a default TYPE
57
// on the class that every other modal extends.
58
ModalRegistry.register(types.DEFAULT, Modal, Modal.TEMPLATE);
59
 
60
/**
61
 * Set up the events required to show the modal and return focus when the modal
62
 * is closed.
63
 *
64
 * @method setUpTrigger
65
 * @private
66
 * @param {Promise} modalPromise The modal instance
67
 * @param {object} triggerElement The jQuery element to open the modal
68
 * @param {object} modalConfig The modal configuration given to the factory
69
 */
70
const setUpTrigger = (modalPromise, triggerElement, modalConfig) => {
71
    // The element that actually shows the modal.
72
    let actualTriggerElement = null;
73
    // Check if the client has provided a callback function to be called
74
    // before the modal is displayed.
75
    const hasPreShowCallback = (typeof modalConfig.preShowCallback == 'function');
76
    // Function to handle the trigger element being activated.
77
    const triggeredCallback = (e, data) => {
78
        const pendingPromise = new Pending('core/modal_factory:setUpTrigger:triggeredCallback');
79
        actualTriggerElement = $(e.currentTarget);
80
 
81
        // eslint-disable-next-line promise/catch-or-return
82
        modalPromise.then(function(modal) {
83
            if (hasPreShowCallback) {
84
                // If the client provided a pre-show callback then execute
85
                // it now before showing the modal.
86
                modalConfig.preShowCallback(actualTriggerElement, modal);
87
            }
88
 
89
            modal.show();
90
 
91
            return modal;
92
        })
93
        .then(pendingPromise.resolve);
94
        data.originalEvent.preventDefault();
95
    };
96
 
97
    // The trigger element can either be a single element or it can be an
98
    // element + selector pair to create a delegated event handler to trigger
99
    // the modal.
100
    if (Array.isArray(triggerElement)) {
101
        const selector = triggerElement[1];
102
        triggerElement = triggerElement[0];
103
 
104
        CustomEvents.define(triggerElement, [CustomEvents.events.activate]);
105
        triggerElement.on(CustomEvents.events.activate, selector, triggeredCallback);
106
    } else {
107
        CustomEvents.define(triggerElement, [CustomEvents.events.activate]);
108
        triggerElement.on(CustomEvents.events.activate, triggeredCallback);
109
    }
110
 
111
    // eslint-disable-next-line promise/catch-or-return
112
    modalPromise.then(function(modal) {
113
        modal.getRoot().on(ModalEvents.hidden, function() {
114
            // Focus on the trigger element that actually launched the modal.
115
            if (actualTriggerElement !== null) {
116
                actualTriggerElement.focus();
117
            }
118
        });
119
 
120
        return modal;
121
    });
122
};
123
 
124
/**
125
 * Create a Modal instance.
126
 *
127
 * @method create
128
 * @param {object} modalConfig The configuration to create the modal instance
129
 * @param {object} triggerElement The trigger HTML jQuery object
130
 * @return {promise} Resolved with a Modal instance
131
 */
132
export const create = (modalConfig, triggerElement) => {
133
    window.console.warn(
134
        'The modal_factory has been deprecated since Moodle 4.3. Please use the create method on your target modal type instead.',
135
    );
136
    // Use of the triggerElement has been deprecated.
137
    const type = modalConfig.type || types.DEFAULT;
138
 
139
    const registryConf = ModalRegistry.get(type);
140
    if (!registryConf) {
141
        Notification.exception({message: `Unable to find modal of type: ${type}`});
142
    }
143
 
144
    const modal = registryConf.module.create(modalConfig);
145
 
146
    if (triggerElement) {
147
        window.console.warn(
148
            'The triggerElement feature of the modal_factory has been deprecated. Please use event listeners instead.',
149
        );
150
        setUpTrigger(modal, triggerElement, modalConfig);
151
    }
152
 
153
    return $.when(new Promise((resolve, reject) => {
154
        modal
155
            .then(resolve)
156
            .catch(reject);
157
    }));
158
};
159
 
160
export default {
161
    create,
162
    types,
163
};