X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=wicket%2Fcomponents%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fwicket%2Fjquery%2FAbstractJQueryBehavior.java;h=cdc72ab8b23629d283ef3bf81f21a8c44c3041fc;hb=HEAD;hp=d49110dd3d14e929162d149d2e3ba950abea3b22;hpb=a55dd335d284bdcc981191b03c23fe85ef253faa;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:
*
* - Creating a ready function which will be invoked for the component
- * - Checking that the component is not a page
- * - Ensuring tha the markup id of the component is output
+ * - Checking whether or not the behavior may be attached to a page using
+ * {@link #isPageAllowed()}. By default, the behavior may be attached to
+ * pages. Behaviors that should not be allowed to be attached to pages should
+ * override this method.
+ * - Ensuring that the markup id of the component is output
* - Creating a call to an intialization function from the ready handler using
* the component id
*
*
- * 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(",");