Skip to content

Instantly share code, notes, and snippets.

@tanhauhau
Created December 12, 2015 13:22
Show Gist options
  • Save tanhauhau/c6522285c8768a24823c to your computer and use it in GitHub Desktop.
Save tanhauhau/c6522285c8768a24823c to your computer and use it in GitHub Desktop.
ionic-image-lazy-load + imgCache
Combination of
1. ionic-image-lazy-load (https://github.com/paveisistemas/ionic-image-lazy-load)
2. imgCache (https://github.com/chrisben/imgcache.js/)
Usage:
<image-lazy-cached image-lazy-src="{{image}}" image-lazy-loader="dots"></image-lazy-cached>
Installation:
1. Cordova File and Cordova File Transfer plugin
2. install imgCache see the link above
3. ImgCache.init(); -- read the doc of imgCache
Note:
'image-lazy-background-image' from ionic-image-lazy-load is not available anymore
angular.module('ionicLazyLoadCache', []);
angular.module('ionicLazyLoadCache')
.directive('lazyScroll', ['$rootScope',
function($rootScope) {
return {
restrict: 'A',
link: function ($scope, $element) {
var origEvent = $scope.$onScroll;
$scope.$onScroll = function () {
$rootScope.$broadcast('lazyScrollEvent');
if(typeof origEvent === 'function'){
origEvent();
}
};
}
};
}])
.directive('imageLazyCached', ['$document', '$timeout', '$ionicScrollDelegate', '$compile',
function ($document, $timeout, $ionicScrollDelegate, $compile) {
return {
restrict: 'E',
template: '<img></img>',
replace: true,
scope: {
lazyScrollResize: "@lazyScrollResize",
imageLazySrc: "@imageLazySrc"
},
link: function ($scope, $element, $attributes) {
if (!$attributes.imageLazyDistanceFromBottomToLoad) {
$attributes.imageLazyDistanceFromBottomToLoad = 0;
}
if (!$attributes.imageLazyDistanceFromRightToLoad) {
$attributes.imageLazyDistanceFromRightToLoad = 0;
}
var loader;
if ($attributes.imageLazyLoader) {
loader = $compile('<div class="image-loader-container"><ion-spinner class="image-loader" icon="' + $attributes.imageLazyLoader + '"></ion-spinner></div>')($scope);
$element.after(loader);
}
$scope.$watch('imageLazySrc', function (oldV, newV) {
if(loader)
loader.remove();
if ($attributes.imageLazyLoader) {
loader = $compile('<div class="image-loader-container"><ion-spinner class="image-loader" icon="' + $attributes.imageLazyLoader + '"></ion-spinner></div>')($scope);
$element.after(loader);
}
var deregistration = $scope.$on('lazyScrollEvent', function () {
// console.log('scroll');
if (isInView()) {
loadImage();
deregistration();
}
}
);
$timeout(function () {
if (isInView()) {
loadImage();
deregistration();
}
}, 500);
});
var deregistration = $scope.$on('lazyScrollEvent', function () {
// console.log('scroll');
if (isInView()) {
loadImage();
deregistration();
}
}
);
function loadImage() {
//Bind "load" event
$element.bind("load", function (e) {
if ($attributes.imageLazyLoader) {
loader.remove();
}
if ($scope.lazyScrollResize == "true") {
//Call the resize to recalculate the size of the screen
$ionicScrollDelegate.resize();
}
$element.unbind("load");
});
//load from cache
loadFromCache();
}
function loadFromCache() {
if (ImgCache.ready) {
ImgCache.cacheFile($scope.imageLazySrc, function() {
ImgCache.getCachedFileURL($scope.imageLazySrc, function(src, tar){
$element[0].src = tar;
})
}, function() {
console.error('Could not download image (ImgCache).');
loader.remove();
});
} else {
$element[0].src = $scope.imageLazySrc;
}
}
function isInView() {
var clientHeight = $document[0].documentElement.clientHeight;
var clientWidth = $document[0].documentElement.clientWidth;
var imageRect = $element[0].getBoundingClientRect();
return (imageRect.top >= 0 && imageRect.top <= clientHeight + parseInt($attributes.imageLazyDistanceFromBottomToLoad))
&& (imageRect.left >= 0 && imageRect.left <= clientWidth + parseInt($attributes.imageLazyDistanceFromRightToLoad));
}
// bind listener
// listenerRemover = scrollAndResizeListener.bindListener(isInView);
// unbind event listeners if element was destroyed
// it happens when you change view, etc
$element.on('$destroy', function () {
deregistration();
});
// explicitly call scroll listener (because, some images are in viewport already and we haven't scrolled yet)
$timeout(function () {
if (isInView()) {
loadImage();
deregistration();
}
}, 500);
}
};
}]);
@tanhauhau
Copy link
Author

@jon-cole
Copy link

jon-cole commented Apr 7, 2016

This is nice. Is it MIT license like the code you linked to from pavei?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment