From e330b1c21476c5d9c80089ca471321fc50817243 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Thu, 2 Feb 2017 12:59:55 +0800 Subject: [PATCH] MDL-40759 icons: Refactor to allow theme icon systems --- lib/amd/build/icon_system.min.js | Bin 201 -> 299 bytes lib/amd/build/icon_system_fontawesome.min.js | Bin 742 -> 828 bytes lib/amd/build/templates.min.js | Bin 6847 -> 6681 bytes lib/amd/src/icon_system.js | 20 ++++++++- lib/amd/src/icon_system_fontawesome.js | 25 +++++++++-- lib/amd/src/templates.js | 33 +++++--------- lib/classes/output/external.php | 20 +++------ lib/classes/output/icon_system.php | 25 ++++++----- lib/classes/output/icon_system_font.php | 39 ----------------- .../output/icon_system_fontawesome.php | 41 ++++++++++++++++++ lib/db/services.php | 4 +- lib/outputrenderers.php | 2 +- lib/outputrequirementslib.php | 4 +- theme/boost/config.php | 2 +- 14 files changed, 116 insertions(+), 99 deletions(-) diff --git a/lib/amd/build/icon_system.min.js b/lib/amd/build/icon_system.min.js index c3aa058086a1737ed3ff4ee3a13c030efdaf4fe3..e30f717638f0ced7c7f4ad236a2db7fca402b5ea 100644 GIT binary patch delta 54 zcmX@fxSDA~5=%)&X7R+N6cxRKqWqHllFEWqz4X+Qkks6QoWzn;zr@^B+qBZW@~ diff --git a/lib/amd/build/icon_system_fontawesome.min.js b/lib/amd/build/icon_system_fontawesome.min.js index 73a476efaed1a4b8809c44dafa91f73bb07cc77d..b6d265d537f0a3c4ef7a766edf192e9af0f4f808 100644 GIT binary patch delta 157 zcmaFHx`%DTKbDe=%;JgvLcsLoAVzc9wEVo1#PZbQ{M^*|%;fyM_}s(-C7r~g^kS>% z*vab{71@hYOG=CK6egc%tYXyx>7VS$G|N}7peVm2zofDtRWCiYBqTMrASbaT)h{tO n)i$j(FS#T$KTkum8lqPzIlm}XzaX;$Vgr(uO0_x}wVKud@J2k0 delta 84 zcmdnP_KbDHKWg4(2^SB{Q&00;Jw8`&er9rhUVL$7aY<@!ypndh9*7U&>ZI8w>A9q)r4|*Xrf6td zPZkh2nS7o_f<>puHfeH#xRtGSSz?hwhOLs4bx~?bX;Ge1aY33f#$$Q5|(2g;+_57C!3*?~#Y7#tD^n=^H?Z408a zVxitrDAFrR%}Yrw@&vj|BUL9&CqpND^ES4Z95Lj2UQ?$eBePf!ulF~f new external_value(PARAM_COMPONENT, 'icon system to load the map for') - ]); + public static function load_fontawesome_icon_map_parameters() { + return new external_function_parameters([]); } /** @@ -112,17 +110,9 @@ class external extends external_api { * @param string $system * @return array the mapping */ - public static function load_icon_map($system) { - $params = self::validate_parameters(self::load_icon_map_parameters(), - array('system' => $system)); - - $system = $params['system']; - - if (!icon_system::is_valid_system($system)) { - throw new coding_exception('Invalid icon system.'); - } + public static function load_fontawesome_icon_map() { - $instance = icon_system::instance($system); + $instance = icon_system::instance(icon_system::FONTAWESOME); $map = $instance->get_icon_name_map(); @@ -144,7 +134,7 @@ class external extends external_api { * * @return external_description */ - public static function load_icon_map_returns() { + 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.'), diff --git a/lib/classes/output/icon_system.php b/lib/classes/output/icon_system.php index e8b1211e02c..6c132c2ff3a 100644 --- a/lib/classes/output/icon_system.php +++ b/lib/classes/output/icon_system.php @@ -42,17 +42,13 @@ use pix_icon; * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ abstract class icon_system { - const STANDARD = 'standard'; - const FONTAWESOME = 'fontawesome'; - const INLINE = 'inline'; - const SYSTEMS = [ self::STANDARD, self::FONTAWESOME, self::INLINE ]; + const STANDARD = '\\core\\output\\icon_system_standard'; + const FONTAWESOME = '\\core\\output\\icon_system_fontawesome'; private static $instance = null; private $map = null; - protected $system = ''; - private function __construct($system) { - $this->system = $system; + private function __construct() { } public final static function instance($type = null) { @@ -63,14 +59,12 @@ abstract class icon_system { return self::$instance; } $type = $PAGE->theme->get_icon_system(); - $system = '\\core\\output\\icon_system_' . $type; - self::$instance = new $system($type); + self::$instance = new $type(); // Default one is a singleton. return self::$instance; } else { - $system = '\\core\\output\\icon_system_' . $type; // Not a singleton. - return new $system($type); + return new $type(); } } @@ -81,9 +75,16 @@ abstract class icon_system { * @return boolean */ public final static function is_valid_system($system) { - return in_array($system, self::SYSTEMS); + return class_exists($system) && is_subclass_of($system, self::class); } + /** + * The name of an AMD module extending core/icon_system + * + * @return string + */ + public abstract function get_amd_name(); + /** * Render the pix icon according to the icon system. * diff --git a/lib/classes/output/icon_system_font.php b/lib/classes/output/icon_system_font.php index 6900df4507d..71c8b7a7682 100644 --- a/lib/classes/output/icon_system_font.php +++ b/lib/classes/output/icon_system_font.php @@ -25,9 +25,6 @@ namespace core\output; -use renderer_base; -use pix_icon; - /** * Class allowing different systems for mapping and rendering icons. * @@ -43,41 +40,5 @@ use pix_icon; */ abstract class icon_system_font extends icon_system { - private $map = []; - - public function get_core_icon_map() { - return []; - } - - public function render_pix_icon(renderer_base $output, pix_icon $icon) { - $subtype = 'pix_icon_' . $this->system; - $subpix = new $subtype($icon); - $data = $subpix->export_for_template($output); - - return $output->render_from_template('core/pix_icon_' . $this->system, $data); - } - - /** - * Overridable function to get a mapping of all icons. - * Default is to do no mapping. - */ - public function get_icon_name_map() { - if ($this->map === []) { - $this->map = $this->get_core_icon_map(); - $callback = 'get_' . $this->system . '_icon_map'; - - if ($pluginsfunction = get_plugins_with_function($callback)) { - foreach ($pluginsfunction as $plugintype => $plugins) { - foreach ($plugins as $pluginfunction) { - $pluginmap = $pluginfunction(); - $this->map += $pluginmap; - } - } - } - - } - return $this->map; - } - } diff --git a/lib/classes/output/icon_system_fontawesome.php b/lib/classes/output/icon_system_fontawesome.php index b83fb1b9e73..444ae013cd8 100644 --- a/lib/classes/output/icon_system_fontawesome.php +++ b/lib/classes/output/icon_system_fontawesome.php @@ -25,6 +25,9 @@ namespace core\output; +use renderer_base; +use pix_icon; + /** * Class allowing different systems for mapping and rendering icons. * @@ -414,5 +417,43 @@ class icon_system_fontawesome extends icon_system_font { 'core:t/viewdetails' => 'fa-list', ]; } + + private $map = []; + + /** + * Overridable function to get a mapping of all icons. + * Default is to do no mapping. + */ + public function get_icon_name_map() { + if ($this->map === []) { + $this->map = $this->get_core_icon_map(); + $callback = 'get_fontawesome_icon_map'; + + if ($pluginsfunction = get_plugins_with_function($callback)) { + foreach ($pluginsfunction as $plugintype => $plugins) { + foreach ($plugins as $pluginfunction) { + $pluginmap = $pluginfunction(); + $this->map += $pluginmap; + } + } + } + + } + return $this->map; + } + + + public function get_amd_name() { + return 'core/icon_system_fontawesome'; + } + + public function render_pix_icon(renderer_base $output, pix_icon $icon) { + $subtype = 'pix_icon_fontawesome'; + $subpix = new $subtype($icon); + $data = $subpix->export_for_template($output); + + return $output->render_from_template('core/pix_icon_fontawesome', $data); + } + } diff --git a/lib/db/services.php b/lib/db/services.php index aa133339f09..5ff30cdef28 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -945,9 +945,9 @@ $functions = array( 'loginrequired' => false, 'ajax' => true, ), - 'core_output_load_icon_map' => array( + 'core_output_load_fontawesome_icon_map' => array( 'classname' => 'core\output\external', - 'methodname' => 'load_icon_map', + 'methodname' => 'load_fontawesome_icon_map', 'description' => 'Load the mapping of names to icons', 'type' => 'read', 'loginrequired' => false, diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 94df7fdda62..181cb1c2793 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -2054,7 +2054,7 @@ class core_renderer extends renderer_base { protected function render_activity_icon(activity_icon $icon) { global $PAGE; - $system = \core\output\icon_system::instance('standard'); + $system = \core\output\icon_system::instance(\core\output\icon_system::STANDARD); return $system->render_pix_icon($this, $icon); } diff --git a/lib/outputrequirementslib.php b/lib/outputrequirementslib.php index ab8c7eaaf60..1406e40af65 100644 --- a/lib/outputrequirementslib.php +++ b/lib/outputrequirementslib.php @@ -313,13 +313,15 @@ class page_requirements_manager { // Otherwise, in some situations, users will get warnings about insecure content // on secure pages from their web browser. + $iconsystem = \core\output\icon_system::instance(); + $this->M_cfg = array( 'wwwroot' => $CFG->httpswwwroot, // Yes, really. See above. 'sesskey' => sesskey(), 'themerev' => theme_get_revision(), 'slasharguments' => (int)(!empty($CFG->slasharguments)), 'theme' => $page->theme->name, - 'iconsystem' => $page->theme->get_icon_system(), + 'iconsystemmodule' => $iconsystem->get_amd_name(), 'jsrev' => $this->get_jsrev(), 'admin' => $CFG->admin, 'svgicons' => $page->theme->use_svg_icons(), diff --git a/theme/boost/config.php b/theme/boost/config.php index c1e32df7855..57e81c4f4b8 100644 --- a/theme/boost/config.php +++ b/theme/boost/config.php @@ -153,4 +153,4 @@ $THEME->yuicssmodules = array(); $THEME->rendererfactory = 'theme_overridden_renderer_factory'; $THEME->requiredblocks = ''; $THEME->addblockposition = BLOCK_ADDBLOCK_POSITION_FLATNAV; -$THEME->iconsystem = 'fontawesome'; +$THEME->iconsystem = \core\output\icon_system::FONTAWESOME; -- 2.43.0