Temporary fix to questions

This commit is contained in:
pietervdvn 2021-10-03 02:50:11 +02:00
parent 4aaf8a841b
commit 2813c31a93
3 changed files with 34 additions and 43 deletions

View file

@ -31,7 +31,7 @@ import {Unit} from "../../Models/Unit";
* Shows the question element. * Shows the question element.
* Note that the value _migh_ already be known, e.g. when selected or when changing the value * Note that the value _migh_ already be known, e.g. when selected or when changing the value
*/ */
export default class TagRenderingQuestion extends VariableUiElement { export default class TagRenderingQuestion extends Combine {
constructor(tags: UIEventSource<any>, constructor(tags: UIEventSource<any>,
configuration: TagRenderingConfig, configuration: TagRenderingConfig,
@ -45,7 +45,7 @@ export default class TagRenderingQuestion extends VariableUiElement {
) { ) {
const applicableMappings = /* const applicableMappings =
UIEventSource.ListStabilized(tags.map(tags => { UIEventSource.ListStabilized(tags.map(tags => {
const applicableMappings : {if: TagsFilter, then: any, ifnot?: TagsFilter}[] = [] const applicableMappings : {if: TagsFilter, then: any, ifnot?: TagsFilter}[] = []
for (const mapping of configuration.mappings ?? []) { for (const mapping of configuration.mappings ?? []) {
@ -69,7 +69,8 @@ export default class TagRenderingQuestion extends VariableUiElement {
applicableMappings.map(applicableMappings => { applicableMappings.map(applicableMappings => {
return TagRenderingQuestion.GenerateFullQuestion(tags, applicableMappings, configuration, options) return TagRenderingQuestion.GenerateFullQuestion(tags, applicableMappings, configuration, options)
}) })
) )*/
super([TagRenderingQuestion.GenerateFullQuestion(tags, (configuration.mappings??[]).filter(mapping => mapping.hideInAnswer !== undefined), configuration, options)])
} }
private static GenerateFullQuestion(tags: UIEventSource<any>, private static GenerateFullQuestion(tags: UIEventSource<any>,
@ -98,19 +99,23 @@ export default class TagRenderingQuestion extends VariableUiElement {
console.error("MultiAnswer failed - probably not a single option was possible", configuration) console.error("MultiAnswer failed - probably not a single option was possible", configuration)
throw "MultiAnswer failed - probably not a single option was possible" throw "MultiAnswer failed - probably not a single option was possible"
} }
const save = async () => { inputElement.GetValue().addCallbackAndRun(s => console.trace("Current selection is ", s))
const save = () => {
console.log("OnSaveTriggered", inputElement)
const selection = inputElement.GetValue().data; const selection = inputElement.GetValue().data;
console.log("Saving changes", selection)
if (selection) { if (selection) {
await (State.state?.changes ?? new Changes()) (State.state?.changes ?? new Changes())
.applyAction(new ChangeTagAction( .applyAction(new ChangeTagAction(
tags.data.id, selection, tags.data tags.data.id, selection, tags.data
)) )).then(_ => {
} console.log("Tagchanges applied")
})
if (options.afterSave) { if (options.afterSave) {
options.afterSave(); options.afterSave();
} }
} }
}
if (options.saveButtonConstr === undefined) { if (options.saveButtonConstr === undefined) {
options.saveButtonConstr = v => new SaveButton(v, options.saveButtonConstr = v => new SaveButton(v,
@ -155,13 +160,15 @@ export default class TagRenderingQuestion extends VariableUiElement {
private static GenerateInputElement(configuration: TagRenderingConfig, private static GenerateInputElement(configuration: TagRenderingConfig,
applicableMappings: {if: TagsFilter, then: any, ifnot?: TagsFilter}[], applicableMappings: {if: TagsFilter, then: any, ifnot?: TagsFilter}[],
applicableUnit: Unit, tagsSource: UIEventSource<any>): InputElement<TagsFilter> { applicableUnit: Unit,
tagsSource: UIEventSource<any>)
: InputElement<TagsFilter> {
let inputEls: InputElement<TagsFilter>[]; let inputEls: InputElement<TagsFilter>[];
const ifNotsPresent = applicableMappings.some(mapping => mapping.ifnot !== undefined) const ifNotsPresent = applicableMappings.some(mapping => mapping.ifnot !== undefined)
function allIfNotsExcept(excludeIndex: number): UIEventSource<TagsFilter[]> { function allIfNotsExcept(excludeIndex: number): TagsFilter[] {
if (configuration.mappings === undefined || configuration.mappings.length === 0) { if (configuration.mappings === undefined || configuration.mappings.length === 0) {
return undefined return undefined
} }
@ -172,34 +179,17 @@ export default class TagRenderingQuestion extends VariableUiElement {
// The multianswer will do the ifnot configuration themself // The multianswer will do the ifnot configuration themself
return undefined return undefined
} }
return tagsSource.map(currentTags => {
const negativeMappings = [] const negativeMappings = []
for (let i = 0; i < configuration.mappings.length; i++) { for (let i = 0; i < applicableMappings.length; i++) {
const mapping = configuration.mappings[i]; const mapping = applicableMappings[i];
if (i === excludeIndex || mapping.ifnot === undefined) { if (i === excludeIndex || mapping.ifnot === undefined) {
continue continue
} }
const hidden = mapping.hideInAnswer
if (hidden === undefined) {
negativeMappings.push(mapping.ifnot) negativeMappings.push(mapping.ifnot)
continue
} }
if (hidden === true) {
continue
}
if ((<TagsFilter>hidden).matchesProperties(currentTags)) {
// This option is currently hidden
continue
}
negativeMappings.push(mapping.ifnot)
}
return Utils.NoNull(negativeMappings) return Utils.NoNull(negativeMappings)
})
} }
@ -213,7 +203,7 @@ export default class TagRenderingQuestion extends VariableUiElement {
const dropdown: InputElement<TagsFilter> = new DropDown("", const dropdown: InputElement<TagsFilter> = new DropDown("",
applicableMappings.map((mapping, i) => { applicableMappings.map((mapping, i) => {
return { return {
value: new And([mapping.if, ...allIfNotsExcept(i).data]), value: new And([mapping.if, ...allIfNotsExcept(i)]),
shown: Translations.WT(mapping.then).Clone() shown: Translations.WT(mapping.then).Clone()
} }
}) })
@ -356,11 +346,11 @@ export default class TagRenderingQuestion extends VariableUiElement {
mapping: { mapping: {
if: TagsFilter, if: TagsFilter,
then: Translation, then: Translation,
}, ifNot?: UIEventSource<TagsFilter[]>): InputElement<TagsFilter> { }, ifNot?: TagsFilter[]): InputElement<TagsFilter> {
let tagging: TagsFilter | UIEventSource<TagsFilter> = mapping.if; let tagging: TagsFilter = mapping.if;
if (ifNot !== undefined) { if (ifNot !== undefined) {
tagging = ifNot.map(ifNots => new And([mapping.if, ...ifNots])) tagging = new And([mapping.if, ...ifNot])
} }
return new FixedInputElement( return new FixedInputElement(

View file

@ -13,6 +13,7 @@
"start:prepare": "ts-node scripts/generateLayerOverview.ts --no-fail && npm run increase-memory", "start:prepare": "ts-node scripts/generateLayerOverview.ts --no-fail && npm run increase-memory",
"start:parallel:parcel": "parcel *.html UI/** Logic/** assets/*.json assets/svg/* assets/generated/* assets/layers/*/*.svg assets/layers/*/*.jpg assets/layers/*/*.png assets/tagRenderings/*.json assets/themes/*/*.svg assets/themes/*/*.jpg assets/themes/*/*.png vendor/* vendor/*/*", "start:parallel:parcel": "parcel *.html UI/** Logic/** assets/*.json assets/svg/* assets/generated/* assets/layers/*/*.svg assets/layers/*/*.jpg assets/layers/*/*.png assets/tagRenderings/*.json assets/themes/*/*.svg assets/themes/*/*.jpg assets/themes/*/*.png vendor/* vendor/*/*",
"start:parallel:tailwindcli": "tailwindcss -i index.css -o css/index-tailwind-output.css --watch", "start:parallel:tailwindcli": "tailwindcss -i index.css -o css/index-tailwind-output.css --watch",
"generate:css": "tailwindcss -i index.css -o css/index-tailwind-output.css",
"test": "ts-node test/TestAll.ts", "test": "ts-node test/TestAll.ts",
"init": "npm ci && npm run generate && npm run generate:editor-layer-index && npm run generate:layouts && npm run clean", "init": "npm ci && npm run generate && npm run generate:editor-layer-index && npm run generate:layouts && npm run clean",
"add-weblate-upstream": "git remote add weblate-layers https://hosted.weblate.org/git/mapcomplete/layer-translations/ ; git remote update weblate-layers", "add-weblate-upstream": "git remote add weblate-layers https://hosted.weblate.org/git/mapcomplete/layer-translations/ ; git remote update weblate-layers",