ResourceLoaderImage module definitions can define a defaultColor

Bug: T197909
Change-Id: I0745e112d11026ed59d8daca990b313305cd094a
This commit is contained in:
jdlrobson 2018-06-25 15:55:19 -07:00 committed by Jdlrobson
parent c2c146037a
commit 8faba225fe
4 changed files with 19 additions and 3 deletions

View file

@ -285,6 +285,9 @@
"class": {
"enum": ["ResourceLoaderImageModule"]
},
"defaultColor": {
"type": "string"
},
"data": {
"type": "string"
},

View file

@ -288,6 +288,9 @@
"class": {
"enum": ["ResourceLoaderImageModule"]
},
"defaultColor": {
"type": "string"
},
"data": {
"type": "string"
},

View file

@ -44,14 +44,18 @@ class ResourceLoaderImage {
* @param string|array $descriptor Path to image file, or array structure containing paths
* @param string $basePath Directory to which paths in descriptor refer
* @param array $variants
* @param string|null $defaultColor of the base variant
* @throws InvalidArgumentException
*/
public function __construct( $name, $module, $descriptor, $basePath, $variants ) {
public function __construct( $name, $module, $descriptor, $basePath, $variants,
$defaultColor = null
) {
$this->name = $name;
$this->module = $module;
$this->descriptor = $descriptor;
$this->basePath = $basePath;
$this->variants = $variants;
$this->defaultColor = $defaultColor;
// Expand shorthands:
// [ "en,de,fr" => "foo.svg" ]
@ -250,7 +254,10 @@ class ResourceLoaderImage {
if ( $variant && isset( $this->variants[$variant] ) ) {
$data = $this->variantize( $this->variants[$variant], $context );
} else {
$data = file_get_contents( $path );
$defaultColor = $this->defaultColor;
$data = $defaultColor ?
$this->variantize( [ 'color' => $defaultColor ], $context ) :
file_get_contents( $path );
}
if ( $format === 'rasterized' ) {

View file

@ -39,6 +39,7 @@ class ResourceLoaderImageModule extends ResourceLoaderModule {
protected $origin = self::ORIGIN_CORE_SITEWIDE;
protected $images = [];
protected $defaultColor = null;
protected $variants = [];
protected $prefix = null;
protected $selectorWithoutVariant = '.{prefix}-{name}';
@ -182,6 +183,7 @@ class ResourceLoaderImageModule extends ResourceLoaderModule {
$this->{$member} = $option;
break;
case 'defaultColor':
case 'prefix':
case 'selectorWithoutVariant':
case 'selectorWithVariant':
@ -264,7 +266,8 @@ class ResourceLoaderImageModule extends ResourceLoaderModule {
$this->getName(),
$fileDescriptor,
$this->localBasePath,
$variantConfig
$variantConfig,
$this->defaultColor
);
$this->imageObjects[$skin][$image->getName()] = $image;
}