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 |
* A registry for the different types of modal.
|
|
|
18 |
*
|
|
|
19 |
* @module core/modal_registry
|
|
|
20 |
* @class modal_registry
|
|
|
21 |
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
import * as Notification from 'core/notification';
|
|
|
25 |
import * as Prefetch from 'core/prefetch';
|
|
|
26 |
|
|
|
27 |
// A singleton registry for all modules to access. Allows types to be
|
|
|
28 |
// added at runtime.
|
|
|
29 |
const registry = new Map();
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Get a registered type of modal.
|
|
|
33 |
*
|
|
|
34 |
* @method get
|
|
|
35 |
* @param {string} type The type of modal to get
|
|
|
36 |
* @return {object} The registered config for the modal
|
|
|
37 |
*/
|
|
|
38 |
export const get = (type) => registry.get(type);
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Register a modal with the registry.
|
|
|
42 |
*
|
|
|
43 |
* @method register
|
|
|
44 |
* @param {string} type The type of modal (must be unique)
|
|
|
45 |
* @param {function} module The modal module (must be a constructor function of type core/modal)
|
|
|
46 |
* @param {string} template The template name of the modal
|
|
|
47 |
*/
|
|
|
48 |
export const register = (type, module, template) => {
|
|
|
49 |
const existing = get(type);
|
|
|
50 |
if (existing && existing.module !== module) {
|
|
|
51 |
Notification.exception({
|
|
|
52 |
message: `Modal of type '${type}' is already registered`,
|
|
|
53 |
});
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
if (!module || typeof module !== 'function') {
|
|
|
57 |
Notification.exception({message: "You must provide a modal module"});
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
if (!template) {
|
|
|
61 |
Notification.exception({message: "You must provide a modal template"});
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
registry.set(type, {module, template});
|
|
|
65 |
|
|
|
66 |
// Prefetch the template.
|
|
|
67 |
Prefetch.prefetchTemplate(template);
|
|
|
68 |
};
|
|
|
69 |
|
|
|
70 |
export default {
|
|
|
71 |
register,
|
|
|
72 |
get,
|
|
|
73 |
};
|