MDL-78316 core: Convert IconSystems to ESM

This commit is contained in:
Andrew Nicols 2023-05-19 10:51:14 +08:00
parent ef93325f27
commit c0043004e1
No known key found for this signature in database
GPG Key ID: 6D1E3157C8CFBF14
9 changed files with 193 additions and 163 deletions

View File

@ -1,10 +1,3 @@
/**
* Icon System base module.
*
* @module core/icon_system
* @copyright 2017 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define("core/icon_system",["jquery"],(function($){var IconSystem=function(){};return IconSystem.prototype.init=function(){return $.when(this)},IconSystem.prototype.renderIcon=function(key,component,title,template){throw new Error("Abstract function not implemented.")},IconSystem.prototype.getTemplateName=function(){throw new Error("Abstract function not implemented.")},IconSystem}));
define("core/icon_system",["exports","core/config"],(function(_exports,_config){var obj;Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.default=void 0,_config=(obj=_config)&&obj.__esModule?obj:{default:obj};var _systemImportTransformerGlobalIdentifier="undefined"!=typeof window?window:"undefined"!=typeof self?self:"undefined"!=typeof global?global:{};class IconSystem{static async instance(){return this.iconSystemInstance||(this.iconSystemInstance=new Promise((async(resolve,reject)=>{const SystemClass=await("function"==typeof _systemImportTransformerGlobalIdentifier.define&&_systemImportTransformerGlobalIdentifier.define.amd?new Promise((function(resolve,reject){_systemImportTransformerGlobalIdentifier.require([_config.default.iconsystemmodule],resolve,reject)})):"undefined"!=typeof module&&module.exports&&"undefined"!=typeof require||"undefined"!=typeof module&&module.component&&_systemImportTransformerGlobalIdentifier.require&&"component"===_systemImportTransformerGlobalIdentifier.require.loader?Promise.resolve(require(_config.default.iconsystemmodule)):Promise.resolve(_systemImportTransformerGlobalIdentifier[_config.default.iconsystemmodule])),instance=new SystemClass;if(!(instance instanceof IconSystem))throw Error("Invalid icon system specified ".concat(_config.default.iconsystemmodule,". Class is not an IconSystem."),SystemClass);try{resolve(instance.init())}catch(error){reject(error)}}))),await this.iconSystemInstance}init(){return Promise.resolve(this)}renderIcon(key,component,title,template){throw new Error("Abstract function not implemented.")}getTemplateName(){throw new Error("Abstract function not implemented.")}}return _exports.default=IconSystem,function(obj,key,value){key in obj?Object.defineProperty(obj,key,{value:value,enumerable:!0,configurable:!0,writable:!0}):obj[key]=value}(IconSystem,"iconSystemInstance",null),_exports.default}));
//# sourceMappingURL=icon_system.min.js.map

View File

@ -1 +1 @@
{"version":3,"file":"icon_system.min.js","sources":["../src/icon_system.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Icon System base module.\n *\n * @module core/icon_system\n * @copyright 2017 Damyon Wiese\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\ndefine(['jquery'], function($) {\n\n /**\n * Icon System abstract class.\n *\n * Any icon system needs to define a module extending this one and return this module name from the php icon_system class.\n *\n * @class core/icon_system\n */\n var IconSystem = function() {\n };\n\n /**\n * Initialise the icon system.\n *\n * @return {Promise}\n * @method init\n */\n IconSystem.prototype.init = function() {\n return $.when(this);\n };\n\n /**\n * Render an icon.\n *\n * The key, component and title come from either the pix mustache helper tag, or the call to templates.renderIcon.\n * The template is the pre-loaded template string matching the template from getTemplateName() in this class.\n * This function must return a string (not a promise) because it is used during the internal rendering of the mustache\n * template (which is unfortunately synchronous). To render the mustache template in this function call\n * core/mustache.render() directly and do not use any partials, blocks or helper functions in the template.\n *\n * @param {String} key\n * @param {String} component\n * @param {String} title\n * @param {String} template\n * @return {String}\n * @method renderIcon\n */\n IconSystem.prototype.renderIcon = function(key, component, title, template) { // eslint-disable-line no-unused-vars\n throw new Error('Abstract function not implemented.');\n };\n\n /**\n * getTemplateName\n *\n * @return {String}\n * @method getTemplateName\n */\n IconSystem.prototype.getTemplateName = function() {\n throw new Error('Abstract function not implemented.');\n };\n\n return /** @alias module:core/icon_system */ IconSystem;\n});\n"],"names":["define","$","IconSystem","prototype","init","when","this","renderIcon","key","component","title","template","Error","getTemplateName"],"mappings":";;;;;;;AAuBAA,0BAAO,CAAC,WAAW,SAASC,OASpBC,WAAa,oBASjBA,WAAWC,UAAUC,KAAO,kBACjBH,EAAEI,KAAKC,OAmBlBJ,WAAWC,UAAUI,WAAa,SAASC,IAAKC,UAAWC,MAAOC,gBACxD,IAAIC,MAAM,uCASpBV,WAAWC,UAAUU,gBAAkB,iBAC7B,IAAID,MAAM,uCAGyBV"}
{"version":3,"file":"icon_system.min.js","sources":["../src/icon_system.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Icon System base module.\n *\n * @module core/icon_system\n * @copyright 2017 Damyon Wiese\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport config from 'core/config';\n\n/**\n * Icon System abstract class.\n *\n * Any icon system needs to define a module extending this one and return this module name from the php icon_system class.\n *\n * @class core/icon_system\n */\nexport default class IconSystem {\n /**\n * A Promise which resolves to the Icon System instance.\n *\n * @private\n * @var {Promise<IconSystem>}\n */\n static iconSystemInstance = null;\n\n /**\n * Factory method to fetch the Icon System currently in use.\n *\n * @returns {Promise<IconSystem>}\n */\n static async instance() {\n if (this.iconSystemInstance) {\n return await this.iconSystemInstance;\n }\n\n this.iconSystemInstance = new Promise(async(resolve, reject) => {\n const SystemClass = await import(config.iconsystemmodule);\n const instance = new SystemClass();\n if (!(instance instanceof IconSystem)) {\n throw Error(`Invalid icon system specified ${config.iconsystemmodule}. Class is not an IconSystem.`, SystemClass);\n }\n\n try {\n resolve(instance.init());\n } catch (error) {\n reject(error);\n }\n });\n\n return await this.iconSystemInstance;\n }\n\n /**\n * Initialise the icon system.\n *\n * @return {Promise<IconSystem>}\n */\n init() {\n return Promise.resolve(this);\n }\n\n /**\n * Render an icon.\n *\n * The key, component and title come from either the pix mustache helper tag, or the call to templates.renderIcon.\n * The template is the pre-loaded template string matching the template from getTemplateName() in this class.\n * This function must return a string (not a promise) because it is used during the internal rendering of the mustache\n * template (which is unfortunately synchronous). To render the mustache template in this function call\n * core/mustache.render() directly and do not use any partials, blocks or helper functions in the template.\n *\n * @param {string} key\n * @param {string} component\n * @param {string} title\n * @param {string} template\n * @returns {string}\n * @method renderIcon\n */\n // eslint-disable-next-line no-unused-vars\n renderIcon(key, component, title, template) {\n throw new Error('Abstract function not implemented.');\n }\n\n /**\n * Get the name of the template.\n *\n * @returns {string}\n * @method getTemplateName\n */\n // eslint-disable-next-line no-unused-vars\n getTemplateName() {\n throw new Error('Abstract function not implemented.');\n }\n}\n"],"names":["IconSystem","this","iconSystemInstance","Promise","async","resolve","reject","SystemClass","config","iconsystemmodule","instance","Error","init","error","renderIcon","key","component","title","template","getTemplateName"],"mappings":"wXAgCqBA,0CAeTC,KAAKC,0BAIJA,mBAAqB,IAAIC,SAAQC,MAAMC,QAASC,gBAC3CC,kOAA2BC,gBAAOC,4SAAPD,gBAAb,4EAAaA,gBAAOC,oBAClCC,SAAW,IAAIH,iBACfG,oBAAoBV,kBAChBW,8CAAuCH,gBAAOC,kDAAiDF,iBAIrGF,QAAQK,SAASE,QACnB,MAAOC,OACLP,OAAOO,kBAbEZ,KAAKC,mBAyB1BU,cACWT,QAAQE,QAAQJ,MAoB3Ba,WAAWC,IAAKC,UAAWC,MAAOC,gBACxB,IAAIP,MAAM,sCAUpBQ,wBACU,IAAIR,MAAM,oNA1EHX,gCAOW"}

View File

@ -1,10 +1,3 @@
/**
* Competency rule points module.
*
* @module core/icon_system_fontawesome
* @copyright 2017 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define("core/icon_system_fontawesome",["core/icon_system","jquery","core/ajax","core/mustache","core/localstorage","core/url"],(function(IconSystem,$,Ajax,Mustache,LocalStorage,Url){var staticMap=null,fetchMap=null,IconSystemFontawesome=function(){IconSystem.apply(this,arguments)};return(IconSystemFontawesome.prototype=Object.create(IconSystem.prototype)).init=function(){var currTheme=M.cfg.theme;if(staticMap)return $.when(this);var map=LocalStorage.get("core_iconsystem/theme/"+currTheme+"/core/iconmap-fontawesome");return map&&(map=JSON.parse(map)),map?(staticMap=map,$.when(this)):(null===fetchMap&&(fetchMap=Ajax.call([{methodname:"core_output_load_fontawesome_icon_system_map",args:{themename:M.cfg.theme}}],!0,!1,!1,0,M.cfg.themerev)[0]),fetchMap.then(function(map){return staticMap={},$.each(map,(function(index,value){staticMap[value.component+"/"+value.pix]=value.to})),LocalStorage.set("core_iconsystem/theme/"+currTheme+"/core/iconmap-fontawesome",JSON.stringify(staticMap)),this}.bind(this)))},IconSystemFontawesome.prototype.renderIcon=function(key,component,title,template){var mappedIcon=staticMap[component+"/"+key],unmappedIcon=!1;void 0===mappedIcon&&(unmappedIcon={attributes:[{name:"src",value:Url.imageUrl(key,component)},{name:"alt",value:title},{name:"title",value:title}]});var context={key:mappedIcon,title:title,alt:title,unmappedIcon:unmappedIcon};return void 0!==title&&""!==title||(context["aria-hidden"]=!0),Mustache.render(template,context).trim()},IconSystemFontawesome.prototype.getTemplateName=function(){return"core/pix_icon_fontawesome"},IconSystemFontawesome}));
define("core/icon_system_fontawesome",["exports","./ajax","./localstorage","./icon_system","./mustache","./config","./url"],(function(_exports,_ajax,_localstorage,_icon_system,Mustache,Config,Url){function _getRequireWildcardCache(nodeInterop){if("function"!=typeof WeakMap)return null;var cacheBabelInterop=new WeakMap,cacheNodeInterop=new WeakMap;return(_getRequireWildcardCache=function(nodeInterop){return nodeInterop?cacheNodeInterop:cacheBabelInterop})(nodeInterop)}function _interopRequireWildcard(obj,nodeInterop){if(!nodeInterop&&obj&&obj.__esModule)return obj;if(null===obj||"object"!=typeof obj&&"function"!=typeof obj)return{default:obj};var cache=_getRequireWildcardCache(nodeInterop);if(cache&&cache.has(obj))return cache.get(obj);var newObj={},hasPropertyDescriptor=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var key in obj)if("default"!==key&&Object.prototype.hasOwnProperty.call(obj,key)){var desc=hasPropertyDescriptor?Object.getOwnPropertyDescriptor(obj,key):null;desc&&(desc.get||desc.set)?Object.defineProperty(newObj,key,desc):newObj[key]=obj[key]}return newObj.default=obj,cache&&cache.set(obj,newObj),newObj}function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}function _defineProperty(obj,key,value){return key in obj?Object.defineProperty(obj,key,{value:value,enumerable:!0,configurable:!0,writable:!0}):obj[key]=value,obj}Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.default=void 0,_localstorage=_interopRequireDefault(_localstorage),_icon_system=_interopRequireDefault(_icon_system),Mustache=_interopRequireWildcard(Mustache),Config=_interopRequireWildcard(Config),Url=_interopRequireWildcard(Url);class IconSystemFontawesome extends _icon_system.default{init(){return IconSystemFontawesome.staticMap||this.getMapFromCache()?Promise.resolve(this):IconSystemFontawesome.fetchPromise?IconSystemFontawesome.fetchPromise:this.fetchMapFromServer()}getMapFromCache(){const map=_localstorage.default.get(IconSystemFontawesome.cacheKey);return map&&(IconSystemFontawesome.staticMap=new Map(JSON.parse(map))),IconSystemFontawesome.staticMap}_fetchMapFromServer(){return(0,_ajax.call)([{methodname:"core_output_load_fontawesome_icon_system_map",args:{themename:Config.theme}}],!0,!1,!1,0,Config.themerev)[0]}async fetchMapFromServer(){return IconSystemFontawesome.fetchPromise=new Promise((async resolve=>{const mapData=await this._fetchMapFromServer();IconSystemFontawesome.staticMap=new Map(Object.entries(mapData).map((_ref=>{let[,value]=_ref;return["".concat(value.component,"/").concat(value.pix),value.to]}))),_localstorage.default.set(IconSystemFontawesome.cacheKey,JSON.stringify(Array.from(IconSystemFontawesome.staticMap.entries()))),resolve(this)})),IconSystemFontawesome.fetchPromise}renderIcon(key,component,title,template){const iconKey="".concat(component,"/").concat(key),mappedIcon=IconSystemFontawesome.staticMap.get(iconKey),context={title:title,unmappedIcon:this.getUnmappedIcon(mappedIcon,key,component,title),alt:title,key:mappedIcon};return void 0!==title&&""!==title||(context["aria-hidden"]=!0),Mustache.render(template,context).trim()}getUnmappedIcon(mappedIcon,key,component,title){return mappedIcon?null:{attributes:[{name:"src",value:Url.imageUrl(key,component)},{name:"alt",value:title},{name:"title",value:title}]}}getTemplateName(){return"core/pix_icon_fontawesome"}}return _exports.default=IconSystemFontawesome,_defineProperty(IconSystemFontawesome,"staticMap",null),_defineProperty(IconSystemFontawesome,"fetchPromise",null),_defineProperty(IconSystemFontawesome,"cacheKey","core_iconsystem/theme/".concat(Config.theme,"/core/iconmap-fontawesome")),_exports.default}));
//# sourceMappingURL=icon_system_fontawesome.min.js.map

File diff suppressed because one or more lines are too long

View File

@ -1,10 +1,10 @@
define("core/icon_system_standard",["exports","./icon_system","./url","./mustache"],(function(_exports,_icon_system,CoreUrl,Mustache){var obj;
/**
* Competency rule points module.
*
* @module core/icon_system_standard
* @copyright 2017 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define("core/icon_system_standard",["core/icon_system","core/url","core/mustache"],(function(IconSystem,CoreUrl,Mustache){var IconSystemStandard=function(){IconSystem.apply(this,arguments)};return(IconSystemStandard.prototype=Object.create(IconSystem.prototype)).renderIcon=function(key,component,title,template){var templatecontext={attributes:[{name:"src",value:CoreUrl.imageUrl(key,component)},{name:"alt",value:title},{name:"title",value:title}]};return void 0!==title&&""!=title||templatecontext.attributes.push({name:"aria-hidden",value:"true"}),Mustache.render(template,templatecontext).trim()},IconSystemStandard.prototype.getTemplateName=function(){return"core/pix_icon"},IconSystemStandard}));
* The Standard icon system.
*
* @module core/icon_system_standard
* @copyright 2017 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/function _getRequireWildcardCache(nodeInterop){if("function"!=typeof WeakMap)return null;var cacheBabelInterop=new WeakMap,cacheNodeInterop=new WeakMap;return(_getRequireWildcardCache=function(nodeInterop){return nodeInterop?cacheNodeInterop:cacheBabelInterop})(nodeInterop)}function _interopRequireWildcard(obj,nodeInterop){if(!nodeInterop&&obj&&obj.__esModule)return obj;if(null===obj||"object"!=typeof obj&&"function"!=typeof obj)return{default:obj};var cache=_getRequireWildcardCache(nodeInterop);if(cache&&cache.has(obj))return cache.get(obj);var newObj={},hasPropertyDescriptor=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var key in obj)if("default"!==key&&Object.prototype.hasOwnProperty.call(obj,key)){var desc=hasPropertyDescriptor?Object.getOwnPropertyDescriptor(obj,key):null;desc&&(desc.get||desc.set)?Object.defineProperty(newObj,key,desc):newObj[key]=obj[key]}return newObj.default=obj,cache&&cache.set(obj,newObj),newObj}Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.default=void 0,_icon_system=(obj=_icon_system)&&obj.__esModule?obj:{default:obj},CoreUrl=_interopRequireWildcard(CoreUrl),Mustache=_interopRequireWildcard(Mustache);class IconSystemStandard extends _icon_system.default{renderIcon(key,component,title,template){const templatecontext={attributes:[{name:"src",value:CoreUrl.imageUrl(key,component)},{name:"alt",value:title},{name:"title",value:title}]};return void 0!==title&&""!=title||templatecontext.attributes.push({name:"aria-hidden",value:"true"}),Mustache.render(template,templatecontext).trim()}getTemplateName(){return"core/pix_icon"}}return _exports.default=IconSystemStandard,_exports.default}));
//# sourceMappingURL=icon_system_standard.min.js.map

View File

@ -1 +1 @@
{"version":3,"file":"icon_system_standard.min.js","sources":["../src/icon_system_standard.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Competency rule points module.\n *\n * @module core/icon_system_standard\n * @copyright 2017 Damyon Wiese\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\ndefine(['core/icon_system', 'core/url', 'core/mustache'],\n function(IconSystem, CoreUrl, Mustache) {\n\n /**\n * IconSystemStandard\n *\n * @class core/icon_system_standard\n */\n var IconSystemStandard = function() {\n IconSystem.apply(this, arguments);\n };\n IconSystemStandard.prototype = Object.create(IconSystem.prototype);\n\n /**\n * Render an icon.\n *\n * @method renderIcon\n * @param {String} key\n * @param {String} component\n * @param {String} title\n * @param {String} template\n * @return {String}\n */\n IconSystemStandard.prototype.renderIcon = function(key, component, title, template) {\n var url = CoreUrl.imageUrl(key, component);\n\n var templatecontext = {\n attributes: [\n {name: 'src', value: url},\n {name: 'alt', value: title},\n {name: 'title', value: title}\n ]\n };\n if (typeof title === \"undefined\" || title == \"\") {\n templatecontext.attributes.push({name: 'aria-hidden', value: 'true'});\n }\n\n var result = Mustache.render(template, templatecontext);\n return result.trim();\n };\n\n /**\n * Get the name of the template to pre-cache for this icon system.\n *\n * @return {String}\n * @method getTemplateName\n */\n IconSystemStandard.prototype.getTemplateName = function() {\n return 'core/pix_icon';\n };\n\n return IconSystemStandard;\n});\n"],"names":["define","IconSystem","CoreUrl","Mustache","IconSystemStandard","apply","this","arguments","prototype","Object","create","renderIcon","key","component","title","template","templatecontext","attributes","name","value","imageUrl","push","render","trim","getTemplateName"],"mappings":";;;;;;;AAuBAA,mCAAO,CAAC,mBAAoB,WAAY,kBAChC,SAASC,WAAYC,QAASC,cAO9BC,mBAAqB,WACrBH,WAAWI,MAAMC,KAAMC,mBAE3BH,mBAAmBI,UAAYC,OAAOC,OAAOT,WAAWO,YAY3BG,WAAa,SAASC,IAAKC,UAAWC,MAAOC,cAGlEC,gBAAkB,CAClBC,WAAY,CACR,CAACC,KAAM,MAAOC,MAJZjB,QAAQkB,SAASR,IAAKC,YAKxB,CAACK,KAAM,MAAOC,MAAOL,OACrB,CAACI,KAAM,QAASC,MAAOL,qBAGV,IAAVA,OAAkC,IAATA,OAChCE,gBAAgBC,WAAWI,KAAK,CAACH,KAAM,cAAeC,MAAO,SAGpDhB,SAASmB,OAAOP,SAAUC,iBACzBO,QASlBnB,mBAAmBI,UAAUgB,gBAAkB,iBACpC,iBAGJpB"}
{"version":3,"file":"icon_system_standard.min.js","sources":["../src/icon_system_standard.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * The Standard icon system.\n *\n * @module core/icon_system_standard\n * @copyright 2017 Damyon Wiese\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport IconSystem from './icon_system';\nimport * as CoreUrl from './url';\nimport * as Mustache from './mustache';\n\n/**\n * The Standard icon system.\n */\nexport default class IconSystemStandard extends IconSystem {\n\n /**\n * Render an icon.\n *\n * @param {string} key\n * @param {string} component\n * @param {string} title\n * @param {string} template\n * @return {string}\n */\n renderIcon(key, component, title, template) {\n const url = CoreUrl.imageUrl(key, component);\n\n const templatecontext = {\n attributes: [\n {name: 'src', value: url},\n {name: 'alt', value: title},\n {name: 'title', value: title},\n ]\n };\n if (typeof title === \"undefined\" || title == \"\") {\n templatecontext.attributes.push({name: 'aria-hidden', value: 'true'});\n }\n\n return Mustache.render(template, templatecontext).trim();\n }\n\n /**\n * Get the name of the template to pre-cache for this icon system.\n *\n * @return {string}\n */\n getTemplateName() {\n return 'core/pix_icon';\n }\n}\n"],"names":["IconSystemStandard","IconSystem","renderIcon","key","component","title","template","templatecontext","attributes","name","value","CoreUrl","imageUrl","push","Mustache","render","trim","getTemplateName"],"mappings":";;;;;;;wqCA8BqBA,2BAA2BC,qBAW5CC,WAAWC,IAAKC,UAAWC,MAAOC,gBAGxBC,gBAAkB,CACpBC,WAAY,CACR,CAACC,KAAM,MAAOC,MAJVC,QAAQC,SAAST,IAAKC,YAK1B,CAACK,KAAM,MAAOC,MAAOL,OACrB,CAACI,KAAM,QAASC,MAAOL,qBAGV,IAAVA,OAAkC,IAATA,OAChCE,gBAAgBC,WAAWK,KAAK,CAACJ,KAAM,cAAeC,MAAO,SAG1DI,SAASC,OAAOT,SAAUC,iBAAiBS,OAQtDC,wBACW"}

View File

@ -21,27 +21,24 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['jquery'], function($) {
/**
* Icon System abstract class.
*
* Any icon system needs to define a module extending this one and return this module name from the php icon_system class.
*
* @class core/icon_system
*/
var IconSystem = function() {
};
import config from 'core/config';
/**
* Icon System abstract class.
*
* Any icon system needs to define a module extending this one and return this module name from the php icon_system class.
*
* @class core/icon_system
*/
export default class IconSystem {
/**
* Initialise the icon system.
*
* @return {Promise}
* @method init
* @return {Promise<IconSystem>}
*/
IconSystem.prototype.init = function() {
return $.when(this);
};
init() {
return Promise.resolve(this);
}
/**
* Render an icon.
@ -52,26 +49,26 @@ define(['jquery'], function($) {
* template (which is unfortunately synchronous). To render the mustache template in this function call
* core/mustache.render() directly and do not use any partials, blocks or helper functions in the template.
*
* @param {String} key
* @param {String} component
* @param {String} title
* @param {String} template
* @return {String}
* @param {string} key
* @param {string} component
* @param {string} title
* @param {string} template
* @returns {string}
* @method renderIcon
*/
IconSystem.prototype.renderIcon = function(key, component, title, template) { // eslint-disable-line no-unused-vars
// eslint-disable-next-line no-unused-vars
renderIcon(key, component, title, template) {
throw new Error('Abstract function not implemented.');
};
}
/**
* getTemplateName
* Get the name of the template.
*
* @return {String}
* @returns {string}
* @method getTemplateName
*/
IconSystem.prototype.getTemplateName = function() {
// eslint-disable-next-line no-unused-vars
getTemplateName() {
throw new Error('Abstract function not implemented.');
};
return /** @alias module:core/icon_system */ IconSystem;
});
}
}

View File

@ -14,120 +14,176 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Competency rule points module.
* An Icon System implementation for FontAwesome.
*
* @module core/icon_system_fontawesome
* @copyright 2017 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['core/icon_system', 'jquery', 'core/ajax', 'core/mustache', 'core/localstorage', 'core/url'],
function(IconSystem, $, Ajax, Mustache, LocalStorage, Url) {
import {call as fetchMany} from './ajax';
import LocalStorage from './localstorage';
import IconSystem from './icon_system';
import * as Mustache from './mustache';
import * as Config from './config';
import * as Url from './url';
var staticMap = null;
var fetchMap = null;
/**
* The FontAwesome icon system.
*/
export default class IconSystemFontawesome extends IconSystem {
/**
* @var {Map} staticMap A map of icon names to FA Icon.
* @private
*/
static staticMap = null;
/**
* IconSystemFontawesome
* @class core/icon_system_fontawesome
* @var {Promise} fetchPromise The promise used when fetching the result
* @private
*/
var IconSystemFontawesome = function() {
IconSystem.apply(this, arguments);
};
IconSystemFontawesome.prototype = Object.create(IconSystem.prototype);
static fetchPromise = null;
/**
* @var {string} cacheKey The key used to store the icon map in LocalStorage.
* @private
*/
static cacheKey = `core_iconsystem/theme/${Config.theme}/core/iconmap-fontawesome`;
/**
* Prefetch resources so later calls to renderIcon can be resolved synchronously.
*
* @method init
* @return {Promise}
* @returns {Promise<IconSystemFontawesome>}
*/
IconSystemFontawesome.prototype.init = function() {
var currTheme = M.cfg.theme;
if (staticMap) {
return $.when(this);
init() {
if (IconSystemFontawesome.staticMap) {
return Promise.resolve(this);
}
var map = LocalStorage.get('core_iconsystem/theme/' + currTheme + '/core/iconmap-fontawesome');
if (this.getMapFromCache()) {
return Promise.resolve(this);
}
if (IconSystemFontawesome.fetchPromise) {
return IconSystemFontawesome.fetchPromise;
}
return this.fetchMapFromServer();
}
/**
* Get the icon map from LocalStorage.
*
* @private
* @returns {Map}
*/
getMapFromCache() {
const map = LocalStorage.get(IconSystemFontawesome.cacheKey);
if (map) {
map = JSON.parse(map);
IconSystemFontawesome.staticMap = new Map(JSON.parse(map));
}
return IconSystemFontawesome.staticMap;
}
if (map) {
staticMap = map;
return $.when(this);
}
/**
* Fetch the map data from the server.
*
* @private
* @returns {Promise}
*/
_fetchMapFromServer() {
return fetchMany([{
methodname: 'core_output_load_fontawesome_icon_system_map',
args: {
themename: Config.theme,
},
}], true, false, false, 0, Config.themerev)[0];
}
if (fetchMap === null) {
fetchMap = Ajax.call([{
methodname: 'core_output_load_fontawesome_icon_system_map',
args: {
themename: M.cfg.theme,
},
}], true, false, false, 0, M.cfg.themerev)[0];
}
/**
* Fetch the map data from the server.
*
* @returns {Promise<IconSystemFontawesome>}
* @private
*/
async fetchMapFromServer() {
IconSystemFontawesome.fetchPromise = new Promise(async(resolve) => {
const mapData = await this._fetchMapFromServer();
return fetchMap.then(function(map) {
staticMap = {};
$.each(map, function(index, value) {
staticMap[value.component + '/' + value.pix] = value.to;
});
LocalStorage.set('core_iconsystem/theme/' + currTheme + '/core/iconmap-fontawesome', JSON.stringify(staticMap));
return this;
}.bind(this));
};
IconSystemFontawesome.staticMap = new Map(Object.entries(mapData).map(([, value]) => ([
`${value.component}/${value.pix}`,
value.to,
])));
LocalStorage.set(
IconSystemFontawesome.cacheKey,
JSON.stringify(Array.from(IconSystemFontawesome.staticMap.entries())),
);
resolve(this);
});
return IconSystemFontawesome.fetchPromise;
}
/**
* Render an icon.
*
* @param {String} key
* @param {String} component
* @param {String} title
* @param {String} template
* @return {String}
* @method renderIcon
* @param {string} key
* @param {string} component
* @param {string} title
* @param {string} template
* @return {string} The rendered HTML content
*/
IconSystemFontawesome.prototype.renderIcon = function(key, component, title, template) {
var mappedIcon = staticMap[component + '/' + key];
var unmappedIcon = false;
if (typeof mappedIcon === "undefined") {
var url = Url.imageUrl(key, component);
renderIcon(key, component, title, template) {
const iconKey = `${component}/${key}`;
const mappedIcon = IconSystemFontawesome.staticMap.get(iconKey);
const unmappedIcon = this.getUnmappedIcon(mappedIcon, key, component, title);
unmappedIcon = {
attributes: [
{name: 'src', value: url},
{name: 'alt', value: title},
{name: 'title', value: title}
]
};
}
var context = {
key: mappedIcon,
title: title,
const context = {
title,
unmappedIcon,
alt: title,
unmappedIcon: unmappedIcon
key: mappedIcon,
};
if (typeof title === "undefined" || title === '') {
context['aria-hidden'] = true;
}
var result = Mustache.render(template, context);
return result.trim();
};
return Mustache.render(template, context).trim();
}
/**
* Get the unamapped icon content, if the icon is not mapped.
*
* @param {object<attributes<array>>} mappedIcon
* @param {string} key
* @param {string} component
* @param {StaticRange} title
* @returns {object<attributes<array>>|null}
* @private
*/
getUnmappedIcon(mappedIcon, key, component, title) {
if (mappedIcon) {
return null;
}
return {
attributes: [
{name: 'src', value: Url.imageUrl(key, component)},
{name: 'alt', value: title},
{name: 'title', value: title}
],
};
}
/**
* Get the name of the template to pre-cache for this icon system.
*
* @return {String}
* @return {string}
* @method getTemplateName
*/
IconSystemFontawesome.prototype.getTemplateName = function() {
getTemplateName() {
return 'core/pix_icon_fontawesome';
};
return /** @alias module:core/icon_system_fontawesome */ IconSystemFontawesome;
});
}
}

View File

@ -14,63 +14,54 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Competency rule points module.
* The Standard icon system.
*
* @module core/icon_system_standard
* @copyright 2017 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['core/icon_system', 'core/url', 'core/mustache'],
function(IconSystem, CoreUrl, Mustache) {
import IconSystem from './icon_system';
import * as CoreUrl from './url';
import * as Mustache from './mustache';
/**
* IconSystemStandard
*
* @class core/icon_system_standard
*/
var IconSystemStandard = function() {
IconSystem.apply(this, arguments);
};
IconSystemStandard.prototype = Object.create(IconSystem.prototype);
/**
* The Standard icon system.
*/
export default class IconSystemStandard extends IconSystem {
/**
* Render an icon.
*
* @method renderIcon
* @param {String} key
* @param {String} component
* @param {String} title
* @param {String} template
* @return {String}
* @param {string} key
* @param {string} component
* @param {string} title
* @param {string} template
* @return {string}
*/
IconSystemStandard.prototype.renderIcon = function(key, component, title, template) {
var url = CoreUrl.imageUrl(key, component);
renderIcon(key, component, title, template) {
const url = CoreUrl.imageUrl(key, component);
var templatecontext = {
const templatecontext = {
attributes: [
{name: 'src', value: url},
{name: 'alt', value: title},
{name: 'title', value: title}
{name: 'title', value: title},
]
};
if (typeof title === "undefined" || title == "") {
templatecontext.attributes.push({name: 'aria-hidden', value: 'true'});
}
var result = Mustache.render(template, templatecontext);
return result.trim();
};
return Mustache.render(template, templatecontext).trim();
}
/**
* Get the name of the template to pre-cache for this icon system.
*
* @return {String}
* @method getTemplateName
* @return {string}
*/
IconSystemStandard.prototype.getTemplateName = function() {
getTemplateName() {
return 'core/pix_icon';
};
return IconSystemStandard;
});
}
}