-
Notifications
You must be signed in to change notification settings - Fork 1
/
angular-erated.js
147 lines (117 loc) · 3.82 KB
/
angular-erated.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
angular
.module('angular-erated', ['angularLoad']);
angular
.module('angular-erated')
.provider('eratedService', [eratedServiceProvider]);
function eratedServiceProvider() {
var apiKey = "";
var defaultConfig = {};
var reviews = [];
this.setApiKey = function(key) {
apiKey = key;
}
this.setDefaultConfig = function(config) {
defaultConfig = config;
}
this.$get = ['$q', '$http', 'angularLoad', function($q, $http, angularLoad) {
var service = {
apiKey: apiKey,
getApiKey: getApiKey,
getUserProfile: getUserProfile,
getDefaultConfig: getDefaultConfig,
addReview: addReview,
getReviews: getReviews,
removeAllReviews: removeAllReviews,
loadSetupScript: loadSetupScript,
setupVars: setupVars
};
return service;
function getApiKey() {
return apiKey;
}
function getUserProfile(emailHash) {
var deferred = $q.defer();
$http.get('//api.erated.co/v1/users/'+emailHash+'?partner='+getApiKey())
.success(function(data) {
if (data.hasOwnProperty('error')) {
deferred.reject(data);
return;
}
deferred.resolve(data);
});
return deferred.promise;
}
function getDefaultConfig() {
return defaultConfig;
}
function addReview(content, asSeller, type) {
reviews.push({
review_content: content,
reviewed_as_seller: asSeller,
review_type: type,
});
}
function getReviews() {
return reviews;
}
function removeAllReviews() {
reviews = [];
}
function loadSetupScript() {
angularLoad.loadScript('//cdn.erated.co/iframe/erated_imp.js');
}
function setupVars(options) {
var reviews = service.getReviews();
window.eRated = {
config: {
align: options.align,
key: apiKey,
color: options.color,
reputationMode: "marketplace",
privacy: {
firstNameOnly: false
},
view: options.view,
type: "html",
width: 380
},
userData: {
name: options.username,
sha1Email: options.emailhash,
location: options.location,
image: options.image,
reputationData: {
numberOfReviews: 0,
percentPositiveReviews: 0,
reviews: reviews
}
}
}
var defaultConfig = service.getDefaultConfig();
for(var key in defaultConfig) {
window.eRated.config[key] = defaultConfig[key];
}
service.removeAllReviews();
return window.eRated;
}
}];
}
angular
.module('angular-erated')
.directive('eratedplugin', ['eratedService', eratedPlugin]);
function eratedPlugin(eratedService) {
var templateContent = '<div class="erated"></div>';
return {
restrict: 'EA',
link: function($scope, $element, attr) {
var options = attr;
window.eRated = null;
window.erated = null;
window.eratedPlugin = null;
window.init_eRated = null;
eratedService.loadSetupScript();
eratedService.setupVars(options);
},
template: templateContent
};
}