2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.wicket.jquery;
18 import java.util.Arrays;
20 import org.apache.wicket.Component;
21 import org.apache.wicket.Page;
22 import org.apache.wicket.behavior.IBehavior;
23 import org.apache.wicket.markup.html.IHeaderResponse;
24 import org.wamblee.wicket.behavior.CompositeBehavior;
27 * Abstract JQuery hehavior class that makes it easy to write jQuery behaviors:
29 * <li>Creating a ready function which will be invoked for the component</li>
30 * <li>Checking that the component is not a page</li>
31 * <li>Ensuring tha the markup id of the component is output</li>
32 * <li>Creating a call to an intialization function from the ready handler using the component id </li>
35 * @author Erik Brakkee
38 public class AbstractJQueryBehavior extends CompositeBehavior {
40 private Component component;
41 private String function;
44 * Constructs the behavior.
47 * Function to be invoked from the ready handler. This function is invoked with a
48 * CSS selector that identifies the component.
50 * Behaviors to add in addition to the basic JQuery stuff.
52 public AbstractJQueryBehavior(String aFunction, IBehavior... aBehaviors) {
53 super(getBehaviors(aBehaviors));
57 private static IBehavior[] getBehaviors(IBehavior[] aBehaviors) {
58 IBehavior[] behaviors = new IBehavior[aBehaviors.length + 1];
59 behaviors[0] = new JQueryHeaderContributor();
60 for (int i = 0; i < aBehaviors.length; i++) {
61 behaviors[i + 1] = aBehaviors[i];
67 public void bind(Component aComponent) {
68 if (component != null) {
69 throw new IllegalStateException(
70 "this kind of handler cannot be attached to " +
71 "multiple components; it is already attached to component " +
72 component + ", but component " + aComponent +
73 " wants to be attached too");
75 if (aComponent instanceof Page) {
76 throw new IllegalStateException(
77 "This behavior cannot be applied to a page: " + aComponent);
79 component = aComponent;
80 super.bind(aComponent);
81 aComponent.setOutputMarkupId(true);
85 public void renderHead(IHeaderResponse aResponse) {
86 super.renderHead(aResponse);
87 String jsString = createReadyFunction(function, component);
88 aResponse.renderJavascript(jsString, null);
92 * Creates a jQuery ready handler that invokes a given javascript function
93 * with the id of a component.
96 * Javascript function to invoke.
98 * Component to invoke the id for.
101 public static String createReadyFunction(String aFunction,
102 Component aComponent) {
103 if (!aComponent.getOutputMarkupId()) {
104 throw new IllegalStateException(
107 " does not have its markup id set so this ready handler will not have any effect");
109 StringBuffer js = new StringBuffer();
110 js.append("jQuery(function(){");
111 js.append("org.wamblee." + aFunction + "(\"#" +
112 aComponent.getMarkupId() + "\");");
114 String jsString = js.toString();