2020-03-20 12:22:07 +01:00
|
|
|
<!--
|
|
|
|
- @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>
|
2020-04-28 10:50:44 +02:00
|
|
|
<Content app-name="forms">
|
|
|
|
<AppContent>
|
|
|
|
<!-- Forms title & description-->
|
|
|
|
<header>
|
2020-05-24 19:14:34 +02:00
|
|
|
<h2 class="form-title">
|
2020-05-09 15:56:01 +02:00
|
|
|
{{ formTitle }}
|
2020-04-29 03:41:02 +02:00
|
|
|
</h2>
|
2020-05-25 18:08:34 +02:00
|
|
|
<!-- Do not wrap the following line between tags! `white-space:pre-line` respects `\n` but would produce additional empty first line -->
|
|
|
|
<!-- eslint-disable-next-line -->
|
|
|
|
<p v-if="!loading && !success" class="form-desc">{{ form.description }}</p>
|
2020-04-30 10:58:04 +02:00
|
|
|
<!-- Only visible if at least one question is marked as mandatory-->
|
2020-05-25 18:08:34 +02:00
|
|
|
<p v-if="mandatoryUsed && !loading && !success" class="info-mandatory">
|
2020-06-16 15:08:50 +02:00
|
|
|
* {{ t('forms', 'Required questions') }}
|
2020-04-30 10:58:04 +02:00
|
|
|
</p>
|
2020-04-28 10:50:44 +02:00
|
|
|
</header>
|
|
|
|
|
|
|
|
<!-- Questions list -->
|
2020-05-26 14:37:22 +02:00
|
|
|
<form v-if="!loading && !success"
|
|
|
|
ref="form"
|
|
|
|
@submit.prevent="onSubmit">
|
2020-04-28 10:50:44 +02:00
|
|
|
<ul>
|
|
|
|
<Questions
|
|
|
|
:is="answerTypes[question.type].component"
|
|
|
|
v-for="(question, index) in validQuestions"
|
|
|
|
ref="questions"
|
|
|
|
:key="question.id"
|
|
|
|
:read-only="true"
|
2020-05-14 20:56:22 +02:00
|
|
|
:answer-type="answerTypes[question.type]"
|
2020-04-28 10:50:44 +02:00
|
|
|
:index="index + 1"
|
2020-05-03 13:07:29 +02:00
|
|
|
:max-string-lengths="maxStringLengths"
|
2020-04-28 15:36:12 +02:00
|
|
|
v-bind="question"
|
2020-05-26 14:37:22 +02:00
|
|
|
:values.sync="answers[question.id]"
|
|
|
|
@keydown.enter="onKeydownEnter"
|
|
|
|
@keydown.ctrl.enter="onKeydownCtrlEnter" />
|
2020-04-28 10:50:44 +02:00
|
|
|
</ul>
|
2020-05-26 14:37:22 +02:00
|
|
|
<input ref="submitButton"
|
|
|
|
class="primary"
|
2020-04-28 15:36:12 +02:00
|
|
|
type="submit"
|
|
|
|
:value="t('forms', 'Submit')"
|
|
|
|
:disabled="loading"
|
|
|
|
:aria-label="t('forms', 'Submit form')">
|
2020-04-28 10:50:44 +02:00
|
|
|
</form>
|
2020-04-28 17:17:10 +02:00
|
|
|
|
|
|
|
<EmptyContent v-else-if="loading" icon="icon-loading">
|
|
|
|
{{ t('forms', 'Submitting form …') }}
|
|
|
|
</EmptyContent>
|
|
|
|
|
|
|
|
<EmptyContent v-else-if="success" icon="icon-checkmark">
|
2020-04-29 03:41:02 +02:00
|
|
|
{{ t('forms', 'Thank you for completing the form!') }}
|
2020-04-28 17:17:10 +02:00
|
|
|
</EmptyContent>
|
2020-04-28 10:50:44 +02:00
|
|
|
</AppContent>
|
|
|
|
</Content>
|
2020-03-20 12:22:07 +01:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2020-04-28 10:50:44 +02:00
|
|
|
import { loadState } from '@nextcloud/initial-state'
|
2020-04-28 15:36:12 +02:00
|
|
|
import { generateUrl } from '@nextcloud/router'
|
|
|
|
import { showError } from '@nextcloud/dialogs'
|
|
|
|
import axios from '@nextcloud/axios'
|
2020-04-28 10:50:44 +02:00
|
|
|
import AppContent from '@nextcloud/vue/dist/Components/AppContent'
|
|
|
|
import Content from '@nextcloud/vue/dist/Components/Content'
|
|
|
|
|
2020-04-28 15:36:12 +02:00
|
|
|
import answerTypes from '../models/AnswerTypes'
|
|
|
|
|
2020-04-28 17:17:10 +02:00
|
|
|
import EmptyContent from '../components/EmptyContent'
|
2020-04-28 10:50:44 +02:00
|
|
|
import Question from '../components/Questions/Question'
|
|
|
|
import QuestionLong from '../components/Questions/QuestionLong'
|
|
|
|
import QuestionShort from '../components/Questions/QuestionShort'
|
|
|
|
import QuestionMultiple from '../components/Questions/QuestionMultiple'
|
2020-05-10 12:44:56 +02:00
|
|
|
import SetWindowTitle from '../utils/SetWindowTitle'
|
2020-04-28 10:50:44 +02:00
|
|
|
|
2020-03-20 12:22:07 +01:00
|
|
|
export default {
|
2020-04-28 09:04:53 +02:00
|
|
|
name: 'Submit',
|
2020-04-28 10:50:44 +02:00
|
|
|
|
|
|
|
components: {
|
|
|
|
AppContent,
|
|
|
|
Content,
|
2020-04-28 17:17:10 +02:00
|
|
|
EmptyContent,
|
2020-04-28 10:50:44 +02:00
|
|
|
Question,
|
|
|
|
QuestionLong,
|
|
|
|
QuestionShort,
|
|
|
|
QuestionMultiple,
|
|
|
|
},
|
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
form: loadState('forms', 'form'),
|
2020-05-03 13:07:29 +02:00
|
|
|
maxStringLengths: loadState('forms', 'maxStringLengths'),
|
2020-04-28 10:50:44 +02:00
|
|
|
answerTypes,
|
2020-04-28 15:36:12 +02:00
|
|
|
answers: {},
|
|
|
|
loading: false,
|
|
|
|
success: false,
|
2020-04-28 10:50:44 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
2020-05-09 15:56:01 +02:00
|
|
|
/**
|
|
|
|
* Return form title, or placeholder if not set
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
formTitle() {
|
|
|
|
if (this.form.title) {
|
|
|
|
return this.form.title
|
|
|
|
}
|
2020-05-09 18:42:29 +02:00
|
|
|
return t('forms', 'New form')
|
2020-05-09 15:56:01 +02:00
|
|
|
},
|
|
|
|
|
2020-04-28 10:50:44 +02:00
|
|
|
validQuestions() {
|
|
|
|
return this.form.questions.filter(question => {
|
|
|
|
// All questions must have a valid title
|
2020-05-17 14:28:39 +02:00
|
|
|
if (question.text?.trim() === '') {
|
2020-04-28 10:50:44 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// If specific conditions provided, test against them
|
|
|
|
if ('validate' in answerTypes[question.type]) {
|
|
|
|
return answerTypes[question.type].validate(question)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
},
|
2020-04-30 10:58:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if at least one question is mandatory
|
|
|
|
* @returns {Boolean}
|
|
|
|
*/
|
|
|
|
mandatoryUsed() {
|
|
|
|
return this.form.questions.reduce(
|
|
|
|
(isUsed, question) => isUsed || question.mandatory
|
|
|
|
, false)
|
|
|
|
},
|
2020-04-28 10:50:44 +02:00
|
|
|
},
|
|
|
|
|
2020-05-09 13:05:18 +02:00
|
|
|
beforeMount() {
|
2020-05-10 12:44:56 +02:00
|
|
|
SetWindowTitle(this.formTitle)
|
2020-05-09 13:05:18 +02:00
|
|
|
},
|
|
|
|
|
2020-04-28 10:50:44 +02:00
|
|
|
methods: {
|
2020-05-26 14:37:22 +02:00
|
|
|
/**
|
|
|
|
* On Enter, focus next form-element
|
|
|
|
* Last form element is the submit button, the form submits on enter then
|
|
|
|
* @param {Object} event The fired event.
|
|
|
|
*/
|
|
|
|
onKeydownEnter(event) {
|
|
|
|
const formInputs = Array.from(this.$refs.form)
|
|
|
|
const sourceInputIndex = formInputs.findIndex(input => input === event.originalTarget)
|
2020-07-28 08:18:59 +02:00
|
|
|
|
2020-05-26 14:37:22 +02:00
|
|
|
// Focus next form element
|
|
|
|
formInputs[sourceInputIndex + 1].focus()
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ctrl+Enter typically fires submit on forms.
|
|
|
|
* Some inputs do automatically, while some need explicit handling
|
|
|
|
*/
|
|
|
|
onKeydownCtrlEnter() {
|
|
|
|
// Using button-click event to not bypass validity-checks and use our specified behaviour
|
|
|
|
this.$refs.submitButton.click()
|
|
|
|
},
|
|
|
|
|
2020-04-28 15:36:12 +02:00
|
|
|
/**
|
|
|
|
* Submit the form after the browser validated it 🚀
|
|
|
|
*/
|
|
|
|
async onSubmit() {
|
|
|
|
this.loading = true
|
|
|
|
|
|
|
|
try {
|
2020-04-30 12:02:23 +02:00
|
|
|
await axios.post(generateUrl('/apps/forms/api/v1/submission/insert'), {
|
2020-04-28 15:36:12 +02:00
|
|
|
formId: this.form.id,
|
|
|
|
answers: this.answers,
|
|
|
|
})
|
|
|
|
this.success = true
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
showError(t('forms', 'There was an error submitting the form'))
|
|
|
|
} finally {
|
|
|
|
this.loading = false
|
|
|
|
}
|
2020-04-28 10:50:44 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2020-03-20 12:22:07 +01:00
|
|
|
}
|
|
|
|
</script>
|
2020-04-28 10:50:44 +02:00
|
|
|
<style lang="scss" scoped>
|
2020-05-24 19:55:38 +02:00
|
|
|
.app-content {
|
2020-04-28 10:50:44 +02:00
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
flex-direction: column;
|
|
|
|
|
2020-04-29 03:41:02 +02:00
|
|
|
// Force hide navigation toggle as there is no navigation
|
2020-04-29 12:04:11 +02:00
|
|
|
// stylelint-disable-next-line selector-pseudo-element-no-unknown
|
2020-05-24 19:55:38 +02:00
|
|
|
::v-deep .app-navigation-toggle {
|
2020-04-29 03:41:02 +02:00
|
|
|
display: none !important;
|
|
|
|
}
|
|
|
|
|
2020-04-28 10:50:44 +02:00
|
|
|
header,
|
|
|
|
form {
|
|
|
|
width: 100%;
|
|
|
|
max-width: 750px;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Title & description header
|
|
|
|
header {
|
2020-04-29 03:41:02 +02:00
|
|
|
margin-top: 44px;
|
2020-04-30 10:58:04 +02:00
|
|
|
margin-bottom: 24px;
|
2020-04-28 10:50:44 +02:00
|
|
|
|
2020-05-24 19:14:34 +02:00
|
|
|
.form-title,
|
2020-05-25 18:08:34 +02:00
|
|
|
.form-desc,
|
|
|
|
.info-mandatory {
|
2020-04-28 10:50:44 +02:00
|
|
|
width: 100%;
|
|
|
|
padding: 0 16px;
|
|
|
|
border: none;
|
|
|
|
}
|
2020-05-24 19:14:34 +02:00
|
|
|
.form-title {
|
2020-05-24 21:06:01 +02:00
|
|
|
font-size: 28px;
|
2020-04-28 10:50:44 +02:00
|
|
|
font-weight: bold;
|
2020-05-24 19:14:34 +02:00
|
|
|
color: var(--color-main-text);
|
|
|
|
min-height: 36px;
|
2020-04-29 03:41:02 +02:00
|
|
|
margin: 32px 0;
|
2020-04-28 10:50:44 +02:00
|
|
|
padding-left: 14px; // align with description (compensate font size diff)
|
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
white-space: nowrap;
|
|
|
|
}
|
2020-04-30 10:58:04 +02:00
|
|
|
.form-desc {
|
2020-05-24 19:14:34 +02:00
|
|
|
font-size: 100%;
|
2020-05-26 23:04:49 +02:00
|
|
|
line-height: 150%;
|
2020-04-30 10:58:04 +02:00
|
|
|
padding-bottom: 20px;
|
2020-04-28 10:50:44 +02:00
|
|
|
resize: none;
|
2020-05-25 18:08:34 +02:00
|
|
|
white-space: pre-line;
|
2020-04-28 10:50:44 +02:00
|
|
|
}
|
2020-05-05 23:37:30 +02:00
|
|
|
|
|
|
|
.info-mandatory {
|
2020-05-25 18:08:34 +02:00
|
|
|
font-size: 100%;
|
|
|
|
padding-bottom: 20px;
|
|
|
|
resize: none;
|
2020-05-05 23:37:30 +02:00
|
|
|
color: var(--color-text-maxcontrast);
|
|
|
|
}
|
2020-04-28 10:50:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
form {
|
2020-04-29 03:41:02 +02:00
|
|
|
.question {
|
|
|
|
// Less padding needed as submit view does not have drag handles
|
|
|
|
padding-left: 16px;
|
|
|
|
}
|
|
|
|
|
2020-04-28 10:50:44 +02:00
|
|
|
input[type=submit] {
|
|
|
|
align-self: flex-end;
|
|
|
|
margin: 5px;
|
2020-04-29 03:41:02 +02:00
|
|
|
margin-bottom: 160px;
|
2020-04-28 10:50:44 +02:00
|
|
|
padding: 10px 20px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|