forms/src/components/Questions/QuestionDropdown.vue
dependabot-preview[bot] e2959be243 Merge
Bumps [sass-loader](https://github.com/webpack-contrib/sass-loader) from 9.0.1 to 9.0.2.
- [Release notes](https://github.com/webpack-contrib/sass-loader/releases)
- [Changelog](https://github.com/webpack-contrib/sass-loader/blob/master/CHANGELOG.md)
- [Commits](https://github.com/webpack-contrib/sass-loader/compare/v9.0.1...v9.0.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Bump @nextcloud/vue from 2.2.0 to 2.2.1

Bumps [@nextcloud/vue](https://github.com/nextcloud/nextcloud-vue) from 2.2.0 to 2.2.1.
- [Release notes](https://github.com/nextcloud/nextcloud-vue/releases)
- [Commits](https://github.com/nextcloud/nextcloud-vue/compare/v2.2.0...v2.2.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Bump vuedraggable from 2.23.2 to 2.24.0

Bumps [vuedraggable](https://github.com/SortableJS/Vue.Draggable) from 2.23.2 to 2.24.0.
- [Release notes](https://github.com/SortableJS/Vue.Draggable/releases)
- [Commits](https://github.com/SortableJS/Vue.Draggable/compare/v2.23.2...v2.24.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Bump @nextcloud/axios from 1.3.2 to 1.3.3

Bumps [@nextcloud/axios](https://github.com/nextcloud/nextcloud-axios) from 1.3.2 to 1.3.3.
- [Release notes](https://github.com/nextcloud/nextcloud-axios/releases)
- [Changelog](https://github.com/nextcloud/nextcloud-axios/blob/master/CHANGELOG.md)
- [Commits](https://github.com/nextcloud/nextcloud-axios/compare/v1.3.2...v1.3.3)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

[tx-robot] updated from transifex

Move to webpack vue global config & clean routes

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>

Lint fix

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>

@nextcloud/webpack-vue-config@1.0.3

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>

[tx-robot] updated from transifex

[tx-robot] updated from transifex

Bump @nextcloud/webpack-vue-config from 1.0.3 to 1.1.0

Bumps [@nextcloud/webpack-vue-config](https://github.com/nextcloud/webpack-vue-config) from 1.0.3 to 1.1.0.
- [Release notes](https://github.com/nextcloud/webpack-vue-config/releases)
- [Commits](https://github.com/nextcloud/webpack-vue-config/compare/v1.0.3...v1.1.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Bump @babel/core from 7.10.4 to 7.10.5

Bumps [@babel/core](https://github.com/babel/babel/tree/HEAD/packages/babel-core) from 7.10.4 to 7.10.5.
- [Release notes](https://github.com/babel/babel/releases)
- [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md)
- [Commits](https://github.com/babel/babel/commits/v7.10.5/packages/babel-core)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

[tx-robot] updated from transifex

Add screenshot of response visualization, adjust readme

Signed-off-by: Jan C. Borchardt <hey@jancborchardt.net>

[tx-robot] updated from transifex

[tx-robot] updated from transifex

Revert "Allow navigation through edit via Tab-Key"

v2.0.0-rc.1

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>

[tx-robot] updated from transifex

Bump webpack from 4.43.0 to 4.44.0

Bumps [webpack](https://github.com/webpack/webpack) from 4.43.0 to 4.44.0.
- [Release notes](https://github.com/webpack/webpack/releases)
- [Commits](https://github.com/webpack/webpack/compare/v4.43.0...v4.44.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

[tx-robot] updated from transifex

Update stylelint config

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>

Fix actions

Signed-off-by: Jonas Rittershofer <jotoeri@users.noreply.github.com>

[tx-robot] updated from transifex

Fix dropdown submission insert

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>

2.0.0

Force enable for master

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>

[tx-robot] updated from transifex

Fix substring utf8

Signed-off-by: Jonas Rittershofer <jotoeri@users.noreply.github.com>

2.0.1

[Security] Bump elliptic from 6.5.0 to 6.5.3

Bumps [elliptic](https://github.com/indutny/elliptic) from 6.5.0 to 6.5.3. **This update includes a security fix.**
- [Release notes](https://github.com/indutny/elliptic/releases)
- [Commits](https://github.com/indutny/elliptic/compare/v6.5.0...v6.5.3)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

[tx-robot] updated from transifex

Increase description length

Signed-off-by: Jonas Rittershofer <jotoeri@users.noreply.github.com>

2.0.2

Bump webpack from 4.44.0 to 4.44.1

Bumps [webpack](https://github.com/webpack/webpack) from 4.44.0 to 4.44.1.
- [Release notes](https://github.com/webpack/webpack/releases)
- [Commits](https://github.com/webpack/webpack/compare/v4.44.0...v4.44.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Bump @babel/core from 7.10.5 to 7.11.0

Bumps [@babel/core](https://github.com/babel/babel/tree/HEAD/packages/babel-core) from 7.10.5 to 7.11.0.
- [Release notes](https://github.com/babel/babel/releases)
- [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md)
- [Commits](https://github.com/babel/babel/commits/v7.11.0/packages/babel-core)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Bump @babel/preset-env from 7.10.4 to 7.11.0

Bumps [@babel/preset-env](https://github.com/babel/babel/tree/HEAD/packages/babel-preset-env) from 7.10.4 to 7.11.0.
- [Release notes](https://github.com/babel/babel/releases)
- [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md)
- [Commits](https://github.com/babel/babel/commits/v7.11.0/packages/babel-preset-env)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

[tx-robot] updated from transifex

[tx-robot] updated from transifex
2020-09-15 16:42:33 +02:00

367 lines
8.5 KiB
Vue

<!--
- @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com>
-
- @author John Molakvoæ <skjnldsv@protonmail.com>
-
- @license GNU AGPL version 3 or any later version
-
- 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"
:mandatory="mandatory"
:edit.sync="edit"
:read-only="readOnly"
:max-question-length="maxStringLengths.questionText"
:title-placeholder="answerType.titlePlaceholder"
:warning-invalid="answerType.warningInvalid"
:content-valid="contentValid"
:shift-drag-handle="shiftDragHandle"
@update:text="onTitleChange"
@update:mandatory="onMandatoryChange"
@delete="onDelete">
<select v-if="!edit"
:id="text"
:name="text"
:multiple="isMultiple"
:required="mandatory"
class="question__content"
@change="onChange">
<option value="">
{{ selectOptionPlaceholder }}
</option>
<option v-for="answer in options"
:key="answer.id"
:value="answer.id"
:selected="isChecked(answer.id)">
{{ answer.text }}
</option>
</select>
<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 { generateUrl } from '@nextcloud/router'
import { showError } from '@nextcloud/dialogs'
import axios from '@nextcloud/axios'
import AnswerInput from './AnswerInput'
import QuestionMixin from '../../mixins/QuestionMixin'
import GenRandomId from '../../utils/GenRandomId'
export default {
name: 'QuestionDropdown',
components: {
AnswerInput,
},
mixins: [QuestionMixin],
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)
}
},
},
methods: {
onChange(event) {
// Get all selected options
const answerIds = [...event.target.options]
.filter(option => option.selected)
.map(option => parseInt(option.value, 10))
// Simple select
if (!this.isMultiple) {
this.$emit('update:values', [answerIds[0]])
return
}
// Emit values and remove duplicates
this.$emit('update:values', [...new Set(answerIds)])
},
/**
* Is the provided answer checked ?
* @param {number} id the answer id
* @returns {boolean}
*/
isChecked(id) {
return this.values.indexOf(id) > -1
},
/**
* Is the provided answer required ?
* This is needed for checkboxes as html5
* doesn't allow to require at least ONE checked.
* So we require the one that are checked or all
* if none are checked yet.
* @param {number} id the answer id
* @returns {boolean}
*/
isRequired(id) {
// false, if question not mandatory
if (!this.mandatory) {
return false
}
// true for simple select
if (!this.isMultiple) {
return true
}
// For checkboxes, only required if no other is checked
return this.areNoneChecked
},
/**
* Update the options
* @param {Array} options options to change
*/
updateOptions(options) {
this.$emit('update:options', options)
},
/**
* 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(),
question_id: 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(generateUrl('/apps/forms/api/v1/option/{id}', { id: option.id }))
.catch(error => {
showError(t('forms', 'There was an issue deleting this option'))
console.error(error)
// 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;
}
// Using type to have a higher order than the input styling of server
.question__input[type=text] {
width: 100%;
// Height 34px + 1px Border
min-height: 35px;
margin: 0;
padding: 0 0;
border: 0;
border-bottom: 1px dotted var(--color-border-dark);
border-radius: 0;
font-size: 14px;
position: relative;
}
// Fix display of select dropdown and adjust to Forms text
select.question__content {
height: 44px;
padding: 12px 0 12px 12px;
font-size: 14px;
}
</style>