import merge from 'deepmerge'; import wrapInSvgString from './utils/wrap-in-svg-string'; import defaultConfig from './sprite.config'; export default class Sprite { /** * @param {Object} [config] */ constructor(config) { this.config = merge(defaultConfig, config || {}); this.symbols = []; } /** * Add new symbol. If symbol with the same id exists it will be replaced. * @param {SpriteSymbol} symbol * @return {boolean} `true` - symbol was added, `false` - replaced */ add(symbol) { const { symbols } = this; const existing = this.find(symbol.id); if (existing) { symbols[symbols.indexOf(existing)] = symbol; return false; } symbols.push(symbol); return true; } /** * Remove symbol & destroy it * @param {string} id * @return {boolean} `true` - symbol was found & successfully destroyed, `false` - otherwise */ remove(id) { const { symbols } = this; const symbol = this.find(id); if (symbol) { symbols.splice(symbols.indexOf(symbol), 1); symbol.destroy(); return true; } return false; } /** * @param {string} id * @return {SpriteSymbol|null} */ find(id) { return this.symbols.filter(s => s.id === id)[0] || null; } /** * @param {string} id * @return {boolean} */ has(id) { return this.find(id) !== null; } /** * @return {string} */ stringify() { const { attrs } = this.config; const stringifiedSymbols = this.symbols.map(s => s.stringify()).join(''); return wrapInSvgString(stringifiedSymbols, attrs); } /** * @return {string} */ toString() { return this.stringify(); } destroy() { this.symbols.forEach(s => s.destroy()); } }