/
QuestionDropdown.vue
335 lines (293 loc) Β· 8.07 KB
/
QuestionDropdown.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
<!--
- @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com>
-
- @author John Molakvoæ <skjnldsv@protonmail.com>
-
- @license AGPL-3.0-or-later
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-->
<template>
<Question v-bind.sync="$attrs"
:text="text"
:name="name"
:description="description"
:is-required="isRequired"
:edit.sync="edit"
:read-only="readOnly"
:max-string-lengths="maxStringLengths"
:title-placeholder="answerType.titlePlaceholder"
:warning-invalid="answerType.warningInvalid"
:content-valid="contentValid"
:shift-drag-handle="shiftDragHandle"
@update:text="onTitleChange"
@update:description="onDescriptionChange"
@update:isRequired="onRequiredChange"
@update:name="onNameChange"
@delete="onDelete">
<template #actions>
<NcActionCheckbox :checked="extraSettings?.shuffleOptions"
@update:checked="onShuffleOptionsChange">
{{ t('forms', 'Shuffle options') }}
</NcActionCheckbox>
</template>
<NcSelect v-if="!edit"
v-model="selectedOption"
:name="name || undefined"
:placeholder="selectOptionPlaceholder"
:multiple="isMultiple"
:required="isRequired"
:options="sortedOptions"
:searchable="false"
label="text"
@input="onInput" />
<ol v-if="edit" class="question__content">
<!-- Answer text input edit -->
<AnswerInput v-for="(answer, index) in options"
:key="index /* using index to keep the same vnode after new answer creation */"
ref="input"
:answer="answer"
:index="index"
:is-unique="!isMultiple"
:is-dropdown="true"
:max-option-length="maxStringLengths.optionText"
@add="addNewEntry"
@delete="deleteOption"
@update:answer="updateAnswer" />
<li v-if="!isLastEmpty || hasNoAnswer" class="question__item">
<input :aria-label="t('forms', 'Add a new answer')"
:placeholder="t('forms', 'Add a new answer')"
class="question__input"
:maxlength="maxStringLengths.optionText"
minlength="1"
type="text"
@click="addNewEntry"
@focus="addNewEntry">
</li>
</ol>
</Question>
</template>
<script>
import { showError } from '@nextcloud/dialogs'
import { emit } from '@nextcloud/event-bus'
import { generateOcsUrl } from '@nextcloud/router'
import axios from '@nextcloud/axios'
import NcActionCheckbox from '@nextcloud/vue/dist/Components/NcActionCheckbox.js'
import NcSelect from '@nextcloud/vue/dist/Components/NcSelect.js'
import AnswerInput from './AnswerInput.vue'
import QuestionMixin from '../../mixins/QuestionMixin.js'
import GenRandomId from '../../utils/GenRandomId.js'
import logger from '../../utils/Logger.js'
export default {
name: 'QuestionDropdown',
components: {
AnswerInput,
NcActionCheckbox,
NcSelect,
},
mixins: [QuestionMixin],
data() {
return {
selectedOption: null,
}
},
computed: {
selectOptionPlaceholder() {
if (this.readOnly) {
return this.answerType.submitPlaceholder
}
return this.answerType.createPlaceholder
},
contentValid() {
return this.answerType.validate(this)
},
isLastEmpty() {
const value = this.options[this.options.length - 1]
return value?.text?.trim().length === 0
},
isMultiple() {
// This can be extended if we want to include support for <select multiple>
return false
},
hasNoAnswer() {
return this.options.length === 0
},
areNoneChecked() {
return this.values.length === 0
},
shiftDragHandle() {
return this.edit && this.options.length !== 0 && !this.isLastEmpty
},
},
watch: {
edit(edit) {
// When leaving edit mode, filter and delete empty options
if (!edit) {
const options = this.options.filter(option => {
if (!option.text) {
this.deleteOptionFromDatabase(option)
return false
}
return true
})
// update parent
this.updateOptions(options)
}
},
},
mounted() {
// Init selected options from values prop
if (this.values) {
const selected = this.values.map(id => this.options.find(option => option.id === id))
this.selectedOption = 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] : [])
},
/**
* Update the options
*
* @param {Array} options options to change
*/
updateOptions(options) {
this.$emit('update:options', options)
emit('forms:last-updated:set', this.$attrs.formId)
},
/**
* Update an existing answer locally
*
* @param {string|number} id the answer id
* @param {object} answer the answer to update
*/
updateAnswer(id, answer) {
const options = this.options.slice()
const answerIndex = options.findIndex(option => option.id === id)
options[answerIndex] = answer
this.updateOptions(options)
},
/**
* Add a new empty answer locally
*/
addNewEntry() {
// If entering from non-edit-mode (possible by click), activate edit-mode
this.edit = true
// Add local entry
const options = this.options.slice()
options.push({
id: GenRandomId(),
questionId: this.id,
text: '',
local: true,
})
// Update question
this.updateOptions(options)
this.$nextTick(() => {
this.focusIndex(options.length - 1)
})
},
/**
* Restore an option locally
*
* @param {object} option the option
* @param {number} index the options index in this.options
*/
restoreOption(option, index) {
const options = this.options.slice()
options.splice(index, 0, option)
this.updateOptions(options)
this.focusIndex(index)
},
/**
* Delete an option
*
* @param {number} id the options id
*/
deleteOption(id) {
const options = this.options.slice()
const optionIndex = options.findIndex(option => option.id === id)
if (options.length === 1) {
// Clear Text, but don't remove. Will be removed, when leaving edit-mode
options[0].text = ''
} else {
// Remove entry
const option = Object.assign({}, this.options[optionIndex])
// delete locally
options.splice(optionIndex, 1)
// delete from Db
this.deleteOptionFromDatabase(option)
}
// Update question
this.updateOptions(options)
this.$nextTick(() => {
this.focusIndex(optionIndex - 1)
})
},
/**
* Delete the option from Db in background.
* Restore option if delete not possible
*
* @param {object} option The option to delete
*/
deleteOptionFromDatabase(option) {
const optionIndex = this.options.findIndex(opt => opt.id === option.id)
if (!option.local) {
// let's not await, deleting in background
axios.delete(generateOcsUrl('apps/forms/api/v2.1/option/{id}', { id: option.id }))
.catch(error => {
logger.error('Error while deleting an option', { option, error })
showError(t('forms', 'There was an issue deleting this option'))
// restore option
this.restoreOption(option, optionIndex)
})
}
},
/**
* Focus the input matching the index
*
* @param {number} index the value index
*/
focusIndex(index) {
const inputs = this.$refs.input
if (inputs && inputs[index]) {
const input = inputs[index]
input.focus()
}
},
},
}
</script>
<style lang="scss" scoped>
.question__content {
display: flex;
flex-direction: column;
}
.question__item {
position: relative;
display: inline-flex;
min-height: 44px;
.question__input {
width: 100%;
position: relative;
margin-right: 46px !important;
}
}
</style>