1 | (function (global, factory) {
|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
3 | typeof define === 'function' && define.amd ? define(factory) :
|
4 | (global.Sprite = factory());
|
5 | }(this, (function () { 'use strict';
|
6 |
|
7 | var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function createCommonjsModule(fn, module) {
|
14 | return module = { exports: {} }, fn(module, module.exports), module.exports;
|
15 | }
|
16 |
|
17 | var deepmerge$1 = createCommonjsModule(function (module, exports) {
|
18 | (function (root, factory) {
|
19 | if (typeof undefined === 'function' && undefined.amd) {
|
20 | undefined(factory);
|
21 | } else {
|
22 | module.exports = factory();
|
23 | }
|
24 | }(commonjsGlobal, function () {
|
25 |
|
26 | function isMergeableObject(val) {
|
27 | var nonNullObject = val && typeof val === 'object';
|
28 |
|
29 | return nonNullObject
|
30 | && Object.prototype.toString.call(val) !== '[object RegExp]'
|
31 | && Object.prototype.toString.call(val) !== '[object Date]'
|
32 | }
|
33 |
|
34 | function emptyTarget(val) {
|
35 | return Array.isArray(val) ? [] : {}
|
36 | }
|
37 |
|
38 | function cloneIfNecessary(value, optionsArgument) {
|
39 | var clone = optionsArgument && optionsArgument.clone === true;
|
40 | return (clone && isMergeableObject(value)) ? deepmerge(emptyTarget(value), value, optionsArgument) : value
|
41 | }
|
42 |
|
43 | function defaultArrayMerge(target, source, optionsArgument) {
|
44 | var destination = target.slice();
|
45 | source.forEach(function(e, i) {
|
46 | if (typeof destination[i] === 'undefined') {
|
47 | destination[i] = cloneIfNecessary(e, optionsArgument);
|
48 | } else if (isMergeableObject(e)) {
|
49 | destination[i] = deepmerge(target[i], e, optionsArgument);
|
50 | } else if (target.indexOf(e) === -1) {
|
51 | destination.push(cloneIfNecessary(e, optionsArgument));
|
52 | }
|
53 | });
|
54 | return destination
|
55 | }
|
56 |
|
57 | function mergeObject(target, source, optionsArgument) {
|
58 | var destination = {};
|
59 | if (isMergeableObject(target)) {
|
60 | Object.keys(target).forEach(function (key) {
|
61 | destination[key] = cloneIfNecessary(target[key], optionsArgument);
|
62 | });
|
63 | }
|
64 | Object.keys(source).forEach(function (key) {
|
65 | if (!isMergeableObject(source[key]) || !target[key]) {
|
66 | destination[key] = cloneIfNecessary(source[key], optionsArgument);
|
67 | } else {
|
68 | destination[key] = deepmerge(target[key], source[key], optionsArgument);
|
69 | }
|
70 | });
|
71 | return destination
|
72 | }
|
73 |
|
74 | function deepmerge(target, source, optionsArgument) {
|
75 | var array = Array.isArray(source);
|
76 | var options = optionsArgument || { arrayMerge: defaultArrayMerge };
|
77 | var arrayMerge = options.arrayMerge || defaultArrayMerge;
|
78 |
|
79 | if (array) {
|
80 | return Array.isArray(target) ? arrayMerge(target, source, optionsArgument) : cloneIfNecessary(source, optionsArgument)
|
81 | } else {
|
82 | return mergeObject(target, source, optionsArgument)
|
83 | }
|
84 | }
|
85 |
|
86 | deepmerge.all = function deepmergeAll(array, optionsArgument) {
|
87 | if (!Array.isArray(array) || array.length < 2) {
|
88 | throw new Error('first argument should be an array with at least two elements')
|
89 | }
|
90 |
|
91 |
|
92 | return array.reduce(function(prev, next) {
|
93 | return deepmerge(prev, next, optionsArgument)
|
94 | })
|
95 | };
|
96 |
|
97 | return deepmerge
|
98 |
|
99 | }));
|
100 | });
|
101 |
|
102 | var namespaces_1 = createCommonjsModule(function (module, exports) {
|
103 | var namespaces = {
|
104 | svg: {
|
105 | name: 'xmlns',
|
106 | uri: 'http://www.w3.org/2000/svg'
|
107 | },
|
108 | xlink: {
|
109 | name: 'xmlns:xlink',
|
110 | uri: 'http://www.w3.org/1999/xlink'
|
111 | }
|
112 | };
|
113 |
|
114 | exports.default = namespaces;
|
115 | module.exports = exports.default;
|
116 | });
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | var objectToAttrsString = function (attrs) {
|
123 | return Object.keys(attrs).map(function (attr) {
|
124 | var value = attrs[attr].toString().replace(/"/g, '"');
|
125 | return (attr + "=\"" + value + "\"");
|
126 | }).join(' ');
|
127 | };
|
128 |
|
129 | var svg = namespaces_1.svg;
|
130 | var xlink = namespaces_1.xlink;
|
131 |
|
132 | var defaultAttrs = {};
|
133 | defaultAttrs[svg.name] = svg.uri;
|
134 | defaultAttrs[xlink.name] = xlink.uri;
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | var wrapInSvgString = function (content, attributes) {
|
142 | if ( content === void 0 ) content = '';
|
143 |
|
144 | var attrs = deepmerge$1(defaultAttrs, attributes || {});
|
145 | var attrsRendered = objectToAttrsString(attrs);
|
146 | return ("<svg " + attrsRendered + ">" + content + "</svg>");
|
147 | };
|
148 |
|
149 | var svg$1 = namespaces_1.svg;
|
150 | var xlink$1 = namespaces_1.xlink;
|
151 |
|
152 | var defaultConfig = {
|
153 | attrs: ( obj = {
|
154 | style: ['position: absolute', 'width: 0', 'height: 0'].join('; '),
|
155 | 'aria-hidden': 'true'
|
156 | }, obj[svg$1.name] = svg$1.uri, obj[xlink$1.name] = xlink$1.uri, obj )
|
157 | };
|
158 | var obj;
|
159 |
|
160 | var Sprite = function Sprite(config) {
|
161 | this.config = deepmerge$1(defaultConfig, config || {});
|
162 | this.symbols = [];
|
163 | };
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | Sprite.prototype.add = function add (symbol) {
|
171 | var ref = this;
|
172 | var symbols = ref.symbols;
|
173 | var existing = this.find(symbol.id);
|
174 |
|
175 | if (existing) {
|
176 | symbols[symbols.indexOf(existing)] = symbol;
|
177 | return false;
|
178 | }
|
179 |
|
180 | symbols.push(symbol);
|
181 | return true;
|
182 | };
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | Sprite.prototype.remove = function remove (id) {
|
190 | var ref = this;
|
191 | var symbols = ref.symbols;
|
192 | var symbol = this.find(id);
|
193 |
|
194 | if (symbol) {
|
195 | symbols.splice(symbols.indexOf(symbol), 1);
|
196 | symbol.destroy();
|
197 | return true;
|
198 | }
|
199 |
|
200 | return false;
|
201 | };
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 | Sprite.prototype.find = function find (id) {
|
208 | return this.symbols.filter(function (s) { return s.id === id; })[0] || null;
|
209 | };
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | Sprite.prototype.has = function has (id) {
|
216 | return this.find(id) !== null;
|
217 | };
|
218 |
|
219 |
|
220 |
|
221 |
|
222 | Sprite.prototype.stringify = function stringify () {
|
223 | var ref = this.config;
|
224 | var attrs = ref.attrs;
|
225 | var stringifiedSymbols = this.symbols.map(function (s) { return s.stringify(); }).join('');
|
226 | return wrapInSvgString(stringifiedSymbols, attrs);
|
227 | };
|
228 |
|
229 |
|
230 |
|
231 |
|
232 | Sprite.prototype.toString = function toString () {
|
233 | return this.stringify();
|
234 | };
|
235 |
|
236 | Sprite.prototype.destroy = function destroy () {
|
237 | this.symbols.forEach(function (s) { return s.destroy(); });
|
238 | };
|
239 |
|
240 | var sprite = new Sprite({ attrs: { id: '__SVG_SPRITE_NODE__' } });
|
241 |
|
242 | return sprite;
|
243 |
|
244 | })));
|