Skip to content

Commit

Permalink
add multiple options with one paste ##337
Browse files Browse the repository at this point in the history
Signed-off-by: hamza221 <[email protected]>
  • Loading branch information
hamza221 committed Nov 12, 2022
1 parent 31b45a2 commit a958837
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
29 changes: 27 additions & 2 deletions src/components/Questions/AnswerInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div :is="pseudoIcon"
v-if="!isDropdown"
class="question__item__pseudoInput" />
<input ref="input"
<textarea ref="input"
:aria-label="t('forms', 'An answer for the {index} option', { index: index + 1 })"
:placeholder="t('forms', 'Answer number {index}', { index: index + 1 })"
:value="answer.text"
Expand All @@ -14,7 +14,7 @@
type="text"
@input="onInput"
@keydown.delete="deleteEntry"
@keydown.enter.prevent="addNewEntry">
@keydown.enter.prevent="addNewEntry"></textarea>

<!-- Delete answer -->
<NcActions>
Expand Down Expand Up @@ -112,6 +112,30 @@ export default {
const answer = Object.assign({}, this.answer)
answer.text = this.$refs.input.value
if(!this.isUnique){
const multipleAswers= this.$refs.input.value.split(/\r?\n/g)
if(multipleAswers.length>1){
/*for (let i = 1; i < multipleAswers.length; i++) {
var option =
{
id: GenRandomId(),
questionId: this.id,
text: multipleAswers[i],
local: true,
}
console.log(multipleAswers.slice(1));*/
this.$emit('multipleAswers',multipleAswers,answer.id)
//this.$emit('update:answer')
return
}
}
if (this.answer.local) {
// Dispatched for creation. Marked as synced
Expand Down Expand Up @@ -221,6 +245,7 @@ export default {
width: 100%;
position: relative;
margin-right: 2px !important;
resize: none;
&--shifted {
left: -30px;
Expand Down
45 changes: 42 additions & 3 deletions src/components/Questions/QuestionMultiple.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
:max-option-length="maxStringLengths.optionText"
@add="addNewEntry"
@delete="deleteOption"
@update:answer="updateAnswer" />
@update:answer="updateAnswer"
@multipleAswers="handleMultipleOptions" />
</template>

<li v-if="(edit && !isLastEmpty) || hasNoAnswer" class="question__item">
Expand Down Expand Up @@ -98,6 +99,7 @@ import QuestionMixin from '../../mixins/QuestionMixin.js'
import GenRandomId from '../../utils/GenRandomId.js'
import logger from '../../utils/Logger.js'


export default {
name: 'QuestionMultiple',

Expand Down Expand Up @@ -180,7 +182,45 @@ export default {
// Radio: create array
this.$emit('update:values', [this.questionValues])
},
async handleMultipleOptions(answers,id){
//step 1: create copy of options to update
//step 2: update options[id] to include the first answers
//step 3: add the rest of options to the end
this.edit = true
let options = this.options.slice()
// console.log(options)
for (let i = 0; i < answers.length; i++) {
if (i === 0) {
const answerIndex = options.findIndex(option => option.id === id)
console.log('charmouta',options[answerIndex].text)
let isasba =options[answerIndex].text.trim().length > 0
options[answerIndex].text = answers[0]
if(isasba){
this.$emit('update:answer', options[answerIndex].id, options[answerIndex].text)

continue
}

}
else{
options.push({
id: GenRandomId(),
questionId: this.id,
text: answers[i],
local: true,
})}

await axios.post(generateOcsUrl('apps/forms/api/v2/option'), {
questionId: this.id,
text: answers[i],
})
}
this.updateOptions(options)

this.$nextTick(() => {
this.focusIndex(options.length - 1)
})
},
/**
* Is the provided answer required ?
* This is needed for checkboxes as html5
Expand Down Expand Up @@ -238,13 +278,12 @@ export default {

// Add local entry
const options = this.options.slice()
options.push({
options.push({
id: GenRandomId(),
questionId: this.id,
text: '',
local: true,
})

// Update question
this.updateOptions(options)

Expand Down

0 comments on commit a958837

Please sign in to comment.