-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpusher-gif.js
123 lines (123 loc) · 3.79 KB
/
pusher-gif.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
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
angular.module("pusher-gif", []).factory("pusherGifService", function() {
var api, reduce;
reduce = function(numerator, denominator) {
var gcd;
gcd = gcd = function(a, b) {
if (b) {
return gcd(b, a % b);
} else {
return a;
}
};
gcd = gcd(numerator, denominator);
return [numerator / gcd, denominator / gcd];
};
api = {};
api.gifCache = {};
api.make = function(width, height) {
var area, areaKey, index, pixels, ratio, _i;
ratio = reduce(Math.floor(width), Math.floor(height));
area = Math.floor(ratio[0] * ratio[1]);
areaKey = area.toString();
if (api.gifCache[areaKey]) {
return api.gifCache[areaKey];
} else {
pixels = new Array(area);
for (index = _i = 1; _i < area; index = _i += 1) {
pixels[_i] = 0;
}
api.gifCache[areaKey] = make_glif(ratio[0], ratio[1], pixels);
return api.gifCache[areaKey];
}
};
return api;
}).provider("pusherGifFPHelper", function() {
var _fpHeaders;
_fpHeaders = void 0;
return {
setFPHeaders: function(headers) {
return _fpHeaders = headers;
},
$get: [
"$q", "$http", function($q, $http) {
var api;
api = {};
api.metaCache = {};
api.metadata = function(item) {
var baseUrl, config, defer, fpUrlParts, sigPolicy;
defer = $q.defer();
if (api.metaCache[item.url]) {
defer.resolve(api.metaCache[item.url]);
} else {
fpUrlParts = item.url.split('?');
baseUrl = fpUrlParts[0];
sigPolicy = '';
if (fpUrlParts.length > 1) {
sigPolicy = "&" + fpUrlParts[1];
}
config = {
method: 'GET',
url: "" + baseUrl + "/metadata?width=true&height=true" + sigPolicy
};
if (_fpHeaders) {
config.headers = _fpHeaders;
}
$http(config).then(function(result) {
if (result && result.data && result.data.width && result.data.height) {
api.metaCache[item.url] = {
width: result.data.width,
height: result.data.height
};
return defer.resolve(api.metaCache[item.url]);
}
});
}
return defer.promise;
};
return api;
}
]
};
}).directive("pusherGif", [
"pusherGifService", "pusherGifFPHelper", function(pusherGifService, fpHelper) {
return {
restrict: "A",
scope: {
calcWidth: '@',
calcHeight: '@',
constrainWidth: '@',
fpFallback: '=?'
},
compile: function(tElem, tAttrs) {
var height, width;
if (tAttrs.width && tAttrs.height) {
width = +tAttrs.width;
height = +tAttrs.height;
tElem.attr('src', pusherGifService.make(width, height));
}
return function(scope, element, attrs) {
var setElSrc;
setElSrc = function(calcWidth, calcHeight) {
if (scope.constrainWidth) {
width = +scope.constrainWidth;
height = (+calcHeight / +calcWidth) * +scope.constrainWidth;
} else {
width = +calcWidth;
height = +calcHeight;
}
return element.attr('src', pusherGifService.make(width, height));
};
if (_.isUndefined(element.attr('src'))) {
if (scope.calcWidth && scope.calcHeight) {
return setElSrc(scope.calcWidth, scope.calcHeight);
} else if (scope.fpFallback) {
return fpHelper.metadata(scope.fpFallback).then(function(dimensions) {
return setElSrc(dimensions.width, dimensions.height);
});
}
}
};
}
};
}
]);