Buy me a coffee :)

Wicket - jQuery UI

jQuery UI integration in Wicket 6.x, Wicket 7.x , Wicket 8.x & Wicket 9.x

Draggable Component

Drag me around


Components / Samples

Description

The draggable component allows to be notified when the drag starts and when the drag stops (onDragStart and onDragStop).

Sources

package com.googlecode.wicket.jquery.ui.samples.jqueryui.draggable;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.html.panel.FeedbackPanel;

import com.googlecode.wicket.jquery.core.resource.StyleSheetPackageHeaderItem;
import com.googlecode.wicket.jquery.core.utils.RequestCycleUtils;
import com.googlecode.wicket.jquery.ui.interaction.draggable.Draggable;
import com.googlecode.wicket.jquery.ui.panel.JQueryFeedbackPanel;

public class ComponentDraggablePage extends AbstractDraggablePage
{
	private static final long serialVersionUID = 1L;

	public ComponentDraggablePage()
	{
		// FeedbackPanel //
		final FeedbackPanel feedback = new JQueryFeedbackPanel("feedback");
		this.add(feedback.setOutputMarkupId(true));

		// Draggable //
		this.add(new Draggable<Void>("draggable") {

			private static final long serialVersionUID = 1L;

			@Override
			public boolean isStopEventEnabled()
			{
				return true;
			}

			@Override
			public void onDragStart(AjaxRequestTarget target, int top, int left)
			{
				this.info(String.format("Drag started - position: {%s, %s}", top, left));

				target.add(feedback);
			}

			@Override
			public void onDragStop(AjaxRequestTarget target, int top, int left)
			{
				double offsetTop = RequestCycleUtils.getQueryParameterValue("offsetTop").toDouble(-1);
				double offsetLeft = RequestCycleUtils.getQueryParameterValue("offsetLeft").toDouble(-1);

				this.info(String.format("Drag stoped - position: {%d, %d}, offset: {%.1f, %.1f}", top, left, offsetTop, offsetLeft));

				target.add(feedback);
			}
		});
	}

	// Methods //

	@Override
	public void renderHead(IHeaderResponse response)
	{
		super.renderHead(response);

		response.render(new StyleSheetPackageHeaderItem(ComponentDraggablePage.class));
	}
}
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
<wicket:head>
	<title>Wicket jQuery UI: draggable component</title>
</wicket:head>
</head>
<body>
<wicket:extend>
	<div id="demo-panel">
		<div wicket:id="draggable" class="ui-widget-content">
			<p>Drag me around</p>
		</div>
		<br/>
		<div wicket:id="feedback" style="width: 360px;"></div>
	</div>
</wicket:extend>
</body>
</html>
.ui-draggable {
	width: 130px;
	height: 60px;
	padding: 0.5em;
	z-index: 99;
}