/
NodeTreeEdit.vue
233 lines (224 loc) · 5.82 KB
/
NodeTreeEdit.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
<template>
<div class="table text-center bg-gray-300">
<template v-if="nodeTree.type === 'RESULT'">
<ResultNodeEdit
v-model="nodeTree.text"
:is-show-delete-button="parentOwnNodeLength !== 1"
@deleteResultNode="handleDeleteResultNode"
@convertToQuestionNode="handleConvertToQuestionNode"
/>
</template>
<template v-if="nodeTree.type === 'QUESTION'">
<QuestionNodeEdit
:width="questionNodeWidth(nodeTree)"
:question-string="nodeTree.text"
:choice-label-strings="nodeTree.choiceNodes.map((a) => a.label)"
:is-show-convert-button="!isRoot"
@updateQuestionString="(v) => (nodeTree.text = v)"
@updateChoiceLabelStrings="handleUpdateChoiceLabelStrings"
@convertToResultNode="handleConvertToResultNode"
@addChoice="handleAddChoice"
/>
<div
class="nodes table relative"
:class="choiceNodesWrapperClass(nodeTree)"
>
<div
v-for="(choiceNode, index) in nodeTree.choiceNodes"
:key="index"
class="node-wrapper table-cell relative"
>
<div class="vertical-bar mb-0"></div>
<div
class="inline-block text-gray-600 bg-gray-200 rounded-full border border-gray-400 mt-0"
:style="{ fontSize: '0.5rem', padding: '0.25rem' }"
>
{{
choiceNode.label === '' ? `選択肢${index + 1}` : choiceNode.label
}}
</div>
<NodeTreeEdit
:node-tree="choiceNode.nodeTree"
:parent-own-node-length="nodeTree.choiceNodes.length"
class="node relative"
@deleteResultNode="handleDeleteResultNodeFromChild(index)"
@convertToResultNode="handleConvertToResultNodeFromChild(index)"
/>
</div>
</div>
</template>
</div>
</template>
<script lang="ts">
import Vue, { PropType } from 'vue'
import { NodeTree, ResultNodeTree, QuestionNodeTree } from '~/types/struct'
import QuestionNodeEdit from '~/components/partials/NodeTreeEditor/QustionNodeEdit.vue'
import ResultNodeEdit from '~/components/partials/NodeTreeEditor/ResultNodeEdit.vue'
export default Vue.extend({
name: 'NodeTreeEdit',
components: {
QuestionNodeEdit,
ResultNodeEdit
},
props: {
isRoot: {
type: Boolean,
required: false,
default: false
},
nodeTree: {
type: Object as PropType<NodeTree>,
required: true
},
parentOwnNodeLength: {
type: Number,
required: false,
default: 1
}
},
methods: {
choiceNodesWrapperClass(questionNodeTree: QuestionNodeTree): string {
return questionNodeTree.choiceNodes.length === 2
? 'before-two-vertical-line'
: 'before-vertical-line'
},
questionNodeWidth(questionNodeTree: QuestionNodeTree): string {
return questionNodeTree.choiceNodes.length === 1
? '16rem'
: `${10 * questionNodeTree.choiceNodes.length}rem`
},
handleDeleteResultNode(): void {
this.$emit('deleteResultNode')
},
handleDeleteResultNodeFromChild(index: number): void {
if (this.nodeTree.type === 'QUESTION') {
this.nodeTree.choiceNodes.splice(index, 1)
}
},
handleConvertToQuestionNode(): void {
this.nodeTree.type = 'QUESTION'
this.$set(this.nodeTree, 'choiceNodes', [
{
label: '',
nodeTree: {
type: 'RESULT',
text: ''
}
},
{
label: '',
nodeTree: {
type: 'RESULT',
text: ''
}
}
])
},
handleUpdateChoiceLabelStrings(labelStrings: string[]): void {
labelStrings.forEach((str, index) => {
if (this.nodeTree.type === 'QUESTION') {
this.nodeTree.choiceNodes[index].label = str
}
})
},
handleConvertToResultNode() {
return this.$emit('convertToResultNode')
},
handleConvertToResultNodeFromChild(index: number) {
if (this.nodeTree.type === 'QUESTION') {
const newResultNodeTree: ResultNodeTree = {
type: 'RESULT',
text: this.nodeTree.choiceNodes[index].nodeTree.text
}
this.nodeTree.choiceNodes[index].nodeTree = newResultNodeTree
}
},
handleAddChoice() {
if (this.nodeTree.type === 'QUESTION') {
this.nodeTree.choiceNodes.push({
label: '',
nodeTree: {
type: 'RESULT',
text: ''
}
})
}
}
}
})
</script>
<style scoped>
.nodes {
margin-top: 1.2rem;
}
.node {
margin-top: 1.5rem;
}
/* | */
.vertical-bar {
width: 2px;
background-color: theme('colors.gray.900');
margin-right: auto;
margin-left: auto;
height: 0.8rem;
}
/* | */
.node:before {
outline: solid 1px theme('colors.gray.900');
content: '';
height: 1.3rem;
left: 50%;
position: absolute;
top: -1.3rem;
}
/* ▼ */
.node:after {
content: '';
position: absolute;
top: -0.4rem;
left: 50%;
margin-left: -0.4rem;
border: 0.4rem solid transparent;
border-top: 0.4rem solid theme('colors.gray.900');
z-index: 2;
}
/* | | */
.before-two-vertical-line:before {
border: solid theme('colors.gray.900');
background-color: theme('colors.gray.300');
z-index: 10;
border-width: 0px 0.1rem 0px 0.1rem;
content: '';
height: 1.2rem;
width: 4rem;
left: 50%;
position: absolute;
margin-left: -2rem;
top: -1.1rem;
}
/* | */
.before-vertical-line:before {
outline: solid 1px theme('colors.gray.900');
content: '';
height: 1.2rem;
width: 0;
left: 50%;
position: absolute;
top: -1.2rem;
}
/* ___ */
.node-wrapper:before {
outline: solid 0.1rem theme('colors.gray.900');
content: '';
left: 0;
position: absolute;
right: 0;
top: 0;
}
.node-wrapper:first-child:before {
left: 50%;
}
.node-wrapper:last-child:before {
right: 50%;
}
</style>