UNPKG

6.68 kBJavaScriptView Raw
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
7var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
8
9
10
11
12
13function createCommonjsModule(fn, module) {
14 return module = { exports: {} }, fn(module, module.exports), module.exports;
15}
16
17var 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
26function 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
34function emptyTarget(val) {
35 return Array.isArray(val) ? [] : {}
36}
37
38function cloneIfNecessary(value, optionsArgument) {
39 var clone = optionsArgument && optionsArgument.clone === true;
40 return (clone && isMergeableObject(value)) ? deepmerge(emptyTarget(value), value, optionsArgument) : value
41}
42
43function 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
57function 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
74function 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
86deepmerge.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 // we are sure there are at least 2 values, so it is safe to have no initial value
92 return array.reduce(function(prev, next) {
93 return deepmerge(prev, next, optionsArgument)
94 })
95};
96
97return deepmerge
98
99}));
100});
101
102var namespaces_1 = createCommonjsModule(function (module, exports) {
103var 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
114exports.default = namespaces;
115module.exports = exports.default;
116});
117
118/**
119 * @param {Object} attrs
120 * @return {string}
121 */
122var objectToAttrsString = function (attrs) {
123 return Object.keys(attrs).map(function (attr) {
124 var value = attrs[attr].toString().replace(/"/g, '&quot;');
125 return (attr + "=\"" + value + "\"");
126 }).join(' ');
127};
128
129var svg = namespaces_1.svg;
130var xlink = namespaces_1.xlink;
131
132var defaultAttrs = {};
133defaultAttrs[svg.name] = svg.uri;
134defaultAttrs[xlink.name] = xlink.uri;
135
136/**
137 * @param {string} [content]
138 * @param {Object} [attributes]
139 * @return {string}
140 */
141var 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
149var svg$1 = namespaces_1.svg;
150var xlink$1 = namespaces_1.xlink;
151
152var 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};
158var obj;
159
160var Sprite = function Sprite(config) {
161 this.config = deepmerge$1(defaultConfig, config || {});
162 this.symbols = [];
163};
164
165/**
166 * Add new symbol. If symbol with the same id exists it will be replaced.
167 * @param {SpriteSymbol} symbol
168 * @return {boolean} `true` - symbol was added, `false` - replaced
169 */
170Sprite.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 * Remove symbol & destroy it
186 * @param {string} id
187 * @return {boolean} `true` - symbol was found & successfully destroyed, `false` - otherwise
188 */
189Sprite.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 * @param {string} id
205 * @return {SpriteSymbol|null}
206 */
207Sprite.prototype.find = function find (id) {
208 return this.symbols.filter(function (s) { return s.id === id; })[0] || null;
209};
210
211/**
212 * @param {string} id
213 * @return {boolean}
214 */
215Sprite.prototype.has = function has (id) {
216 return this.find(id) !== null;
217};
218
219/**
220 * @return {string}
221 */
222Sprite.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 * @return {string}
231 */
232Sprite.prototype.toString = function toString () {
233 return this.stringify();
234};
235
236Sprite.prototype.destroy = function destroy () {
237 this.symbols.forEach(function (s) { return s.destroy(); });
238};
239
240var sprite = new Sprite({ attrs: { id: '__SVG_SPRITE_NODE__' } });
241
242return sprite;
243
244})));