This repository has been archived by the owner on Nov 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcomponent-model.js
255 lines (221 loc) · 9.09 KB
/
component-model.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
var ComponentService = angular.module('registry').service("Component", function ($http, $window, $sce, $q) {
function preProcessComponent(component) {
component.columns = {
build: null, tests: null, readme: null, demos: null, reference: null, website: null };
/*
component.tags = [];
if(component.latest){
_(component.latest.shields).forEach(function (shield, key) {
switch (key) {
case 'build': case 'tests': case 'website': case 'reference':{
component.columns[key] =
$sce.trustAsHtml('<a class="shield" href="'+shield.href+'"><img src="'+shield.img+'"></a>');
component.tags.push("has:"+key);
} break;
}
});
}
if (false) { // TODO
component.columns['jsdocs'] =
$sce.trustAsHtml('<a href="???">???</a>');
component.tags.push("has:jsdocs");
}
// remove duplicates - angular doesn't like them
component.tags = _.uniq(component.tags);
*/
//// use a little ✗ for missing columns
_(component.columns).forEach(function (colHTML, type) {
// if (!colHTML) {
// component.columns[type] = $sce.trustAsHtml('✗');
// }
});
// defaults
component.avatar = "https://sigil.cupcake.io/" + component.name;
component.registryHeight = "400";
/*
// readme
if (!_(component.readmeFilename).isEmpty() &&
!component.readme.substring(0, 5) === "ERROR") {
component.columns['readme'] =
$sce.trustAsHtml('<a href="'+component.readmeFilename+'">README</a>');
//component.tags.push("has:readme");
}
*/
component.strCreated = moment(component.created).fromNow();
component.strModified = moment(component.modified).fromNow();
// modified and created are two keys
component.releases = component.versions;
if(component.bugs){
component.issueHref = component.bugs.url;
}
if(component.author !== undefined){
component.avatar = "https://sigil.cupcake.io/" + component.author.name;
}
component.downloads = component.npmDownloads;
component.tags = component.keywords;
// remove duplicates - angular doesn't like them
component.tags = _.uniq(component.tags);
component.authors = "Unknown";
if(component.author !== undefined){
component.authors = [component.author];
if(component.author.url != undefined){
component.authors[0].url = component.author.url;
}else if(component.github != undefined && component.github.owner != undefined){
component.authors[0].url = "https://github.com/" + component.github.owner.login;
}
}
if(component.contributors !== undefined){
component.authors = component.contributors;
}
// snippets
if(component.latest !== undefined && component.latest.sniper !== undefined){
// maybe the github repo might be wrong
if(component.latest.sniper.srcs != undefined){
component.snipNum = Object.keys(component.latest.sniper.srcs).length;
component.columns['demos'] =
$sce.trustAsHtml('<img src="http://img.shields.io/badge/%23-'+component.snipNum+'-blue.svg">');
component.snips = _.keys(component.latest.sniper.srcs);
}
if(component.snips != undefined){
component.selectedSnip = component.latest.sniper.first || component.snips[0];
updateSnippets(component);
component.tags.push("has:demos");
}
}
// biojs stuff
if(component.latest !== undefined && component.latest.biojs !== undefined){
component.registryHeight = component.latest.biojs.registryHeight || component.registryHeight;
component.registryHeight.trim().replace("px", ""); // just in case
}
// github
if(component.github !== undefined && component.github.owner !== undefined){
//component.starbutton = $sce.trustAsResourceUrl("http://ghbtns.com/github-btn.html?user=" + component.github.owner.login
//+ "&repo=" + component.name + "&type=watch&count=true");
component.stars = component.github.stargazers_count;
component.watchers = component.github.subscribers_count;
component.forks = component.github.forks_count;
component.issues = component.github.open_issues_count;
//component.issueHref = component.issues_url;
component.avatar = component.github.owner.avatar_url;
component.src = component.github.html_url;
component.commits = component.github.commits;
if(component.github.contribs !== undefined){
component.contributors = Object.keys(component.github.contribs).length;
}else{
component.contributors = 0;
}
component.readmeSrc = "http://github-raw-cors-proxy.herokuapp.com/"+component.github.full_name+ "/blob/"+component.github.default_branch+"/" + component.readmeFilename;
}else{
console.log("no github repo found for %s", component.name);
component.stars = 0;
}
// copy to lowercase (for searching)
component.lName = component.name.toLowerCase();
if( component.description !== undefined ){
component.lDescription = component.description.toLowerCase();
}
// biojs settings overwrite github
if(component.latest !== undefined && component.latest.biojs !== undefined){
var cBio = component.latest.biojs;
if(cBio.logo !== undefined){
var hasFileExtension = /^.*\.[^\\]+$/;
// fallback to a default logo
if(! hasFileExtension.test(cBio.logo)){
cBio.logo += "logo.png";
}
cBio.logo = component.github.raw_url + cBio.logo;
component.avatar = cBio.logo;
}
}
if(component.iotags){
component.iotags = _.map(component.iotags, function(msg){
var text;
switch(msg){
case "sparse_doc":
text = "The documentation of this component shows major deficiencies";
break;
default:
text = "The documentation of this component has the error: " + msg;
break;
}
var type = "danger";
types = {
"danger": ["bad_docu"]
}
_.each(types, function(v, key){
if(v.indexOf(msg) >= 0){
type = key;
}
});
return {
type: type,
text: text
}
});
}
// download links
component.dl = {};
component.dl.html = "https://wzrd.in/bundle/" + component.name + "@" + component.version;
if(component.latest !== undefined && component.latest.style !== undefined){
component.dl.css = "http://parce.li/bundle/" + component.name + "@" + component.version;
}
component.citeHref = "";
component.readme = "";
return component;
}
function getPackage(name,components){
for(var index in components){
// search for package - probably there is a more efficient way
if(components[index].name === name){
return index;
}
}
return -1;
}
function Component() {}
Component.prototype = {};
Component.constructor = Component;
Component.list = [];
Component.single = function single(name) {
var p = $q.defer();
var url = REGISTRY_URL + '/detail/' + name;
//url = 'http://localhost:3000/detail/'+ name;
$http.get(url).success(function(resp) {
//all.push(resp[key]);
if(!resp.error) {
var com = preProcessComponent(resp);
var index = getPackage(name,Component.list);
if(index >= 0){
Component.list[index] = com;
}else{
Component.list.push(com);
}
p.resolve(com);
} else {
p.resolve(null)
}
});
return p.promise;
};
Component.query = function query() {
var all = [];
var p = $q.defer();
// ugly workaround to inject code - try GET
var url = REGISTRY_URL + '/all?short=1';
//url = 'http://localhost:3000/all?short=1';
$http.get(url).success(function(resp) {
if(all.length === 0){
Object.keys(resp).forEach(function(key) {
all.push(resp[key]);
preProcessComponent(resp[key]);
});
Component.list = all;
p.resolve(all);
}else{
p.reject("already loaded");
}
});
return p.promise;
};
return Component;
});