Repository navigation
Expand file tree
/
Copy pathangular-embedly.js
More file actions
executable file
·156 lines (139 loc) · 6.08 KB
/
Copy pathangular-embedly.js
File metadata and controls
executable file
·156 lines (139 loc) · 6.08 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Created by moran on 12/06/14.
*/
var angularEmbedly = angular.module('angular-embedly', []);
/**
* Created by moran on 12/06/14.
*/
(function (module) {
module.provider('embedlyService', function () {
var key;
var secure;
this.setKey = function(userKey) {
key = userKey;
return key;
}
this.getKey = function() {
return key;
}
this.secure = function(value) {
if (!value) {
return secure;
}
secure = value;
}
function getProtocol() {
return secure ? 'https' : 'https' ;
}
function embedly($http) {
this.embed = function(inputUrl, maxwidth, scheme) {
var escapedUrl = encodeURIComponent(inputUrl);
var embedlyRequest = getProtocol() + '://api.embed.ly/1/oembed?key=' + key + '&url=' + escapedUrl;
if(typeof maxwidth !== 'undefined'){
embedlyRequest = embedlyRequest + '&maxwidth=' + maxwidth;
}
if(typeof scheme !== 'undefined'){
embedlyRequest = embedlyRequest + '&scheme=' + scheme;
}
return $http({method: 'GET', url: embedlyRequest});
};
this.extract = function(inputUrl) {
var escapedUrl = encodeURIComponent(inputUrl);
var embedlyRequest = getProtocol() + '://api.embed.ly/1/extract?key=' + key + '&url=' + escapedUrl;
return $http({method: 'GET', url: embedlyRequest});
};
}
this.$get = ['$http', function($http) {
return new embedly($http);
}];
})
})(angularEmbedly);
/**
* Created by moran on 12/06/14.
*/
(function (module) {
module.controller('emEmbedCtrl', ['$scope', function($scope) {
$scope.embedCode = '';
}])
})(angularEmbedly);
/**
* Created by moran on 12/06/14.
*/
(function (module) {
module.directive('emEmbed', ['embedlyService', function(embedlyService) {
return {
restrict: 'E',
scope:{
urlsearch: '@',
maxwidth: '@',
scheme: '@',
onempty: '&'
},
controller: 'emEmbedCtrl',
link: function(scope, element, attributes) {
// This function should be called when the oEmbed returns no embed code
function handleEmpty(){
if(scope.onempty != undefined && typeof(scope.onempty) == "function"){
scope.onempty();
}
}
scope.$parent.loading_embedly = false;
scope.$watch('urlsearch', function(newVal) {
var previousEmbedCode = scope.embedCode;
if (newVal) {
scope.$parent.loading_embedly = true;
embedlyService.embed(newVal, scope.maxwidth, scope.scheme)
.then(function(data){
scope.$parent.loading_embedly = false;
switch(data.data.type) {
case 'video':
if(data.data.html == undefined){
handleEmpty();
}else{
scope.embedCode = data.data.html;
}
break;
case 'rich':
if(data.data.html == undefined){
handleEmpty();
}else{
scope.embedCode = data.data.html;
}
break;
case 'photo':
if(data.data.url == undefined){
handleEmpty();
}else{
if (scope.maxwidth) {
scope.embedCode = '<img style="max-width: ' + scope.maxwidth + 'px;" src="' + data.data.url + '">';
} else {
scope.embedCode = '<img src="' + data.data.url + '">';
}
}
break;
default:
// call the dev's handling code, he probably assumed he would get a video
// or photo (otherwise he'd use a different tool), so for him this result
// is the same as an empty result.
handleEmpty();
scope.embedCode = '';
}
if(previousEmbedCode !== scope.embedCode) {
// embed code was changed from last call and has to be replaced in DOM
element.html('<div>' + scope.embedCode + '</div>');
}
}, function(error) {
// promise rejected
scope.$parent.loading_embedly = false;
var previousEmbedCode = scope.embedCode;
scope.embedCode = '';
if(previousEmbedCode !== scope.embedCode) {
element.html('<div>' + scope.embedCode + '</div>');
}
});
}
});
}
};
}])
})(angularEmbedly);