forked from eclipsesource/jsonforms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.vue
226 lines (207 loc) · 4.99 KB
/
App.vue
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
<script lang="ts">
import { defineComponent } from 'vue';
import { JsonForms, JsonFormsChangeEvent } from '../../config/jsonforms';
import { vanillaRenderers, mergeStyles, defaultStyles } from '../../src';
import '../../vanilla.css';
import { JsonFormsI18nState } from '@jsonforms/core';
import { ErrorObject } from 'ajv';
import { getExamples } from '../../../examples';
import get from 'lodash/get';
// mergeStyles combines all classes from both styles definitions into one
const myStyles = mergeStyles(defaultStyles, {
control: { root: 'my-control' },
});
const examples = getExamples();
export default defineComponent({
name: 'App',
components: {
JsonForms,
},
provide() {
return {
styles: myStyles,
};
},
data: function () {
const i18n: Partial<JsonFormsI18nState> = { locale: 'en' };
const additionalErrors: ErrorObject[] = [];
return {
data: {},
renderers: Object.freeze(vanillaRenderers),
currentExampleName: examples[0].name,
examples,
config: {
hideRequiredAsterisk: true,
},
i18n,
additionalErrors,
};
},
computed: {
example() {
const name = (this as any).currentExampleName;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return examples.find((ex) => ex.name === name)!;
},
},
beforeMount() {
const searchURL = new URL(String(window.location));
const name = searchURL.hash?.substring(1);
const exists = name && examples.find((ex) => ex.name === name);
if (name && exists) {
this.currentExampleName = name;
}
},
methods: {
onChange(event: JsonFormsChangeEvent) {
console.log(event);
this.data = event.data;
const searchURL = new URL(String(window.location));
searchURL.hash = this.currentExampleName;
window.history.pushState({}, '', searchURL);
},
onExampleChange(event: any) {
this.currentExampleName = event.target.value;
},
translationChange(event: any) {
try {
const input = JSON.parse(event.target.value);
(this as any).i18n.translate = (
key: string,
defaultMessage: string | undefined
) => {
const translated = get(input, key) as string;
return translated ?? defaultMessage;
};
} catch (error) {
console.log('invalid translation input');
}
},
},
});
</script>
<template>
<div class="container">
<header>
<h1>Welcome to JSON Forms with Vue Vanilla</h1>
<p>More Forms. Less Code.</p>
</header>
<aside class="example-selector">
<div class="data">
<details>
<summary>data</summary>
<pre
>{{ JSON.stringify(data, null, 2) }}
</pre>
</details>
<details>
<summary>schema</summary>
<pre
>{{ JSON.stringify(example.schema, null, 2) }}
</pre>
</details>
<details>
<summary>uischema</summary>
<pre
>{{ JSON.stringify(example.uischema, null, 2) }}
</pre>
</details>
<h5>i18n translator</h5>
<textarea @change="translationChange"></textarea>
</div>
</aside>
<div class="tools">
<h4>Select Example:</h4>
<select v-model="currentExampleName" @change="onExampleChange($event)">
<option
v-for="option in examples"
:key="option.name"
:value="option.name"
:label="option.label"
>
{{ option.label }}
</option>
</select>
</div>
<main class="form">
<article>
<json-forms
:key="example.name"
:data="example.data"
:schema="example.schema"
:uischema="example.uischema"
:renderers="renderers"
:config="config"
:i18n="i18n"
:additional-errors="additionalErrors"
@change="onChange"
>
</json-forms>
</article>
</main>
</div>
</template>
<style scoped>
.container {
display: grid;
grid-template-columns: 0 30% auto 0;
grid-column-gap: 2rem;
grid-row-gap: 1rem;
grid-template-areas:
'header header header header'
'. tools tools .'
'. aside main .';
}
.container > header {
grid-area: header;
background-color: #00021e;
padding: 2rem;
color: white;
text-align: center;
}
.container > aside {
grid-area: aside;
}
.container > main {
grid-area: main;
}
.container > .tools {
grid-area: tools;
display: flex;
align-items: center;
justify-content: center;
gap: 1rem;
}
aside .data {
background-color: white;
padding: 2rem;
}
aside summary,
aside h5 {
font-size: 0.83em;
font-weight: bold;
margin: 0 0 1em;
}
aside summary {
cursor: default;
}
aside details pre {
background: #eee;
border: 0;
min-height: 300px;
max-height: 500px;
overflow: scroll;
}
main article {
background-color: white;
padding: 1rem;
}
</style>
<style>
body {
margin: 0;
padding: 0;
font-family: sans-serif;
background: #f3f4fa;
}
</style>