mapcomplete/UI/Popup/QuestionBox.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

149 lines
5.4 KiB
TypeScript
Raw Normal View History

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