MDL-40759 icons: Refactor to allow theme icon systems
[moodle.git] / lib / amd / src / icon_system_fontawesome.js
CommitLineData
95b06c13
DW
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 * Competency rule points module.
18 *
19 * @package core
20 * @copyright 2017 Damyon Wiese
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24define(['core/icon_system', 'jquery', 'core/ajax', 'core/mustache', 'core/localstorage'],
25 function(IconSystem, $, Ajax, Mustache, LocalStorage) {
26
27 var staticMap = null;
28 var fetchMap = null;
29
30 /**
31 * IconSystemFontawesome
32 */
33 var IconSystemFontawesome = function() {
34 IconSystem.apply(this, arguments);
35 };
36 IconSystemFontawesome.prototype = Object.create(IconSystem.prototype);
37
e330b1c2
DW
38 /**
39 * Prefetch resources so later calls to renderIcon can be resolved synchronously.
40 *
41 * @method init
42 * @return Promise
43 */
95b06c13
DW
44 IconSystemFontawesome.prototype.init = function() {
45 if (staticMap) {
e330b1c2 46 return $.when(this);
95b06c13
DW
47 }
48
49 var map = LocalStorage.get('core/iconmap-fontawesome');
7776c9e1
DW
50 if (map) {
51 map = JSON.parse(map);
52 }
95b06c13
DW
53
54 if (map) {
55 staticMap = map;
e330b1c2 56 return $.when(this);
95b06c13
DW
57 }
58
59 if (fetchMap === null) {
60 fetchMap = Ajax.call([{
e330b1c2
DW
61 methodname: 'core_output_load_fontawesome_icon_map',
62 args: []
95b06c13
DW
63 }], true, false)[0];
64 }
65
66 return fetchMap.then(function(map) {
67 staticMap = {};
68 $.each(map, function(index, value) {
69 staticMap[value.component + '/' + value.pix] = value.to;
70 }.bind(this));
7776c9e1 71 LocalStorage.set('core/iconmap-fontawesome', JSON.stringify(staticMap));
e330b1c2 72 return this;
95b06c13
DW
73 }.bind(this));
74 };
75
76 /**
77 * Render an icon.
78 *
79 * @param {String} key
80 * @param {String} component
81 * @param {String} title
82 * @param {String} template
83 * @return {String}
84 * @method renderIcon
85 */
86 IconSystemFontawesome.prototype.renderIcon = function(key, component, title, template) { //eslint-disable-line no-unused-vars
87 var mappedIcon = staticMap[component + '/' + key];
88 if (typeof mappedIcon === "undefined") {
89 mappedIcon = component + '/' + key;
90 }
91
92 var context = {
93 key: mappedIcon,
94 title: title
95 };
96
97 return Mustache.render(template, context);
98 };
99
e330b1c2
DW
100 /**
101 * Get the name of the template to pre-cache for this icon system.
102 *
103 * @return {String}
104 * @method getTemplateName
105 */
106 IconSystemFontawesome.prototype.getTemplateName = function() {
107 return 'core/pix_icon_fontawesome';
108 };
109
95b06c13
DW
110 return /** @alias module:core/icon_system_fontawesome */ IconSystemFontawesome;
111
112});