You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2013/07/05 11:02:01 UTC
[1/2] git commit: Don't use string concatenation when there is a
StringBuilder on hand
Updated Branches:
refs/heads/event-delegating-behavior [created] ccf4ca1b2
Don't use string concatenation when there is a StringBuilder on hand
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/31787e89
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/31787e89
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/31787e89
Branch: refs/heads/event-delegating-behavior
Commit: 31787e89bfae2b1261a3c4dfe3f46894a4db9a00
Parents: 2acaf1b
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Fri Jul 5 10:42:30 2013 +0300
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Fri Jul 5 10:44:38 2013 +0300
----------------------------------------------------------------------
.../apache/wicket/ajax/AbstractDefaultAjaxBehavior.java | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/wicket/blob/31787e89/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java b/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java
index fefb23b..85f950c 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/AbstractDefaultAjaxBehavior.java
@@ -509,10 +509,14 @@ public abstract class AbstractDefaultAjaxBehavior extends AbstractAjaxBehavior
}
sb.append("};\n");
if (attributes.getExtraParameters().isEmpty())
- sb.append("attrs." + AjaxAttributeName.EXTRA_PARAMETERS + " = params;\n");
+ {
+ sb.append("attrs.").append(AjaxAttributeName.EXTRA_PARAMETERS).append(" = params;\n");
+ }
else
- sb.append("attrs." + AjaxAttributeName.EXTRA_PARAMETERS + " = Wicket.merge(attrs."
- + AjaxAttributeName.EXTRA_PARAMETERS + ", params);\n");
+ {
+ sb.append("attrs.").append(AjaxAttributeName.EXTRA_PARAMETERS).append(" = Wicket.merge(attrs.")
+ .append(AjaxAttributeName.EXTRA_PARAMETERS).append(", params);\n");
+ }
sb.append("Wicket.Ajax.ajax(attrs);\n");
return sb;
}
[2/2] git commit: Experimenting with JavaScript event delegation
Posted by mg...@apache.org.
Experimenting with JavaScript event delegation
Add a AjaxEventBehavior that "disables" the event binding of the AjaxEventBehaviors on the same event in the children of the host component.
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/ccf4ca1b
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/ccf4ca1b
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/ccf4ca1b
Branch: refs/heads/event-delegating-behavior
Commit: ccf4ca1b25c5b485dc61c9642d625dde331b3a52
Parents: 31787e8
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Fri Jul 5 12:00:08 2013 +0300
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Fri Jul 5 12:00:08 2013 +0300
----------------------------------------------------------------------
.../apache/wicket/ajax/AjaxEventBehavior.java | 44 ++++++-
.../wicket/ajax/EventDelegatingBehavior.java | 117 +++++++++++++++++++
.../ajax/res/js/EventDelegatingBehavior.js.tmpl | 36 ++++++
3 files changed, 193 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/wicket/blob/ccf4ca1b/wicket-core/src/main/java/org/apache/wicket/ajax/AjaxEventBehavior.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/AjaxEventBehavior.java b/wicket-core/src/main/java/org/apache/wicket/ajax/AjaxEventBehavior.java
index 0cee432..15dd971 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ajax/AjaxEventBehavior.java
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/AjaxEventBehavior.java
@@ -16,7 +16,12 @@
*/
package org.apache.wicket.ajax;
+import java.util.HashSet;
+import java.util.List;
+
import org.apache.wicket.Component;
+import org.apache.wicket.MetaDataKey;
+import org.apache.wicket.Page;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
@@ -43,13 +48,19 @@ import org.apache.wicket.util.lang.Args;
* behavior is invoked.
*
* @since 1.2
- *
+ *
* @author Igor Vaynberg (ivaynberg)
*/
public abstract class AjaxEventBehavior extends AbstractDefaultAjaxBehavior
{
private static final long serialVersionUID = 1L;
+ /**
+ * Meta data that indicates that there is EventDelegatingBehavior somewhere in the page hierarchy
+ * The key is the event name and the value is a boolean flag.
+ */
+ protected static final MetaDataKey<HashSet<String>> EVENT_NAME_PAGE_KEY = new MetaDataKey<HashSet<String>>() {};
+
private final String event;
/**
@@ -80,9 +91,34 @@ public abstract class AjaxEventBehavior extends AbstractDefaultAjaxBehavior
if (component.isEnabledInHierarchy())
{
- CharSequence js = getCallbackScript(component);
-
- response.render(OnDomReadyHeaderItem.forScript(js.toString()));
+ boolean found = false;
+ HashSet<String> enabledEvents = component.getPage().getMetaData(EVENT_NAME_PAGE_KEY);
+ if (enabledEvents != null && enabledEvents.contains(getEvent()))
+ {
+ Component cursor = component.getParent();
+ while (cursor != null && cursor instanceof Page == false)
+ {
+ List<EventDelegatingBehavior> behaviors = cursor.getBehaviors(EventDelegatingBehavior.class);
+ for (EventDelegatingBehavior behavior : behaviors)
+ {
+ if (getEvent().equalsIgnoreCase(behavior.getEvent()))
+ {
+ CharSequence attributes = renderAjaxAttributes(component);
+ behavior.contributeComponentAttributes(component.getMarkupId(), attributes);
+ found = true;
+ break;
+ }
+ }
+ cursor = cursor.getParent();
+ }
+ }
+
+ if (found == false)
+ {
+ CharSequence js = getCallbackScript(component);
+
+ response.render(OnDomReadyHeaderItem.forScript(js.toString()));
+ }
}
}
http://git-wip-us.apache.org/repos/asf/wicket/blob/ccf4ca1b/wicket-core/src/main/java/org/apache/wicket/ajax/EventDelegatingBehavior.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/EventDelegatingBehavior.java b/wicket-core/src/main/java/org/apache/wicket/ajax/EventDelegatingBehavior.java
new file mode 100644
index 0000000..e4a046f
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/EventDelegatingBehavior.java
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.ajax;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.Page;
+import org.apache.wicket.ajax.json.JSONException;
+import org.apache.wicket.ajax.json.JSONObject;
+import org.apache.wicket.ajax.json.JsonFunction;
+import org.apache.wicket.util.template.PackageTextTemplate;
+
+/**
+ * A behavior that collects the Ajax Request attributes for all AjaxEventBehaviors
+ * on the same event in the children components of the host component.
+ * This way there is only one JavaScript event binding and all the Ajax call listeners are preserved.
+ */
+public class EventDelegatingBehavior extends AjaxEventBehavior
+{
+ private Map<String, CharSequence> attrsMap;
+
+ private boolean initialized = false;
+
+ /**
+ * Construct.
+ *
+ * @param event the event this behavior will be attached to
+ */
+ public EventDelegatingBehavior(String event)
+ {
+ super(event);
+ }
+
+ @Override
+ public void onConfigure(Component component)
+ {
+ super.onConfigure(component);
+
+ if (initialized == false)
+ {
+ Page page = component.getPage();
+ HashSet<String> enabledEvents = page.getMetaData(EVENT_NAME_PAGE_KEY);
+ if (enabledEvents == null)
+ {
+ enabledEvents = new HashSet<>();
+ page.setMetaData(EVENT_NAME_PAGE_KEY, enabledEvents);
+ }
+ enabledEvents.add(getEvent());
+ initialized = true;
+ }
+ }
+
+ /**
+ * Collects the Ajax Request Attributes for a AjaxEventBehavior in a child component
+ * @param componentId
+ * @param attributes
+ */
+ protected final void contributeComponentAttributes(String componentId, CharSequence attributes)
+ {
+ if (attrsMap == null)
+ {
+ attrsMap = new HashMap<>();
+ }
+ attrsMap.put(componentId, new JsonFunction(attributes));
+ }
+
+ @Override
+ public void detach(Component component)
+ {
+ super.detach(component);
+ attrsMap = null;
+ }
+
+ @Override
+ protected CharSequence getCallbackScript(Component component)
+ {
+ PackageTextTemplate template = new PackageTextTemplate(EventDelegatingBehavior.class, "res/js/EventDelegatingBehavior.js.tmpl");
+
+ Map<String, Object> variables = new HashMap<>();
+ variables.put("componentMarkupId", component.getMarkupId());
+ variables.put("event", getEvent());
+
+ JSONObject attributesMap = new JSONObject(attrsMap);
+ try
+ {
+ variables.put("attributesMap", attributesMap.toString(2));
+ } catch (JSONException e)
+ {
+ e.printStackTrace();
+ }
+
+ return template.asString(variables);
+ }
+
+ @Override
+ protected final void onEvent(AjaxRequestTarget target)
+ {
+ throw new UnsupportedOperationException("Executed the event delegating behavior should never happen");
+ }
+}
http://git-wip-us.apache.org/repos/asf/wicket/blob/ccf4ca1b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/EventDelegatingBehavior.js.tmpl
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/EventDelegatingBehavior.js.tmpl b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/EventDelegatingBehavior.js.tmpl
new file mode 100644
index 0000000..901af64
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/res/js/EventDelegatingBehavior.js.tmpl
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+Wicket.Event.add('${componentMarkupId}', '${event}', function(event) {
+
+ var map = ${attributesMap};
+
+console.log(map);
+
+ var id = event.target.id;
+
+console.log(id);
+
+ if (map[id]) {
+
+ var attrs = map[id];
+console.log(3, attrs);
+ var call = new Wicket.Ajax.Call();
+ call.ajax(attrs);
+ }
+
+});