Repository: juancabrera/react-asset-loader Branch: master Commit: acac7e928c62 Files: 7 Total size: 10.5 KB Directory structure: gitextract_ohqdivx9/ ├── LICENSE ├── LoadAssets.js ├── README.md ├── examples/ │ ├── gifs.html │ └── video.html ├── index.js └── package.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) Juan Cabrera (juan.me) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: LoadAssets.js ================================================ var LoadAssets = React.createClass({ getInitialState: function () { // 'loaded' state by default is false return {loaded: false}; }, componentDidMount: function () { var _self = this, totalAssets = this.props.assets.length, loadedAssets = 0 ; // Start loading all the assets Array.prototype.forEach.call(this.props.assets, function(asset) { _self.loadAsset(asset.uri, function(e) { loadedAssets++; if (loadedAssets == totalAssets) { // when all the assets are loaded set state 'loaded' to true _self.setState({loaded: true}); // when all the assets are loaded call the callback function if any if (typeof(_self.props.onLoad) === "function") _self.props.onLoad(); } }); }); }, loadAsset: function(uri, callback) { // preload if asset is image if (uri.toLowerCase().match("jpg|jpeg|gif|png|webp") !== null) { var image = new Image(); image.src = uri; image.onload = callback; } // preload if asset is video if (uri.toLowerCase().match("mp4|webm|ogv") !== null) { var video = document.createElement('video'); var source = document.createElement('source'); source.src = uri; video.setAttribute("preload", "auto"); video.appendChild(source); video.addEventListener('canplaythrough', callback, false); } }, render: function() { var output = []; if (!this.state.loaded) { // asset not loaded yet - loading UI output.push(
); } else { // asset fully loaded - show asset var assets = this.props.assets.map(function(asset) { var assetOutput; // it's an image if (asset.uri.toLowerCase().match("jpg|jpeg|gif|png|webp") !== null) { assetOutput = (); } // it's a video if (asset.uri.toLowerCase().match("mp4|webm|ogv") !== null) { // TODO: make it smart so it will create a video element with many sources instead of many video elements for different video formats assetOutput = ( ); } // adding props if any if (asset.attributes !== undefined) { Array.prototype.forEach.call(asset.attributes, function(a) { assetOutput.props[Object.keys(a)[0]] = a[Object.keys(a)[0]]; }); } output.push(assetOutput); }); } return (
{output}
); } }); ================================================ FILE: README.md ================================================ # React Asset Loader A simple react component for loading assets. It allows you to load from a single asset to multiple assets without affecting the pageload speed. You can also add custom classes, attributes and callback functions. ## Concept We all get scared of heavy websites, there are tons and tons of articles telling us why we shouldn't build websites using heavy assets like videos or gifs. But sometimes, you just need to, sometimes you just need to build a cool interactive experience with videos, or so something cool with gifs, or a long page with lots of high quality images, etc. This component is precisely for that. ## How it works This component allows you to load all the assets you want without affecting the pageload speed, this is because it will load the page and then it will start loading all the assets, it adds a `
` wrapper while it's loading the asset so you can have a nice custom loader, when the assets are loaded it will replace the `loading` wrapper with the actual assets. ## Install ``` npm install react-asset-loader --save-dev ``` Or just grab the component [LoadAsset.js](https://raw.githubusercontent.com/juancabrera/react-asset-loader/master/LoadAssets.js) directly. ## Usage #### Load a single asset ```javascript ``` #### Load multiple assets When you need to load more than one asset for one experience. It will be loadad after all the assets passed are loaded. ```javascript // or also var videos = [ {"uri":"/static/videos/video1.mp4"}, {"uri":"/static/videos/video2.mp4"} ] ``` #### Adding classes You can add classes to each asset you pass to the component. ```javascript ``` #### Adding attributes You can also add attributes to each asset you pass to the component. ```javascript // or also var video = [{ "uri":"/static/videos/video.mp4", "attributes": [ {"autoPlay":"true"}, {"loop":"true"} ] }] ``` #### Custom onLoad callback You can have a custom callback for when your assets are loaded. ```javascript ``` ## Examples You'll need a webserver in order to run the examples (CORS). The quickest way to do this is just run this on the project folder: ```python -m SimpleHTTPServer``` ## Feedback and contributions Are more than welcome 👊 ## License MIT Copyright (c) [Juan Cabrera](http://juan.me) ================================================ FILE: examples/gifs.html ================================================
================================================ FILE: examples/video.html ================================================
================================================ FILE: index.js ================================================ 'use strict'; module.exports = { LoadAssets: require('./LoadAssets') }; ================================================ FILE: package.json ================================================ { "name": "react-asset-loader", "version": "0.0.1", "description": "A simple react component for loading assets. It allows you to load from a single asset to multiple assets without affecting the pageload speed. You can also add custom classes, attributes and callback functions.", "main": "index.js", "author": { "name": "Juan Cabrera", "email": "juan.acc@gmail.com", "url" : "http://juan.me/" }, "homepage": "https://github.com/juancabrera/react-asset-loader", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "https://github.com/juancabrera/react-asset-loader" }, "keywords": [ "react", "asset-loader", "image", "video" ], "licenses": [ { "type": "MIT", "url": "https://github.com/juancabrera/react-asset-loader/blob/master/LICENSE" } ], "bugs": { "url": "https://github.com/juancabrera/react-asset-loader/issues" } }