X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=wicket%2Fcomponents%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fwicket%2Fjquery%2FAbstractJQueryBehavior.java;h=cdc72ab8b23629d283ef3bf81f21a8c44c3041fc;hb=6c5dc665abfe021a7add911d1ca167113f4ccae8;hp=d49110dd3d14e929162d149d2e3ba950abea3b22;hpb=74740c132a0713c9505a775bf345795cf2198c95;p=utils diff --git a/wicket/components/src/main/java/org/wamblee/wicket/jquery/AbstractJQueryBehavior.java b/wicket/components/src/main/java/org/wamblee/wicket/jquery/AbstractJQueryBehavior.java index d49110dd..cdc72ab8 100644 --- a/wicket/components/src/main/java/org/wamblee/wicket/jquery/AbstractJQueryBehavior.java +++ b/wicket/components/src/main/java/org/wamblee/wicket/jquery/AbstractJQueryBehavior.java @@ -29,22 +29,28 @@ import flexjson.JSONSerializer; * Abstract JQuery hehavior class that makes it easy to write jQuery behaviors: * * - * The ready function will be invoked as part of a ready handler and will invoke - * a function with two arguments. The first is the id of the component and the second - * is a configuration object. + * The ready function will be invoked as part of a ready handler and will invoke + * a function with two arguments. The first is the selector of the component and + * the second is a configuration object. In case the behavior is attached to a + * component, a selector is used based on the unique markup id. When used on a + * page, the selector matches with the "body" of the page. *

- * The second parameter is obtained through a call to {@link #getConfigurationJavascript()}. + * The second parameter is obtained through a call to + * {@link #getConfigurationJavascript()}. * * * @author Erik Brakkee * - * + * */ public class AbstractJQueryBehavior extends CompositeBehavior { @@ -68,6 +74,15 @@ public class AbstractJQueryBehavior extends CompositeBehavior { function = aFunction; } + /** + * Determines whether the behavior is allowed to be attached toa page. + * + * @return True. + */ + protected boolean isPageAllowed() { + return true; + } + private static IBehavior[] getBehaviors(IBehavior[] aBehaviors) { IBehavior[] behaviors = new IBehavior[aBehaviors.length + 1]; behaviors[0] = new JQueryHeaderContributor(); @@ -86,13 +101,15 @@ public class AbstractJQueryBehavior extends CompositeBehavior { component + ", but component " + aComponent + " wants to be attached too"); } - if (aComponent instanceof Page) { + if (!isPageAllowed() && aComponent instanceof Page) { throw new IllegalStateException( "This behavior cannot be applied to a page: " + aComponent); } component = aComponent; super.bind(aComponent); - aComponent.setOutputMarkupId(true); + if (!(aComponent instanceof Page)) { + aComponent.setOutputMarkupId(true); + } } @Override @@ -115,7 +132,7 @@ public class AbstractJQueryBehavior extends CompositeBehavior { * @return */ String createReadyFunction() { - if (!component.getOutputMarkupId()) { + if (!(component instanceof Page) && !component.getOutputMarkupId()) { throw new IllegalStateException( "The component " + component + @@ -124,7 +141,11 @@ public class AbstractJQueryBehavior extends CompositeBehavior { StringBuffer js = new StringBuffer(); js.append("jQuery(function(){"); js.append(function + "("); - js.append("\"#" + component.getMarkupId() + "\""); + String selector = "body"; + if (!(component instanceof Page)) { + selector = "#" + component.getMarkupId(); + } + js.append("\"" + selector + "\""); String config = getConfigurationJavascript(); if (config != null) { js.append(",");