-
Notifications
You must be signed in to change notification settings - Fork 58
/
x-icon.js
139 lines (116 loc) · 3.5 KB
/
x-icon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// @doc
// https://material.google.com/style/icons.html#icons-system-icons
// @copyright
// © 2016-2017 Jarosław Foksa
"use strict";
{
let {html, svg} = Xel.utils.element;
let shadowTemplate = html`
<template>
<link rel="stylesheet" href="node_modules/xel/stylesheets/x-icon.css" data-vulcanize>
<svg id="svg" preserveAspectRatio="none" viewBox="0 0 100 100" width="24px" height="24px"></svg>
</template>
`;
class XIconElement extends HTMLElement {
constructor() {
super();
this._shadowRoot = this.attachShadow({mode: "closed"});
this._shadowRoot.append(document.importNode(shadowTemplate.content, true));
for (let element of this._shadowRoot.querySelectorAll("[id]")) {
this["#" + element.id] = element;
}
}
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue === newValue) {
return;
}
else if (name === "name") {
this._update();
}
else if (name === "iconset") {
this._update();
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
static get observedAttributes() {
return ["name", "iconset"];
}
// @type
// string
// @default
// ""
// @attribute
get name() {
return this.hasAttribute("name") ? this.getAttribute("name") : "";
}
set name(name) {
this.setAttribute("name", name);
}
// @type
// string
// @default
// "node_modules/xel/images/icons.svg"
// @attribute
get iconset() {
return this.hasAttribute("iconset") ? this.getAttribute("iconset") : `node_modules/xel/images/icons.svg`;
}
set iconset(iconset) {
this.setAttribute("iconset", iconset);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
async _update() {
if (this.name === "") {
this["#svg"].innerHTML = "";
}
else {
if (this.iconset.startsWith("http") && new URL(this.iconset).origin !== window.location.origin) {
let symbol = await this._getSymbol(this.name, this.iconset);
if (symbol) {
this["#svg"].innerHTML = `${symbol.outerHTML}<use href="#${this.name}" width="100%" height="100%"></use>`
}
}
else {
let href = `${this.iconset}#${this.name}`;
this["#svg"].innerHTML = `<use href="${href}" width="100%" height="100%"></use>`;
}
}
}
_getSymbol(name, url) {
return new Promise(async (resolve) => {
let iconset = null;
let cache = XIconElement._cache || [];
if (!XIconElement._cache) {
XIconElement._cache = cache;
}
if (cache[url]) {
iconset = cache[url];
}
else {
let fetchResponse;
try {
fetchResponse = await fetch(url);
}
catch (error) {
fetchResponse = null;
}
if (fetchResponse && fetchResponse.ok) {
try {
let iconsetSVG = await fetchResponse.text();
iconset = svg`${iconsetSVG}`;
cache[url] = iconset;
}
catch (error) {
}
}
}
if (iconset) {
let symbol = iconset.querySelector(`#${name}`);
if (symbol) {
resolve(symbol);
}
}
});
}
}
customElements.define("x-icon", XIconElement);
}