-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathQuestionDropdown.vue
More file actions
213 lines (189 loc) Β· 5.11 KB
/
Copy pathQuestionDropdown.vue
File metadata and controls
213 lines (189 loc) Β· 5.11 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
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
<!--
- SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<Question
v-bind="questionProps"
:title-placeholder="answerType.titlePlaceholder"
:warning-invalid="answerType.warningInvalid"
:content-valid="contentValid"
:shift-drag-handle="shiftDragHandle"
v-on="commonListeners">
<template #actions>
<NcActionCheckbox
:checked="extraSettings?.shuffleOptions"
@update:checked="onShuffleOptionsChange">
{{ t('forms', 'Shuffle options') }}
</NcActionCheckbox>
<NcActionButton close-after-click @click="isOptionDialogShown = true">
<template #icon>
<IconContentPaste :size="20" />
</template>
{{ t('forms', 'Add multiple options') }}
</NcActionButton>
</template>
<NcSelect
v-if="readOnly"
:value="selectedOption"
:name="name || undefined"
:placeholder="selectOptionPlaceholder"
:multiple="isMultiple"
:required="isRequired"
:options="sortedOptions"
:searchable="false"
label="text"
:aria-label-combobox="selectOptionPlaceholder"
@input="onInput" />
<template v-else>
<div v-if="isLoading">
<NcLoadingIcon :size="64" />
</div>
<Draggable
v-else
v-model="sortedOptions"
class="question__content"
animation="200"
direction="vertical"
handle=".option__drag-handle"
invert-swap
tag="ul"
@change="saveOptionsOrder"
@start="isDragging = true"
@end="isDragging = false">
<TransitionGroup
:name="
isDragging
? 'no-external-transition-on-drag'
: 'options-list-transition'
">
<!-- Answer text input edit -->
<AnswerInput
v-for="(answer, index) in sortedOptions"
:key="answer.local ? 'option-local' : answer.id"
ref="input"
:answer="answer"
:form-id="formId"
is-dropdown
:index="index"
:is-unique="!isMultiple"
:max-index="options.length - 1"
:max-option-length="maxStringLengths.optionText"
@create-answer="onCreateAnswer"
@update:answer="updateAnswer"
@delete="deleteOption"
@focus-next="focusNextInput"
@move-up="onOptionMoveUp(index)"
@move-down="onOptionMoveDown(index)"
@tabbed-out="checkValidOption" />
</TransitionGroup>
</Draggable>
</template>
<!-- Add multiple options modal -->
<OptionInputDialog
:open.sync="isOptionDialogShown"
@multiple-answers="handleMultipleOptions" />
</Question>
</template>
<script>
import Draggable from 'vuedraggable'
import NcActionCheckbox from '@nextcloud/vue/components/NcActionCheckbox'
import NcActionButton from '@nextcloud/vue/components/NcActionButton'
import NcLoadingIcon from '@nextcloud/vue/components/NcLoadingIcon'
import NcSelect from '@nextcloud/vue/components/NcSelect'
import IconContentPaste from 'vue-material-design-icons/ContentPaste.vue'
import AnswerInput from './AnswerInput.vue'
import OptionInputDialog from '../OptionInputDialog.vue'
import QuestionMixin from '../../mixins/QuestionMixin.js'
import QuestionMultipleMixin from '../../mixins/QuestionMultipleMixin.ts'
export default {
name: 'QuestionDropdown',
components: {
AnswerInput,
Draggable,
IconContentPaste,
NcActionButton,
NcActionCheckbox,
NcLoadingIcon,
NcSelect,
OptionInputDialog,
},
mixins: [QuestionMixin, QuestionMultipleMixin],
data() {
return {
isDragging: false,
isLoading: false,
isOptionDialogShown: false,
}
},
computed: {
selectOptionPlaceholder() {
if (this.readOnly) {
return this.answerType.submitPlaceholder
}
return this.answerType.createPlaceholder
},
isMultiple() {
// This can be extended if we want to include support for <select multiple>
return false
},
shiftDragHandle() {
return !this.readOnly && this.options.length !== 0 && !this.isLastEmpty
},
selectedOption() {
if (!this.values) {
return null
}
const selected = this.values.map((id) =>
this.options.find((option) => option.id === id),
)
return this.isMultiple ? selected : selected[0]
},
},
methods: {
onInput(option) {
if (Array.isArray(option)) {
this.$emit('update:values', [
...new Set(option.map((opt) => opt.id)),
])
return
}
// Simple select
this.$emit('update:values', option ? [option.id] : [])
},
},
}
</script>
<style lang="scss" scoped>
.question__content {
display: flex;
flex-direction: column;
gap: var(--default-grid-baseline);
}
.question__item {
position: relative;
display: inline-flex;
min-height: var(--default-clickable-area);
.question__input {
width: calc(100% - var(--default-clickable-area));
position: relative;
inset-inline-start: -12px;
margin-inline-end: 32px !important;
}
}
.options-list-transition-move,
.options-list-transition-enter-active,
.options-list-transition-leave-active {
transition: all var(--animation-slow) ease;
}
.options-list-transition-enter-from,
.options-list-transition-leave-to {
opacity: 0;
transform: translateX(44px);
}
/* ensure leaving items are taken out of layout flow so that moving
animations can be calculated correctly. */
.options-list-transition-leave-active {
position: absolute;
}
</style>