|
16 | 16 | v-bind:reverse="reverse"
|
17 | 17 | />
|
18 | 18 |
|
| 19 | + <slot></slot> |
| 20 | + |
19 | 21 | <!-- Complete/Submit screen slots -->
|
20 | 22 | <div v-if="isOnLastStep" class="vff-animate f-fade-in-up field-submittype">
|
21 | 23 | <slot name="complete">
|
|
127 | 129 | https://www.ditdot.hr/en
|
128 | 130 | */
|
129 | 131 |
|
130 |
| - import FlowFormQuestion from './Question.vue' |
131 |
| - import QuestionModel from '../models/QuestionModel' |
| 132 | + import FlowFormQuestion from './FlowFormQuestion.vue' |
| 133 | + import QuestionModel, { ChoiceOption, LinkOption, QuestionType } from '../models/QuestionModel' |
132 | 134 | import LanguageModel from '../models/LanguageModel'
|
133 | 135 | import { IsMobile } from '../mixins/IsMobile'
|
134 | 136 |
|
|
139 | 141 | },
|
140 | 142 |
|
141 | 143 | props: {
|
142 |
| - questions:{ |
| 144 | + questions: { |
143 | 145 | type: Array,
|
144 | 146 | validator: value => value.every(q => q instanceof QuestionModel)
|
145 | 147 | },
|
|
212 | 214 | },
|
213 | 215 |
|
214 | 216 | activeQuestionId() {
|
215 |
| - const question = this.questions[this.activeQuestionIndex] |
| 217 | + const question = this.questionModels[this.activeQuestionIndex] |
216 | 218 |
|
217 | 219 | if (this.isOnLastStep) {
|
218 | 220 | return '_submit'
|
|
271 | 273 | }
|
272 | 274 |
|
273 | 275 | return false
|
| 276 | + }, |
| 277 | +
|
| 278 | + questionModels: { |
| 279 | + cache: false, |
| 280 | +
|
| 281 | + get() { |
| 282 | + if (this.questions && this.questions.length) { |
| 283 | + return this.questions |
| 284 | + } |
| 285 | +
|
| 286 | + const questions = [] |
| 287 | +
|
| 288 | + if (!this.questions) { |
| 289 | + const classMap = { |
| 290 | + 'options': ChoiceOption, |
| 291 | + 'descriptionLink': LinkOption |
| 292 | + } |
| 293 | +
|
| 294 | + this |
| 295 | + .$slots |
| 296 | + .default |
| 297 | + .filter(q => q.tag && q.tag.indexOf('Question') !== -1) |
| 298 | + .forEach(q => { |
| 299 | + const attrs = q.data.attrs |
| 300 | + let model = new QuestionModel() |
| 301 | +
|
| 302 | + if (q.componentInstance.question !== null) { |
| 303 | + model = q.componentInstance.question |
| 304 | + } |
| 305 | +
|
| 306 | + if (q.data.model) { |
| 307 | + model.answer = q.data.model.value |
| 308 | + } |
| 309 | +
|
| 310 | + Object.keys(model).forEach(key => { |
| 311 | + if (attrs[key] !== undefined) { |
| 312 | + if (typeof model[key] === 'boolean') { |
| 313 | + model[key] = attrs[key] !== false |
| 314 | + } else if (key in classMap) { |
| 315 | + const |
| 316 | + classReference = classMap[key], |
| 317 | + options = [] |
| 318 | +
|
| 319 | + attrs[key].forEach(option => { |
| 320 | + const instance = new classReference() |
| 321 | +
|
| 322 | + Object.keys(instance).forEach(instanceKey => { |
| 323 | + if (option[instanceKey] !== undefined) { |
| 324 | + instance[instanceKey] = option[instanceKey] |
| 325 | + } |
| 326 | + }) |
| 327 | +
|
| 328 | + options.push(instance) |
| 329 | + }) |
| 330 | +
|
| 331 | + model[key] = options |
| 332 | + } else { |
| 333 | + switch(key) { |
| 334 | + case 'type': |
| 335 | + if (Object.values(QuestionType).indexOf(attrs[key]) !== -1) { |
| 336 | + model[key] = attrs[key] |
| 337 | + } else { |
| 338 | + for (const questionTypeKey in QuestionType) { |
| 339 | + if (questionTypeKey.toLowerCase() === attrs[key].toLowerCase()) { |
| 340 | + model[key] = QuestionType[questionTypeKey] |
| 341 | + break |
| 342 | + } |
| 343 | + } |
| 344 | + } |
| 345 | + break |
| 346 | +
|
| 347 | + default: |
| 348 | + model[key] = attrs[key] |
| 349 | + break |
| 350 | + } |
| 351 | + } |
| 352 | + } |
| 353 | + }) |
| 354 | +
|
| 355 | + q.componentInstance.question = model |
| 356 | +
|
| 357 | + questions.push(model) |
| 358 | + }) |
| 359 | + } |
| 360 | +
|
| 361 | + return questions |
| 362 | + } |
274 | 363 | }
|
275 | 364 | },
|
276 | 365 |
|
|
298 | 387 | setQuestionListActivePath() {
|
299 | 388 | const questions = []
|
300 | 389 |
|
301 |
| - if (!this.questions.length) { |
| 390 | + if (!this.questionModels.length) { |
302 | 391 | return
|
303 | 392 | }
|
304 | 393 |
|
|
308 | 397 | nextId
|
309 | 398 |
|
310 | 399 | do {
|
311 |
| - let question = this.questions[index] |
| 400 | + let question = this.questionModels[index] |
312 | 401 |
|
313 | 402 | question.setIndex(serialIndex)
|
314 | 403 | question.language = this.language
|
|
321 | 410 | nextId = question.getJumpId()
|
322 | 411 | if (nextId) {
|
323 | 412 | if (nextId === '_submit') {
|
324 |
| - index = this.questions.length |
| 413 | + index = this.questionModels.length |
325 | 414 | } else {
|
326 |
| - for (let i = 0; i < this.questions.length; i++) { |
327 |
| - if (this.questions[i].id === nextId) { |
| 415 | + for (let i = 0; i < this.questionModels.length; i++) { |
| 416 | + if (this.questionModels[i].id === nextId) { |
328 | 417 | index = i
|
329 | 418 | break
|
330 | 419 | }
|
|
334 | 423 | ++index
|
335 | 424 | }
|
336 | 425 | } else {
|
337 |
| - index = this.questions.length |
| 426 | + index = this.questionModels.length |
338 | 427 | }
|
339 | 428 |
|
340 | 429 | ++serialIndex
|
341 |
| - } while (index < this.questions.length) |
| 430 | + } while (index < this.questionModels.length) |
342 | 431 |
|
343 | 432 | this.questionListActivePath = questions
|
344 | 433 | },
|
|
0 commit comments