+/*
+ * Copyright 2005-2010 the original author or authors.
+ *
+ * Licensed 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.wamblee.wicket.jquery;
+
+import java.util.Arrays;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.Page;
+import org.apache.wicket.behavior.IBehavior;
+import org.apache.wicket.markup.html.IHeaderResponse;
+import org.wamblee.wicket.behavior.CompositeBehavior;
+
+/**
+ * Abstract JQuery hehavior class that performs some useful basic behaviors for
+ * the behavior such as:
+ * <ul>
+ * <li>Creating a ready function which will be invoked for the component</li>
+ * <li>Checking that the component is not a page</li>
+ * <li>Creating a call to an intialization function from the ready handler using the component id </li>
+ * </ul>
+ *
+ * @author Erik Brakkee
+ *
+ */
+public class AbstractJQueryBehavior extends CompositeBehavior {
+
+ private Component component;
+ private String function;
+
+ /**
+ * Constructs the behavior.
+ *
+ * @param aFunction
+ * Ready function to be invoked.
+ * @param aBehaviors
+ * Behaviors to add in addition to the basic JQuery stuff.
+ */
+ public AbstractJQueryBehavior(String aFunction, IBehavior... aBehaviors) {
+ super(getBehaviors(aBehaviors));
+ function = aFunction;
+ }
+
+ private static IBehavior[] getBehaviors(IBehavior[] aBehaviors) {
+ IBehavior[] behaviors = new IBehavior[aBehaviors.length + 1];
+ behaviors[0] = new JQueryHeaderContributor();
+ for (int i = 0; i < aBehaviors.length; i++) {
+ behaviors[i + 1] = aBehaviors[i];
+ }
+ return behaviors;
+ }
+
+ @Override
+ public void bind(Component aComponent) {
+ if (component != null) {
+ throw new IllegalStateException(
+ "this kind of handler cannot be attached to " +
+ "multiple components; it is already attached to component " +
+ component + ", but component " + aComponent +
+ " wants to be attached too");
+ }
+ if (aComponent instanceof Page) {
+ throw new IllegalStateException(
+ "This behavior cannot be applied to a page: " + aComponent);
+ }
+ component = aComponent;
+ super.bind(aComponent);
+ aComponent.setOutputMarkupId(true);
+ }
+
+ @Override
+ public void renderHead(IHeaderResponse aResponse) {
+ super.renderHead(aResponse);
+ String jsString = createReadyFunction(function, component);
+ aResponse.renderJavascript(jsString, null);
+ }
+
+ /**
+ * Creates a jQuery ready handler that invokes a given javascript function
+ * with the id of a component.
+ *
+ * @param aFunction
+ * Javascript function to invoke.
+ * @param aComponent
+ * Component to invoke the id for.
+ * @return
+ */
+ public static String createReadyFunction(String aFunction,
+ Component aComponent) {
+ if (!aComponent.getOutputMarkupId()) {
+ throw new IllegalStateException(
+ "The component " +
+ aComponent +
+ " does not have its markup id set so this ready handler will not have any effect");
+ }
+ StringBuffer js = new StringBuffer();
+ js.append("jQuery(function(){");
+ js.append("org.wamblee." + aFunction + "(\"#" +
+ aComponent.getMarkupId() + "\");");
+ js.append("});");
+ String jsString = js.toString();
+ return jsString;
+ }
+}