|
| 1 | +<template> |
| 2 | + <div class="f-radios-wrap"> |
| 3 | + <ul class="f-radios" v-bind:class="{'f-numbers': question.max}" role="listbox"> |
| 4 | + <li |
| 5 | + v-for="(option, index) in question.options" |
| 6 | + v-on:click.prevent="toggleAnswer(option)" |
| 7 | + v-bind:class="{'f-selected': option.selected}" |
| 8 | + v-bind:key="'m' + index" |
| 9 | + v-bind:aria-label="getLabel(index)" |
| 10 | + role="option" |
| 11 | + > |
| 12 | + <div class="f-label-wrap"> |
| 13 | + <span class="f-key">{{ getToggleKey(index) }}</span> |
| 14 | + <span v-if="option.choiceLabel()" class="f-label">{{ option.choiceLabel() }}</span> |
| 15 | + </div> |
| 16 | + </li> |
| 17 | + </ul> |
| 18 | + </div> |
| 19 | +</template> |
| 20 | + |
| 21 | +<script> |
| 22 | + /* |
| 23 | + Copyright (c) 2020 - present, DITDOT Ltd. - MIT Licence |
| 24 | + https://github.com/ditdot-dev/vue-flow-form |
| 25 | + https://www.ditdot.hr/en |
| 26 | + */ |
| 27 | +
|
| 28 | + import BaseType from './BaseType.vue' |
| 29 | + import { ChoiceOption, QuestionType } from '../../models/QuestionModel' |
| 30 | +
|
| 31 | + export default { |
| 32 | + extends: BaseType, |
| 33 | + name: QuestionType.OpinionScale, |
| 34 | +
|
| 35 | + beforeMount() { |
| 36 | + if (this.question.max && !this.question.options.length) { |
| 37 | + const |
| 38 | + min = this.question.min || 1, |
| 39 | + max = this.question.max |
| 40 | + for (let i = min; i <= max; i++) { |
| 41 | + this.question.options.push(new ChoiceOption({value: i.toString()})) |
| 42 | + } |
| 43 | + } |
| 44 | + }, |
| 45 | +
|
| 46 | + mounted() { |
| 47 | + this.addKeyListener() |
| 48 | + }, |
| 49 | +
|
| 50 | + beforeUnmount() { |
| 51 | + this.removeKeyListener() |
| 52 | + }, |
| 53 | +
|
| 54 | + watch: { |
| 55 | + active(value) { |
| 56 | + if (value) { |
| 57 | + this.addKeyListener() |
| 58 | + } else { |
| 59 | + this.removeKeyListener() |
| 60 | + } |
| 61 | + } |
| 62 | + }, |
| 63 | + |
| 64 | + methods: { |
| 65 | + addKeyListener() { |
| 66 | + this.removeKeyListener() |
| 67 | + document.addEventListener('keyup', this.onKeyListener) |
| 68 | + }, |
| 69 | +
|
| 70 | + removeKeyListener() { |
| 71 | + document.removeEventListener('keyup', this.onKeyListener) |
| 72 | + }, |
| 73 | +
|
| 74 | + /** |
| 75 | + * Listens for keyboard events (A, B, C, ...) |
| 76 | + */ |
| 77 | + onKeyListener($event) { |
| 78 | + if (this.active && $event.key && $event.key.length === 1) { |
| 79 | + let keyCode = $event.key.toUpperCase().charCodeAt(0) |
| 80 | +
|
| 81 | + if (keyCode >= 65 && keyCode <= 90) { |
| 82 | + let index = keyCode - 65 |
| 83 | +
|
| 84 | + if (index > -1) { |
| 85 | + let option = this.question.options[index] |
| 86 | +
|
| 87 | + if (option) { |
| 88 | + this.toggleAnswer(option) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + }, |
| 94 | +
|
| 95 | + getLabel(index) { |
| 96 | + return this.language.ariaMultipleChoice.replace(':letter', this.getToggleKey(index)) |
| 97 | + }, |
| 98 | +
|
| 99 | + getToggleKey(index) { |
| 100 | + const key = 65 + index |
| 101 | +
|
| 102 | + if (key <= 90) { |
| 103 | + return String.fromCharCode(key) |
| 104 | + } |
| 105 | +
|
| 106 | + return '' |
| 107 | + }, |
| 108 | +
|
| 109 | + toggleAnswer(option) { |
| 110 | + for (let i = 0; i < this.question.options.length; i++) { |
| 111 | + let o = this.question.options[i] |
| 112 | +
|
| 113 | + if (o.selected) { |
| 114 | + this._toggleAnswer(o) |
| 115 | + } |
| 116 | + } |
| 117 | +
|
| 118 | + this._toggleAnswer(option) |
| 119 | + }, |
| 120 | +
|
| 121 | + _toggleAnswer(option) { |
| 122 | + const optionValue = option.choiceValue() |
| 123 | + console.log(option, optionValue) |
| 124 | +
|
| 125 | + option.toggle() |
| 126 | +
|
| 127 | + this.dataValue = option.selected ? optionValue : null |
| 128 | + |
| 129 | + if (this.isValid() && this.question.nextStepOnAnswer && !this.disabled) { |
| 130 | + this.$emit('next') |
| 131 | + } |
| 132 | +
|
| 133 | + this.setAnswer(this.dataValue) |
| 134 | + }, |
| 135 | +
|
| 136 | + _removeAnswer(value) { |
| 137 | + const index = this.dataValue.indexOf(value) |
| 138 | +
|
| 139 | + if (index !== -1) { |
| 140 | + this.dataValue.splice(index, 1) |
| 141 | + } |
| 142 | + }, |
| 143 | + }, |
| 144 | +
|
| 145 | + computed: { |
| 146 | + hasValue() { |
| 147 | + if (this.question.options.filter(o => o.selected).length) { |
| 148 | + return true |
| 149 | + } |
| 150 | +
|
| 151 | + return false |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +</script> |
0 commit comments