-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSVGIcon-bundle.js
More file actions
276 lines (275 loc) · 10.4 KB
/
SVGIcon-bundle.js
File metadata and controls
276 lines (275 loc) · 10.4 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* jshint node: true */
/* global define, self */
(function (root, factory) {
var depends= [];
var getDep;
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(depends, factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
getDep= function(name){ return require(name); };
module.exports = factory.apply(root, depends.map(getDep));
} else {
// Browser globals (root is window)
getDep= function(name){ return root[name]; };
root.SVGIcon = factory.apply(root, depends.map(getDep));
}
}(typeof self !== 'undefined' ? self : this, function (/* ..._dependencies */) {
"use strict";
var _dependencies= Array.prototype.slice.call(arguments);
/**
* Creates elemnet in *svg* namespace
* @method
* @private
* @global
*/
const createElement= document.createElementNS.bind(document, "http://www.w3.org/2000/svg");
/**
* Caching "events info" primary for `attributeChangedCallback`. This in fact caches all function arguments to reproduce calling later.
* @private
*/
class EventFronta{
/**
* Refister new event
* @param {Array} event_info All necessary infos for later invoking
* @param {String} [method="push"] In fact name of operation in `Array.prototype` (not used any more). Understands as enum of: *push*, *unshift*.
*/
add(event_info, method= "push"){
if(typeof this._listeners === "undefined") this._listeners= [];
this._listeners[method](event_info);
}
/**
* Proccess all cached events
* @param {Function} callback Function accepting array of cached arguments
*/
serveAll(callback){
if(typeof this._listeners === "undefined") return true;
let fronta_item;
while((fronta_item= this._listeners.shift()))
callback(fronta_item);
this.clear();
}
/**
* Empty cache
*/
clear(){
Reflect.deleteProperty(this, "_listeners");
}
}
/**
* Sets 'xlink:href' for given element
* @private
* @global
* @param {SVGUseElement} element
* @param {String} value
*/
const setHref= (element, value)=> element.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", value);
/**
* Contains options for generating default styles for `<svg-icon>`. Changes makes sence only before fisrt `<svg-icon>` is created. See {@link style.cerate}.
* @typedef {Object} style_options
* @property {boolean} [allow=true] Allow creating global default styles
* @property {string} [fit=contain] CSS `fit` property of `<svg>` inside `<svg-icon>`
* @property {string} [size_variable=--svg-icon-size] The full name of CSS variable for changin icon size (width and height)
*/
/**
* @namespace
* @private
* @global
*/
const style= {
/**
* @property {style_options} options
* @memberof style
* @public
*/
options: { allow: true, fit: "contain", size_variable: "--svg-icon-size" },
/**
* Keeping information the global style was created – see {@link style.cerate}
* @property {boolean} [is_created=false]
* @memberof style
* @private
*/
is_created: false,
/**
* Creates new `<style>` inside `<head>` with default styling of `<svg-icon>` (displays block and size)
* @method
* @memberof style
* @public
*/
create(){
if(!this.options.allow||this.is_created) return false;
const style_el= document.createElement("style");
style_el.type="text/css";
const { size_variable, fit }= this.options;
style_el.innerHTML=
`svg-icon { display: block; width: var(${size_variable}, 1em); height: var(${size_variable}, 1em); }` +
`svg-icon svg { width: 100%; height: 100%; object-fit: ${fit}; }`;
document.head.appendChild(style_el);
this.is_created= true;
}
};
/**
* Options for setting/getting/using aliases (mainly `separator`). Changes affect only newly created tags `<svg-icon>` since modification!
* @typedef {object} aliases_options
* @property {string} [separator=-] Separator for aliases: `alias`**separator**`icon_name`.
*/
/**
* Grouping alises functionalities
* @namespace
* @private
* @global
*/
const aliases= {
/**
* @property {aliases_options} options
* @memberof aliases
* @public
*/
options: { separator: "-" },
/**
* Contains all registered aliases
* @property {Map|null} list
* @private
*/
list: null,
/**
* Existence check
* @memberof aliases
* @public
* @param {string} alias Alias name
* @returns {boolean}
*/
has: function(alias){ return Boolean(this.list) && this.list.has(alias); },
/**
* Get coresponding path for given `alias` name. Use {@link aliases.has} before for existence check!
* @memberof aliases
* @public
* @param {string} alias Alias name
*/
get: function(alias){ return this.list.get(alias); }
};
/**
* Registers new alias – this affect only newly created tags `<svg-icon>` since modification!
* @global
* @public
* @param {string} alias Alias name
* @param {string} target Corresponding full path
* @example
* setAlias("icon", "icons_file.svg#");
* document.body.innerHTML+= '<svg-icon use="icon-icon_name"></svg-icon>';
* //is equivalent to
* document.body.innerHTML+= '<svg-icon use="icons_file.svg#icon_name"></svg-icon>';
*/
function setAlias(alias, target){
if(!aliases.list) aliases.list= new Map();
return aliases.list.set(alias, target);
}
/**
* Removes registered alias – this affect only newly created tags `<svg-icon>` since modification!
* @global
* @public
* @param {string} alias Alias name
*/
function removeAlias(alias){
if(!aliases.list) return false;
aliases.list.delete(alias);
if(!aliases.list.size) aliases.list= null;
return true;
}
/**
* Intended to changing defaults options in {@link style.options} and {@link aliases.options}
* @global
* @public
* @param {object} def
* @param {style_options} [def.style] Changing style options
* @param {aliases_options} [def.aliases] Changing aliases options
*/
function changeOptions({ style: style_options, aliases: aliases_options }= {}){
if(style_options) Object.assign(style.options, style_options);
if(aliases_options) Object.assign(aliases.options, aliases_options);
}
/**
* SVGIcon Custom Element. When created new `<svg-icon>` tag it registers global style – see {@link style}. Also {@link EventFronta} for attributes changes (before element mounitg) is registered there.
* @extends HTMLElement
* @public
* @property {HTMLElement} _icon Current icon (`<use>` tag) reference
* @property {EventFronta} _onmount_attributes Log events for
*/
class SVGIconElement extends HTMLElement{
constructor(){
super();
style.create();
/* instance vars */
this._icon= null;
this._onmount_attributes= new EventFronta();
}
/**
* Prepare `<svg>` and `<use>` tag for icon.
* @public
* @memberof SVGIconElement
* @returns {HTMLElement} `<use>` reference
*/
renderIcon(){
const svg= createElement("svg");
const icon= createElement("use");
svg.appendChild(icon);
this.appendChild(svg);
return icon;
}
/**
* Sets `href` of current icon ({@link SVGIconElement} properties)
* @public
* @memberof SVGIconElement
*/
setIcon(href){
if(!href||!this._icon) return false;
const { separator }= aliases.options;
const [ alias_candidate, ...rest ]= href.split(separator);
setHref(
this._icon,
aliases.has(alias_candidate) ? aliases.get(alias_candidate)+rest.join(separator) : href
);
}
/**
* Life cycle callback: This method is called when element is mounted to DOM. It renders icon ({@link SVGIconElement#renderIcon}) and process all cached *attributeChange* events.
* @public
* @memberof SVGIconElement
*/
connectedCallback(){
this._icon= this.renderIcon();
this._onmount_attributes.serveAll(args=> this.attributeChangedCallback(...args));
}
/**
* Life cycle callback: Called when element is removed from DOM. It clears icon and listenres.
* @public
* @memberof SVGIconElement
*/
disconnectedCallback(){
this._onmount_attributes.clear();
this._onmount_attributes= null;
this._icon= null;
}
/**
* All properties theirs changes will be cached by {@link SVGIconElement#attributeChangedCallback}
* @public
* @memberof SVGIconElement
*/
static get observedAttributes(){ return [ "use" ]; }
/**
* Life cycle callback: Element atribute change handler (see {@link SVGIconElement.observedAttributes}). It calls {@link SVGIconElement#setIcon} or save events params into {@link SVGIconElement} (if element wasn’t mounted).
* @public
* @memberof SVGIconElement
*/
attributeChangedCallback(...args){
const [ property, old_value, new_value ]= args;
if(old_value===new_value||property!=="use") return false;
if(!this._icon) return this._onmount_attributes.add(args);
return this.setIcon(new_value);
}
}
customElements.define("svg-icon", SVGIconElement);
return { setAlias, removeAlias, changeOptions, SVGIconElement };
}));