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
 * Icon System base module.
18
 *
19
 * @module core/icon_system
20
 * @copyright  2017 Damyon Wiese
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import config from 'core/config';
25
 
26
/**
27
 * Icon System abstract class.
28
 *
29
 * Any icon system needs to define a module extending this one and return this module name from the php icon_system class.
30
 *
31
 * @class core/icon_system
32
 */
33
export default class IconSystem {
34
    /**
35
     * A Promise which resolves to the Icon System instance.
36
     *
37
     * @private
38
     * @var {Promise<IconSystem>}
39
     */
40
    static iconSystemInstance = null;
41
 
42
    /**
43
     * Factory method to fetch the Icon System currently in use.
44
     *
45
     * @returns {Promise<IconSystem>}
46
     */
47
    static async instance() {
48
        if (this.iconSystemInstance) {
49
            return await this.iconSystemInstance;
50
        }
51
 
52
        this.iconSystemInstance = (async () => {
53
            const SystemClass = await import(config.iconsystemmodule);
54
            const instance = new SystemClass();
55
            if (!(instance instanceof IconSystem)) {
56
                window.console.error('Class is not an IconSystem', SystemClass);
57
                throw Error(`Invalid icon system specified ${config.iconsystemmodule}. Class is not an IconSystem.`);
58
            }
59
 
60
            return await instance.init();
61
        })();
62
 
63
        return await this.iconSystemInstance;
64
    }
65
 
66
    /**
67
     * Initialise the icon system.
68
     *
69
     * @return {Promise<IconSystem>}
70
     */
71
    init() {
72
        return Promise.resolve(this);
73
    }
74
 
75
    /**
76
     * Render an icon.
77
     *
78
     * The key, component and title come from either the pix mustache helper tag, or the call to templates.renderIcon.
79
     * The template is the pre-loaded template string matching the template from getTemplateName() in this class.
80
     * This function must return a string (not a promise) because it is used during the internal rendering of the mustache
81
     * template (which is unfortunately synchronous). To render the mustache template in this function call
82
     * core/mustache.render() directly and do not use any partials, blocks or helper functions in the template.
83
     *
84
     * @param {string} key
85
     * @param {string} component
86
     * @param {string} title
87
     * @param {string} template
88
     * @returns {string}
89
     * @method renderIcon
90
     */
91
    // eslint-disable-next-line no-unused-vars
92
    renderIcon(key, component, title, template) {
93
        throw new Error('Abstract function not implemented.');
94
    }
95
 
96
    /**
97
     * Get the name of the template.
98
     *
99
     * @returns {string}
100
     * @method getTemplateName
101
     */
102
    // eslint-disable-next-line no-unused-vars
103
    getTemplateName() {
104
        throw new Error('Abstract function not implemented.');
105
    }
106
}