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
27
28
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
/**
* Created by PAVEI on 30/09/2014.
* Updated by Ross Martin on 12/05/2014
* Updated by Davide Pastore on 04/14/2015
* Updated by Michel Vidailhet on 05/12/2015
*/
angular.module('ionicLazyLoad', []);
angular.module('ionicLazyLoad')
.directive('lazyScroll', ['$rootScope', '$timeout',
function($rootScope, $timeout) {
return {
restrict: 'A',
link: function ($scope, $element) {
var scrollTimeoutId = 0;
$scope.invoke = function () {
$rootScope.$broadcast('lazyScrollEvent');
};
$element.bind('scroll', function () {
$timeout.cancel(scrollTimeoutId);
// wait and then invoke listeners (simulates stop event)
scrollTimeoutId = $timeout($scope.invoke, 0);
});
}
};
}])
.directive('imageLazySrc', ['$document', '$timeout', '$ionicScrollDelegate', '$compile',
function ($document, $timeout, $ionicScrollDelegate, $compile) {
return {
restrict: 'A',
scope: {
lazyScrollResize: "@lazyScrollResize",
imageLazyBackgroundImage: "@imageLazyBackgroundImage"
},
link: function ($scope, $element, $attributes) {
if (!$attributes.imageLazyDistanceFromBottomToLoad) {
$attributes.imageLazyDistanceFromBottomToLoad = 0;
}
if (!$attributes.imageLazyDistanceFromRightToLoad) {
$attributes.imageLazyDistanceFromRightToLoad = 0;
}
if ($attributes.imageLazyLoader) {
var 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();
}
}
);
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();
}
});
if ($scope.imageLazyBackgroundImage == "true") {
var bgImg = new Image();
bgImg.onload = function () {
if ($attributes.imageLazyLoader) {
loader.remove();
}
$element[0].style.backgroundImage = 'url(' + $attributes.imageLazySrc + ')'; // set style attribute on element (it will load image)
if ($scope.lazyScrollResize == "true") {
//Call the resize to recalculate the size of the screen
$ionicScrollDelegate.resize();
}
};
bgImg.src = $attributes.imageLazySrc;
} else {
$element[0].src = $attributes.imageLazySrc; // set src attribute on element (it will load image)
}
}
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);
}
};
}]);
|