-
Notifications
You must be signed in to change notification settings - Fork 4
/
EditAdditionalInfo.vue
535 lines (525 loc) · 17 KB
/
EditAdditionalInfo.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
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
525
526
527
528
529
530
531
532
533
534
535
<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
<v-form
id="editAdditionalInfo"
ref="editAdditionalInfo"
v-model="formValid"
>
<v-card>
<v-card-title class="grey lighten-4 blue--text">
Edit Additional Information
</v-card-title>
<Alerts target="additionalInformation" />
<v-card-text>
<v-container
fluid
>
<div
v-if="isPolicy"
class="d-flex full-width pb-3"
>
<b>
For more information on how to complete these sections, please see our
<a
href="https://fairsharing.gitbook.io/fairsharing/additional-information/policy-content"
target="_blank"
>
documentation.
</a>
</b>
</div>
<v-row
v-for="(field, fieldName, fieldIndex) in getFields('array')"
:key="'arrayFields_' + fieldIndex"
>
<v-col cols="12">
<div>
<v-tooltip
v-if="getFields('array')[fieldName]['description']"
class="d-inline-block mr-2"
bottom
>
<template #activator="{ on }">
<v-icon v-on="on">
fa-question-circle
</v-icon>
</template>
{{ getFields('array')[fieldName]['description'] }}
</v-tooltip>
<b class="body-1 blue--text"> {{ cleanString(fieldName).toUpperCase() }} </b>
<v-tooltip
bottom
class="d-inline-block mr-2"
>
<template #activator="{ on }">
<v-icon
small
class="blue--text white ml-2 iconReposition"
v-on="on"
@click="createItem(fieldName,
allowedFields.definitions[field.items.$ref.replace('#/definitions/', '')].properties,
allowedFields.definitions[field.items.$ref.replace('#/definitions/', '')].required || []
)"
>
fas fa-plus-circle
</v-icon>
</template>
Add a new {{ cleanString(fieldName) }}
</v-tooltip>
</div>
<v-container fluid>
<v-row v-if="!fields[fieldName] || fields[fieldName].length === 0">
No {{ cleanString(fieldName) }} for this record.
</v-row>
<v-row>
<v-col
v-for="(item, itemIndex) in fields[fieldName]"
:key="'arrayField_' + itemIndex"
cols="12"
xs="12"
sm="12"
md="6"
lg="4"
xl="3"
>
<v-card
height="100%"
class="d-flex flex-column grey lighten-4"
>
<v-card-text
style="flex-grow: 1;"
class="pb-0"
>
<div
v-for="(subField, subfieldName, subfieldIndex) in item"
:key="'arrayFieldSubField_' + subfieldIndex"
>
<b>{{ cleanString(subfieldName).toUpperCase() }}: </b> {{ subField }}
</div>
</v-card-text>
<v-card-actions>
<v-spacer />
<v-btn
class="success"
fab
x-small
@click="showOverlay(itemIndex,
fieldName, item, allowedFields.definitions[field.items.$ref.replace('#/definitions/', '')].properties,
allowedFields.definitions[field.items.$ref.replace('#/definitions/', '')].required || []
)"
>
<v-icon
x-small
>
fa-pen
</v-icon>
</v-btn>
<v-btn
class="error"
fab
x-small
@click="removeItem(fieldName, itemIndex)"
>
<v-icon x-small>
fa-trash
</v-icon>
</v-btn>
</v-card-actions>
</v-card>
</v-col>
</v-row>
</v-container>
</v-col>
</v-row>
<v-divider v-if="Object.keys(getFields('array')).length > 0" />
<v-row>
<v-col
v-for="(field, fieldName, fieldIndex) in getFields('object')"
:key="'objectField_' + fieldIndex"
cols="12"
xs="12"
sm="12"
md="12"
lg="6"
xl="3"
>
<v-card
class="grey lighten-4"
height="100%"
>
<v-card-title>
<v-tooltip
v-if="getFields('object')[fieldName]['description']"
bottom
class="d-inline-block mr-2"
>
<template #activator="{ on }">
<v-icon v-on="on">
fa-question-circle
</v-icon>
</template>
{{ getFields('object')[fieldName]['description'] }}
</v-tooltip>
<b class="body-1 blue--text"> {{ cleanString(fieldName).toUpperCase() }}: </b>
</v-card-title>
<v-card-text>
<FieldInput
v-for="(prop, propName, propIndex) in sortObject(field.properties)"
:key="'prop_' + propIndex"
:field-name="fieldName"
:field-props="prop"
:subfield-name="propName"
/>
<!-- put required in the input above -->
</v-card-text>
</v-card>
</v-col>
</v-row>
<v-divider v-if="Object.keys(getFields('object')).length > 0" />
<v-row v-if="Object.keys(getFields('enum')).length > 0">
<!-- there are currently no fields with type: enum -->
<v-col cols="12">
<b class="body-1 blue--text"> BASE FIELDS: </b>
</v-col>
<v-col
xs="12"
sm="12"
md="12"
lg="4"
xl="2"
class="pt-0"
>
<FieldInput
v-for="(field, fieldName, fieldIndex) in getFields('enum')"
:key="'switchField_' + fieldIndex"
:field-name="fieldName"
:field-props="field"
/>
</v-col>
</v-row>
<v-divider v-if="Object.keys(getFields('enum')).length > 0" />
<v-row v-if="Object.keys(getFields('string')).length > 0">
<v-col
v-for="(field, fieldName, fieldIndex) in getFields('string')"
:key="'stringField_' + fieldIndex"
cols="12"
xs="12"
sm="12"
md="12"
lg="8"
xl="10"
>
<FieldInput
:field-name="fieldName"
:field-props="field"
/>
</v-col>
</v-row>
</v-container>
</v-card-text>
<v-card-actions>
<v-btn
:disabled="!formValid"
class="info"
:loading="saving"
@click="saveRecord(false)"
>
Save and continue
</v-btn>
<v-btn
:disabled="!formValid"
class="info"
:loading="saving"
@click="saveRecord(true)"
>
Save And Exit
</v-btn>
</v-card-actions>
</v-card>
<v-fade-transition>
<v-overlay
v-if="overlay.show"
:absolute="false"
opacity="0.8"
:dark="false"
>
<v-card width="800px">
<v-form
id="editAdditionalInformationOverlay"
ref="editAdditionalInformationOverlay"
v-model="subFormValid"
>
<v-card-title class="green white--text">
Edit {{ cleanString(overlay.fieldName) }} {{ overlay.id + 1 }}
</v-card-title>
<v-card-text class="pt-4">
<div
v-for="(field, fieldName, fieldIndex) in overlay.template"
:key="'templateField_' + fieldIndex"
class="d-flex flex-row reposition"
>
<v-tooltip
v-if="overlay.template[fieldName].description"
bottom
class="d-inline-block mr-2"
>
<template #activator="{ on }">
<v-icon v-on="on">
fa-question-circle
</v-icon>
</template>
{{ overlay.template[fieldName].description }}
</v-tooltip>
<v-text-field
v-if="!field.enum"
v-model="overlay.fields[fieldName]"
:label="fieldName"
outlined
class="field"
:rules="rules(fieldName, overlay.required)"
/>
<v-autocomplete
v-else
v-model="overlay.fields[fieldName]"
:label="fieldName"
outlined
:items="field.enum"
class="field"
:rules="rules(fieldName, overlay.required)"
/>
</div>
</v-card-text>
<v-card-actions>
<v-btn
:disabled="!subFormValid"
class="success"
@click="addItem()"
>
Submit item
</v-btn>
<v-btn
class="error"
@click="hideOverlay()"
>
Cancel
</v-btn>
</v-card-actions>
</v-form>
</v-card>
</v-overlay>
</v-fade-transition>
</v-form>
</template>
<script>
import { isEqual } from "lodash"
import Vue from "vue"
import {mapActions, mapGetters, mapMutations,mapState} from "vuex";
import sortObj from "@/utils/generalUtils"
import { isRequired,isUrl } from "@/utils/rules.js"
import stringUtils from '@/utils/stringUtils'
import Alerts from "../Alerts";
import FieldInput from "./FieldInput";
const diff = require("deep-object-diff").diff;
export default {
name: "EditAdditionalInfo",
components: { Alerts, FieldInput },
mixins: [ stringUtils, sortObj ],
data() {
return {
isPolicy: false,
initialized: false,
overlay: {
show: false,
id: null,
fieldName: null,
fields: null,
template: null
},
saving: false,
subFormValid: false,
formValid: false
}
},
computed: {
...mapGetters("record", ["getSection"]),
...mapState("users", ["user"]),
...mapState("editor", ["allowedFields"]),
fields() {
return this.getSection("additionalInformation").data
},
initialData(){
return this.getSection("additionalInformation").initialData;
},
message(){
let error = this.getSection("additionalInformation").error;
return {
error: error,
value: this.getSection("additionalInformation").message
};
}
},
watch: {
fields: {
deep: true,
/* istanbul ignore next */
handler(newVal) {
this.submitChanges(newVal);
}
}
},
mounted(){
if (this.allowedFields.id && this.allowedFields.id.includes('policy')) {
this.isPolicy = true;
}
this.$nextTick(() => {this.$refs['editAdditionalInfo'].validate()});
},
methods: {
...mapActions("record", ["updateAdditionalInformation"]),
...mapMutations("record", ["setAdditionalInformationSubField", "removeAdditionalInformationSubField"]),
getFields(type) {
let output = {};
if (this.allowedFields && this.allowedFields.properties){
Object.keys(this.sortObj(this.allowedFields.properties)).forEach((fieldName) => {
if (this.allowedFields.properties[fieldName].type === type
&& !this.allowedFields.properties[fieldName].enum) {
output[fieldName] = this.allowedFields.properties[fieldName]
}
else if (type === 'string' && this.allowedFields.properties[fieldName].enum){
// TODO: These types must be checked, or perhaps this check removed, if the schema is modified
// TODO: to add additional properties.
let expected = new Set(["yes", "no", "not found"]);
let fieldEnum = new Set(this.allowedFields.properties[fieldName].enum);
if (isEqual(expected, fieldEnum)) {
output[fieldName] = this.allowedFields.properties[fieldName]
}
}
});
}
return output;
},
showOverlay(id, fieldName, item, template, required){
this.overlay = {
show: true,
id,
fieldName,
template,
required,
fields: JSON.parse(JSON.stringify(item))
};
/* istanbul ignore next */
this.$nextTick(() => {
if(this.$refs['editAdditionalInformationOverlay']) this.$refs['editAdditionalInformationOverlay'].validate()}
);
},
hideOverlay(){
this.overlay = {
show: false,
id: null,
fieldName: null,
fields: null,
template: null
}
},
createItem(fieldName, template, required){
this.overlay = {
show: true,
id: null,
fieldName,
template,
required,
fields: {}
};
Object.keys(template).forEach(field => {
Vue.set(this.overlay.fields, field, null)
});
/* istanbul ignore next */
this.$nextTick(() => {
if(this.$refs['editAdditionalInformationOverlay']) this.$refs['editAdditionalInformationOverlay'].validate()}
);
},
addItem(){
this.setAdditionalInformationSubField({
fieldName: this.overlay.fieldName,
id: this.overlay.id,
fieldValue: this.overlay.fields
});
this.overlay = {
show: false,
id: null,
fieldName: null,
fields: null,
required: null,
template: null
}
// It appears that this isn't called when a field is edited; it's not clear why the watcher doesn't
// detect the change. So, it is explicitly called here.
// https://github.com/FAIRsharing/fairsharing.github.io/issues/1718
this.submitChanges(this.fields);
},
removeItem(fieldName, index){
this.removeAdditionalInformationSubField({
id: index,
fieldName
})
},
rules(fieldName, required){
let rules = [];
if (this.overlay.template[fieldName].format && this.overlay.template[fieldName].format === 'uri') {
rules.push(isUrl());
}
if (required && required.indexOf(fieldName) > -1) {
rules.push(isRequired());
}
return rules;
},
async saveRecord(redirect){
this.saving = true;
await this.updateAdditionalInformation({
fields: Object.keys(this.allowedFields.properties),
id: this.$route.params.id,
token: this.user().credentials.token
});
this.saving = false;
if (this.message.error || !redirect){
this.$scrollTo("#mainHeader");
}
else {
await this.$router.push({path: '/' + this.$route.params.id})
}
},
submitChanges(newVal){
let delta = 0;
let changes = diff(this.initialData, newVal);
Object.keys(changes).forEach(change => {
if (changes[change] !== null && Object.keys(changes[change]).length > 0) delta += 1
});
this.$store.commit("record/setChanges", {
section: "additionalInformation",
value: delta
});
this.overlay = {
show: false,
id: null,
fieldName: null,
fields: null,
template: null
}
},
sortObject(obj) {
return Object.keys(obj).sort().reduce((res, key) => (res[key] = obj[key], res), {});
}
}
}
</script>
<style>
.iconReposition {
position: relative;
top: -2px;
}
.v-tooltip__content {
max-width: 400px;
}
#editAdditionalInfo .reposition .fa{
position: relative;
top: -14px;
}
</style>