-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
103 lines (93 loc) · 2.41 KB
/
app.js
File metadata and controls
103 lines (93 loc) · 2.41 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
// Copyright 2025 Digital Bazaar, Inc.
//
// SPDX-License-Identifier: BSD-3-Clause
import { createApp, reactive } from 'https://unpkg.com/petite-vue?module'
import { SVGViewer } from './viewers/svg-mustache.js';
import { HTMLViewer } from './viewers/html.js';
const examplesBaseUrl = window.location.hostname !== 'localhost' ?
'https://examples.vcplayground.org/credentials/' :
'http://localhost:8788/credentials/';
// components
function KVList(value, ignoreKeys = []) {
return {
$template: '#kv-list',
value,
ignoreKeys
};
}
function RenderMethod(renderMethod, idx = 0) {
return {
$template: '#render-method',
idx,
renderMethod
};
}
async function fetchExamples() {
const examples = await fetch(`${examplesBaseUrl}index.json`)
.then((r) => r.json());
return examples;
}
window.app = createApp({
// components
HTMLViewer,
SVGViewer,
KVList,
RenderMethod,
// global state
store: reactive({
credential: {}
}),
// local state
credentialString: "",
filename: "",
landscape: "",
landscapeSVG: "",
parseError: "",
examples: await fetchExamples(),
// reactive set
setCredential(credential) {
this.store.credential = {};
this.$nextTick(() => {
this.store.credential = credential;
});
},
// methods
async pickFile() {
const [fileHandle] = await window.showOpenFilePicker();
this.filename = fileHandle.name;
const file = await fileHandle.getFile();
const text = await file.text();
try {
this.credentialString = text;
this.setCredential(JSON.parse(this.credentialString));
this.parseError = "";
} catch(error) {
// TODO: error on selected files should be reported somewhere else
// ...and be blocking...
this.parseError = error.message;
console.error(error);
};
},
loadCredential(url) {
if (url) this.credentialUrl = url;
fetch(this.credentialUrl)
.then((r) => r.json())
.then((credential) => {
this.setCredential(credential);
this.credentialString = JSON.stringify(credential, null, 2);
})
.catch(console.error);
},
getCredential($event) {
try {
this.setCredential(JSON.parse($event.target.value));
this.parseError = "";
} catch(error) {
this.parseError = error.message;
console.error(error);
}
},
loadExampleCredential(event) {
this.loadCredential(event.target.value);
}
}).mount();