-
Notifications
You must be signed in to change notification settings - Fork 5
/
TodoApp.java
289 lines (253 loc) · 12.8 KB
/
TodoApp.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package org.genericsystem.todomvc;
import java.util.function.Predicate;
import org.genericsystem.api.core.Snapshot;
import org.genericsystem.common.Generic;
import org.genericsystem.defaults.tools.BidirectionalBinding;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.ReactorStatics;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.annotations.Attribute;
import org.genericsystem.reactor.annotations.BindAction;
import org.genericsystem.reactor.annotations.Children;
import org.genericsystem.reactor.annotations.DependsOnModel;
import org.genericsystem.reactor.annotations.DirectSelect;
import org.genericsystem.reactor.annotations.ForEach;
import org.genericsystem.reactor.annotations.SetText;
import org.genericsystem.reactor.annotations.StyleClass;
import org.genericsystem.reactor.annotations.Switch;
import org.genericsystem.reactor.annotations.TagName;
import org.genericsystem.reactor.appserver.ApplicationServer;
import org.genericsystem.reactor.context.ContextAction.CANCEL;
import org.genericsystem.reactor.context.ContextAction.FLUSH;
import org.genericsystem.reactor.context.ContextAction.REMOVE;
import org.genericsystem.reactor.context.ObservableListExtractor.SUBINSTANCES;
import org.genericsystem.reactor.context.TagSwitcher;
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.HtmlFooter;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlH1;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlHeader;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlHyperLink;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlInputText;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlLabel.GSLabelDisplayer;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlLi;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlSpan;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlStrong;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlUl;
import org.genericsystem.reactor.gscomponents.RootTagImpl;
import org.genericsystem.reactor.gscomponents.TagImpl;
import org.genericsystem.todomvc.TodoApp.RootDiv;
import org.genericsystem.todomvc.TodoApp.RootDiv.ContentDiv;
import org.genericsystem.todomvc.TodoApp.RootDiv.ContentDiv.TodoInput;
import org.genericsystem.todomvc.TodoApp.RootDiv.ContentDiv.TodosContent;
import org.genericsystem.todomvc.TodoApp.RootDiv.ContentDiv.TodosContent.TodoItem;
import org.genericsystem.todomvc.TodoApp.RootDiv.ContentDiv.TodosContent.TodoItem.ItemContent;
import org.genericsystem.todomvc.TodoApp.RootDiv.ContentDiv.TodosContent.TodoItem.ItemContent.TodoCheckBox;
import org.genericsystem.todomvc.TodoApp.RootDiv.ContentDiv.TodosFooter;
import org.genericsystem.todomvc.TodoApp.RootDiv.ContentDiv.TodosFooter.ClearCompletedButton;
import org.genericsystem.todomvc.TodoApp.RootDiv.ContentDiv.TodosFooter.ItemsLeft;
import org.genericsystem.todomvc.TodoApp.RootDiv.ContentDiv.TodosFooter.Filters;
import org.genericsystem.todomvc.TodoApp.RootDiv.ContentDiv.TodosFooter.Filters.ModeActiveLink;
import org.genericsystem.todomvc.TodoApp.RootDiv.ContentDiv.TodosFooter.Filters.ModeAllLink;
import org.genericsystem.todomvc.TodoApp.RootDiv.ContentDiv.TodosFooter.Filters.ModeCompleteLink;
import org.genericsystem.todomvc.TodoApp.RootDiv.MainFooter;
import org.genericsystem.todomvc.Todos.Completed;
import io.reactivex.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableObjectValue;
import javafx.beans.value.ObservableValue;
/**
* @author Nicolas Feybesse
*
*/
@DependsOnModel({ Todos.class, Completed.class })
@Children({ RootDiv.class })
public class TodoApp extends RootTagImpl {
public static final String FILTER_MODE = "mode";
public static final String COMPLETED = "completed";
public static void main(String[] mainArgs) {
ApplicationServer.startSimpleGenericApp(mainArgs, TodoApp.class, "/todo2/");
}
protected static Property<Predicate<ObservableValue<Generic>>> getModeProperty(Tag tag, Context model) {
return tag.getContextProperty(FILTER_MODE, model);
}
@Override
public void init() {
addContextAttribute(FILTER_MODE, c -> new SimpleObjectProperty<Predicate<ObservableValue<Generic>>>(ALL));
}
public static class StateFilter implements TagSwitcher {
@Override
public ObservableValue<Boolean> apply(Context context, Tag tag) {
Generic todo = context.getGeneric();
ObservableValue<Generic> completed = todo.getObservableHolder(todo.getRoot().find(Completed.class));
Property<Predicate<ObservableValue<Generic>>> mode = getModeProperty(tag, context);
return Bindings.createBooleanBinding(() -> mode.getValue().test(completed), completed, mode);
}
}
static Predicate<ObservableValue<Generic>> ALL = o -> true;
static Predicate<ObservableValue<Generic>> ACTIVE = completedObs -> {
return completedObs.getValue() != null ? Boolean.FALSE.equals(completedObs.getValue().getValue()) : true;
};
static Predicate<ObservableValue<Generic>> COMPLETE = ACTIVE.negate();
static Predicate<Generic> completed = todo -> {
Generic completed = todo.getHolder(todo.getRoot().find(Completed.class));
return completed != null && Boolean.TRUE.equals(completed.getValue());
};
protected static Snapshot<Generic> getTodos(Context context) {
return context.find(Todos.class).getSubInstances();
}
protected static Snapshot<Generic> getCompletedTodos(Context context) {
return context.find(Completed.class).getSubInstances().filter(comp -> Boolean.TRUE.equals(comp.getValue())).map(comp -> comp.getComponent(0));
}
@Children({ ContentDiv.class, MainFooter.class })
public static class RootDiv extends HtmlDiv {
@StyleClass("todoapp")
@Children({ HtmlHeader.class, TodosContent.class, TodosFooter.class })
// Header, contains title + input
@StyleClass(path = HtmlHeader.class, value = "header")
@Children(path = HtmlHeader.class, value = { HtmlH1.class, TodoInput.class })
// Page title
@SetText(path = { HtmlHeader.class, HtmlH1.class}, value = "todos")
public static class ContentDiv extends FlexDiv {
@StyleClass("new-todo")
@Attribute(name = "placeholder", value = "What needs to be done?")
public static class TodoInput extends HtmlInputText {
@Override
public void init() {
bindAction(model -> {
String value = getDomNodeAttributes(model).get("value");
if (value != null && !value.isEmpty())
model.find(Todos.class).addInstance(value);
getDomNodeAttributes(model).put("value", null);
});
}
}
@StyleClass("main")
@Children(HtmlUl.class)
// Settings for the list
@StyleClass(path = HtmlUl.class, value = "todo-list")
@Children(path = HtmlUl.class, value = TodoItem.class)
@DirectSelect(path = HtmlUl.class, value = Todos.class)
public static class TodosContent extends FlexDiv {
@Children(ItemContent.class)
@ForEach(SUBINSTANCES.class)
@Switch(StateFilter.class)
public static class TodoItem extends HtmlLi {
@Override
public void init() {
addContextAttribute(COMPLETED, context -> new SimpleBooleanProperty(completed.test(context.getGeneric())));
bindOptionalStyleClass(COMPLETED, COMPLETED);
}
@StyleClass("view")
@Children({ TodoCheckBox.class, GSLabelDisplayer.class, HtmlButton.class })
// Remove button appearing on the right when hovering
@StyleClass(path = HtmlButton.class, value = "destroy")
@BindAction(path = HtmlButton.class, value = REMOVE.class)
public static class ItemContent extends HtmlDiv {
@StyleClass("toggle")
@TagName(value = TagName.INPUT, type = TagName.CHECKBOX)
public static class TodoCheckBox extends TagImpl {
@Override
public void init() {
addPrefixBinding(todo -> {
Property<Boolean> completedProperty = getContextProperty(COMPLETED, todo);
ObservableValue<Generic> completed = todo.getGeneric().getObservableHolder(todo.getGeneric().getRoot().find(Completed.class));
Property<Generic> completedGenericProperty = new SimpleObjectProperty<>(completed.getValue());
completed.addListener((ov, v, nv) -> completedGenericProperty.setValue(nv));
BidirectionalBinding.bind(completedProperty, completedGenericProperty, b -> todo.getGeneric().isAlive() ? todo.getGeneric().setHolder(todo.find(Completed.class), b) : null,
g -> g == null ? false : (Boolean) g.getValue());
});
addPrefixBinding(todo -> {
if (Boolean.TRUE.equals(getContextObservableValue(COMPLETED, todo).getValue())) {
getDomNodeAttributes(todo).put(ReactorStatics.CHECKED, ReactorStatics.CHECKED);
}
});
bindOptionalBiDirectionalAttribute(COMPLETED, ReactorStatics.CHECKED, ReactorStatics.CHECKED);
}
}
}
}
}
@StyleClass("footer")
@Children(HtmlDiv.class)
@Children(path = HtmlDiv.class, value = { HtmlSpan.class, Filters.class, ClearCompletedButton.class })
@Children(path = { HtmlDiv.class, HtmlSpan.class }, value = ItemsLeft.class)
@StyleClass(path = { HtmlDiv.class, HtmlSpan.class }, value = "todo-count")
public static class TodosFooter extends HtmlFooter {
@Override
public void init() {
addContextAttribute("hasNoTodo", context -> new SimpleBooleanProperty());
addPrefixBinding(context -> context.getHtmlDomNode(this).getDisposables().add(getTodos(context).setOnChanged()
.map(set -> set.isEmpty()).subscribe(empty -> getContextProperty("hasNoTodo", context).setValue(empty))));
bindOptionalStyleClass("hidden", "hasNoTodo");
}
public static class ItemsLeft extends HtmlStrong {
@Override
public void init() {
bindText(context -> Observable.combineLatest(getTodos(context).setOnChanged(), getCompletedTodos(context).setOnChanged(),
(todos, completed) -> todos.size() - completed.size()).map(size -> size + " item" + (size > 1 ? "s" : "") + " left"));
}
}
@StyleClass("filters")
@Children({ HtmlLi.class, HtmlLi.class, HtmlLi.class })
@Children(path = HtmlLi.class, pos = 0, value = ModeAllLink.class)
@Children(path = HtmlLi.class, pos = 1, value = ModeActiveLink.class)
@Children(path = HtmlLi.class, pos = 2, value = ModeCompleteLink.class)
public static class Filters extends HtmlUl {
@SetText("All")
public static class ModeAllLink extends HtmlHyperLink {
@Override
public void init() {
bindAction(model -> getModeProperty(this, model).setValue(ALL));
bindOptionalStyleClass("selected", "allMode", model -> Bindings.equal((ObservableObjectValue<?>) getModeProperty(this, model), ALL));
}
}
@SetText("Actives")
public static class ModeActiveLink extends HtmlHyperLink {
@Override
public void init() {
bindAction(model -> getModeProperty(this, model).setValue(ACTIVE));
bindOptionalStyleClass("selected", "activeMode", model -> Bindings.equal((ObservableObjectValue<?>) getModeProperty(this, model), ACTIVE));
}
}
@SetText("Completes")
public static class ModeCompleteLink extends HtmlHyperLink {
@Override
public void init() {
bindAction(model -> getModeProperty(this, model).setValue(COMPLETE));
bindOptionalStyleClass("selected", "completeMode", model -> Bindings.equal((ObservableObjectValue<?>) getModeProperty(this, model), COMPLETE));
}
}
}
@StyleClass("clear-completed")
public static class ClearCompletedButton extends HtmlButton {
@Override
public void init() {
bindAction(model -> getCompletedTodos(model).toList().forEach(Generic::remove));
bindText(model -> getCompletedTodos(model).setOnChanged().map(list -> list.size()).map(size -> "Clear completed (" + size + ")"));
addContextAttribute("hasNoCompleted", context -> new SimpleBooleanProperty());
addPrefixBinding(context -> context.getHtmlDomNode(this).getDisposables().add(getCompletedTodos(context).setOnChanged()
.map(set -> set.isEmpty()).subscribe(empty -> getContextProperty("hasNoCompleted", context).setValue(empty))));
bindOptionalStyleClass("hidden", "hasNoCompleted");
}
}
}
}
@StyleClass("save-cancel")
@Children({ HtmlButton.class, HtmlButton.class})
// Save button
@StyleClass(path = HtmlButton.class, pos = 0, value = "save")
@SetText(path = HtmlButton.class, pos = 0, value = "Save")
@BindAction(path = HtmlButton.class, pos = 0, value = FLUSH.class)
// Cancel button
@StyleClass(path = HtmlButton.class, pos = 1, value = "cancel")
@SetText(path = HtmlButton.class, pos = 1, value = "Cancel")
@BindAction(path = HtmlButton.class, pos = 1, value = CANCEL.class)
public static class MainFooter extends HtmlFooter {
}
}
}