From 5b4ac765db57d03cbf4ebf1acf2bff6177652496 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Mon, 30 May 2016 16:11:37 +0300 Subject: [PATCH] Add support for event listeners in template elements --- .../impl/TemplateElementStateProvider.java | 26 ++++++++++++++++++- .../nodefeature/TemplateOverridesMap.java | 5 ++-- .../uitest/ui/BasicTemplateView.java | 7 +++-- .../uitest/ui/BasicTemplateView.html | 2 +- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/hummingbird-server/src/main/java/com/vaadin/hummingbird/dom/impl/TemplateElementStateProvider.java b/hummingbird-server/src/main/java/com/vaadin/hummingbird/dom/impl/TemplateElementStateProvider.java index 34dd27b4a79..2ce813a9473 100644 --- a/hummingbird-server/src/main/java/com/vaadin/hummingbird/dom/impl/TemplateElementStateProvider.java +++ b/hummingbird-server/src/main/java/com/vaadin/hummingbird/dom/impl/TemplateElementStateProvider.java @@ -34,8 +34,11 @@ import com.vaadin.hummingbird.dom.EventRegistrationHandle; import com.vaadin.hummingbird.dom.Style; import com.vaadin.hummingbird.nodefeature.ComponentMapping; +import com.vaadin.hummingbird.nodefeature.ElementChildrenList; +import com.vaadin.hummingbird.nodefeature.ElementListenerMap; import com.vaadin.hummingbird.nodefeature.ModelMap; import com.vaadin.hummingbird.nodefeature.NodeFeature; +import com.vaadin.hummingbird.nodefeature.OverrideElementData; import com.vaadin.hummingbird.nodefeature.ParentGeneratorHolder; import com.vaadin.hummingbird.nodefeature.TemplateEventHandlerNames; import com.vaadin.hummingbird.nodefeature.TemplateMap; @@ -126,10 +129,18 @@ public int size() { ComponentMapping.class, TemplateMap.class, ParentGeneratorHolder.class, TemplateEventHandlerNames.class }; + @SuppressWarnings("unchecked") private static Class[] rootNodeFeatures = Stream .concat(Stream.of(requiredFeatures), Stream.of(rootOnlyFeatures)) .toArray(Class[]::new); + @SuppressWarnings("unchecked") + private static Class[] overrideNodeFeatures = Stream + .of(OverrideElementData.class, ElementChildrenList.class, + ParentGeneratorHolder.class, ComponentMapping.class, + ElementListenerMap.class) + .toArray(Class[]::new); + private static final String CANT_MODIFY_MESSAGE = "Can't modify element defined in a template"; private ElementTemplateNode templateNode; @@ -301,7 +312,10 @@ private void modifyChildren(StateNode node, public EventRegistrationHandle addEventListener(StateNode node, String eventType, DomEventListener listener, String[] eventDataExpressions) { - throw new UnsupportedOperationException(CANT_MODIFY_MESSAGE); + ElementListenerMap listeners = getOrCreateOverrideNode(node) + .getFeature(ElementListenerMap.class); + + return listeners.add(eventType, listener, eventDataExpressions); } @Override @@ -432,4 +446,14 @@ public static StateNode createRootNode() { public static StateNode createSubModelNode() { return new StateNode(requiredFeatures); } + + /** + * Creates a new state node with all features needed for a state node use as + * an override node. + * + * @return a new state node, not null + */ + public static StateNode createOverrideNode() { + return new StateNode(overrideNodeFeatures); + } } diff --git a/hummingbird-server/src/main/java/com/vaadin/hummingbird/nodefeature/TemplateOverridesMap.java b/hummingbird-server/src/main/java/com/vaadin/hummingbird/nodefeature/TemplateOverridesMap.java index 9ae3aa4620f..2ff5092a69f 100644 --- a/hummingbird-server/src/main/java/com/vaadin/hummingbird/nodefeature/TemplateOverridesMap.java +++ b/hummingbird-server/src/main/java/com/vaadin/hummingbird/nodefeature/TemplateOverridesMap.java @@ -16,6 +16,7 @@ package com.vaadin.hummingbird.nodefeature; import com.vaadin.hummingbird.StateNode; +import com.vaadin.hummingbird.dom.impl.TemplateElementStateProvider; import com.vaadin.hummingbird.template.TemplateNode; /** @@ -56,9 +57,7 @@ public StateNode get(TemplateNode templateNode, boolean create) { StateNode overrideNode = (StateNode) get(key); if (overrideNode == null && create) { - overrideNode = new StateNode(OverrideElementData.class, - ElementChildrenList.class, ParentGeneratorHolder.class, - ComponentMapping.class); + overrideNode = TemplateElementStateProvider.createOverrideNode(); overrideNode.getFeature(OverrideElementData.class) .setTemplateNode(templateNode); diff --git a/hummingbird-tests/test-root-context/src/main/java/com/vaadin/hummingbird/uitest/ui/BasicTemplateView.java b/hummingbird-tests/test-root-context/src/main/java/com/vaadin/hummingbird/uitest/ui/BasicTemplateView.java index 233b2d401a8..6f20a0bd74e 100644 --- a/hummingbird-tests/test-root-context/src/main/java/com/vaadin/hummingbird/uitest/ui/BasicTemplateView.java +++ b/hummingbird-tests/test-root-context/src/main/java/com/vaadin/hummingbird/uitest/ui/BasicTemplateView.java @@ -30,6 +30,8 @@ public class BasicTemplateView extends Template implements View { @Id("container") private Div container; + @Id("clearModel") + private Button clearModel; public BasicTemplateView() { assert container != null; @@ -49,11 +51,8 @@ public BasicTemplateView() { }); getElement().getNode().getFeature(TemplateMap.class) .setChild(childSlotContent.getElement().getNode()); - } - @EventHandler - private void clearModel() { - setModelValue(null); + clearModel.addClickListener(e -> setModelValue(null)); } @EventHandler diff --git a/hummingbird-tests/test-root-context/src/main/resources/com/vaadin/hummingbird/uitest/ui/BasicTemplateView.html b/hummingbird-tests/test-root-context/src/main/resources/com/vaadin/hummingbird/uitest/ui/BasicTemplateView.html index ae65e061b04..e5ac219d5fe 100644 --- a/hummingbird-tests/test-root-context/src/main/resources/com/vaadin/hummingbird/uitest/ui/BasicTemplateView.html +++ b/hummingbird-tests/test-root-context/src/main/resources/com/vaadin/hummingbird/uitest/ui/BasicTemplateView.html @@ -3,7 +3,7 @@ @include otherfile.html@
@child@ - +
{{modelValue}}