-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvgSprite.min.js.map
1 lines (1 loc) · 15.2 KB
/
svgSprite.min.js.map
1
{"version":3,"file":null,"sources":["src/utils.js","src/dom/css.js","src/dom/insert.js","src/dom/loaded.js","src/svg/parse.js","src/svg/cache.js","src/svg/xlink.js","src/svg/inject.js","src/svg/inline.js","src/index.js"],"sourcesContent":["// do nothing\nexport function noop () {}\n\n// map array-like lists\nexport function map (mapper, list) { return [].map.call(list, mapper) }\n\n// is string utility\nexport function string (x) { return 'string' === typeof x }\n\n// minification purpose\nexport const doc = document\n\n// matches polyfill\nexport const matches = Element.prototype.matches\n\t|| function (selector) { return -1 !== [].indexOf.call(doc.querySelectorAll(selector), this) }\n\n// query selector helper\nexport function find (selector, element) {\n\treturn element ? element.querySelector(selector) : null\n}\n\n// data-attr dataset read even when no support (IE10)\nexport function dataset (name, element) {\n\treturn element.dataset ?\n\t\telement.dataset[name]\n\t\t: element.getAttribute('data-' + name.replace(/[A-Z]/, uppercase => `-${uppercase.toLowerCase()}`))\n}\n\n// simple XHR get\nexport function get (url, callback) {\n\tconst request = new XMLHttpRequest\n\trequest.onreadystatechange = () => {\n\t\tconst status = 4 === request.readyState && request.status\n\t\tif (200 > status)\n\t\t\treturn\n\t\tif (!status || 400 <= status)\n\t\t\tthrow Error(`${status} ${request.statusText} ${url}`)\n\t\tcallback(request.responseXML || request.responseText)\n\t}\n\trequest.open('GET', url)\n\trequest.send()\n\treturn request\n}\n\n// raf wrapper / polyfill\nconst w = window\nconst raf = w.requestAnimationFrame\nexport const async = raf || (callback => setTimeout(callback, 16))\nasync.cancel = raf ? w.cancelAnimationFrame : clearTimeout\n\n// wait predicate to be true before calling callback (use polling)\nexport function wait (predicate, callback) {\n\tasync(predicate() ? callback : () => wait(predicate, callback))\n}\n","import {doc} from 'utils'\n\nconst sheet = doc.head.appendChild(doc.createElement('style')).sheet // inject a styleseet in the document\n\n/**\n * Inject CSS rules\n *\n * @param {String} selector CSS selector\n * @param {String} properties CSS rule body properties\n * @return {Function} Remove the inserted CSS rule\n */\nexport default function css (selector, properties) {\n\t// see https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/insertRule#Restrictions\n\tconst index = sheet.insertRule(`${selector} { ${properties} }`, sheet.cssRules.length)\n\treturn () => (sheet.deleteRule(index), sheet.insertRule('x{}', index))\n}\n","import {doc, matches} from 'utils'\nimport css from 'dom/css'\n\nconst animation = 'svg-insert'\n\n// init: add svg-insert animation css rule\nArray('', '-webkit-', '-moz-', '-ms-', '-o-').some(vendor => {\n\ttry { // insertRule throws on invalid @keyframes\n\t\treturn css(`@${vendor}keyframes ${animation}`, 'from { opacity: .99 } to { opacity: 1 }')\n\t} catch (error) {}\n})\n\n// add / remove animationstart event listener (with vendor prefixes)\nfunction listener (action, handler) { \n\tconst events = ['animationstart', 'MSAnimationStart', 'webkitAnimationStart']\n\tfor (let vendor in events)\n\t\tdoc[`${action}EventListener`](events[vendor], handler)\n}\n\n/**\n * Watch specific elements insertion in DOM (using animationstart event trick)\n *\n * @param {Function} callback Callback recieving every targeted element inserted in DOM\n * @param {String} selector CSS selector to target specific elements\n * @return {Function} Return the list of processed elements, stop watching if called with truthy argument\n */\nexport default function insert (callback, selector) {\n\tconst rule = `animation: ${animation} 1ms !important`\n\tconst remove = css(selector, `-webkit-${rule}; ${rule}`)\n\tconst inserted = []\n\tconst handler = event => {\n\t\tconst target = event.target\n\t\tif (event.animationName === animation\n\t\t\t&& matches.call(target, selector)\n\t\t\t&& -1 === inserted.indexOf(target)) { // only once, even if animation start again\n\t\t\tinserted.push(target)\n\t\t\tcallback(target)\n\t\t}\n\t}\n\tlistener('add', handler)\n\treturn clean => {\n\t\tif (clean) {\n\t\t\tremove()\n\t\t\tlistener('remove', handler)\n\t\t}\n\t\treturn inserted\n\t}\n}\n","import {doc} from 'utils'\n\n/**\n * Is the document loaded ?\n *\n * @return {Boolean}\n */\nexport default function loaded () {\n\treturn /interactive|complete/.test(doc.readyState)\n}\n","import {doc} from 'utils'\n\nconst dom = doc.createElement('div')\n\n/**\n * Parse SVG source\n *\n * @param {Document|String} source SVG source (usually returned by XHR as XMLDocument or markup source text)\n * @throws {Error} When source can't be parsed\n * @return {Element} Parsed SVG DOM Element\n */\nexport default function parse (source) {\n\tconst svg = source.documentElement || ((dom.innerHTML = String(source).trim()), dom.firstChild)\n\tif (!/svg/i.test(svg.nodeName))\n\t\tthrow Error(`Not a svg ${source}`)\n\treturn svg\n}\n","import {noop, doc, get, wait} from 'utils'\nimport loaded from 'dom/loaded'\nimport parse from 'svg/parse'\n\n/**\n * Cache SVG sprites and inject them in DOM\n *\n * @param {String} source SVG sprite source (URL or markup)\n * @param {Function?} callback Optional callback recieving the sprite SVG DOM Element injected\n * @return {Object} Request as stored in cache: a decorated XHR object or a simple object when markup is provided\n */\nexport default function cache (source, callback = noop) {\n\tlet request = cache[source]\n\tif (request) {\n\t\tconst cb = request.cb\n\t\trequest.svg ?\n\t\t\tcallback(request.svg) // done\n\t\t\t: (request.cb = svg => { cb(svg), callback(svg) }) // pending\n\t\treturn request\n\t}\n\tconst markup = /^\\s*<svg\\b/.test(source)\n\tconst append = function (svg) {\n\t\tsvg = doc.body.appendChild(parse(svg))\n\t\tsvg.setAttribute('data-source', markup ? 'markup' : source)\n\t\tsvg.setAttribute('display', 'none')\n\t\trequest.svg = svg\n\t\trequest.cb(svg)\n\t}\n\trequest = markup ?\n\t\t(wait(loaded, append.bind(null, source)), {}) // wait document to be loaded\n\t\t: get(source, append) // XHR request\n\trequest.cb = callback\n\tcache[source] = request\n\treturn request\n}\n","/**\n * Getter / setter of xlink attibute\n *\n * @param {Element} element DOM Element\n * @param {String?} href Optional for getter, a href link for setter\n * @return {String|null} Attribute value when called as getter / void when setter\n */\nexport default function xlink (element, href) {\n\treturn element[`${href ? 'set' : 'get'}AttributeNS`]('http://www.w3.org/1999/xlink', 'href', href)\n}\n","import {dataset, find} from 'utils'\nimport cache from 'svg/cache'\nimport parse from 'svg/parse'\nimport xlink from 'svg/xlink'\n\n/**\n * Create callback that append to given DOM element a SVG pointing to an inlined SVG sprite\n *\n * @param {Function} before Callback to validate / customize the injected SVG\n * @param {String} source SVG sprite source\n * @return {Function} Cache the sprite, inject it in DOM only once. In given element, append a SVG pointing to one of the sprite symbol\n */\nexport default function inject (before, source) {\n\treturn element => {\n\t\tcache(source, sprite => {\n\t\t\tconst fragment = dataset('svg', element)\n\t\t\tconst svg = parse(`<svg role=\"presentation\"><use xlink:href=\"#${fragment}\"/></svg>`)\n\t\t\tif (false !== before(svg, fragment, xlink.bind(null, find('use', svg)), element, sprite, source))\n\t\t\t\telement.appendChild(svg)\n\t\t})\n\t}\n}\n","import cache from 'svg/cache'\nimport xlink from 'svg/xlink'\n\n/**\n * Create callback that swap SVG use's xlink:href referencing external SVG sprite to point to the inlined SVG sprite\n *\n * @param {Function} before Callback to validate / customize the SVG use's xlink:href attribute swapping\n * @return {Function} Cache the sprite, inject it in DOM only once. For given SVG element, change the use's xlink:href to point to the inlined sprite symbol\n */\nexport default function inline (before) {\n\treturn element => {\n\t\tconst asset = xlink(element)\n\t\tif (asset)\n\t\t\tasset.replace(/^(.+?)(#(.+))$/, (_, url, hash, fragment) => {\n\t\t\t\tcache(url, sprite => {\n\t\t\t\t\tif (false !== before(element.ownerSVGElement, fragment, sprite, url))\n\t\t\t\t\t\txlink(element, hash)\n\t\t\t\t})\n\t\t\t})\n\t}\n}\n","import {string, map, find, noop} from 'utils'\nimport insert from 'dom/insert'\nimport inject from 'svg/inject'\nimport inline from 'svg/inline'\n\n/**\n * Apply the handler to every elements\n *\n * @param {Function} handler How to process each element\n * @param {HTMLCollection|NodeList|Array|String} elements List of elements or CSS selector\n * @param {Function} finder Find SVG Element relative to processed element\n * @return {Function} Return the list of processed elements. If 'elements' is a selector stop processing new elements if called with truthy argument\n */\nfunction call (handler, elements, finder) {\n\tif (string(elements)) {\n\t\tconst inserted = insert(handler, elements)\n\t\treturn (clean) => map(finder, inserted(clean))\n\t}\n\tmap(handler, elements)\n\treturn () => map(finder, elements)\n} \n\n/**\n * Target use tags pointing to external resource\n * \n * @param {HTMLCollection|NodeList|Array|String} elements List of elements or CSS selector\n * @return {String|Array} Filtered list / modified selector to target use tag pointing to external resource\n */\nfunction use (elements) {\n\tconst external = 'use[*|href]:not([*|href^=\"#\"])'\n\treturn string(elements) ?\n\t\t`${elements} ${external}`\n\t\t: map(find.bind(null, external), elements).filter(Boolean)\n}\n\n/**\n * Inject SVGs using SVG sprite symbols\n * \n * @param {Object} settings Specify 'source' of the sprite. Optionally, 'elements' as CSS selector or DOM list and 'before' callback\n * @return {Function} List processed SVG, stop processing new elements when called with truthy argument\n */\nexport default function svgSprite (settings) {\n\treturn call(\n\t\tinject(settings.before || noop, settings.source)\n\t\t, settings.elements || '[data-svg]'\n\t\t, find.bind(null, 'svg')\n\t)\n}\n\n/**\n * Inline SVGs pointing to external resources\n *\n * @param {Object?} settings Optional, 'elements' as CSS selector or DOM list and 'before' callback\n * @return {Function} List processed SVG, stop processing new elements when called with truthy argument\n */\nsvgSprite.inline = function (settings = {}) {\n\treturn call(\n\t\tinline(settings.before || noop)\n\t\t, use(settings.elements || 'svg')\n\t\t, element => element.ownerSVGElement\n\t)\n}\n"],"names":["noop","map","mapper","list","call","string","x","find","selector","element","querySelector","dataset","name","getAttribute","replace","uppercase","toLowerCase","get","url","callback","const","request","XMLHttpRequest","onreadystatechange","status","readyState","Error","responseXML","responseText","open","send","wait","predicate","async","css","properties","index","sheet","insertRule","cssRules","length","deleteRule","listener","action","handler","events","let","vendor","doc","insert","rule","animation","remove","inserted","event","target","animationName","matches","indexOf","push","clean","loaded","test","parse","source","svg","documentElement","dom","innerHTML","String","trim","firstChild","nodeName","cache","cb","markup","append","body","appendChild","setAttribute","bind","xlink","href","inject","before","sprite","fragment","inline","asset","_","hash","ownerSVGElement","elements","finder","use","external","filter","Boolean","svgSprite","settings","document","Element","prototype","querySelectorAll","this","w","window","raf","requestAnimationFrame","setTimeout","cancel","cancel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