forked from sunsus/ngImgCache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngImgCache.js
50 lines (48 loc) · 1.56 KB
/
ngImgCache.js
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
angular.module('ngImgCache', [])
.run(function($log) {
ImgCache.options.debug = false;
ImgCache.options.chromeQuota = 50*1024*1024;
ImgCache.init(function() {
$log.debug('ImgCache init: success!');
}, function(){
$log.error('ImgCache init: error! Check the log for errors');
});
})
.service('CacheImages', function($q){
return {
checkCacheStatus : function(src){
var deferred = $q.defer();
ImgCache.isCached(src, function(path, success) {
if (success) {
deferred.resolve(path);
} else {
ImgCache.cacheFile(src, function() {
ImgCache.isCached(src, function(path, success) {
deferred.resolve(path);
}, deferred.reject);
}, deferred.reject);
}
}, deferred.reject);
return deferred.promise;
}
};
})
// <img ng-cache ng-src="..." />
.directive('ngCache', function() {
return {
restrict: 'A',
link: function(scope, el, attrs) {
attrs.$observe('ngSrc', function(src) {
ImgCache.isCached(src, function(path, success) {
if (success) {
ImgCache.useCachedFile(el);
} else {
ImgCache.cacheFile(src, function() {
ImgCache.useCachedFile(el);
});
}
});
});
}
};
});