-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSidebar.vue
More file actions
524 lines (469 loc) · 19 KB
/
Copy pathSidebar.vue
File metadata and controls
524 lines (469 loc) · 19 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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
<template>
<!-- Sidebar showing phyloreferences and phylogenies -->
<div id="sidebar-wrapper">
<!-- Sidebar section: information about this particular PHYX file -->
<div class="card bg-dark">
<div class="list-group list-group-flush">
<a
class="list-group-item list-group-item-action bg-dark text-light disabled"
href="javascript: void(0)"
>
Actions
</a>
<a
class="list-group-item list-group-item-action"
href="javascript: void(0)"
onclick="$('#file-input').trigger('click')"
>
Read from local JSON file
</a>
<input
id="file-input"
type="file"
class="d-none"
@change="loadPhyxFromFileInputById('#file-input')"
>
<a
class="list-group-item list-group-item-action"
href="javascript: void(0)"
onclick="$('.phyx-examples').toggleClass('d-none')"
>
Read an example file
</a>
<a
v-for="example of examplePHYXURLs"
href="javascript: void(0)"
class="list-group-item list-group-item-action phyx-examples d-none small"
@click="loadPhyxFromURL(example.url)"
>
● {{ example.title }}
</a>
<a
class="list-group-item list-group-item-action phyx-examples d-none small"
href="javascript: void(0)"
onclick="$('.phyx-examples').toggleClass('d-none')"
>
<em>Close list of examples</em>
</a>
<a
class="list-group-item list-group-item-action"
href="javascript: void(0)"
@click="downloadAsJSON()"
>
Save
<ModifiedIcon
message="This Phyx file has been modified after it was loaded! Use 'Save' to save your changes."
:compare="currentPhyx"
:compare-to="loadedPhyx"
/>
</a>
<a
class="list-group-item list-group-item-action"
href="javascript: void(0)"
@click="downloadAsJSONLD()"
>
Export as JSON-LD
</a>
<a
class="list-group-item list-group-item-action"
href="javascript: void(0)"
@click="downloadAsNQuads()"
>
Export as ontology
</a>
<a
class="list-group-item list-group-item-action start-reasoning"
href="javascript: void(0)"
@click="reasonOverPhyloreferences()"
>
<span v-if="reasoningInProgress">
(Reasoning in progress)
</span>
<span v-else>
Resolve against phylogenies
</span>
</a>
<a
class="list-group-item list-group-item-action"
href="javascript: void(0)"
onclick="$('#advanced-options').modal()"
>
Advanced
</a>
</div>
</div>
<!-- Sidebar section: list of phyloreferences in PHYX file -->
<div class="card mt-2 bg-dark">
<!-- List of phyloreferences -->
<div class="list-group list-group-flush">
<a
href="javascript: void(0)"
class="list-group-item list-group-item-action bg-dark text-light disabled"
>
Phyloreferences
</a>
<a
class="list-group-item list-group-item-action"
href="javascript: void(0)"
:class="{active: !selectedPhyloref && !selectedPhylogeny}"
@click="$store.commit('changeDisplay', {})"
>
<em>Summary</em>
</a>
<template v-for="(phyloref, phylorefIndex) of phylorefs">
<a
href="javascript: void(0)"
class="h6 list-group-item list-group-item-action border-dark"
:class="{active: selectedPhyloref === phyloref}"
@click="$store.commit('changeDisplay', {phyloref})"
>
<span v-if="phyloref.label">
{{ phyloref.label }}
</span>
<span v-else>
{{ 'Phyloreference ' + (phylorefIndex + 1) }}
</span>
<!-- Add a warning if this phyloreference has changed -->
<ModifiedIcon
message="This phyloreference has been modified after it was loaded! Use 'Save' to save your changes."
:compare="phyloref"
:compare-to="loadedPhyx.phylorefs[phylorefIndex]"
/>
</a>
<!-- Add the following information if this phyloreference is selected -->
<template v-if="selectedPhyloref === phyloref">
<a
v-for="(specifier, specifierIndex) of phyloref.internalSpecifiers"
href="javascript: void(0)"
class="list-group-item list-group-item-action"
:class="{'active border-dark': selectedSpecifier === specifier}"
@click="$store.commit('changeDisplay', { phyloref, specifier })"
>
● <strong>Internal:</strong> {{ getSpecifierLabel(specifier) }}
</a>
<a
v-for="(specifier, specifierIndex) of phyloref.externalSpecifiers"
href="javascript: void(0)"
class="list-group-item list-group-item-action"
:class="{'active border-dark': selectedSpecifier === specifier}"
@click="$store.commit('changeDisplay', { phyloref, specifier })"
>
● <strong>External:</strong> {{ getSpecifierLabel(specifier) }}
</a>
<a
href="javascript: void(0)"
class="list-group-item list-group-item-action"
@click="$store.commit('addSpecifier', { phyloref })"
>
● <em>Add specifier</em>
</a>
</template>
</template>
<!-- Let users add phyloreferences directly from the sidebar -->
<a
class="list-group-item list-group-item-action border-dark"
href="javascript: void(0)"
@click="$store.commit('createEmptyPhyloref')"
>
<em>Add phyloreference</em>
</a>
</div>
</div>
<!-- Sidebar section: list of phylogenies in PHYX file -->
<div class="card mt-2 bg-dark">
<div class="list-group list-group-flush">
<a
href="javascript: void(0)"
class="list-group-item list-group-item-action bg-dark text-light disabled"
>
Phylogenies
</a>
<a
v-for="(phylogeny, phylogenyIndex) of phylogenies"
href="javascript: void(0)"
class="h6 list-group-item list-group-item-action border-dark"
:class="{active: selectedPhylogeny === phylogeny}"
@click="$store.commit('changeDisplay', {phylogeny})"
>
{{ phylogeny.label || `Phylogeny ${phylogenyIndex + 1}` }}
<!-- Add a warning if this phylogeny has changed -->
<ModifiedIcon
message="This phylogeny has been modified after it was loaded! Use 'Save' to save your changes."
:compare="phylogeny"
:compare-to="loadedPhyx.phylogenies[phylogenyIndex]"
/>
</a>
<a
class="list-group-item list-group-item-action border-dark"
href="javascript: void(0)"
@click="$store.commit('createEmptyPhylogeny')"
>
<em>Add phylogeny</em>
</a>
<a
class="list-group-item list-group-item-action"
href="javascript: void(0)"
@click="$store.dispatch('createPhylogenyFromOpenTree')"
>
<em>Add Open Tree of Life phylogeny</em>
</a>
</div>
</div>
</div><!-- End of sidebar -->
</template>
<script>
/*
* Creates the sidebar that displays:
* - Menu items (save, load, reason)
* - A list of all phyloreferences and a button to add more phylorefs.
* - A list of all phylogenies and a button to add more phylogenies.
*/
import Vue from 'vue';
import { has } from 'lodash';
import { mapState, mapGetters } from 'vuex';
import { saveAs } from 'file-saver';
import { signer } from 'x-hub-signature';
import zlib from 'zlib';
import { PhyxWrapper, PhylorefWrapper, TaxonomicUnitWrapper } from '@phyloref/phyx';
import ModifiedIcon from '../icons/ModifiedIcon.vue';
export default {
name: 'Sidebar',
components: {
ModifiedIcon,
},
data() {
return {
reasoningInProgress: false, // Set when reasoning is in progress.
};
},
computed: {
downloadFilenameForPhyx() {
// Return a filename to be used to name downloads of this Phyx document.
// The default download filename to use if no phylorefs are present.
const DEFAULT_DOWNLOAD_FILENAME = 'download';
if (!this.currentPhyx || !this.phylorefs) {
return DEFAULT_DOWNLOAD_FILENAME;
}
// Determine all phyloref labels in this document. Non-Latin characters will be replaced with '_' to avoid
// creating filenames using non-ASCII Unicode characters. As per the UI, unlabeled phylorefs will be referred
// to as 'Phyloref 1', 'Phyloref 2', and so on.
const phylorefLabels = this.phylorefs.map((p, index) => (has(p, 'label') ? p.label.replaceAll(/\W/g, '_') : `Phyloref_${index + 1}`));
// Construct a download filename depending on the number of phylorefs, which is in the form:
// - Phyloref_1
// - Phyloref_1_and_Phyloref_2
// - Phyloref_1_Phyloref_2_and_Phyloref_3
// - Phyloref_1_Phyloref_2_and_2_others
// - ...
if (phylorefLabels.length === 0) return DEFAULT_DOWNLOAD_FILENAME;
if (phylorefLabels.length === 1) return phylorefLabels[0];
if (phylorefLabels.length === 2) return `${phylorefLabels[0]}_and_${phylorefLabels[1]}`;
if (phylorefLabels.length === 3) return `${phylorefLabels[0]}_${phylorefLabels[1]}_and_${phylorefLabels[2]}`;
return `${phylorefLabels[0]}_${phylorefLabels[1]}_and_${phylorefLabels.length - 2}_others`;
},
examplePHYXURLs() {
// Returns a list of example files to display in the "Examples" menu.
return [
{
url: 'examples/brochu_2003.json',
title: 'Brochu 2003',
},
];
},
...mapGetters([
'phyxTitle',
]),
...mapState({
phyx: state => state.phyx,
currentPhyx: state => state.phyx.currentPhyx,
loadedPhyx: state => state.phyx.loadedPhyx,
phylorefs: state => state.phyx.currentPhyx.phylorefs,
phylogenies: state => state.phyx.currentPhyx.phylogenies,
selectedPhyloref: state => state.ui.display.phyloref,
selectedPhylogeny: state => state.ui.display.phylogeny,
selectedSpecifier: state => state.ui.display.specifier,
}),
},
methods: {
getSpecifierLabel(specifier) {
// Get the label for a particular specifier.
// TODO: We need to include verbatimSpecifier first because of
// https://github.com/phyloref/phyx.js/issues/14
return specifier.verbatimSpecifier || new TaxonomicUnitWrapper(specifier).label || 'Undefined specifier';
},
promptAndSetDict(message, dict, key) {
// Given a dictionary and key, we prompt the user (using window.prompt)
// to provide a new value for that dictionary and key. If one is provided,
// we replace it.
const response = window.prompt(message, dict[key]);
if (response !== undefined && response !== null) Vue.set(dict, key, response);
},
loadPhyxFromURL(url) {
// Change the current PHYX to that in the provided URL.
// Is the user sure that they want to do this?
if(this.$store.getters.loadedPhyxChanged) {
if(!confirm('The current Phyx file has been modified! Are you sure you want to discard these changes by loading another file?')) {
return;
}
}
$.getJSON(url)
.done((data) => {
this.$store.commit('setCurrentPhyx', data);
this.$store.commit('setLoadedPhyx', data);
// Reset the display.
this.$store.commit('changeDisplay', {});
})
.fail((error) => {
if (error.status === 200) {
alert(`Could not load PHYX file '${url}': file malformed, see console for details.`);
} else {
alert(`Could not load PHYX file '${url}': server error ${error.status} ${error.statusText}`);
}
// throw new Error(`Could not load PHYX file ${url}: ${error}`);
});
},
loadPhyxFromFileInputById(fileInputId) {
//
// Load a JSON file from the local file system using FileReader. fileInput
// needs to be an HTML element representing an <input type="file"> in which
// the user has selected the local file they wish to load.
//
// This code is based on https://stackoverflow.com/a/21446426/27310
if (typeof window.FileReader !== 'function') {
alert('The FileReader API is not supported on this browser.');
return;
}
const $fileInput = $(fileInputId);
if (!$fileInput) {
alert('Programmer error: No file input element specified.');
return;
}
if (!$fileInput.prop('files')) {
alert('File input element found, but files property missing: try another browser?');
return;
}
if (!$fileInput.prop('files')[0]) {
alert('Please select a file before attempting to load it.');
return;
}
// Is the user sure that they want to do this?
if(this.$store.getters.loadedPhyxChanged) {
if(!confirm('The current Phyx file has been modified! Are you sure you want to discard these changes by loading another file?')) {
return;
}
}
const [file] = $fileInput.prop('files');
const fr = new FileReader();
fr.onload = ((e) => {
const lines = e.target.result;
const phyx = JSON.parse(lines);
this.$store.commit('setCurrentPhyx', phyx);
this.$store.commit('setLoadedPhyx', phyx);
// Reset the display.
this.$store.commit('changeDisplay', {});
});
fr.readAsText(file);
},
downloadAsJSON() {
// Provide the JSON file as a download to the browser.
const content = [JSON.stringify(this.$store.state.phyx.currentPhyx, undefined, 4)];
// Save to local hard drive.
const jsonFile = new File(content, `${this.downloadFilenameForPhyx}.json`, { type: 'application/json;charset=utf-8' });
saveAs(jsonFile, `${this.downloadFilenameForPhyx}.json`);
// saveAs(jsonFile) doesn't report on whether the user accepted the download
// or not. We assume, possibly incorrectly, that they did and that the
// current JSON content has been saved. We therefore reset testcaseAsLoaded
// so we can watch for other changes.
//
// A more sophisticated API like https://github.com/jimmywarting/StreamSaver.js
// might be able to let us know if the file was saved correctly.
//
// Deep-copy the testcase into a 'testcaseAsLoaded' variable in our
// model. We deep-compare this.testcase with this.testcaseAsLoaded to
// determine if the loaded model has been modified.
this.$store.commit('setLoadedPhyx');
},
downloadAsJSONLD() {
// Exports the PHYX file as an OWL/JSON-LD file, which can be opened in
// Protege or converted into OWL/XML or other formats.
const wrapped = new PhyxWrapper(this.$store.state.phyx.currentPhyx);
const content = [JSON.stringify([wrapped.asJSONLD()], undefined, 4)];
// Save to local hard drive.
const jsonldFile = new File(content, `${this.downloadFilenameForPhyx}.jsonld`, { type: 'application/json;charset=utf-8' });
saveAs(jsonldFile, `${this.downloadFilenameForPhyx}.jsonld`);
},
downloadAsNQuads() {
// Exports the PHYX file as an OWL/N-Quads file, which can be opened in
// Protege or converted into other RDF formats.
const wrapped = new PhyxWrapper(this.$store.state.phyx.currentPhyx);
// TODO: we need a baseIRI here because of https://github.com/phyloref/phyx.js/issues/113
// Once that is fixed in phyx.js, we can remove it here.
wrapped.toRDF('https://example.phyloref.org/phyx#').then((content) => {
// Save to local hard drive.
const nqFile = new File([content], `${this.downloadFilenameForPhyx}.owl`, { type: 'application/n-quads;charset=utf-8' });
saveAs(nqFile, `${this.downloadFilenameForPhyx}.owl`);
});
},
reasonOverPhyloreferences() {
// Reason over all the phyloreferences and store the results on
// the Vue model at vm.reasoningResults so we can access them.
// Are we already reasoning? If so, ignore.
if (this.reasoningInProgress) return;
// Reset the existing reasoning information.
this.$store.commit('setReasoningResults', undefined);
// Disable "Reason" buttons so they can't be reused.
this.reasoningInProgress = true;
// Make sure that the Reason button is updated before we convert the Phyx
// file into JSON-LD.
const outerThis = this;
Vue.nextTick(function () {
// Prepare JSON-LD file for submission.
const jsonld = JSON.stringify([new PhyxWrapper(
outerThis.$store.state.phyx.currentPhyx,
d3.layout.newick_parser,
).asJSONLD()]);
// To improve upload speed, let's Gzip the file before upload.
const jsonldGzipped = zlib.gzipSync(jsonld);
// Prepare request for submission.
const query = $.param({
// Convert Gzipped data into a string in Base64.
jsonldGzipped: Buffer.from(jsonldGzipped).toString('base64')
}).replace(/%20/g, '+'); // $.post will do this automatically,
// but we need to do this here so our
// signature works.
// Sign it with an X-Hub-Signature.
const sign = signer({
algorithm: 'sha1',
secret: outerThis.$config.JPHYLOREF_X_HUB_SIGNATURE_SECRET,
});
const signature = sign(new Buffer(query));
console.log('Query: ', query);
console.log('Signature: ', signature);
$.post({
url: outerThis.$config.JPHYLOREF_SUBMISSION_URL,
data: query,
headers: {
'X-Hub-Signature': signature,
},
}).done((data) => {
outerThis.$store.commit('setReasoningResults', data);
// console.log('Data retrieved: ', data);
}).fail((jqXHR, textStatus, errorThrown) => {
// We can try using the third argument, but it appears to be the
// HTTP status (e.g. 'Internal Server Error'). So we default to that,
// but look for a better one in the JSON response from the server, if
// available.
let error = errorThrown;
if (has(jqXHR, 'responseJSON') && has(jqXHR.responseJSON, 'error')) {
error = jqXHR.responseJSON.error;
}
if (error === undefined || error === '') error = 'unknown error';
alert(`Error occurred on server while reasoning: ${error}`);
}).always(() => {
// Reset "Reasoning" buttons to their usual state.
outerThis.reasoningInProgress = false;
});
});
},
},
};
</script>