├── .gitignore ├── .eslintrc ├── bower.json ├── index.html ├── package.json ├── README.md ├── LICENSE ├── svg-sprite-injector.js └── sprite.svg /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | root: true 2 | 3 | rules: 4 | semi: [2, 'always'] 5 | semi-spacing: [2] 6 | quotes: [2, 'single'] 7 | curly: [2, 'all'] 8 | brace-style: [2, '1tbs', { allowSingleLine: false }] 9 | space-after-keywords: [2, 'always'] 10 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "svg-sprite-injector", 3 | "main": "svg-sprite-injector.js", 4 | "homepage": "https://github.com/TrySound/svg-sprite-injector", 5 | "authors": [ 6 | "Bogdan Chadkin " 7 | ], 8 | "description": "Async load svg-sprites", 9 | "keywords": [ 10 | "svg", 11 | "sprite", 12 | "injector", 13 | "svgstore" 14 | ], 15 | "license": "MIT", 16 | "ignore": [ 17 | "**/.*", 18 | "node_modules", 19 | "bower_components", 20 | "test", 21 | "tests" 22 | ] 23 | } 24 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SVG Sprite Injector 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "svg-sprite-injector", 3 | "version": "1.0.0", 4 | "description": "Async load svg-sprites", 5 | "main": "svg-sprite-injector.js", 6 | "files": [ 7 | "svg-sprite-injector.js" 8 | ], 9 | "scripts": { 10 | "test": "eslint svg-sprite-injector.js" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/TrySound/svg-sprite-injector.git" 15 | }, 16 | "keywords": [ 17 | "svg", 18 | "sprite", 19 | "svgstore", 20 | "injector" 21 | ], 22 | "author": "Bogdan Chadkin ", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/TrySound/svg-sprite-injector/issues" 26 | }, 27 | "homepage": "https://github.com/TrySound/svg-sprite-injector#readme", 28 | "devDependencies": { 29 | "eslint": "^1.3.1" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # svg-sprite-injector 2 | Async load svg-sprites 3 | 4 | > Inspired by http://osvaldas.info/caching-svg-sprite-in-localstorage 5 | 6 | ## Usage 7 | 8 | Just simply call 9 | ```js 10 | svgSpriteInjector('sprite.svg'); 11 | ``` 12 | 13 | ## Cache by localStorage 14 | 15 | ```js 16 | svgSpriteInjector('sprite.svg', { 17 | revision: 1 // Revision identifier 18 | }); 19 | ``` 20 | 21 | ## Params in DOM 22 | 23 | ```js 24 | svgSpriteInjector(document.body); 25 | ``` 26 | 27 | ```html 28 | 29 |
30 | 31 | ``` 32 | 33 | ## Combine separated SVG's 34 | 35 | For combine svg files into one with elements use 36 | [gulp-svgstore](https://github.com/w0rm/gulp-svgstore) 37 | or 38 | [grunt-svgstore](https://github.com/FWeinb/grunt-svgstore) 39 | 40 | ## License 41 | 42 | [The MIT License (MIT)](LICENSE) 43 | 44 | Copyright © 2015 Bogdan Chadkin 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Bogdan Chadkin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /svg-sprite-injector.js: -------------------------------------------------------------------------------- 1 | ;(function(window, document) { 2 | 'use strict'; 3 | 4 | var isSvg = document.createElementNS && document.createElementNS( 'http://www.w3.org/2000/svg', 'svg' ).createSVGRect; 5 | var localStorage = false; 6 | try { 7 | localStorage = 'localStorage' in window && window['localStorage'] !== null ? window.localStorage : false; 8 | } catch (e) { 9 | } 10 | 11 | function svgSpriteInjector(source, opts) { 12 | var file; 13 | opts = opts || {}; 14 | 15 | if (source instanceof Node) { 16 | file = source.getAttribute('data-svg-sprite'); 17 | opts.revision = source.getAttribute('data-svg-sprite-revision') || opts.revision; 18 | } else if (typeof source === 'string') { 19 | file = source; 20 | } 21 | 22 | if (isSvg) { 23 | if (file) { 24 | injector(file, opts); 25 | } else { 26 | console.error('svg-sprite-injector: undefined sprite filename!'); 27 | } 28 | } else { 29 | console.error('svg-sprite-injector require ie9 or greater!'); 30 | } 31 | }; 32 | 33 | function injector(filepath, opts) { 34 | var name = 'injectedSVGSprite' + filepath, 35 | revision = opts.revision, 36 | request; 37 | 38 | // localStorage cache 39 | if (revision !== undefined && localStorage && localStorage[name + 'Rev'] == revision) { 40 | return injectOnLoad(localStorage[name]); 41 | } 42 | 43 | // Async load 44 | request = new XMLHttpRequest(); 45 | request.open('GET', filepath, true); 46 | request.onreadystatechange = function (e) { 47 | var data; 48 | 49 | if (request.readyState === 4 && request.status >= 200 && request.status < 400) { 50 | injectOnLoad(data = request.responseText); 51 | if (revision !== undefined && localStorage) { 52 | localStorage[name] = data; 53 | localStorage[name + 'Rev'] = revision; 54 | } 55 | } 56 | }; 57 | request.send(); 58 | } 59 | 60 | function injectOnLoad(data) { 61 | if (data) { 62 | if (document.body) { 63 | injectData(data); 64 | } else { 65 | document.addEventListener('DOMContentLoaded', injectData.bind(null, data)); 66 | } 67 | } 68 | } 69 | 70 | function injectData(data) { 71 | var body = document.body; 72 | body.insertAdjacentHTML('afterbegin', data); 73 | if (body.firstChild.tagName === 'svg') { 74 | body.firstChild.style.display = 'none'; 75 | } 76 | } 77 | 78 | if (typeof exports === 'object') { 79 | module.exports = svgSpriteInjector; 80 | } else { 81 | window.svgSpriteInjector = svgSpriteInjector; 82 | } 83 | 84 | } (window, document)); 85 | -------------------------------------------------------------------------------- /sprite.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | --------------------------------------------------------------------------------