This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
411 lines (391 loc) · 12.3 KB
/
index.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/**
* @copyright 2017-2020 John Wiley & Sons, Inc.
* @license MIT
**/
const N3 = require('n3');
const VueX = require('vuex');
const Vue = require('vue');
Vue.use(VueX);
const VueLevelGraph = require('./src/vue-levelgraph.js');
Vue.use(VueLevelGraph, {name: 'askos'});
const term = require('./src/utils.js').term;
// Use futuristic Fetch() API
require('es6-promise').polyfill();
require('isomorphic-fetch');
let context = {
"@context": {
"dcterms": "http://purl.org/dc/terms/",
"foaf": "http://xmlns.com/foaf/0.1/",
"owl": "http://www.w3.org/2002/07/owl#",
"prov": "http://www.w3.org/ns/prov#",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"skos": "http://www.w3.org/2004/02/skos/core#",
"xml": "http://www.w3.org/XML/1998/namespace",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"@base": "https://vocabulary.wiley.com/PublicationType/",
"@language": "en"
}
};
window.context = context;
let curie = function(v) {
// we may not have a value yet
if (undefined === v) return v;
let found = Object.entries(context['@context'])
.filter((el) => v.startsWith(el[1]));
if (found.length > 0) {
return found[0][0] + ':' + v.replace(found[0][1], '');
} else {
// curie-ing failed...return passed in value...
return v;
}
};
let uncurie = function(v) {
return N3.Util.expandPrefixedName(v, context['@context']);
};
window.curie = curie;
window.uncurie = uncurie;
Vue.filter('curie', curie);
Vue.mixin({methods: {curie, uncurie}});
// clean up @value objects + (optional) @language selection
Vue.filter('@value', (v, lang) => {
if (Array.isArray(v) && v.length === 1) {
// TODO: ...yeah...there's more stuff to happen here...
if (v[0]['@language'] === lang) {
return v[0]['@value'];
} else {
// TODO: should this fall back to an unknown lang?
return v[0]['@value'];
}
} else if (typeof v === 'object') {
// this one's a real object...not an array
return v['@value'];
}
});
const store = new VueX.Store({
strict: true,
state: {
current_scheme: '', // URI of ConceptScheme
current_concept: '' // URI of Concept
},
mutations: {
setActiveScheme(state, payload) {
state.current_scheme = payload.scheme;
},
setActiveConcept(state, payload) {
state.current_concept = payload.concept;
}
},
actions: {
setActiveScheme({ commit }, payload) {
commit('setActiveConcept', '');
commit('setActiveScheme', payload);
}
}
});
Vue.component('vue-semantic-multi-select', require('./src/vue-semantic-multi-select.vue'));
Vue.component('skos-viewer', require('./src/viewer.vue'));
Vue.component('rdf-item', require('./src/rdf-item.vue'));
Vue.component('skos-concept-scheme', require('./src/concept-scheme.vue'));
Vue.component('skos-concept-scheme-form', require('./src/concept-scheme-form.vue'));
Vue.component('skos-concept-scheme-filter-link', require('./src/concept-scheme-filter-link.vue'));
Vue.component('skos-concept', require('./src/concept.vue'));
Vue.component('skos-concept-form', require('./src/concept-form.vue'));
Vue.component('skos-concept-table', require('./src/concept-table.vue'));
Vue.component('skos-concept-filter-link', require('./src/concept-filter-link.vue'));
function removeEmpties(spo) {
// TODO: certainly there's a better way to "clean" this object...later...maybe
if (spo.subject === '') delete spo.subject;
if (spo.predicate === '') delete spo.predicate;
if (spo.object === '') delete spo.object;
return spo;
}
// from https://stackoverflow.com/a/42389266
Vue.directive('click-outside', {
bind: function (el, binding, vnode) {
el.event = function (event) {
// here I check that click was outside the el and his childrens
if (!(el == event.target || el.contains(event.target))) {
// and if it did, call method provided in attribute value
vnode.context[binding.expression](event);
}
};
document.body.addEventListener('click', el.event)
},
unbind: function (el) {
document.body.removeEventListener('click', el.event)
},
});
window.app = new Vue({
el: '#app',
store,
data: {
config: {
jsonld: {
base: '',
overwrite: false
},
n3: {}
},
context,
table: [],
active_tab: 'editor',
filter: {
subject: '',
predicate: '',
object: ''
},
limit: 100,
offset: 0,
filtered: false,
output_jsonld: '',
output_n3: ''
},
watch: {
active_tab(v) {
if (v === 'triples') {
this.displayTriples();
}
}
},
computed: {
actual_filter: function() {
var object = this.filter.object.trim();
if (object !== '' && !isNaN(object)) {
// search for the RDF materialized integer
object = '"' + String(object) + '"^^http://www.w3.org/2001/XMLSchema#integer';
}
var spo = {
subject: this.filter.subject.trim(),
predicate: this.filter.predicate.trim(),
object: object
};
spo = removeEmpties(spo);
return spo;
},
actual_table: function() {
return this.table.slice(this.offset, this.limit+this.offset);
},
unique_predicates() {
let rv = {};
this.table.forEach((triple) => {
rv[triple.predicate] = '';
});
return Object.keys(rv);
},
unique_subjects() {
let rv = {};
this.table.forEach((triple) => {
rv[triple.subject] = '';
});
return Object.keys(rv);
}
},
methods: {
isActiveTab(tab) {
return (tab === this.active_tab);
},
displayTriples: function(spo) {
if (!(spo)) spo = {};
var self = this;
self.$db.get(spo, function(err, list) {
self.table = list;
// TODO: ugh...context collapse...
self.$refs.skos.getSchemes();
});
},
importSKOS(ev) {
let self = this;
// determine the file extension
let filename = ev.target.files[0].name;
let ext = filename.substring(filename.lastIndexOf('.')+1, filename.length) || undefined;
// pick the right parser/loader
let processor = (ext === 'json' || ext === 'jsonld') ? 'jsonld' : 'n3';
// do the loading/importing
let reader = new FileReader();
reader.onload = (load_event) => {
let text = load_event.target.result;
// load any existing additions into the local state
let additions = {};
if ('askos-context' in localStorage) {
additions = JSON.parse(localStorage['askos-context']);
}
// extract prefixes and persist them
if (processor === 'jsonld') {
// pull them straight out of the @context
// TODO: ...this assumes a single, top-level @context...probably
// should use jsonld.js to find stuff
let temp = JSON.parse(text)['@context'];
Object.keys(temp).forEach((key) => {
additions[key] = temp[key];
});
localStorage['askos-context'] = JSON.stringify(additions);
} else {
let parser = N3.Parser();
parser.parse(text, (error, triple, _prefixes) => {
if (!triple) {
// remove the base/default namespace
// TODO: probably check to be sure it's also mapped to something
// else...maybe make one up?
console.log('_prefixes', _prefixes);
delete _prefixes[''];
// TODO: this overwrites existing prefixes...
Object.keys(_prefixes).forEach((key) => {
additions[key] = _prefixes[key];
});
localStorage['askos-context'] = JSON.stringify(additions);
}
});
}
// add additional prefixes
self.$db[processor].put(text, (err) => {
if (err) console.error(err);
// TODO: more context collapse...also...hits the DB twice...
self.$refs.skos.getSchemes();
self.$refs.skos.getConcepts();
});
};
reader.readAsText(ev.target.files[0], 'UTF-8');
},
put: function() {
var self = this;
// TODO: refs are handy, but this feels "off"
if (this.input_type === 'jsonld') {
this.$db[this.input_type].put(
this.$refs[this.input_type].value,
this.config[this.input_type],
function(err, obj) {
// do something after the obj is inserted
self.displayTriples({});
});
} else {
// ...the new .put() signatures no longer match between JSON-LD & N3 extensions
// TODO: help make them match (again)
this.$db[this.input_type].put(
this.$refs[this.input_type].value,
function(err, obj) {
// do something after the obj is inserted
self.displayTriples({});
});
}
},
del: function() {
var self = this;
// TODO: this is JSON-LD only...time to move stuff around...
this.$db[this.input_type].del(
this.$refs[this.input_type].value,
this.config[this.input_type],
function(err, obj) {
// do something after the obj is inserted
self.displayTriples();
});
},
empty: function() {
var self = this;
// this...is dangerous...unless demo
if (confirm("Really? You're sure?")) {
self.$db.get({}, function(err, list) {
if (err) console.error(err);
self.$db.del(list, function(err) {
if (err) console.error(err);
self.displayTriples();
});
});
}
},
applyFilter: function() {
// always reset offset so we start at the beginning
this.offset = 0;
if (this.filter.subject === ''
&& this.filter.predicate === ''
&& this.filter.object === '') {
this.filtered = false;
} else {
this.filtered = true;
}
this.displayTriples(this.actual_filter);
},
removeFilter: function(filter) {
if (undefined === filter) {
this.filter = {
subject: '',
predicate: '',
object: ''
};
} else {
this.filter[filter] = '';
}
this.applyFilter();
},
setFilter: function(spo) {
// TODO: add some error handling and such
this.filter.subject = spo.subject;
this.filter.predicate = spo.predicate;
this.filter.object = spo.object;
},
addSPO: function() {
var self = this;
if (self.filter.subject !== '' && self.filter.predicate !== '' && self.filter.object !== '') {
self.$db.put(self.filter, function(err) {
if (err) {
console.error(err);
} else {
self.table.unshift(self.filter);
self.filter = {
subject: '',
predicate: '',
object: ''
};
self.applyFilter();
}
});
}
},
removeSPO: function(spo, idx) {
var self = this;
// remove it from the database
self.$db.del(spo, function(err) {
if (err) {
console.error(err);
} else {
// remove it from the page/app state
self.table.splice(idx, 1);
}
});
// TODO: switch to the changes feed once that's a thing
},
output: function(type) {
var self = this;
var filter = removeEmpties(JSON.parse(JSON.stringify(this.filter)));
if (type === 'n3') {
self.$db.n3.get(filter, function(err, rv) {
self.output_n3 = rv;
// TODO: hackilicious...we should fix this...
self.$refs['output-n3'].editor.setValue(self.output_n3);
self.$refs['output-n3'].refresh();
});
} else {
self.$db.get(filter, function(err, rv) {
if (err) throw err;
var graphs = [];
rv.forEach((triple) => {
graphs.push({
subject: term(triple.subject),
predicate: term(triple.predicate),
object: term(triple.object)
});
});
jsonld.fromRDF({'@default': graphs}, function(err, rv) {
self.output_jsonld = JSON.stringify(rv, null, ' ');
// TODO: hackilicious...we should fix this...
self.$refs['output-jsonld'].editor.setValue(self.output_jsonld);
self.$refs['output-jsonld'].refresh();
});
});
}
}
},
components: {
'context-form': require('./src/context-form.vue')
}
});