2022-06-05 02:24:14 +02:00
|
|
|
import { Store, UIEventSource } from "../../Logic/UIEventSource"
|
2020-10-27 01:01:34 +01:00
|
|
|
import TagRenderingQuestion from "./TagRenderingQuestion"
|
|
|
|
import Translations from "../i18n/Translations"
|
2021-05-17 00:18:21 +02:00
|
|
|
import Combine from "../Base/Combine"
|
2021-06-22 03:16:45 +02:00
|
|
|
import BaseUIElement from "../BaseUIElement"
|
2021-06-28 00:45:49 +02:00
|
|
|
import { VariableUiElement } from "../Base/VariableUIElement"
|
2021-08-07 23:11:34 +02:00
|
|
|
import TagRenderingConfig from "../../Models/ThemeConfig/TagRenderingConfig"
|
|
|
|
import { Unit } from "../../Models/Unit"
|
2021-10-03 21:44:43 +02:00
|
|
|
import Lazy from "../Base/Lazy"
|
2020-10-27 01:01:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates all the questions, one by one
|
|
|
|
*/
|
2021-06-28 00:45:49 +02:00
|
|
|
export default class QuestionBox extends VariableUiElement {
|
2021-11-10 18:42:31 +01:00
|
|
|
public readonly skippedQuestions: UIEventSource<number[]>
|
2022-06-05 02:24:14 +02:00
|
|
|
public readonly restingQuestions: Store<BaseUIElement[]>
|
2022-09-08 21:40:48 +02:00
|
|
|
|
2022-01-19 20:34:04 +01:00
|
|
|
constructor(
|
|
|
|
state,
|
|
|
|
options: {
|
2021-11-12 01:44:13 +01:00
|
|
|
tagsSource: UIEventSource<any>
|
|
|
|
tagRenderings: TagRenderingConfig[]
|
|
|
|
units: Unit[]
|
|
|
|
showAllQuestionsAtOnce?: boolean | UIEventSource<boolean>
|
|
|
|
}
|
|
|
|
) {
|
2021-06-28 00:45:49 +02:00
|
|
|
const skippedQuestions: UIEventSource<number[]> = new UIEventSource<number[]>([])
|
2020-10-27 01:01:34 +01:00
|
|
|
|
2021-11-10 18:42:31 +01:00
|
|
|
const tagsSource = options.tagsSource
|
|
|
|
const units = options.units
|
2021-11-12 01:44:13 +01:00
|
|
|
options.showAllQuestionsAtOnce = options.showAllQuestionsAtOnce ?? false
|
2021-11-10 18:42:31 +01:00
|
|
|
const tagRenderings = options.tagRenderings
|
2020-12-08 23:44:34 +01:00
|
|
|
.filter((tr) => tr.question !== undefined)
|
2021-11-10 18:42:31 +01:00
|
|
|
.filter((tr) => tr.question !== null)
|
2022-09-08 21:40:48 +02:00
|
|
|
|
2021-11-10 18:42:31 +01:00
|
|
|
const tagRenderingQuestions = tagRenderings.map(
|
|
|
|
(tagRendering, i) =>
|
2022-09-08 21:40:48 +02:00
|
|
|
new Lazy(
|
|
|
|
() =>
|
2022-01-19 20:34:04 +01:00
|
|
|
new TagRenderingQuestion(tagsSource, tagRendering, state, {
|
2021-11-10 18:42:31 +01:00
|
|
|
units: units,
|
|
|
|
afterSave: () => {
|
|
|
|
// We save and indicate progress by pinging and recalculating
|
|
|
|
skippedQuestions.ping()
|
2022-09-08 21:40:48 +02:00
|
|
|
},
|
2021-11-10 18:42:31 +01:00
|
|
|
cancelButton: Translations.t.general.skip
|
2022-09-08 21:40:48 +02:00
|
|
|
.Clone()
|
2022-02-04 00:45:22 +01:00
|
|
|
.SetClass("btn btn-secondary")
|
2021-11-10 18:42:31 +01:00
|
|
|
.onClick(() => {
|
|
|
|
skippedQuestions.data.push(i)
|
|
|
|
skippedQuestions.ping()
|
2022-09-08 21:40:48 +02:00
|
|
|
}),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
2021-11-10 18:42:31 +01:00
|
|
|
|
|
|
|
const skippedQuestionsButton = Translations.t.general.skippedQuestions.onClick(() => {
|
|
|
|
skippedQuestions.setData([])
|
2022-09-08 21:40:48 +02:00
|
|
|
})
|
2021-11-12 01:44:13 +01:00
|
|
|
tagsSource.map(
|
2022-09-08 21:40:48 +02:00
|
|
|
(tags) => {
|
2021-11-10 18:42:31 +01:00
|
|
|
if (tags === undefined) {
|
|
|
|
return undefined
|
2022-09-08 21:40:48 +02:00
|
|
|
}
|
2021-11-10 18:42:31 +01:00
|
|
|
for (let i = 0; i < tagRenderingQuestions.length; i++) {
|
|
|
|
let tagRendering = tagRenderings[i]
|
|
|
|
|
|
|
|
if (skippedQuestions.data.indexOf(i) >= 0) {
|
|
|
|
continue
|
2022-09-08 21:40:48 +02:00
|
|
|
}
|
2021-11-10 18:42:31 +01:00
|
|
|
if (tagRendering.IsKnown(tags)) {
|
2022-09-08 21:40:48 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-11-10 18:42:31 +01:00
|
|
|
if (tagRendering.condition) {
|
2022-01-19 20:34:04 +01:00
|
|
|
if (!tagRendering.condition.matchesProperties(tags)) {
|
2021-11-10 18:42:31 +01:00
|
|
|
// Filtered away by the condition, so it is kindof known
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// this value is NOT known - this is the question we have to show!
|
2022-09-08 21:40:48 +02:00
|
|
|
return i
|
2021-11-10 18:42:31 +01:00
|
|
|
}
|
|
|
|
return undefined // The questions are depleted
|
2022-09-08 21:40:48 +02:00
|
|
|
},
|
2021-11-12 01:44:13 +01:00
|
|
|
[skippedQuestions]
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
|
|
|
|
2021-11-10 18:42:31 +01:00
|
|
|
const questionsToAsk: Store<BaseUIElement[]> = tagsSource.map(
|
|
|
|
(tags) => {
|
|
|
|
if (tags === undefined) {
|
2022-09-08 21:40:48 +02:00
|
|
|
return []
|
2021-11-10 18:42:31 +01:00
|
|
|
}
|
2022-01-17 21:33:03 +01:00
|
|
|
const qs = []
|
|
|
|
for (let i = 0; i < tagRenderingQuestions.length; i++) {
|
2021-11-12 01:44:13 +01:00
|
|
|
let tagRendering = tagRenderings[i]
|
2022-09-08 21:40:48 +02:00
|
|
|
|
2021-11-12 01:44:13 +01:00
|
|
|
if (skippedQuestions.data.indexOf(i) >= 0) {
|
2022-09-08 21:40:48 +02:00
|
|
|
continue
|
|
|
|
}
|
2022-01-17 21:33:03 +01:00
|
|
|
if (tagRendering.IsKnown(tags)) {
|
2022-09-08 21:40:48 +02:00
|
|
|
continue
|
|
|
|
}
|
2022-01-17 21:33:03 +01:00
|
|
|
if (tagRendering.condition && !tagRendering.condition.matchesProperties(tags)) {
|
|
|
|
// Filtered away by the condition, so it is kindof known
|
|
|
|
continue
|
|
|
|
}
|
2021-06-28 00:45:49 +02:00
|
|
|
|
2021-11-10 18:42:31 +01:00
|
|
|
// this value is NOT known - this is the question we have to show!
|
2021-11-12 01:44:13 +01:00
|
|
|
qs.push(tagRenderingQuestions[i])
|
|
|
|
}
|
|
|
|
return qs
|
|
|
|
},
|
|
|
|
[skippedQuestions]
|
|
|
|
)
|
2021-06-28 00:45:49 +02:00
|
|
|
|
2021-11-12 01:44:13 +01:00
|
|
|
super(
|
|
|
|
questionsToAsk.map((allQuestions) => {
|
2021-11-10 18:42:31 +01:00
|
|
|
const els: BaseUIElement[] = []
|
2021-11-12 01:44:13 +01:00
|
|
|
if (
|
|
|
|
options.showAllQuestionsAtOnce === true ||
|
|
|
|
options.showAllQuestionsAtOnce["data"]
|
|
|
|
) {
|
|
|
|
els.push(...questionsToAsk.data)
|
|
|
|
} else {
|
|
|
|
els.push(allQuestions[0])
|
2021-06-28 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (skippedQuestions.data.length > 0) {
|
2021-11-10 18:42:31 +01:00
|
|
|
els.push(skippedQuestionsButton)
|
2021-06-28 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2021-11-10 18:42:31 +01:00
|
|
|
return new Combine(els).SetClass("block mb-8")
|
|
|
|
})
|
2021-06-28 00:45:49 +02:00
|
|
|
)
|
|
|
|
|
2021-11-10 18:42:31 +01:00
|
|
|
this.skippedQuestions = skippedQuestions
|
2021-11-12 01:44:13 +01:00
|
|
|
this.restingQuestions = questionsToAsk
|
2020-10-27 01:01:34 +01:00
|
|
|
}
|
|
|
|
}
|