|
| 1 | +/* |
| 2 | + * angular-lazy-load |
| 3 | + * |
| 4 | + * Copyright(c) 2014 Paweł Wszoła <wszola.p@gmail.com> |
| 5 | + * MIT Licensed |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * @author Paweł Wszoła (wszola.p@gmail.com) |
| 11 | + * |
| 12 | + */ |
| 13 | + |
| 14 | +angular.module('angularLazyImg', []); |
| 15 | + |
| 16 | +angular.module('angularLazyImg').factory('LazyImgMagic', [ |
| 17 | + '$window', '$rootScope', 'lazyImgConfig', 'lazyImgHelpers', |
| 18 | + function($window, $rootScope, lazyImgConfig, lazyImgHelpers){ |
| 19 | + 'use strict'; |
| 20 | + |
| 21 | + var winDimensions, $win, images, isListening, options; |
| 22 | + var checkImagesT, saveWinOffsetT, containers; |
| 23 | + |
| 24 | + images = []; |
| 25 | + isListening = false; |
| 26 | + options = lazyImgConfig.getOptions(); |
| 27 | + $win = angular.element($window); |
| 28 | + winDimensions = lazyImgHelpers.getWinDimensions(); |
| 29 | + saveWinOffsetT = lazyImgHelpers.throttle(function(){ |
| 30 | + winDimensions = lazyImgHelpers.getWinDimensions(); |
| 31 | + }, 60); |
| 32 | + containers = [options.container || $win]; |
| 33 | + |
| 34 | + function checkImages(){ |
| 35 | + for(var i = images.length - 1; i >= 0; i--){ |
| 36 | + var image = images[i]; |
| 37 | + if(image && lazyImgHelpers.isElementInView(image.$elem[0], options.offset, winDimensions)){ |
| 38 | + loadImage(image); |
| 39 | + images.splice(i, 1); |
| 40 | + } |
| 41 | + } |
| 42 | + if(images.length === 0){ stopListening(); } |
| 43 | + } |
| 44 | + |
| 45 | + checkImagesT = lazyImgHelpers.throttle(checkImages, 30); |
| 46 | + |
| 47 | + function listen(param){ |
| 48 | + containers.forEach(function (container) { |
| 49 | + container[param]('scroll', checkImagesT); |
| 50 | + container[param]('touchmove', checkImagesT); |
| 51 | + }); |
| 52 | + $win[param]('resize', checkImagesT); |
| 53 | + $win[param]('resize', saveWinOffsetT); |
| 54 | + } |
| 55 | + |
| 56 | + function startListening(){ |
| 57 | + isListening = true; |
| 58 | + setTimeout(function(){ |
| 59 | + checkImages(); |
| 60 | + listen('on'); |
| 61 | + }, 1); |
| 62 | + } |
| 63 | + |
| 64 | + function stopListening(){ |
| 65 | + isListening = false; |
| 66 | + listen('off'); |
| 67 | + } |
| 68 | + |
| 69 | + function removeImage(image){ |
| 70 | + var index = images.indexOf(image); |
| 71 | + if(index !== -1) { |
| 72 | + images.splice(index, 1); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + function loadImage(photo){ |
| 77 | + var img = new Image(); |
| 78 | + img.onerror = function(){ |
| 79 | + if(options.errorClass){ |
| 80 | + photo.$elem.addClass(options.errorClass); |
| 81 | + } |
| 82 | + $rootScope.$emit('lazyImg:error', photo); |
| 83 | + options.onError(photo); |
| 84 | + }; |
| 85 | + img.onload = function(){ |
| 86 | + setPhotoSrc(photo.$elem, photo.src); |
| 87 | + if(options.successClass){ |
| 88 | + photo.$elem.addClass(options.successClass); |
| 89 | + } |
| 90 | + $rootScope.$emit('lazyImg:success', photo); |
| 91 | + options.onSuccess(photo); |
| 92 | + }; |
| 93 | + img.src = photo.src; |
| 94 | + } |
| 95 | + |
| 96 | + function setPhotoSrc($elem, src){ |
| 97 | + if ($elem[0].nodeName.toLowerCase() === 'img') { |
| 98 | + $elem[0].src = src; |
| 99 | + } else { |
| 100 | + $elem.css('background-image', 'url("' + src + '")'); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // PHOTO |
| 105 | + function Photo($elem){ |
| 106 | + this.$elem = $elem; |
| 107 | + } |
| 108 | + |
| 109 | + Photo.prototype.setSource = function(source){ |
| 110 | + this.src = source; |
| 111 | + images.unshift(this); |
| 112 | + if (!isListening){ startListening(); } |
| 113 | + }; |
| 114 | + |
| 115 | + Photo.prototype.removeImage = function(){ |
| 116 | + removeImage(this); |
| 117 | + if(images.length === 0){ stopListening(); } |
| 118 | + }; |
| 119 | + |
| 120 | + Photo.prototype.checkImages = function(){ |
| 121 | + checkImages(); |
| 122 | + }; |
| 123 | + |
| 124 | + Photo.addContainer = function (container) { |
| 125 | + stopListening(); |
| 126 | + containers.push(container); |
| 127 | + startListening(); |
| 128 | + }; |
| 129 | + |
| 130 | + Photo.removeContainer = function (container) { |
| 131 | + stopListening(); |
| 132 | + containers.splice(containers.indexOf(container), 1); |
| 133 | + startListening(); |
| 134 | + }; |
| 135 | + |
| 136 | + return Photo; |
| 137 | + |
| 138 | + } |
| 139 | +]); |
| 140 | + |
| 141 | +angular.module('angularLazyImg').provider('lazyImgConfig', function() { |
| 142 | + 'use strict'; |
| 143 | + |
| 144 | + this.options = { |
| 145 | + offset : 100, |
| 146 | + errorClass : null, |
| 147 | + successClass : null, |
| 148 | + onError : function(){}, |
| 149 | + onSuccess : function(){} |
| 150 | + }; |
| 151 | + |
| 152 | + this.$get = function() { |
| 153 | + var options = this.options; |
| 154 | + return { |
| 155 | + getOptions: function() { |
| 156 | + return options; |
| 157 | + } |
| 158 | + }; |
| 159 | + }; |
| 160 | + |
| 161 | + this.setOptions = function(options) { |
| 162 | + angular.extend(this.options, options); |
| 163 | + }; |
| 164 | +}); |
| 165 | +angular.module('angularLazyImg').factory('lazyImgHelpers', [ |
| 166 | + '$window', function($window){ |
| 167 | + 'use strict'; |
| 168 | + |
| 169 | + function getWinDimensions(){ |
| 170 | + return { |
| 171 | + height: $window.innerHeight, |
| 172 | + width: $window.innerWidth |
| 173 | + }; |
| 174 | + } |
| 175 | + |
| 176 | + function isElementInView(elem, offset, winDimensions) { |
| 177 | + var rect = elem.getBoundingClientRect(); |
| 178 | + var bottomline = winDimensions.height + offset; |
| 179 | + return ( |
| 180 | + rect.left >= 0 && rect.right <= winDimensions.width + offset && ( |
| 181 | + rect.top >= 0 && rect.top <= bottomline || |
| 182 | + rect.bottom <= bottomline && rect.bottom >= 0 - offset |
| 183 | + ) |
| 184 | + ); |
| 185 | + } |
| 186 | + |
| 187 | + // http://remysharp.com/2010/07/21/throttling-function-calls/ |
| 188 | + function throttle(fn, threshhold, scope) { |
| 189 | + var last, deferTimer; |
| 190 | + return function () { |
| 191 | + var context = scope || this; |
| 192 | + var now = +new Date(), |
| 193 | + args = arguments; |
| 194 | + if (last && now < last + threshhold) { |
| 195 | + clearTimeout(deferTimer); |
| 196 | + deferTimer = setTimeout(function () { |
| 197 | + last = now; |
| 198 | + fn.apply(context, args); |
| 199 | + }, threshhold); |
| 200 | + } else { |
| 201 | + last = now; |
| 202 | + fn.apply(context, args); |
| 203 | + } |
| 204 | + }; |
| 205 | + } |
| 206 | + |
| 207 | + return { |
| 208 | + isElementInView: isElementInView, |
| 209 | + getWinDimensions: getWinDimensions, |
| 210 | + throttle: throttle |
| 211 | + }; |
| 212 | + |
| 213 | + } |
| 214 | +]); |
| 215 | +angular.module('angularLazyImg') |
| 216 | + .directive('lazyImg', [ |
| 217 | + '$rootScope', 'LazyImgMagic', function ($rootScope, LazyImgMagic) { |
| 218 | + 'use strict'; |
| 219 | + |
| 220 | + function link(scope, element, attributes) { |
| 221 | + var lazyImage = new LazyImgMagic(element); |
| 222 | + attributes.$observe('lazyImg', function (newSource) { |
| 223 | + if (newSource) { |
| 224 | + // in angular 1.3 it might be nice to remove observer here |
| 225 | + lazyImage.setSource(newSource); |
| 226 | + } |
| 227 | + }); |
| 228 | + scope.$on('$destroy', function () { |
| 229 | + lazyImage.removeImage(); |
| 230 | + }); |
| 231 | + $rootScope.$on('lazyImg.runCheck', function () { |
| 232 | + lazyImage.checkImages(); |
| 233 | + }); |
| 234 | + $rootScope.$on('lazyImg:refresh', function () { |
| 235 | + lazyImage.checkImages(); |
| 236 | + }); |
| 237 | + } |
| 238 | + |
| 239 | + return { |
| 240 | + link: link, |
| 241 | + restrict: 'A' |
| 242 | + }; |
| 243 | + } |
| 244 | + ]) |
| 245 | + .directive('lazyImgContainer', [ |
| 246 | + 'LazyImgMagic', function (LazyImgMagic) { |
| 247 | + 'use strict'; |
| 248 | + |
| 249 | + function link(scope, element) { |
| 250 | + LazyImgMagic.addContainer(element); |
| 251 | + scope.$on('$destroy', function () { |
| 252 | + LazyImgMagic.removeContainer(element); |
| 253 | + }); |
| 254 | + } |
| 255 | + |
| 256 | + return { |
| 257 | + link: link, |
| 258 | + restrict: 'A' |
| 259 | + }; |
| 260 | + } |
| 261 | + ]); |
0 commit comments