Skip to content

Commit

Permalink
Add support for event listeners in template elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur- committed May 30, 2016
1 parent 4033176 commit 5b4ac76
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -126,10 +129,18 @@ public int size() {
ComponentMapping.class, TemplateMap.class,
ParentGeneratorHolder.class, TemplateEventHandlerNames.class };

@SuppressWarnings("unchecked")
private static Class<? extends NodeFeature>[] rootNodeFeatures = Stream
.concat(Stream.of(requiredFeatures), Stream.of(rootOnlyFeatures))
.toArray(Class[]::new);

@SuppressWarnings("unchecked")
private static Class<? extends NodeFeature>[] 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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 <code>null</code>
*/
public static StateNode createOverrideNode() {
return new StateNode(overrideNodeFeatures);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@include otherfile.html@
<div id="container"></div>
@child@
<button id="clearModel" (click)="$server.clearModel()">Clear model value</button>
<button id="clearModel">Clear model value</button>
<button id="modelText" (click)="$server.setModelText()">Set text model value</button>
<button id="modelBoolean" (click)="$server.setModelBoolean()">Set text boolean value</button>
<div id="bindings" [title]="modelValue" [class.name]="modelValue">{{modelValue}}</div>
Expand Down

0 comments on commit 5b4ac76

Please sign in to comment.