2018-06-30 01:05:17 +02:00
|
|
|
/* global BUILD */
|
|
|
|
//
|
|
|
|
import animationsCss from '../static/css/animations.css';
|
|
|
|
import transformationsCss from '../static/css/transformations.css';
|
|
|
|
|
|
|
|
|
|
|
|
//= ======================================================
|
|
|
|
const CACHE = {}; // iconName: Promise()
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Custom Element for displaying icon
|
|
|
|
*/
|
|
|
|
export class BoxIconElement extends HTMLElement {
|
|
|
|
static get cdnUrl() {
|
|
|
|
// BUILD.DATA.VERSION is injected by webpack during a build.
|
|
|
|
// Value is same as package.json#version property.
|
|
|
|
return `https://unpkg.com/boxicons@${BUILD.DATA.VERSION}/svg`;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* The html tag name to be use
|
|
|
|
* @type {String}
|
|
|
|
*/
|
|
|
|
static get tagName() { return 'box-icon'; }
|
|
|
|
|
|
|
|
static get observedAttributes() {
|
|
|
|
return [
|
|
|
|
'name',
|
|
|
|
'color',
|
|
|
|
'size',
|
|
|
|
'rotate',
|
|
|
|
'flip',
|
|
|
|
'border',
|
|
|
|
'animation',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a promise that should resolve with a string - the svg source.
|
|
|
|
*
|
|
|
|
* @param {String} iconUrl
|
|
|
|
* The url to the icon that should be loaded.
|
|
|
|
*
|
|
|
|
* @return {Promise<String, Error>}
|
|
|
|
*/
|
|
|
|
static getIconSvg(iconUrl) {
|
2018-06-30 02:12:50 +02:00
|
|
|
if (iconUrl && CACHE[iconUrl]) {
|
|
|
|
return CACHE[iconUrl];
|
|
|
|
}
|
|
|
|
CACHE[iconUrl] = new Promise((resolve, reject) => {
|
|
|
|
const request = new XMLHttpRequest();
|
|
|
|
request.addEventListener('load', function () {
|
|
|
|
if (this.status < 200 || this.status >= 300) {
|
|
|
|
reject(new Error(`${this.status} ${this.responseText}`));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(this.responseText);
|
|
|
|
});
|
|
|
|
request.onerror = reject;
|
|
|
|
request.onabort = reject;
|
|
|
|
request.open('GET', iconUrl);
|
|
|
|
request.send();
|
|
|
|
});
|
|
|
|
return CACHE[iconUrl];
|
2018-06-30 01:05:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define (register) the custom element
|
|
|
|
*
|
|
|
|
* @param {String} [tagName=this.tagName]
|
|
|
|
*/
|
|
|
|
static define(tagName) {
|
|
|
|
customElements.define(tagName || this.tagName, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.$ui = this.attachShadow({ mode: 'open' });
|
|
|
|
this.$ui.innerHTML = `
|
|
|
|
<style>
|
|
|
|
:host {
|
|
|
|
display: inline-block;
|
2018-06-30 02:12:50 +02:00
|
|
|
width: 1em;
|
|
|
|
height: 1em;
|
2018-06-30 01:05:17 +02:00
|
|
|
}
|
2018-06-30 02:44:28 +02:00
|
|
|
:host([size=xs]) {
|
|
|
|
width: 0.8rem;
|
|
|
|
height: 0.8rem;
|
|
|
|
}
|
|
|
|
:host([size=sm]) {
|
|
|
|
width: 1.55rem;
|
|
|
|
height: 1.55rem;
|
|
|
|
}
|
|
|
|
:host([size=md]) {
|
|
|
|
width: 2.25rem;
|
|
|
|
height: 2.25rem;
|
|
|
|
}
|
|
|
|
:host([size=lg]) {
|
|
|
|
width: 3.0rem;
|
|
|
|
height: 3.0rem;
|
|
|
|
}
|
2018-06-30 01:05:17 +02:00
|
|
|
#icon,
|
|
|
|
svg {
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
}
|
|
|
|
${animationsCss}
|
|
|
|
${transformationsCss}
|
|
|
|
</style>
|
|
|
|
<div id="icon"></div>`;
|
|
|
|
|
2018-06-30 02:12:50 +02:00
|
|
|
this._state = {
|
|
|
|
$iconHolder: this.$ui.getElementById('icon'),
|
|
|
|
};
|
2018-06-30 01:05:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
attributeChangedCallback(attr, oldVal, newVal) {
|
2018-06-30 02:12:50 +02:00
|
|
|
switch (attr) {
|
|
|
|
case 'name':
|
|
|
|
handleNameChange(this, oldVal, newVal);
|
|
|
|
break;
|
2018-06-30 02:44:28 +02:00
|
|
|
case 'color':
|
|
|
|
this._state.$iconHolder.style.fill = newVal || "";
|
|
|
|
break;
|
|
|
|
case 'size':
|
|
|
|
handleSizeChange(this, oldVal, newVal);
|
|
|
|
break;
|
2018-06-30 02:12:50 +02:00
|
|
|
}
|
2018-06-30 01:05:17 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-30 02:12:50 +02:00
|
|
|
|
|
|
|
function handleNameChange(inst, oldVal, newVal) {
|
2018-06-30 02:44:28 +02:00
|
|
|
const state = inst._state;
|
|
|
|
state.currentName = newVal;
|
|
|
|
state.$iconHolder.textContent = '';
|
2018-06-30 02:12:50 +02:00
|
|
|
|
|
|
|
if (newVal) {
|
|
|
|
const iconUrl = `${inst.constructor.cdnUrl}/${newVal}.svg`;
|
|
|
|
inst.constructor.getIconSvg(iconUrl)
|
|
|
|
.then((iconData) => {
|
2018-06-30 02:44:28 +02:00
|
|
|
if (state.currentName === newVal) {
|
|
|
|
state.$iconHolder.innerHTML = iconData;
|
2018-06-30 02:12:50 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error(`Failed to load icon: ${iconUrl + "\n"}${error}`); //eslint-disable-line
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-30 02:44:28 +02:00
|
|
|
function handleSizeChange(inst, oldVal, newVal) {
|
|
|
|
const state = inst._state;
|
|
|
|
|
|
|
|
if (state.size) {
|
|
|
|
state.$iconHolder.style.width = state.$iconHolder.style.height = "";
|
|
|
|
state.size = state.sizeType = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the size is not one of the short-hand ones, then it must be a
|
|
|
|
// css size unit - add it directly to the icon holder.
|
|
|
|
if (newVal && !/^(xs|sm|md|lg)$/.test(state.size)) {
|
|
|
|
state.size = newVal.trim();
|
|
|
|
state.$iconHolder.style.width = state.$iconHolder.style.height = state.size;
|
|
|
|
}
|
|
|
|
}
|