mirror of
https://github.com/atisawd/boxicons.git
synced 2024-11-13 19:04:52 +01:00
Refactor of getIconSvg() method
This commit is contained in:
parent
9f521ed7f4
commit
5d008d83b2
2 changed files with 43 additions and 41 deletions
|
@ -1,12 +1,11 @@
|
|||
/* global BUILD */
|
||||
//
|
||||
import animationsCss from '../static/css/animations.css';
|
||||
import transformationsCss from '../static/css/transformations.css';
|
||||
|
||||
|
||||
//= ======================================================
|
||||
const CACHE = {}; // iconName: Promise()
|
||||
const CSS_CLASS_PREFIX = "bx-";
|
||||
const CSS_CLASS_PREFIX = 'bx-';
|
||||
const CSS_CLASS_PREFIX_ROTATE = `${CSS_CLASS_PREFIX}rotate-`;
|
||||
const CSS_CLASS_PREFIX_FLIP = `${CSS_CLASS_PREFIX}flip-`;
|
||||
|
||||
|
@ -41,12 +40,13 @@ export class BoxIconElement extends HTMLElement {
|
|||
/**
|
||||
* Returns a promise that should resolve with a string - the svg source.
|
||||
*
|
||||
* @param {String} iconUrl
|
||||
* The url to the icon that should be loaded.
|
||||
* @param {String} iconName
|
||||
* The icon name (file name) to the icon that should be loaded.
|
||||
*
|
||||
* @return {Promise<String, Error>}
|
||||
*/
|
||||
static getIconSvg(iconUrl) {
|
||||
static getIconSvg(iconName) {
|
||||
const iconUrl = `${this.cdnUrl}/${iconName}.svg`;
|
||||
if (iconUrl && CACHE[iconUrl]) {
|
||||
return CACHE[iconUrl];
|
||||
}
|
||||
|
@ -135,7 +135,7 @@ ${transformationsCss}
|
|||
handleNameChange(this, oldVal, newVal);
|
||||
break;
|
||||
case 'color':
|
||||
$iconHolder.style.fill = newVal || "";
|
||||
$iconHolder.style.fill = newVal || '';
|
||||
break;
|
||||
case 'size':
|
||||
handleSizeChange(this, oldVal, newVal);
|
||||
|
@ -174,15 +174,14 @@ function handleNameChange(inst, oldVal, newVal) {
|
|||
state.$iconHolder.textContent = '';
|
||||
|
||||
if (newVal) {
|
||||
const iconUrl = `${inst.constructor.cdnUrl}/${newVal}.svg`;
|
||||
inst.constructor.getIconSvg(iconUrl)
|
||||
inst.constructor.getIconSvg(newVal)
|
||||
.then((iconData) => {
|
||||
if (state.currentName === newVal) {
|
||||
state.$iconHolder.innerHTML = iconData;
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(`Failed to load icon: ${iconUrl + "\n"}${error}`); //eslint-disable-line
|
||||
console.error(`Failed to load icon: ${newVal + "\n"}${error}`); //eslint-disable-line
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -191,7 +190,7 @@ function handleSizeChange(inst, oldVal, newVal) {
|
|||
const state = inst._state;
|
||||
|
||||
if (state.size) {
|
||||
state.$iconHolder.style.width = state.$iconHolder.style.height = "";
|
||||
state.$iconHolder.style.width = state.$iconHolder.style.height = '';
|
||||
state.size = state.sizeType = null;
|
||||
}
|
||||
|
||||
|
@ -202,3 +201,5 @@ function handleSizeChange(inst, oldVal, newVal) {
|
|||
state.$iconHolder.style.width = state.$iconHolder.style.height = state.size;
|
||||
}
|
||||
}
|
||||
|
||||
export default BoxIconElement;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { BoxIconElement } from './box-icon-element';
|
||||
|
||||
export { BoxIconElement } from './box-icon-element';
|
||||
export { BoxIconElement };
|
||||
export default BoxIconElement;
|
||||
|
||||
BoxIconElement.define();
|
||||
|
|
Loading…
Reference in a new issue