MDL-69215 output: Correct font awesome for themes

This commit is contained in:
Andrew Nicols
2020-07-16 07:29:22 +08:00
parent ee23a8cf25
commit 4a4a367251
9 changed files with 228 additions and 27 deletions
+1 -1
View File
@@ -1,2 +1,2 @@
define ("core/icon_system_fontawesome",["core/icon_system","jquery","core/ajax","core/mustache","core/localstorage","core/url"],function(a,b,c,d,e,f){var g=null,h=null,i=function(){a.apply(this,arguments)};i.prototype=Object.create(a.prototype);i.prototype.init=function(){if(g){return b.when(this)}var a=e.get("core/iconmap-fontawesome");if(a){a=JSON.parse(a)}if(a){g=a;return b.when(this)}if(null===h){h=c.call([{methodname:"core_output_load_fontawesome_icon_map",args:[]}],!0,!1,!1,0,M.cfg.themerev)[0]}return h.then(function(a){g={};b.each(a,function(a,b){g[b.component+"/"+b.pix]=b.to});e.set("core/iconmap-fontawesome",JSON.stringify(g));return this}.bind(this))};i.prototype.renderIcon=function(a,b,c,e){var h=g[b+"/"+a],i=!1;if("undefined"==typeof h){var j=f.imageUrl(a,b);i={attributes:[{name:"src",value:j},{name:"alt",value:c},{name:"title",value:c}]}}var k={key:h,title:c,alt:c,unmappedIcon:i};if("undefined"==typeof c||""===c){k["aria-hidden"]=!0}var l=d.render(e,k);return l.trim()};i.prototype.getTemplateName=function(){return"core/pix_icon_fontawesome"};return i});
define ("core/icon_system_fontawesome",["core/icon_system","jquery","core/ajax","core/mustache","core/localstorage","core/url"],function(a,b,c,d,e,f){var g=null,h=null,i=function(){a.apply(this,arguments)};i.prototype=Object.create(a.prototype);i.prototype.init=function(){if(g){return b.when(this)}var a=e.get("core/iconmap-fontawesome");if(a){a=JSON.parse(a)}if(a){g=a;return b.when(this)}if(null===h){h=c.call([{methodname:"core_output_load_fontawesome_icon_system_map",args:{themename:M.cfg.theme}}],!0,!1,!1,0,M.cfg.themerev)[0]}return h.then(function(a){g={};b.each(a,function(a,b){g[b.component+"/"+b.pix]=b.to});e.set("core/iconmap-fontawesome",JSON.stringify(g));return this}.bind(this))};i.prototype.renderIcon=function(a,b,c,e){var h=g[b+"/"+a],i=!1;if("undefined"==typeof h){var j=f.imageUrl(a,b);i={attributes:[{name:"src",value:j},{name:"alt",value:c},{name:"title",value:c}]}}var k={key:h,title:c,alt:c,unmappedIcon:i};if("undefined"==typeof c||""===c){k["aria-hidden"]=!0}var l=d.render(e,k);return l.trim()};i.prototype.getTemplateName=function(){return"core/pix_icon_fontawesome"};return i});
//# sourceMappingURL=icon_system_fontawesome.min.js.map
File diff suppressed because one or more lines are too long
+4 -2
View File
@@ -58,8 +58,10 @@ define(['core/icon_system', 'jquery', 'core/ajax', 'core/mustache', 'core/locals
if (fetchMap === null) {
fetchMap = Ajax.call([{
methodname: 'core_output_load_fontawesome_icon_map',
args: []
methodname: 'core_output_load_fontawesome_icon_system_map',
args: {
themename: M.cfg.theme,
},
}], true, false, false, 0, M.cfg.themerev)[0];
}
@@ -0,0 +1,98 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* A web service to load the mapping of moodle pix names to fontawesome icon names.
*
* @package core
* @category external
* @copyright 2020 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\external\output\icon_system;
use external_api;
use external_function_parameters;
use external_multiple_structure;
use external_single_structure;
use external_value;
use core\output\icon_system_fontawesome;
use theme_config;
/**
* Web service to load font awesome icon maps.
*
* @package core
* @category external
* @copyright 2020 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class load_fontawesome_map extends external_api {
/**
* Description of the parameters suitable for the `execute` function.
*
* @return external_function_parameters
*/
public static function execute_parameters() {
return new external_function_parameters([
'themename' => new external_value(PARAM_ALPHANUMEXT, 'The theme to fetch the map for'),
]);
}
/**
* Return a mapping of icon names to icons.
*
* @param string $themename The theme to fetch icons for
* @return array the mapping
*/
public static function execute(string $themename) {
[
'themename' => $themename,
] = self::validate_parameters(self::execute_parameters(), [
'themename' => $themename,
]);
$theme = theme_config::load($themename);
$instance = icon_system_fontawesome::instance($theme->get_icon_system());
$result = [];
foreach ($instance->get_icon_name_map() as $from => $to) {
[$component, $pix] = explode(':', $from);
$result[] = [
'component' => $component,
'pix' => $pix,
'to' => $to,
];
}
return $result;
}
/**
* Description of the return value for the `execute` function.
*
* @return external_description
*/
public static function execute_returns() {
return new external_multiple_structure(new external_single_structure([
'component' => new external_value(PARAM_COMPONENT, 'The component for the icon.'),
'pix' => new external_value(PARAM_RAW, 'Value to map the icon from.'),
'to' => new external_value(PARAM_RAW, 'Value to map the icon to.'),
]));
}
}
+15 -22
View File
@@ -34,6 +34,7 @@ use core_component;
use moodle_exception;
use context_system;
use theme_config;
use core\external\output\icon_system\load_fontawesome_map;
/**
* This class contains a list of webservice functions related to output.
@@ -202,24 +203,13 @@ class external extends external_api {
/**
* Return a mapping of icon names to icons.
*
* @deprecated since Moodle 3.10
* @return array the mapping
*/
public static function load_fontawesome_icon_map() {
$instance = icon_system::instance(icon_system::FONTAWESOME);
global $PAGE;
$map = $instance->get_icon_name_map();
$result = [];
foreach ($map as $from => $to) {
list($component, $pix) = explode(':', $from);
$one = [];
$one['component'] = $component;
$one['pix'] = $pix;
$one['to'] = $to;
$result[] = $one;
}
return $result;
return load_fontawesome_map::execute($PAGE->theme->name);
}
/**
@@ -228,13 +218,16 @@ class external extends external_api {
* @return external_description
*/
public static function load_fontawesome_icon_map_returns() {
return new external_multiple_structure(new external_single_structure(
array(
'component' => new external_value(PARAM_COMPONENT, 'The component for the icon.'),
'pix' => new external_value(PARAM_RAW, 'Value to map the icon from.'),
'to' => new external_value(PARAM_RAW, 'Value to map the icon to.')
)
));
return load_fontawesome_map::execute_returns();
}
/**
* The `load_fontawesome_icon_map` function has been replaced with
* @see load_fontawesome_map::execute()
*
* @return bool
*/
public static function load_fontawesome_icon_map_is_deprecated() {
return true;
}
}
+8
View File
@@ -1646,6 +1646,14 @@ $functions = array(
'loginrequired' => false,
'ajax' => true,
),
'core_output_load_fontawesome_icon_system_map' => array(
'classname' => 'core\external\output\icon_system\load_fontawesome_map',
'methodname' => 'execute',
'description' => 'Load the mapping of moodle pix names to fontawesome icon names',
'type' => 'read',
'loginrequired' => false,
'ajax' => true,
),
// Question related functions.
'core_question_update_flag' => array(
'classname' => 'core_question_external',
@@ -0,0 +1,98 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* External functions test for record_feedback_action.
*
* @package core
* @category test
* @copyright 2020 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\external\output\icon_system;
use externallib_advanced_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* Class record_userfeedback_action_testcase
*
* @copyright 2020 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass core\external\output\icon_system\load_fontawesome_map
*/
class load_fontawesome_map_testcase extends externallib_advanced_testcase {
/**
* Perform setup before these tests are run.
*/
public static function setUpBeforeClass(): void {
global $CFG;
// In normal operation the external_api classes will have been loaded by the caller.
// The load_fontawesome_map class should not need to supplement our lack of autoloading of these classes.
require_once($CFG->libdir . '/externallib.php');
}
/**
* Ensure that a valid theme which uses fontawesome returns a map.
*
* @covers ::execute_parameters
* @covers ::execute
* @covers ::execute_returns
* @dataProvider valid_fontawesome_theme_provider
* @param string $themename
*/
public function test_execute(string $themename): void {
$result = load_fontawesome_map::execute($themename);
$this->assertIsArray($result);
foreach ($result as $value) {
$this->assertArrayHasKey('component', $value);
$this->assertArrayHasKey('pix', $value);
$this->assertArrayHasKey('to', $value);
}
}
/**
* Ensure that an invalid theme cannot be loaded.
*/
public function test_execute_invalid_themename(): void {
$result = load_fontawesome_map::execute('invalidtheme');
$this->assertDebuggingCalled(
'This page should be using theme invalidtheme which cannot be initialised. Falling back to the site theme boost'
);
$this->assertIsArray($result);
}
/**
* Data provider for valid themes to use with the execute function.
*
* @return array
*/
public function valid_fontawesome_theme_provider(): array {
return [
'Boost theme' => ['boost'],
'Classic theme (extends boost)' => ['classic'],
];
}
}
+2
View File
@@ -29,6 +29,8 @@ information provided here is intended especially for developers.
- course_in_list (now: core_course_list_element)
- coursecat (now: core_course_category)
* The form element 'htmleditor', which was deprecated in 3.6, has been removed.
* The `core_output_load_fontawesome_icon_map` web service has been deprecated and replaced by
`core_output_load_fontawesome_icon_system_map` which takes the name of the theme to generate the icon system map for.
=== 3.9 ===
* Following function has been deprecated, please use \core\task\manager::run_from_cli().
+1 -1
View File
@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2020071100.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2020071500.00; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.0dev (Build: 20200711)'; // Human-friendly version name