-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathQuestionMixin.js
More file actions
404 lines (367 loc) Β· 8.02 KB
/
Copy pathQuestionMixin.js
File metadata and controls
404 lines (367 loc) Β· 8.02 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
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
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { showError } from '@nextcloud/dialogs'
import { emit } from '@nextcloud/event-bus'
import { generateOcsUrl } from '@nextcloud/router'
import axios from '@nextcloud/axios'
import debounce from 'debounce'
import { INPUT_DEBOUNCE_MS } from '../models/Constants.ts'
import logger from '../utils/Logger.js'
import OcsResponse2Data from '../utils/OcsResponse2Data.js'
import Question from '../components/Questions/Question.vue'
export default {
inheritAttrs: false,
props: {
/**
* Question-Id
*/
id: {
type: Number,
required: true,
},
/**
* ID of the form
*/
formId: {
type: Number,
default: null,
},
/**
* The question title
*/
text: {
type: String,
required: true,
},
/**
* Question Description
*/
description: {
type: String,
required: true,
},
/**
* Required-Setting
*/
isRequired: {
type: Boolean,
required: true,
},
/**
* The index of the question
*/
index: {
type: Number,
required: true,
},
/**
* Technical name
*/
name: {
type: String,
default: '',
},
/**
* The user answers
*/
values: {
type: Array,
default() {
return []
},
},
/**
* The question list of answers
*/
options: {
type: Array,
required: true,
},
/**
* Order of the question
*/
order: {
type: Number,
default: -1,
},
/**
* Question type
*/
type: {
type: String,
default: null,
},
/**
* Answer type model object
*/
answerType: {
type: Object,
required: true,
},
/**
* Submission or Edit-Mode
*/
readOnly: {
type: Boolean,
default: false,
},
/**
* Database-Restrictions
*/
maxStringLengths: {
type: Object,
required: true,
},
/**
* Extra settings
*/
extraSettings: {
default: () => {
return {}
},
},
/**
* Mime-Types and file extensions that are allowed to be uploaded
*/
accept: {
type: Array,
default() {
return []
},
},
/**
* Can question be moved up in order?
*/
canMoveUp: {
type: Boolean,
default: false,
},
/**
* Can question be moved down in order?
*/
canMoveDown: {
type: Boolean,
default: false,
},
},
components: {
Question,
},
computed: {
questionProps() {
const props = { ...this.$props }
const allowedKeys = Object.keys(Question.props)
Object.keys(props).forEach((key) => {
if (!allowedKeys.includes(key)) {
delete props[key]
}
})
return props
},
/**
* Listeners for all questions to forward
*/
commonListeners() {
return {
clone: this.onClone,
delete: this.onDelete,
'update:text': this.onTitleChange,
'update:description': this.onDescriptionChange,
'update:isRequired': this.onRequiredChange,
'update:name': this.onNameChange,
'move-down': (...args) => this.$emit('move-down', ...args),
'move-up': (...args) => this.$emit('move-up', ...args),
}
},
},
methods: {
/**
* Override to allow custom validation
*/
async validate() {
return true
},
/**
* Forward the title change to the parent and store to db
*
* @param {string} text the title
*/
onTitleChange: debounce(function (text) {
this.$emit('update:text', text)
this.saveQuestionProperty('text', text)
}, INPUT_DEBOUNCE_MS),
/**
* Forward the description change to the parent and store to db
*
* @param {string} description the description
*/
onDescriptionChange: debounce(function (description) {
this.$emit('update:description', description)
this.saveQuestionProperty('description', description)
}, INPUT_DEBOUNCE_MS),
/**
* Forward the required change to the parent and store to db
*
* @param {boolean} isRequiredValue new isRequired Value
*/
onRequiredChange: debounce(function (isRequiredValue) {
this.$emit('update:isRequired', isRequiredValue)
this.saveQuestionProperty('isRequired', isRequiredValue)
}, INPUT_DEBOUNCE_MS),
/**
* Create mapper to forward the required change to the parent and store to db
*
* Either an object containing the *changed* settings.
*
* @param {object} newSettings changed settings
*/
onExtraSettingsChange: debounce(function (newSettings) {
const newExtraSettings = { ...this.extraSettings, ...newSettings }
this.$emit('update:extraSettings', newExtraSettings)
this.saveQuestionProperty('extraSettings', newExtraSettings)
}, INPUT_DEBOUNCE_MS),
/**
* Forward the technical-name change to the parent and store to db
*
* @param {string} name The new technical name of the input
*/
onNameChange: debounce(function (name) {
this.$emit('update:name', name)
this.saveQuestionProperty('name', name)
}, INPUT_DEBOUNCE_MS),
/**
* Forward the required change to the parent and store to db
*
* @param {boolean} shuffle Should options be shuffled
*/
onShuffleOptionsChange(shuffle) {
return this.onExtraSettingsChange({ shuffleOptions: shuffle })
},
/**
* Forward the answer(s) change to the parent
*
* @param {Array} values the array of answers
*/
onValuesChange(values) {
this.$emit('update:values', values)
},
/**
* Delete this question
*/
onDelete() {
this.$emit('delete')
},
/**
* Clone this question.
*/
onClone() {
this.$emit('clone')
},
/**
* Don't automatically submit form on Enter, parent will handle that
* To be called with prevent: @keydown.enter.prevent="onKeydownEnter"
*
* @param {object} event The fired event
*/
onKeydownEnter(event) {
this.$emit('keydown', event)
},
/**
* Focus the first focusable element
*/
focus() {
this.$el.scrollIntoView({ behavior: 'smooth' })
this.$nextTick(() => {
const title = this.$el.querySelector(
'.question__header__title__text__input',
)
if (title) {
title.focus()
}
})
},
/**
* Shuffle an array using Fisher-Yates
*
* @param {Array} input Input array to shuffle
* @return {Array} Shuffled input array
*/
shuffleArray(input) {
const shuffled = [...input]
let idx = shuffled.length
while (--idx > 0) {
const rndIdx = Math.floor(Math.random() * (idx + 1))
;[shuffled[rndIdx], shuffled[idx]] = [
shuffled[idx],
shuffled[rndIdx],
]
}
return shuffled
},
async saveQuestionProperty(key, value) {
try {
// TODO: add loading status feedback ?
await axios.patch(
generateOcsUrl(
'apps/forms/api/v3/forms/{id}/questions/{questionId}',
{
id: this.formId,
questionId: this.id,
},
),
{
keyValuePairs: {
[key]: value,
},
},
)
emit('forms:last-updated:set', this.formId)
} catch (error) {
logger.error('Error while saving question', { error })
showError(t('forms', 'Error while saving question'))
}
},
/**
* Handles multiple options for a question.
*
* @param {Array<string>} answers - The array of answers for the question.
*/
async handleMultipleOptions(answers) {
this.isLoading = true
try {
const response = await axios.post(
generateOcsUrl(
'apps/forms/api/v3/forms/{id}/questions/{questionId}/options',
{
id: this.formId,
questionId: this.id,
},
),
{
optionTexts: answers, // Send the entire array of answers at once
},
)
const newServerOptions = OcsResponse2Data(response) // Assuming this function can handle arrays
const options = this.options.slice()
newServerOptions.forEach((option) => {
options.push({
id: option.id, // Use the ID from the server
questionId: this.id,
text: option.text,
local: false,
})
})
this.updateOptions(options)
this.$nextTick(() => {
this.focusIndex(options.length - 1)
})
} catch (error) {
logger.error('Error while saving question options', { error })
showError(t('forms', 'Error while saving question options'))
}
this.isLoading = false
},
},
}