forked from area17/twill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectorField.vue
executable file
·90 lines (86 loc) · 2.23 KB
/
ConnectorField.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
<template>
<div>
<template v-if="!keepAlive">
<div v-if="open">
<slot></slot>
</div>
</template>
<template v-else>
<div v-show="open">
<slot></slot>
</div>
</template>
</div>
</template>
<script>
import isEqual from 'lodash/isEqual'
import clone from 'lodash/clone'
import { mapState, mapGetters } from 'vuex'
export default {
name: 'A17ConnectorField',
props: {
fieldName: {
type: String,
required: true
},
requiredFieldValues: {
default: ''
},
inModal: {
type: Boolean,
default: false
},
keepAlive: {
type: Boolean,
default: false
},
isValueEqual: { // requiredFieldValues must be equal (or different) to the stored value to show
type: Boolean,
default: true
}
},
computed: {
storedValue: function () {
if (this.inModal) return this.modalFieldValueByName(this.fieldName)
else return this.fieldValueByName(this.fieldName)
},
...mapGetters([
'fieldValueByName',
'modalFieldValueByName'
]),
...mapState({
fields: state => state.form.fields, // Fields in the form
modalFields: state => state.form.modalFields // Fields in the create/edit modal
})
},
data: function () {
return {
open: false
}
},
watch: {
storedValue: function (fieldInstore) {
this.toggleVisibility(fieldInstore)
}
},
methods: {
toggleVisibility: function (value) {
const newValue = clone(value)
const newFieldValues = clone(this.requiredFieldValues)
// sort requiredFieldValues and value if is array, so the order of values is the same
if (Array.isArray(newFieldValues)) newFieldValues.sort()
if (Array.isArray(newValue)) newValue.sort()
// update visiblity
if (this.isValueEqual) this.open = isEqual(newValue, newFieldValues)
else this.open = !isEqual(newValue, newFieldValues)
}
},
mounted: function () {
const self = this
// init show/hide
this.$nextTick(function () {
self.toggleVisibility(this.storedValue)
})
}
}
</script>