|
| 1 | +<!-- |
| 2 | + - SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors |
| 3 | + - SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | +--> |
| 5 | + |
| 6 | +<template> |
| 7 | + <Question |
| 8 | + v-bind="questionProps" |
| 9 | + :title-placeholder="answerType.titlePlaceholder" |
| 10 | + :warning-invalid="answerType.warningInvalid" |
| 11 | + v-on="commonListeners"> |
| 12 | + <template #actions> |
| 13 | + <NcActionInput |
| 14 | + v-model="lowestOption" |
| 15 | + type="multiselect" |
| 16 | + :clearable="false" |
| 17 | + :label="t('forms', 'Lowest value')" |
| 18 | + label-outside |
| 19 | + :options="[0, 1]" |
| 20 | + required> |
| 21 | + <template #icon> |
| 22 | + <IconPencil :size="20" /> |
| 23 | + </template> |
| 24 | + </NcActionInput> |
| 25 | + <NcActionInput |
| 26 | + v-model="highestOption" |
| 27 | + type="multiselect" |
| 28 | + :clearable="false" |
| 29 | + :label="t('forms', 'Highest value')" |
| 30 | + label-outside |
| 31 | + :options="[2, 3, 4, 5, 6, 7, 8, 9, 10]" |
| 32 | + required> |
| 33 | + <template #icon> |
| 34 | + <IconPencil :size="20" /> |
| 35 | + </template> |
| 36 | + </NcActionInput> |
| 37 | + <NcActionInput |
| 38 | + v-model="labelLowest" |
| 39 | + :label="t('forms', 'Label for lowest value')" |
| 40 | + label-outside |
| 41 | + :show-trailing-button="false"> |
| 42 | + <template #icon> |
| 43 | + <IconPencil :size="20" /> |
| 44 | + </template> |
| 45 | + {{ t('forms', 'Label for lowest value') }} |
| 46 | + </NcActionInput> |
| 47 | + <NcActionInput |
| 48 | + v-model="labelHighest" |
| 49 | + :label="t('forms', 'Label for highest value')" |
| 50 | + label-outside |
| 51 | + :show-trailing-button="false"> |
| 52 | + <template #icon> |
| 53 | + <IconPencil :size="20" /> |
| 54 | + </template> |
| 55 | + {{ t('forms', 'Label for highest value') }} |
| 56 | + </NcActionInput> |
| 57 | + </template> |
| 58 | + |
| 59 | + <div class="question__content"> |
| 60 | + <template v-if="labelLowest !== ''">{{ labelLowest }}</template> |
| 61 | + <NcCheckboxRadioSwitch |
| 62 | + v-for="option in scaleOptions" |
| 63 | + :key="option" |
| 64 | + class="question__content" |
| 65 | + :disabled="!readOnly" |
| 66 | + :checked="questionValues" |
| 67 | + :value="option.toString()" |
| 68 | + :name="`${id}-answer`" |
| 69 | + button-variant |
| 70 | + button-variant-grouped="horizontal" |
| 71 | + type="radio" |
| 72 | + :required="checkRequired(option)" |
| 73 | + @update:checked="onChange" |
| 74 | + @keydown.enter.exact.prevent="onKeydownEnter"> |
| 75 | + {{ option }} |
| 76 | + </NcCheckboxRadioSwitch> |
| 77 | + <template v-if="labelHighest !== ''">{{ labelHighest }}</template> |
| 78 | + </div> |
| 79 | + </Question> |
| 80 | +</template> |
| 81 | + |
| 82 | +<script> |
| 83 | +import NcActionInput from '@nextcloud/vue/components/NcActionInput' |
| 84 | +import NcCheckboxRadioSwitch from '@nextcloud/vue/components/NcCheckboxRadioSwitch' |
| 85 | +
|
| 86 | +import QuestionMixin from '../../mixins/QuestionMixin.js' |
| 87 | +
|
| 88 | +import IconPencil from 'vue-material-design-icons/Pencil.vue' |
| 89 | +
|
| 90 | +export default { |
| 91 | + name: 'QuestionLinearScale', |
| 92 | +
|
| 93 | + components: { |
| 94 | + IconPencil, |
| 95 | + NcActionInput, |
| 96 | + NcCheckboxRadioSwitch, |
| 97 | + }, |
| 98 | +
|
| 99 | + mixins: [QuestionMixin], |
| 100 | +
|
| 101 | + data() { |
| 102 | + return { |
| 103 | + isLoading: false, |
| 104 | +
|
| 105 | + lowestOption: 1, |
| 106 | + highestOption: 5, |
| 107 | + labelLowest: 'Lowest', |
| 108 | + labelHighest: 'Highest', |
| 109 | + } |
| 110 | + }, |
| 111 | +
|
| 112 | + computed: { |
| 113 | + scaleOptions() { |
| 114 | + return Array.from( |
| 115 | + { length: this.highestOption - this.lowestOption + 1 }, |
| 116 | + (_, i) => i + this.lowestOption, |
| 117 | + ) |
| 118 | + }, |
| 119 | +
|
| 120 | + isUnique() { |
| 121 | + return this.answerType.unique === true |
| 122 | + }, |
| 123 | +
|
| 124 | + questionValues() { |
| 125 | + return this.isUnique ? this.values?.[0] : this.values |
| 126 | + }, |
| 127 | + }, |
| 128 | +
|
| 129 | + methods: { |
| 130 | + onChange(value) { |
| 131 | + this.$emit('update:values', this.isUnique ? [value].flat() : value) |
| 132 | + }, |
| 133 | +
|
| 134 | + /** |
| 135 | + * Is the provided answer required ? |
| 136 | + * This is needed for checkboxes as html5 |
| 137 | + * doesn't allow to require at least ONE checked. |
| 138 | + * So we require the one that are checked or all |
| 139 | + * if none are checked yet. |
| 140 | + * |
| 141 | + * @return {boolean} |
| 142 | + */ |
| 143 | + checkRequired() { |
| 144 | + // false, if question not required |
| 145 | + if (!this.isRequired) { |
| 146 | + return false |
| 147 | + } |
| 148 | +
|
| 149 | + // true for Radiobuttons |
| 150 | + if (this.isUnique) { |
| 151 | + return true |
| 152 | + } |
| 153 | +
|
| 154 | + // For checkboxes, only required if no other is checked |
| 155 | + return false |
| 156 | + }, |
| 157 | + }, |
| 158 | +} |
| 159 | +</script> |
| 160 | +
|
| 161 | +<style lang="scss" scoped> |
| 162 | +.question__content { |
| 163 | + all: unset; |
| 164 | + display: flex; |
| 165 | + flex-direction: row; |
| 166 | + justify-content: center; |
| 167 | + width: 100%; |
| 168 | + align-items: center; |
| 169 | +} |
| 170 | +</style> |
0 commit comments