Skip to content

Commit 62ce395

Browse files
committed
Merge pull request #31 from vish250491/master
Pull request after laze loading bgImage
2 parents 23297e5 + 729cf57 commit 62ce395

File tree

11 files changed

+304
-23
lines changed

11 files changed

+304
-23
lines changed

bower.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"angular-mocks": "~1.4.3",
1616
"angular-animate": "~1.4.3",
1717
"angular-route": "~1.4.3",
18-
"angular-bootstrap": "~0.13.3"
18+
"angular-bootstrap": "~0.13.3",
19+
"ng-responsive-image": "~0.1.10",
20+
"angular-lazy-img": "~1.2.2"
1921
}
2022
}

control/design/index.html

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<script src="../../js/ui-bootstrap.min.js"></script>
1717
<script src="../../js/angular-color-picker.js"></script>
1818
<script src="../../js/shared.js"></script>
19-
<script type="text/javascript" src="/app/scripts/framework/pluginAPI/datastoreAPI.js"></script>
19+
2020
</head>
2121
<body ng-controller="folderPluginCtrl" ng-cloak ng-show="datastoreInitialized">
2222
<div ng-form="frmMain">
@@ -191,7 +191,7 @@
191191
backgroundImage: {},
192192
selectedLayout: 1,
193193
backgroundblur: 0,
194-
colorType: 'A'
194+
colors: {}
195195
};
196196
}
197197
else {
@@ -204,9 +204,9 @@
204204
}
205205

206206
$scope.data.design = $scope.data.design || {};
207-
if (!$scope.data.design.colorType) {
208-
$scope.data.design.colorType = "A";
209-
}
207+
$scope.data.design.colors = $scope.data.design.colors || {};
208+
209+
$scope.colorType = angular.equals({}, $scope.data.design.colors) ? "A" : "C";
210210

211211
/*
212212
* watch for changes in data and trigger the saveData function on change
@@ -317,11 +317,9 @@
317317
};
318318

319319
$scope.setColorType = function (colorType) {
320-
if ($scope.data.design.colorType === colorType) return;
321-
$scope.data.design.colorType = colorType;
322-
if ($scope.data.design.colorType === "C" && !$scope.data.design.colors) {
323-
$scope.data.design.colors = $scope.defaultColors;
324-
}
320+
if ($scope.colorType === colorType) return;
321+
$scope.colorType = colorType;
322+
$scope.data.design.colors = {};
325323
};
326324

327325
angular.element($window).bind('mousedown', function () {

js/shared.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ folderPluginShared.getDefaultScopeData = function () {
4343
backgroundImage: null,
4444
logoImage: null,
4545
selectedLayout: 1,
46-
backgroundblur: 0,
47-
colorType: 'A'
46+
backgroundblur: 0
4847
}
4948
};
5049
};

karma.conf.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ module.exports = function(config) {
2222
'test/assets/bower_components/angular-bootstrap/ui-bootstrap.min.js',
2323
'test/assets/bower_components/angular-mocks/angular-mocks.js',
2424
'control/content/**/*.js',
25-
'widget/**/*.js',
2625
'test/assets/*.js',
2726
'test/**/*.spec.js'
2827
],
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@
123123
height: 100%;
124124
}
125125

126+
.overlay {
127+
position: absolute;
128+
top: 0;
129+
left: 0;
130+
width: 100%;
131+
}
132+
126133

127134
/* Media Queries */
128135
@media (max-width:500px){
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
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+
]);

widget/images/16x9.png

13.3 KB
Loading

widget/images/1x1.png

13.3 KB
Loading

widget/images/239x100.png

12.3 KB
Loading

widget/images/4x3.png

15.9 KB
Loading

0 commit comments

Comments
 (0)