All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.wicket.jquery.ui.widget.menu.MenuBehavior Maven / Gradle / Ivy

/*
 * 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 com.googlecode.wicket.jquery.ui.widget.menu;

import java.util.Map;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.CallbackParameter;
import org.apache.wicket.util.lang.Args;

import com.googlecode.wicket.jquery.core.JQueryEvent;
import com.googlecode.wicket.jquery.core.Options;
import com.googlecode.wicket.jquery.core.ajax.IJQueryAjaxAware;
import com.googlecode.wicket.jquery.core.ajax.JQueryAjaxBehavior;
import com.googlecode.wicket.jquery.core.utils.RequestCycleUtils;
import com.googlecode.wicket.jquery.ui.JQueryUIBehavior;

/**
 * Provides a jQuery menu behavior.
 *
 * @author Sebastien Briquet - sebfz1
 * @since 1.4.2
 * @since 1.6.2
 */
public abstract class MenuBehavior extends JQueryUIBehavior implements IJQueryAjaxAware
{
	private static final long serialVersionUID = 1L;
	public static final String METHOD = "menu";
	
	private final IMenuListener listener;
	private JQueryAjaxBehavior onSelectAjaxBehavior;

	/**
	 * Constructor
	 *
	 * @param selector the html selector (ie: "#myId")
	 * @param listener the {@link IMenuListener}
	 */
	public MenuBehavior(String selector, IMenuListener listener)
	{
		this(selector, new Options(), listener);
	}

	/**
	 * Constructor
	 *
	 * @param selector the html selector (ie: "#myId")
	 * @param options the {@link Options}
	 * @param listener the {@link IMenuListener}
	 */
	public MenuBehavior(String selector, Options options, IMenuListener listener)
	{
		super(selector, METHOD, options);
		
		this.listener = Args.notNull(listener, "listener");
	}

	// Properties //

	/**
	 * Gets the reference map of hash/menu-item.
* * @return the non-null {@link Map} */ protected abstract Map getMenuItemMap(); // Methods // @Override public void bind(Component component) { super.bind(component); this.onSelectAjaxBehavior = this.newOnSelectAjaxBehavior(this); component.add(this.onSelectAjaxBehavior); } // Events // @Override public void onConfigure(Component component) { this.setOption("select", this.onSelectAjaxBehavior.getCallbackFunction()); super.onConfigure(component); } @Override public void onAjax(AjaxRequestTarget target, JQueryEvent event) { if (event instanceof SelectEvent) { IMenuItem item = this.getMenuItemMap().get(((SelectEvent) event).getHash()); if (item != null) { item.onClick(target); this.listener.onClick(target, item); } } } // Factories // /** * Gets a new {@link JQueryAjaxBehavior} that will be wired to the 'select' event * * @param source the {@link IJQueryAjaxAware} * @return a new {@code OnSelectAjaxBehavior} by default */ protected JQueryAjaxBehavior newOnSelectAjaxBehavior(IJQueryAjaxAware source) { return new OnSelectAjaxBehavior(source); } // Ajax classes // /** * Provides a {@link JQueryAjaxBehavior} that aims to be wired to the 'select' event */ protected static class OnSelectAjaxBehavior extends JQueryAjaxBehavior { private static final long serialVersionUID = 1L; public OnSelectAjaxBehavior(IJQueryAjaxAware source) { super(source); } @Override protected CallbackParameter[] getCallbackParameters() { return new CallbackParameter[] { CallbackParameter.context("event"), CallbackParameter.context("ui"), // lf CallbackParameter.resolved("hash", "ui.item[0].id") }; // fixes #266 } @Override protected JQueryEvent newEvent() { return new SelectEvent(); } } // Event objects // /** * Provides an event object that will be broadcasted by the {@link OnSelectAjaxBehavior} callback */ protected static class SelectEvent extends JQueryEvent { private final String hash; /** * Constructor */ public SelectEvent() { super(); this.hash = RequestCycleUtils.getQueryParameterValue("hash").toString(); } /** * Gets the menu's id-hash * * @return the id-hash */ public String getHash() { return this.hash; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy