-
Notifications
You must be signed in to change notification settings - Fork 5
/
QuestionDiv.java
198 lines (165 loc) · 7.68 KB
/
QuestionDiv.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package org.genericsystem.quiz.app.pages.components;
import java.util.List;
import org.genericsystem.common.Generic;
import org.genericsystem.quiz.app.pages.components.QuestionDiv.AnswerDiv.QuizCheckBox;
import org.genericsystem.quiz.app.pages.components.QuestionDiv.Empty;
import org.genericsystem.quiz.app.pages.components.QuestionDiv.FooterDiv;
import org.genericsystem.quiz.app.pages.components.QuestionDiv.FooterDiv.FinishBtn;
import org.genericsystem.quiz.app.pages.components.QuestionDiv.FooterDiv.NextBtn;
import org.genericsystem.quiz.app.pages.components.QuestionDiv.FooterDiv.PreviousBtn;
import org.genericsystem.quiz.app.pages.components.QuestionDiv.UnitDiv;
import org.genericsystem.quiz.model.UserAnswer;
import org.genericsystem.quiz.utils.QuizContextAction.NEXT_TAG;
import org.genericsystem.quiz.utils.QuizContextAction.PREVIOUS_TAG;
import org.genericsystem.quiz.utils.QuizContextAction.SAVE_QUIZ_RESULT;
import org.genericsystem.quiz.utils.QuizExtractors.ANSWERS_EXTRACTOR;
import org.genericsystem.quiz.utils.QuizExtractors.QUESTIONS_EXTRACTOR;
import org.genericsystem.quiz.utils.QuizStepper;
import org.genericsystem.reactor.HtmlDomNode;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.annotations.BindAction;
import org.genericsystem.reactor.annotations.BindText;
import org.genericsystem.reactor.annotations.Children;
import org.genericsystem.reactor.annotations.ForEach;
import org.genericsystem.reactor.annotations.SelectContext;
import org.genericsystem.reactor.annotations.SetText;
import org.genericsystem.reactor.annotations.Stepper2;
import org.genericsystem.reactor.annotations.Style;
import org.genericsystem.reactor.annotations.StyleClass;
import org.genericsystem.reactor.context.ObservableContextSelector.SELECTION_SELECTOR;
import org.genericsystem.reactor.contextproperties.SelectionDefaults;
import org.genericsystem.reactor.contextproperties.StepperDefaults;
import org.genericsystem.reactor.gscomponents.CheckBoxWithValue;
import org.genericsystem.reactor.gscomponents.DivWithTitle.TitleDiv;
import org.genericsystem.reactor.gscomponents.FlexDiv;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlButton;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlDiv;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlH2;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlLabel;
@Children({ UnitDiv.class, FooterDiv.class, Empty.class })
//
@Style(name = "flex", value = "1")
@Style(name = "padding", value = "10px")
@Style(name = "background-color", value = "#DCDCDC")
@Style(name = "border-radius", value = "10px")
@Style(path = { UnitDiv.class, TitleDiv.class }, name = "background-color", value = "#708090")
@Style(path = { UnitDiv.class, TitleDiv.class }, name = "padding", value = "10px")
@Style(path = { UnitDiv.class, TitleDiv.class }, name = "margin", value = "10px")
//
@ForEach(path = UnitDiv.class, value = QUESTIONS_EXTRACTOR.class)
@BindText(path = { UnitDiv.class, TitleDiv.class, HtmlH2.class })
@SelectContext(SELECTION_SELECTOR.class)
@Stepper2(switchClass = UnitDiv.class, headerClass = Empty.class)
public class QuestionDiv extends FlexDiv implements StepperDefaults, SelectionDefaults {
// TODO Remplacer cette classe (temporaire, afin de faire fonctionner le stepper)
// par une autre Div affichant une information non steppable
// Ou faire évoluer le Stepper afin qu'il puisse prendre uniquement la switchClass en paramètre
public static class Empty extends HtmlDiv {
}
@Children({ TitleDiv.class, AnswersDiv.class })
//
@Style(name = "display", value = "flex")
@Style(name = "flex", value = "1")
@Style(name = "min-height", value = "100%")
@Style(name = "flex-direction", value = "column")
@Style(path = HtmlDiv.class, name = "border-radius", value = "10px")
public static class UnitDiv extends HtmlDiv {
}
@Children(AnswerDiv.class)
//
@Style(name = "display", value = "flex")
@Style(name = "flex-wrap", value = "wrap")
@Style(name = "justify-content", value = "space-around")
@Style(name = "margin", value = "10px")
@Style(name = "border", value = "1px solid silver")
@Style(name = "align-items", value = "center")
@StyleClass("fdirection-r-r-c")
public static class AnswersDiv extends HtmlDiv {
}
@Children({ QuizCheckBox.class, HtmlLabel.class })
//
@Style(name = "min-width", value = "174px")
@Style(name = "display", value = "flex")
@Style(name = "flex-wrap", value = "nowrap")
@Style(name = "flex-direction", value = "row")
@Style(path = QuizCheckBox.class, name = "margin-right", value = "10px")
@Style(path = QuizCheckBox.class, name = "min-height", value = "15px")
@Style(path = QuizCheckBox.class, name = "min-width", value = "15px")
@Style(path = HtmlLabel.class, name = "margin-top", value = "10px")
@Style(path = HtmlLabel.class, name = "margin-bottom", value = "10px")
@Style(path = HtmlLabel.class, name = "word-wrap", value = "break-word")
@Style(path = HtmlLabel.class, name = "hyphens", value = "auto")
@Style(path = HtmlLabel.class, name = "max-width", value = "100%")
@StyleClass("width-35-35-90")
@StyleClass(path = HtmlLabel.class, value = "vertical-align")
@StyleClass(path = QuizCheckBox.class, value = "vertical-align")
//
@ForEach(ANSWERS_EXTRACTOR.class)
@BindText(path = HtmlLabel.class)
public static class AnswerDiv extends HtmlDiv {
@Override
public void init() {
// Mets un for entre un label et une checkbox
// Les 2 Tags doivent être des children de la meme div parente et etre des enfant directs de cette div
addPostfixBinding(context -> {
List<HtmlDomNode> nodes = context.getHtmlDomNode(this).getChildren();
String idTag = null;
Tag label = null;
for (HtmlDomNode n : nodes) {
if (n.getTag() instanceof QuizCheckBox)
idTag = n.getId();
if (n.getTag() instanceof HtmlLabel)
label = n.getTag();
}
if (label != null && idTag != null)
label.addAttribute(context, "for", idTag);
});
}
public static class QuizCheckBox extends CheckBoxWithValue {
@Override
public void init() {
// affiche les réponses précédentes du User logged
initValueProperty(context -> {
Generic userA = context.getGeneric().getLink(context.find(UserAnswer.class), getLoggedUserProperty(context).getValue());
if (userA == null)
userA = context.getGeneric().setLink(context.find(UserAnswer.class), false, getLoggedUserProperty(context).getValue());
return (Boolean) userA.getValue();
});
// Place un listener sur la checkbox qui écoute l'état de la checkbox et modifie la valeur de UserAnswer en conséquence
addConvertedValueChangeListener((context, nva) -> context.getGeneric().getLink(context.find(UserAnswer.class), getLoggedUserProperty(context).getValue()).updateValue(nva));
}
}
}
@Children({ PreviousBtn.class, NextBtn.class, FinishBtn.class })
//
@Style(name = "display", value = "flex")
@Style(name = "justify-content", value = "space-around")
@Style(name = "align-self", value = "flex-end")
@Style(name = "width", value = "100%")
@StyleClass(path = HtmlButton.class, value = "monitorButton")
public static class FooterDiv extends HtmlDiv {
@SetText("Next >")
//
@Style(name = "text-align", value = "center")
//
@BindAction(NEXT_TAG.class)
public static class NextBtn extends HtmlButton implements QuizStepper {
}
@SetText("< Previous")
//
@Style(name = "display", value = "none")
@Style(name = "text-align", value = "center")
//
@BindAction(PREVIOUS_TAG.class)
public static class PreviousBtn extends HtmlButton implements QuizStepper {
}
@SetText("Finish")
//
@Style(name = "display", value = "none")
@Style(name = "text-align", value = "center")
//
@BindAction({ SAVE_QUIZ_RESULT.class })
public static class FinishBtn extends HtmlButton implements QuizStepper {
}
}
}