sprite.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
  7. function createCommonjsModule(fn, module) {
  8. return module = { exports: {} }, fn(module, module.exports), module.exports;
  9. }
  10. var deepmerge = createCommonjsModule(function (module, exports) {
  11. (function (root, factory) {
  12. if (typeof undefined === 'function' && undefined.amd) {
  13. undefined(factory);
  14. } else {
  15. module.exports = factory();
  16. }
  17. }(commonjsGlobal, function () {
  18. function isMergeableObject(val) {
  19. var nonNullObject = val && typeof val === 'object';
  20. return nonNullObject
  21. && Object.prototype.toString.call(val) !== '[object RegExp]'
  22. && Object.prototype.toString.call(val) !== '[object Date]'
  23. }
  24. function emptyTarget(val) {
  25. return Array.isArray(val) ? [] : {}
  26. }
  27. function cloneIfNecessary(value, optionsArgument) {
  28. var clone = optionsArgument && optionsArgument.clone === true;
  29. return (clone && isMergeableObject(value)) ? deepmerge(emptyTarget(value), value, optionsArgument) : value
  30. }
  31. function defaultArrayMerge(target, source, optionsArgument) {
  32. var destination = target.slice();
  33. source.forEach(function(e, i) {
  34. if (typeof destination[i] === 'undefined') {
  35. destination[i] = cloneIfNecessary(e, optionsArgument);
  36. } else if (isMergeableObject(e)) {
  37. destination[i] = deepmerge(target[i], e, optionsArgument);
  38. } else if (target.indexOf(e) === -1) {
  39. destination.push(cloneIfNecessary(e, optionsArgument));
  40. }
  41. });
  42. return destination
  43. }
  44. function mergeObject(target, source, optionsArgument) {
  45. var destination = {};
  46. if (isMergeableObject(target)) {
  47. Object.keys(target).forEach(function (key) {
  48. destination[key] = cloneIfNecessary(target[key], optionsArgument);
  49. });
  50. }
  51. Object.keys(source).forEach(function (key) {
  52. if (!isMergeableObject(source[key]) || !target[key]) {
  53. destination[key] = cloneIfNecessary(source[key], optionsArgument);
  54. } else {
  55. destination[key] = deepmerge(target[key], source[key], optionsArgument);
  56. }
  57. });
  58. return destination
  59. }
  60. function deepmerge(target, source, optionsArgument) {
  61. var array = Array.isArray(source);
  62. var options = optionsArgument || { arrayMerge: defaultArrayMerge };
  63. var arrayMerge = options.arrayMerge || defaultArrayMerge;
  64. if (array) {
  65. return Array.isArray(target) ? arrayMerge(target, source, optionsArgument) : cloneIfNecessary(source, optionsArgument)
  66. } else {
  67. return mergeObject(target, source, optionsArgument)
  68. }
  69. }
  70. deepmerge.all = function deepmergeAll(array, optionsArgument) {
  71. if (!Array.isArray(array) || array.length < 2) {
  72. throw new Error('first argument should be an array with at least two elements')
  73. }
  74. // we are sure there are at least 2 values, so it is safe to have no initial value
  75. return array.reduce(function(prev, next) {
  76. return deepmerge(prev, next, optionsArgument)
  77. })
  78. };
  79. return deepmerge
  80. }));
  81. });
  82. var namespaces_1 = createCommonjsModule(function (module, exports) {
  83. var namespaces = {
  84. svg: {
  85. name: 'xmlns',
  86. uri: 'http://www.w3.org/2000/svg'
  87. },
  88. xlink: {
  89. name: 'xmlns:xlink',
  90. uri: 'http://www.w3.org/1999/xlink'
  91. }
  92. };
  93. exports.default = namespaces;
  94. module.exports = exports.default;
  95. });
  96. /**
  97. * @param {Object} attrs
  98. * @return {string}
  99. */
  100. var objectToAttrsString = function (attrs) {
  101. return Object.keys(attrs).map(function (attr) {
  102. var value = attrs[attr].toString().replace(/"/g, '&quot;');
  103. return (attr + "=\"" + value + "\"");
  104. }).join(' ');
  105. };
  106. var svg = namespaces_1.svg;
  107. var xlink = namespaces_1.xlink;
  108. var defaultAttrs = {};
  109. defaultAttrs[svg.name] = svg.uri;
  110. defaultAttrs[xlink.name] = xlink.uri;
  111. /**
  112. * @param {string} [content]
  113. * @param {Object} [attributes]
  114. * @return {string}
  115. */
  116. var wrapInSvgString = function (content, attributes) {
  117. if ( content === void 0 ) content = '';
  118. var attrs = deepmerge(defaultAttrs, attributes || {});
  119. var attrsRendered = objectToAttrsString(attrs);
  120. return ("<svg " + attrsRendered + ">" + content + "</svg>");
  121. };
  122. var svg$1 = namespaces_1.svg;
  123. var xlink$1 = namespaces_1.xlink;
  124. var defaultConfig = {
  125. attrs: ( obj = {
  126. style: ['position: absolute', 'width: 0', 'height: 0'].join('; '),
  127. 'aria-hidden': 'true'
  128. }, obj[svg$1.name] = svg$1.uri, obj[xlink$1.name] = xlink$1.uri, obj )
  129. };
  130. var obj;
  131. var Sprite = function Sprite(config) {
  132. this.config = deepmerge(defaultConfig, config || {});
  133. this.symbols = [];
  134. };
  135. /**
  136. * Add new symbol. If symbol with the same id exists it will be replaced.
  137. * @param {SpriteSymbol} symbol
  138. * @return {boolean} `true` - symbol was added, `false` - replaced
  139. */
  140. Sprite.prototype.add = function add (symbol) {
  141. var ref = this;
  142. var symbols = ref.symbols;
  143. var existing = this.find(symbol.id);
  144. if (existing) {
  145. symbols[symbols.indexOf(existing)] = symbol;
  146. return false;
  147. }
  148. symbols.push(symbol);
  149. return true;
  150. };
  151. /**
  152. * Remove symbol & destroy it
  153. * @param {string} id
  154. * @return {boolean} `true` - symbol was found & successfully destroyed, `false` - otherwise
  155. */
  156. Sprite.prototype.remove = function remove (id) {
  157. var ref = this;
  158. var symbols = ref.symbols;
  159. var symbol = this.find(id);
  160. if (symbol) {
  161. symbols.splice(symbols.indexOf(symbol), 1);
  162. symbol.destroy();
  163. return true;
  164. }
  165. return false;
  166. };
  167. /**
  168. * @param {string} id
  169. * @return {SpriteSymbol|null}
  170. */
  171. Sprite.prototype.find = function find (id) {
  172. return this.symbols.filter(function (s) { return s.id === id; })[0] || null;
  173. };
  174. /**
  175. * @param {string} id
  176. * @return {boolean}
  177. */
  178. Sprite.prototype.has = function has (id) {
  179. return this.find(id) !== null;
  180. };
  181. /**
  182. * @return {string}
  183. */
  184. Sprite.prototype.stringify = function stringify () {
  185. var ref = this.config;
  186. var attrs = ref.attrs;
  187. var stringifiedSymbols = this.symbols.map(function (s) { return s.stringify(); }).join('');
  188. return wrapInSvgString(stringifiedSymbols, attrs);
  189. };
  190. /**
  191. * @return {string}
  192. */
  193. Sprite.prototype.toString = function toString () {
  194. return this.stringify();
  195. };
  196. Sprite.prototype.destroy = function destroy () {
  197. this.symbols.forEach(function (s) { return s.destroy(); });
  198. };
  199. return Sprite;
  200. })));