Repository: David-Desmaisons/Vue.ImagesLoaded Branch: master Commit: 97792cb429ec Files: 28 Total size: 46.6 KB Directory structure: gitextract_gmn_0ea_/ ├── .babelrc ├── .editorconfig ├── .gitignore ├── LICENSE ├── README.md ├── build/ │ ├── build.js │ ├── check-versions.js │ ├── dev-client.js │ ├── dev-server.js │ ├── utils.js │ ├── webpack.base.conf.js │ ├── webpack.dev.conf.js │ └── webpack.prod.conf.js ├── config/ │ ├── dev.env.js │ ├── index.js │ └── prod.env.js ├── debug.log ├── dist/ │ └── vueimagesloaded.js ├── example/ │ ├── App.vue │ ├── components/ │ │ └── Hello.vue │ ├── index.html │ ├── main.js │ └── simple/ │ ├── css/ │ │ └── main.css │ ├── index.html │ └── js/ │ └── main.js ├── isObject.js ├── package.json └── src/ └── imagesLoadedDirective.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .babelrc ================================================ { "presets": ["es2015", "stage-2"], "plugins": ["transform-runtime", "add-module-exports", "transform-es2015-modules-umd"], "comments": false } ================================================ FILE: .editorconfig ================================================ root = true [*] charset = utf-8 indent_style = space indent_size = 2 end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true ================================================ FILE: .gitignore ================================================ .DS_Store node_modules/ npm-debug.log /example/simple/bower_components/ ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2016 David Desmaisons 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: README.md ================================================ # Vue.ImagesLoaded [![GitHub open issues](https://img.shields.io/github/issues/David-Desmaisons/Vue.ImagesLoaded.svg?maxAge=2592000)](https://github.com/David-Desmaisons/Vue.ImagesLoaded/issues?q=is%3Aopen+is%3Aissue) [![GitHub closed issues](https://img.shields.io/github/issues-closed/David-Desmaisons/Vue.ImagesLoaded.svg?maxAge=2592000)](https://github.com/David-Desmaisons/Vue.ImagesLoaded/issues?q=is%3Aissue+is%3Aclosed) [![Npm download](https://img.shields.io/npm/dt/vue-images-loaded.svg?maxAge=2592000)](https://www.npmjs.com/package/vue-images-loaded) [![Npm version](https://img.shields.io/npm/v/vue-images-loaded.svg?maxAge=2592000)](https://www.npmjs.com/package/vue-images-loaded) [![vue2](https://img.shields.io/badge/vue-2.x-brightgreen.svg)](https://vuejs.org/) [![MIT License](https://img.shields.io/github/license/David-Desmaisons/Vue.ImagesLoaded.svg)](https://github.com/David-Desmaisons/Vue.ImagesLoaded/blob/master/LICENSE) A Vue.js 2.0 directive to detect when images have been loaded, based on [imagesLoaded](http://imagesloaded.desandro.com/) This directive allows to get a callback when children images are loaded in a container element.
Plays nicely with [vue.isotope](https://github.com/David-Desmaisons/Vue.Isotope) to allow re-layout when images are loaded. ## Demo ![demo gif](vueimagesloaded.gif) ## Typical usage ```HTML
``` ```javascript import imagesLoaded from 'vue-images-loaded' export default { directives: { imagesLoaded }, methods: { imageProgress(instance, image ) { const result = image.isLoaded ? 'loaded' : 'broken'; console.log( 'image is ' + result + ' for ' + image.img.src ); } ``` ## Isotope Example ```HTML
{{element.name}}
{{element.id}} Not found
``` ```javascript import imagesLoaded from 'vue-images-loaded' export default { directives: { imagesLoaded }, methods: { layout () { this.$refs.cpt.layout('masonry'); } } ``` ## API ### Using directive - When used without argument nor modifiers: ```HTML
``` Directive value:
```javascript function loaded(instance){ //... } ``` loaded is a `Function` triggered after all images have been either loaded or confirmed broken.
Function parameter: ImagesLoaded instance - When used with `on` argument but no modifiers: ```HTML
``` Directive value:
```javascript listener:{ done(instance){ //... }, fail(instance){ //... } } ``` listener is an `Object` containing callback functions.
Function should be named and will received arguments as described in [Imagesloaded](http://imagesloaded.desandro.com/) - When used with `on` argument and modifier: ```HTML
``` Directive value:
```javascript function callback(instance, img){ //... } ``` callback is a `Function` triggered acording to modifier name `always`, `done`, `fail`, `progress`.
Function parameter: ImagesLoaded instance, and image information for `progess` only. ### ImagesLoaded instance - Properties: - imagesLoaded.images
`Array` of LoadingImage instances for each image detected
### LoadingImage instance - Property: - LoadingImage.img `Image` - The img element - LoadingImage.isLoaded `Boolean` - true when the image has succesfully loaded ## Installation - Available through bower and npm: ``` js npm install vue-images-loaded --save ``` ``` js bower install vue.ImagesLoaded -save ``` - #### For Modules ``` js // ES6 import imagesLoaded from 'vue-images-loaded' ... export default { directives: { imagesLoaded, } ... // ES5 var imagesLoaded = require('vue-images-loaded') ``` - #### For ` ================================================ FILE: example/components/Hello.vue ================================================ ================================================ FILE: example/index.html ================================================ isostope-example
================================================ FILE: example/main.js ================================================ // The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import App from './App' import 'bulma/css/bulma.css' /* eslint-disable no-new */ new Vue({ el: '#app', template: '', components: { App } }) ================================================ FILE: example/simple/css/main.css ================================================ .item { background-color: #eee; padding: 10px; /*width: 200px; height: 200px;*/ margin-bottom: 10px; box-sizing: border-box; font-family: monospace; color: #333; border: 2px solid #b6b5b4; } * { box-sizing: border-box; } body { font-family: sans-serif; } /* ---- button ---- */ .button { display: inline-block; padding: 0.5em 1.0em; background: #EEE; border: none; border-radius: 7px; background-image: linear-gradient( to bottom, hsla(0, 0%, 0%, 0), hsla(0, 0%, 0%, 0.2) ); color: #222; font-family: sans-serif; font-size: 16px; text-shadow: 0 1px white; cursor: pointer; } .button:hover { background-color: #8CF; text-shadow: 0 1px hsla(0, 0%, 100%, 0.5); color: #222; } .button:active, .button.is-checked { background-color: #28F; } .is-checked { background-color: #28F; } .button.is-checked { color: white; text-shadow: 0 -1px hsla(0, 0%, 0%, 0.8); } .button:active { box-shadow: inset 0 1px 10px hsla(0, 0%, 0%, 0.8); } /* ---- button-group ---- */ .button-group { margin-bottom: 20px; } .button-group:after { content: ''; display: block; clear: both; } .button-group .button { float: left; border-radius: 0; margin-left: 0; margin-right: 1px; } .button-group .button:first-child { border-radius: 0.5em 0 0 0.5em; } .button-group .button:last-child { border-radius: 0 0.5em 0.5em 0; } /* ---- isotope ---- */ .grid { border: 1px solid #333; } /* clear fix */ .grid:after { content: ''; display: block; clear: both; } .isoDefault { min-height: 210px; } /* ---- .element-item ---- */ .element-item { position: relative; float: left; width: 100px; height: 100px; margin: 5px; padding: 10px; background: #888; color: #262524; } .element-item > * { margin: 0; padding: 0; } .element-item .name { position: absolute; left: 10px; top: 60px; text-transform: none; letter-spacing: 0; font-size: 12px; font-weight: normal; } .element-item .symbol { position: absolute; left: 10px; top: 0px; font-size: 42px; font-weight: bold; color: white; } .element-item .number { position: absolute; right: 8px; top: 5px; } .element-item .weight { position: absolute; left: 10px; top: 76px; font-size: 12px; } .element-item.alkali { background: #F00; background: hsl( 0, 100%, 50%); } .element-item.alkaline-earth { background: #F80; background: hsl( 36, 100%, 50%); } .element-item.lanthanoid { background: #FF0; background: hsl( 72, 100%, 50%); } .element-item.actinoid { background: #0F0; background: hsl( 108, 100%, 50%); } .element-item.transition { background: #0F8; background: hsl( 144, 100%, 50%); } .element-item.post-transition { background: #0FF; background: hsl( 180, 100%, 50%); } .element-item.metalloid { background: #08F; background: hsl( 216, 100%, 50%); } .element-item.diatomic { background: #00F; background: hsl( 252, 100%, 50%); } .element-item.halogen { background: #F0F; background: hsl( 288, 100%, 50%); } .element-item.noble-gas { background: #F08; background: hsl( 324, 100%, 50%); } ================================================ FILE: example/simple/index.html ================================================ Simple Example

Vue isotope

{{element.name}}
{{element.id}} Not found

================================================ FILE: example/simple/js/main.js ================================================ var count=0; function getImageSrc () { var size = Math.random() * 3 + 1; var width = Math.random() * 110 + 100; width = Math.round( width * size ); var height = Math.round( 140 * size ); var rando = Math.ceil( Math.random() * 1000 ); // 10% chance of broken image src // random parameter to prevent cached images return rando < 100 ? 'http://foo/broken-' + rando + '.jpg' : // use lorempixel for great random images 'http://lorempixel.com/' + width + '/' + height + '/' + '?' + rando; } var vm = new Vue({ el: "#main", directives: { imagesLoaded: window.VueImagesLoaded }, data: { list:[ {name:"John", id:25, src: getImageSrc ()}, {name:"Joao", id: 7, src: getImageSrc ()}, {name:"Albert", id: 12, src: getImageSrc ()}, {name:"Jean", id: 100, src: getImageSrc ()}], selected: null, sortOption:null, filterOption:null, filterText: "" }, methods:{ getOptions: function () { var _this = this return { layoutMode: 'masonry', masonry: { gutter: 10 }, getSortData: { id: "id", name: function(itemElem){ return itemElem.name.toLowerCase(); } }, getFilterData:{ isEven: function(itemElem){ return itemElem.id % 2 === 0; }, isOdd: function(itemElem){ return itemElem.id % 2 !== 0; }, filterByText: function(itemElem){ return itemElem.name.toLowerCase().includes(_this.filterText.toLowerCase()); } } } }, add: function () { this.list.push({name:'Juan', id:count++, src: getImageSrc ()}); }, layout: function () { this.$refs.cpt.layout('masonry'); }, replace: function(){ this.list=[{name:'Edgard', id: count++, src: getImageSrc ()}, {name:'James', id:count++, src: getImageSrc ()}] }, remove: function(){ if (this.list.length) this.list.splice(0, 1) } } }); ================================================ FILE: isObject.js ================================================ var convert = require('./convert'), func = convert('isObject', require('../isObject'), require('./_falseOptions')); func.placeholder = require('./placeholder'); module.exports = func; ================================================ FILE: package.json ================================================ { "name": "vue-images-loaded", "version": "1.1.2", "description": "Vue.js 2.0 directive to detect image loading", "main": "dist/vueimagesloaded.js", "files": [ "dist/vueimagesloaded.js" ], "keywords": [ "vue", "vuejs", "imagesLoaded", "web-components" ], "author": "David Desmaisons", "scripts": { "dev": "node build/dev-server.js", "build": "node build/build.js" }, "dependencies": { "imagesloaded": "^4.1.1", "vue": "^2.1.0" }, "devDependencies": { "vue-bulma-progress-bar": "^1.0.0", "autoprefixer": "^6.4.0", "babel-core": "^6.0.0", "babel-loader": "^6.0.0", "babel-plugin-add-module-exports": "^0.2.1", "babel-plugin-transform-runtime": "^6.0.0", "babel-preset-es2015": "^6.0.0", "babel-preset-stage-2": "^6.0.0", "babel-register": "^6.0.0", "bulma": "^0.3.0", "chalk": "^1.1.3", "connect-history-api-fallback": "^1.1.0", "css-loader": "^0.25.0", "eventsource-polyfill": "^0.9.6", "express": "^4.13.3", "extract-text-webpack-plugin": "^1.0.1", "file-loader": "^0.9.0", "function-bind": "^1.0.2", "html-webpack-plugin": "^2.8.1", "http-proxy-middleware": "^0.17.2", "json-loader": "^0.5.4", "node-sass": "^4.1.1", "opn": "^4.0.2", "ora": "^0.3.0", "sass-loader": "^4.1.1", "semver": "^5.3.0", "shelljs": "^0.7.4", "url-loader": "^0.5.7", "vue-loader": "^10.0.0", "vue-style-loader": "^1.0.0", "vue-template-compiler": "^2.1.0", "vueisotope": "^2.0.3", "webpack": "^1.13.2", "webpack-dev-middleware": "^1.8.3", "webpack-hot-middleware": "^2.12.2", "webpack-merge": "^0.14.1" }, "engines": { "node": ">= 4.0.0", "npm": ">= 3.0.0" }, "bugs": { "url": "https://github.com/David-Desmaisons/Vue.ImagesLoaded/issues" }, "license": "MIT", "homepage": "https://github.com/David-Desmaisons/Vue.ImagesLoaded#readme", "repository": { "type": "git", "url": "https://github.com/David-Desmaisons/Vue.ImagesLoaded.git" } } ================================================ FILE: src/imagesLoadedDirective.js ================================================ import imagesLoaded from 'imagesloaded' import Vue from 'vue' function isEqual (firstArray, secondArray) { const length = firstArray.length if ( length != secondArray.length) { return false; } for (let i = 0; i < length; i++) { const first = firstArray[i], second = secondArray[i] if ((first.img!==second.img) || (first.src!==second.src)){ return false; } } return true; } function checkFunction(callBack, message=''){ if (typeof callBack !=='function'){ throw `imageLoaded directive error: objet ${callBack} should be a function ${message}` } } function registerImageLoaded(imgLoad, {value, arg, modifiers}) { if (!arg) { checkFunction(value) imgLoad.on('always', (inst) => setTimeout(() => value(inst)) ) return } const hasModifier = !!modifiers && !!Object.keys(modifiers).length const keys = hasModifier ? modifiers : value; const getCallBack = hasModifier ? (key) => {return value;} : (key) => value[key]; for (var key in keys) { const callBack = getCallBack(key) checkFunction(callBack, !hasModifier? `property ${key} of ${value}` : '') imgLoad[arg](key, (inst, img) => setTimeout(() => callBack(inst, img))) } } function applyImagesLoaded (el, binding) { const newContext = imagesLoaded( el ); const contextImages = newContext.images.map(img => {return {img: img.img, src: img.img.src} }) const oldcontextImages = el.__imagesLoaded__.context if (isEqual(oldcontextImages, contextImages)) { return } registerImageLoaded( newContext, binding) Object.assign(el.__imagesLoaded__, {context: contextImages, imageLoaded: newContext}) } export default { bind (el) { el.__imagesLoaded__ = { context: [] } }, inserted (el, binding){ applyImagesLoaded(el, binding) }, componentUpdated (el, binding){ Vue.nextTick( () => { applyImagesLoaded(el, binding) }); }, unbind (el, binding) { el.__imagesLoaded__ = null } }