Skip to content

Commit

Permalink
gs-todomvc: Some simplifications, add hidden styleClass when appropriate
Browse files Browse the repository at this point in the history
Hide the footer if there is no todo, and the link to clear the completed todos
if there is no completed todo. The former version of the bindings did not work
because they used the “hide” CSS class but the class has been renamed to
“hidden” in the CSS file.
  • Loading branch information
fducroquet committed Oct 2, 2017
1 parent bfbf4d6 commit 987d898
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions gs-todomvc/src/main/java/org/genericsystem/todomvc/TodoApp.java
@@ -1,7 +1,6 @@
package org.genericsystem.todomvc;

import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.genericsystem.api.core.Snapshot;
import org.genericsystem.common.Generic;
Expand Down Expand Up @@ -122,8 +121,8 @@ protected static Snapshot<Generic> getTodos(Context context) {
return context.find(Todos.class).getSubInstances();
}

protected static Snapshot<Generic> getCompleted(Context context) {
return context.find(Completed.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({ MyDiv.class, MyHtmlFooter2.class })
Expand Down Expand Up @@ -178,10 +177,7 @@ public static class MyHtmlLi extends HtmlLi {

@Override
public void init() {
addContextAttribute(COMPLETED, model -> {
Generic completed = model.getGeneric().getHolder(model.getGeneric().getRoot().find(Completed.class));
return new SimpleBooleanProperty(completed != null && Boolean.TRUE.equals(completed.getValue()) ? true : false);
});
addContextAttribute(COMPLETED, context -> new SimpleBooleanProperty(completed.test(context.getGeneric())));
bindOptionalStyleClass(COMPLETED, COMPLETED);
}

Expand All @@ -197,7 +193,7 @@ 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());
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());
Expand Down Expand Up @@ -240,8 +236,10 @@ public static class MyHtmlFooter1 extends TagImpl {

@Override
public void init() {
// Broken
// bindOptionalStyleClass("hide", "hasNoTodo", model -> Bindings.createBooleanBinding(() -> getTodos(this, model).size() == 0 ? true : false, getTodos(this, model)));
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");
}

@Children({ MyHtmlSpan.class, MyHtmlUl2.class, MyHtmlButton.class })
Expand All @@ -256,9 +254,8 @@ public static class MyHtmlStrong extends TagImpl {

@Override
public void init() {
bindText(model -> Observable.combineLatest(getTodos(model).setOnChanged(), getCompleted(model).setOnChanged(), (todos, comp) ->
todos.stream().filter(g -> !completed.test(g)).collect(Collectors.toList())).map(list -> list.size())
.map(size -> size + " item" + (size > 1 ? "s" : "") + " left"));
bindText(context -> Observable.combineLatest(getTodos(context).setOnChanged(), getCompletedTodos(context).setOnChanged(),
(todos, completed) -> todos.size() - completed.size()).map(size -> size + " item" + (size > 1 ? "s" : "") + " left"));
}
}
}
Expand Down Expand Up @@ -315,12 +312,12 @@ public static class MyHtmlButton extends HtmlButton {

@Override
public void init() {
bindAction(model -> getTodos(model).filter(completed).toList().forEach(Generic::remove));
bindText(model -> getCompleted(model).setOnChanged()
.map(instances -> instances.stream().filter(g -> Boolean.TRUE.equals(g.getValue())).collect(Collectors.toList()))
.map(list -> list.size()).map(size -> "Clear completed (" + size + ")"));
// Broken
// bindOptionalStyleClass("hide", "hasNoCompleted", model -> Bindings.createBooleanBinding(() -> getCompletedTodos(model).size() == 0 ? true : false, getCompletedTodos(model)));
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");
}
}
}
Expand Down

0 comments on commit 987d898

Please sign in to comment.