-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
353 lines (308 loc) · 16.4 KB
/
app.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
(function() {
var my_app = angular.module('generatorApp', []);
my_app.controller('viewerCtrl', [ '$http', '$scope',
function($http, $scope) {
let viewer = this;
$scope.make_comparison = function(){
$http.get($scope.current_comparison).then(function(res){
$scope.output = viewer.process_data(res.data);
if (res.data.hasOwnProperty("labels")){
$scope.labels = res.data["labels"];
}
});
};
let inputs_URL = "https://api.github.com/repos/FAIRsharing/JSONschema-compare-and-view/contents/inputs";
$http.get(inputs_URL).then(function(response) {
$scope.available_comparison = [];
for (let file_iterator in response.data) {
if (response.data.hasOwnProperty(file_iterator)) {
let inputFile = response.data[file_iterator];
if (inputFile.hasOwnProperty('path')) {
let file_URL = "https://raw.githubusercontent.com/FAIRsharing/JSONschema-compare-and-view/master/" + inputFile['path'];
$scope.available_comparison.push(file_URL);
}
}
}
/* get query params */
let query = location.search.substr(1);
if (query!==""){
let result = {};
query.split("&").forEach(function(part) {
let item = part.split("=");
result[item[0]] = decodeURIComponent(item[1]);
});
if (result.hasOwnProperty('target')){
$scope.current_comparison = result['target'];
$scope.make_comparison();
}
else {
$scope.current_comparison = $scope.available_comparison[0];
$scope.make_comparison();
}
}
else {
$scope.current_comparison = $scope.available_comparison[0];
$scope.make_comparison();
}
});
viewer.process_data = function(data){
let output = {
"header": ["", data.network1.name, data.network2.name],
"content": {
"overlapped_schemas": {},
"isolated_schemas": {}
}
};
let processed_schemas = {
"network1": [],
"network2": []
};
let ignoredKeys = [
"@context",
"@id",
"@type",
"$schema"
];
for (let i in data["overlaps"]){
if (data["overlaps"].hasOwnProperty(i)){
/* INITIATE NEEDED VAR */
let overlap = data["overlaps"][i];
let iterator = "overlap" + i.toString();
let schema_1_name = overlap[0][0].toLowerCase() + '_schema.json';
let schema_2_name = overlap[0][1].toLowerCase() + '_schema.json';
let schema_1_link = data['network1']['schemas'][schema_1_name]['id'];
let schema_2_link = data['network2']['schemas'][schema_2_name]['id'];
let schema_1 = data.network1["schemas"][schema_1_name];
let schema_2 = data.network2["schemas"][schema_2_name];
let title_1 = data.network1["schemas"][schema_1_name].title;
let title_2 = data.network2["schemas"][schema_2_name].title;
let name = overlap[0][0];
if (overlap[0][0].indexOf("_") > -1){
name = "";
let schema_local_name_array = overlap[0][0].split("_");
for (let sch_name in schema_local_name_array){
let schemaRawName = schema_local_name_array[sch_name];
name += schemaRawName.charAt(0).toUpperCase() + schemaRawName.slice(1);;
}
}
let local_link = viewer.get_link(name, data.network1['contexts'][schema_1_name]);
let base_type = data.network1["contexts"][schema_1_name][name];
let local_output = [base_type, title_1, title_2, local_link, schema_1_link, schema_2_link];
processed_schemas["network1"].push(schema_1_name);
processed_schemas["network2"].push(schema_2_name);
output.content.overlapped_schemas[iterator] = {};
output.content.overlapped_schemas[iterator]["schemas"] = local_output;
output.content.overlapped_schemas[iterator]["fields"] = [];
/* Pre-process the overlaps */
let overlapped_fields = {};
for (let fields in overlap[1]['overlapping fields']){
if (overlap[1]['overlapping fields'].hasOwnProperty(fields)){
overlapped_fields[overlap[1]['overlapping fields'][fields][0]] = overlap[1]['overlapping fields'][fields][1];
overlapped_fields[overlap[1]['overlapping fields'][fields][1]] = overlap[1]['overlapping fields'][fields][0];
}
}
for (let field in schema_1["properties"]){
if (schema_1["properties"].hasOwnProperty(field)){
if (Object.keys(overlapped_fields).indexOf(field) !==-1){
let link = viewer.get_link(field, data['network1']['contexts'][schema_1_name]);
let local_output = [
viewer.process_name(data.network1['contexts'][schema_1_name][field]),
field,
overlapped_fields[field],
link
];
output.content.overlapped_schemas[iterator]["fields"].push(local_output)
}
else if (ignoredKeys.indexOf(field) === -1){
if (data.network1['contexts'][schema_1_name].hasOwnProperty(field)){
let link = viewer.get_link(field, data['network1']['contexts'][schema_1_name]);
let local_output = [
viewer.process_name(data.network1['contexts'][schema_1_name][field]),
field,
false,
link
];
output.content.overlapped_schemas[iterator]["fields"].push(local_output)
}
}
}
}
for (let field in schema_2["properties"]){
if (schema_2["properties"].hasOwnProperty(field)){
if (ignoredKeys.indexOf(field) === -1 && Object.keys(overlapped_fields).indexOf(field) === -1){
if (data.network2['contexts'][schema_2_name].hasOwnProperty(field)){
let link = viewer.get_link(field, data['network2']['contexts'][schema_2_name]);
let local_output = [
viewer.process_name(data.network2['contexts'][schema_2_name][field]),
false,
field,
link
];
if (!output.content.overlapped_schemas[iterator]["fields"].includes(local_output)){
output.content.overlapped_schemas[iterator]["fields"].push(local_output)
}
}
}
}
}
}
}
let it = 0;
for (let schemaName in data.network1.schemas){
if (data.network1.schemas.hasOwnProperty(schemaName) && processed_schemas.network1.indexOf(schemaName) === -1){
let iterator = "schema" + it.toString();
let schemaValue = data.network1.schemas[schemaName];
let attribute = schemaName.replace("_schema.json", "").split("_");
for (let it in attribute){
attribute[it] = attribute[it].charAt(0).toUpperCase() + attribute[it].slice(1);
}
attribute = attribute.join().replace(/,/g, "");
if (data.network1["contexts"].hasOwnProperty(schemaName)){
let schema_base_type = data.network1["contexts"][schemaName][attribute];
console.log(attribute);
output.content.isolated_schemas[iterator] = {};
output.content.isolated_schemas[iterator]["schemas"] = [schema_base_type, schemaValue.title, false];
output.content.isolated_schemas[iterator]["fields"] = [];
for (let field in schemaValue['properties']){
if (schemaValue['properties'].hasOwnProperty(field)
&& ignoredKeys.indexOf(field) === -1
&& data.network1["contexts"][schemaName].hasOwnProperty(field)){
let field_base_type = viewer.process_name(data.network1["contexts"][schemaName][field]);
if (field_base_type !== false){
let link = viewer.get_link(field, data['network1']['contexts'][schemaName]);
output.content.isolated_schemas[iterator]["fields"].push([
field_base_type,
field,
false,
link
])
}
}
}
}
}
it++;
}
for (let schemaName in data.network2.schemas){
if (data.network2.schemas.hasOwnProperty(schemaName) && processed_schemas.network2.indexOf(schemaName) === -1){
let iterator = "schema" + it.toString();
let schemaValue = data.network2.schemas[schemaName];
let attribute = schemaName.replace("_schema.json", "").split("_");
for (let it in attribute){
attribute[it] = attribute[it].charAt(0).toUpperCase() + attribute[it].slice(1);
}
attribute = attribute.join().replace(/,/g, "");
if (data.network2["contexts"].hasOwnProperty(schemaName)){
let schema_base_type = data.network2["contexts"][schemaName][attribute];
output.content.isolated_schemas[iterator] = {};
output.content.isolated_schemas[iterator]["schemas"] = [schema_base_type, false, schemaValue.title];
output.content.isolated_schemas[iterator]["fields"] = [];
for (let field in schemaValue['properties']){
if (schemaValue['properties'].hasOwnProperty(field)
&& ignoredKeys.indexOf(field) === -1
&& data.network2["contexts"][schemaName].hasOwnProperty(field)){
let field_base_type = viewer.process_name(data.network2["contexts"][schemaName][field]);
if (field_base_type !== false){
let link = viewer.get_link(field, data['network2']['contexts'][schemaName]);
output.content.isolated_schemas[iterator]["fields"].push([
field_base_type,
false,
field,
link
])
}
}
}
}
}
it++;
}
return output;
};
viewer.process_name = function(field){
if (typeof field === 'string') {
return field
}
else if (field.hasOwnProperty('@id') && field["@id"]!==""){
return field["@id"]
}
else {
return false;
}
};
viewer.get_link = function(field, context){
let parser = document.createElement('a');
parser.href = context[field];
if (parser.protocol === 'https:' || parser.protocol === 'http:'){
return context[field]
}
else{
return context[parser.protocol.replace(":", '')] + context[field].replace(parser.protocol, '');
}
}
}]
);
my_app.directive('schemaOverlap', function() {
return {
restrict: 'A',
templateUrl: 'include/schemaOverlap.html',
scope: {
schemaOverlap: '=',
labels: "="
},
link: function($scope) {
$scope.$watch('schemaOverlap', function(schemaOverlap){
if(schemaOverlap){
$scope.json_source = $scope.schemaOverlap;
$scope.term_labels = $scope.labels
}
});
}
}
});
my_app.directive('isolatedSchema', function() {
return {
restrict: 'A',
templateUrl: 'include/isolated_schema.html',
scope: {
isolatedSchema: '=',
labels: '='
},
link: function($scope) {
$scope.$watch('isolatedSchema', function(isolatedSchema){
if(isolatedSchema)
$scope.json_source = $scope.isolatedSchema;
$scope.term_labels = $scope.labels
});
}
}
});
my_app.directive('fieldLabel', function() {
return {
restrict: 'A',
templateUrl: 'include/label.html',
scope: {
fieldLabel: '=',
labels: "="
},
link: function($scope) {
$scope.$watch('fieldLabel', function(fieldLabel){
if(fieldLabel){
$scope.json_source = $scope.fieldLabel;
$scope.term_labels = $scope.labels;
}
});
}
}
});
my_app.filter('typeOf', function() {
return function (obj) {
return typeof obj;
};
});
my_app.filter('labels', function() {
return function(obj) {
return obj.replace('.json', '').replace(/_/g, ' ').replace("https://raw.githubusercontent.com/FAIRsharing/JSONschema-compare-and-view/master/inputs/", "");
}
})
})();