-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathQuestionShort.vue
More file actions
265 lines (242 loc) Β· 6.57 KB
/
QuestionShort.vue
File metadata and controls
265 lines (242 loc) Β· 6.57 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
<!--
- 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"
v-on="commonListeners">
<div class="question__content">
<input
ref="input"
:aria-label="
t(
'forms',
'A short answer for the question β{text}β',
{
text,
},
undefined,
{ escape: false },
)
"
:placeholder="submissionInputPlaceholder"
:disabled="!readOnly"
:name="name || undefined"
:required="isRequired"
:value="values[0]"
class="question__input"
dir="auto"
:maxlength="maxStringLengths.answerText"
minlength="1"
:type="validationObject.inputType"
:step="validationObject.inputType === 'number' ? 'any' : undefined"
@input="onInput"
@keydown.enter.exact.prevent="onKeydownEnter" />
<NcActions
v-if="!readOnly"
:id="validationTypeMenuId"
:aria-label="
t('forms', 'Input types (currently: {type})', {
type: validationObject.label,
})
"
:container="`#${validationTypeMenuId}`"
:open.sync="isValidationTypeMenuOpen"
class="validation-type-menu__toggle"
type="tertiary-no-background">
<template #icon>
<component :is="validationObject.icon" :size="20" />
</template>
<NcActionRadio
v-for="(
validationTypeObject, validationTypeName
) in validationTypes"
:key="validationTypeName"
:model-value="validationType"
:name="`${id}_validationMenu`"
:value="validationTypeName"
@update:model-value="onChangeValidationType(validationTypeName)">
{{ validationTypeObject.label }}
</NcActionRadio>
<NcActionInput
v-if="validationType === 'regex'"
ref="regexInput"
:label="t('forms', 'Regular expression for input validation')"
:value="validationRegex"
@input="onInputRegex"
@submit="onSubmitRegex">
<template #icon>
<IconRegex :size="20" />
</template>
/^[a-z]{3}$/i
<!-- ^ Some example RegExp for the placeholder text -->
</NcActionInput>
</NcActions>
</div>
</Question>
</template>
<script>
import { splitRegex, validateExpression } from '../../utils/RegularExpression.js'
import validationTypes from '../../models/ValidationTypes.js'
import QuestionMixin from '../../mixins/QuestionMixin.js'
import IconRegex from 'vue-material-design-icons/Regex.vue'
import NcActions from '@nextcloud/vue/components/NcActions'
import NcActionInput from '@nextcloud/vue/components/NcActionInput'
import NcActionRadio from '@nextcloud/vue/components/NcActionRadio'
export default {
name: 'QuestionShort',
components: {
IconRegex,
NcActions,
NcActionInput,
NcActionRadio,
},
mixins: [QuestionMixin],
data() {
return {
validationTypes,
isValidationTypeMenuOpen: false,
}
},
computed: {
submissionInputPlaceholder() {
if (!this.readOnly) {
return (
this.validationObject.createPlaceholder
|| this.answerType.createPlaceholder
)
}
return (
this.validationObject.submitPlaceholder
|| this.answerType.submitPlaceholder
)
},
/**
* Current user input validation type
*/
validationObject() {
return validationTypes[this.validationType]
},
/**
* Name of the current validation type, fallsback to 'text'
*/
validationType() {
return this.extraSettings?.validationType || 'text'
},
/**
* Id of the validation type menu
*/
validationTypeMenuId() {
return 'q' + this.index + '__validation_menu'
},
/**
* The regular expression
*/
validationRegex() {
return this.extraSettings?.validationRegex || ''
},
},
methods: {
onInput() {
/** @type {HTMLObjectElement} */
const input = this.$refs.input
/** @type {string} */
const value = input.value
input.setCustomValidity('')
// Only check non empty values, this question might not be required, if not already invalid
if (value) {
// Then check native browser validation (might be better then our)
// If the browsers validation succeeds either the browser does not implement a validation
// or it is valid, so we double check by running our custom validation.
if (
!input.checkValidity()
|| !this.validationObject.validate(
value,
splitRegex(this.validationRegex),
)
) {
input.setCustomValidity(this.validationObject.errorMessage)
}
}
this.$emit('update:values', [value])
},
/**
* Change input type
*
* @param {string} validationType new input type
*/
onChangeValidationType(validationType) {
if (validationType === 'regex') {
// Make sure to also submit a regex (even if empty)
this.onExtraSettingsChange({
validationType,
validationRegex: this.validationRegex,
})
} else {
// For all other types except regex we close the menu (for regex we keep it open to allow entering a regex)
this.isValidationTypeMenuOpen = false
this.onExtraSettingsChange({
validationType:
validationType === 'text' ? undefined : validationType,
})
}
},
/**
* Validate and save regex if valid
*
* Ensures the regex is enclosed with delimters, as required for PCRE,
* and regex is only using modifiers supported by JS *and* PHP
*
* @param {InputEvent|SubmitEvent} event input event
* @return {boolean} true if the regex is valid
*/
onInputRegex(event) {
if (event?.isComposing) {
return false
}
const input = this.$refs.regexInput.$el.querySelector('input')
const validationRegex = input.value
// remove potential previous validity
input.setCustomValidity('')
if (!validateExpression(validationRegex)) {
input.setCustomValidity(t('forms', 'Invalid regular expression'))
return false
}
this.onExtraSettingsChange({ validationRegex })
return true
},
/**
* Same as `onInputRegex` but for convinience also closes the menu
*
* @param {SubmitEvent} event regex submit event
*/
onSubmitRegex(event) {
if (this.onInputRegex(event)) {
this.isValidationTypeMenuOpen = false
}
},
},
}
</script>
<style lang="scss" scoped>
.question__input {
width: 100%;
min-height: var(--default-clickable-area);
&:disabled {
width: calc(100% - var(--default-clickable-area)) !important;
margin-inline-start: -12px;
}
}
.validation-type-menu__toggle {
position: relative;
inset-inline-end: calc(4px + var(--default-clickable-area));
inset-block-start: 4px;
}
:deep(input:invalid) {
// nextcloud/server#36548
border-color: var(--color-error) !important;
}
</style>