| 
									
										
										
										
											2020-10-27 01:01:34 +01:00
										 |  |  | import {UIEventSource} from "../../Logic/UIEventSource"; | 
					
						
							|  |  |  | 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[]>; | 
					
						
							|  |  |  |     public readonly currentQuestion: UIEventSource<number | undefined>; | 
					
						
							| 
									
										
										
										
											2020-10-27 01:01:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-10 18:42:31 +01:00
										 |  |  |     constructor(options: { tagsSource: UIEventSource<any>, tagRenderings: TagRenderingConfig[], units: Unit[] }) { | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |         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) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const tagRenderingQuestions = tagRenderings | 
					
						
							|  |  |  |             .map((tagRendering, i) => | 
					
						
							|  |  |  |                 new Lazy(() => new TagRenderingQuestion(tagsSource, tagRendering, | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         units: units, | 
					
						
							|  |  |  |                         afterSave: () => { | 
					
						
							|  |  |  |                             // We save and indicate progress by pinging and recalculating
 | 
					
						
							|  |  |  |                             skippedQuestions.ping(); | 
					
						
							|  |  |  |                         }, | 
					
						
							|  |  |  |                         cancelButton: Translations.t.general.skip.Clone() | 
					
						
							|  |  |  |                             .SetClass("btn btn-secondary mr-3") | 
					
						
							|  |  |  |                             .onClick(() => { | 
					
						
							|  |  |  |                                 skippedQuestions.data.push(i); | 
					
						
							|  |  |  |                                 skippedQuestions.ping(); | 
					
						
							|  |  |  |                             }) | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 ))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const skippedQuestionsButton = Translations.t.general.skippedQuestions | 
					
						
							|  |  |  |             .onClick(() => { | 
					
						
							|  |  |  |                 skippedQuestions.setData([]); | 
					
						
							|  |  |  |             }) | 
					
						
							| 
									
										
										
										
											2021-06-28 00:45:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-10 18:42:31 +01:00
										 |  |  |         const currentQuestion: UIEventSource<number | undefined> = tagsSource.map(tags => { | 
					
						
							|  |  |  |             if (tags === undefined) { | 
					
						
							|  |  |  |                 return undefined; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             for (let i = 0; i < tagRenderingQuestions.length; i++) { | 
					
						
							|  |  |  |                 let tagRendering = tagRenderings[i]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (skippedQuestions.data.indexOf(i) >= 0) { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (tagRendering.IsKnown(tags)) { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 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!
 | 
					
						
							|  |  |  |                 return i | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return undefined; // The questions are depleted
 | 
					
						
							|  |  |  |         }, [skippedQuestions]) | 
					
						
							| 
									
										
										
										
											2021-06-28 00:45:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-10 18:42:31 +01:00
										 |  |  |         super(currentQuestion.map(i => { | 
					
						
							|  |  |  |                 const els: BaseUIElement[] = [] | 
					
						
							|  |  |  |                 if (i !== undefined) { | 
					
						
							|  |  |  |                     els.push(tagRenderingQuestions[i]) | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |         this.currentQuestion = currentQuestion | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-27 01:01:34 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |