Skip to content

Commit

Permalink
JQueryBehavior: Added #detach(ART) and #remove(ART), may be useful so…
Browse files Browse the repository at this point in the history
…meday; wicketstuff#172

Kendo UI: Added KendoDestroyListener and Initializer; wicketstuff#172
Added InputWindow#newFeedbackPanel, having ContainerFeedbackMessageFilter
  • Loading branch information
Sebastien Briquet committed May 26, 2015
1 parent 37a3b35 commit d9dd12d
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 33 deletions.
Expand Up @@ -21,6 +21,7 @@
import java.util.Map.Entry;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.util.lang.Args;
Expand Down Expand Up @@ -72,7 +73,7 @@ public JQueryBehavior(String selector, String method)
public JQueryBehavior(String selector, String method, Options options)
{
super(method);

Args.notNull(options, "options");

this.method = method;
Expand Down Expand Up @@ -103,6 +104,33 @@ public void renderHead(Component component, IHeaderResponse response)
}
}

/**
* Removes the element matching the current selector from the DOM.<br/>
* The {@code #detach} method is the same as {@code #remove}, except that {@code #detach} keeps all jQuery data associated with the removed elements.<br/>
* This method is useful when removed elements are to be reinserted into the DOM at a later time.
*
* @param target the {@link AjaxRequestTarget}
* @see #remove(AjaxRequestTarget)
*/
public void detach(AjaxRequestTarget target)
{
target.prependJavaScript(String.format("jQuery('%s').detach();", this.selector));
}

/**
* Removes the element matching the current selector from the DOM.<br/>
* Use {@code #remove} when you want to remove the element itself, as well as everything inside it.<br/>
* In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.<br/>
* To remove the elements without removing data and events, use {@code #detach} instead.
*
* @param target the {@link AjaxRequestTarget}
* @see #detach(AjaxRequestTarget)
*/
public void remove(AjaxRequestTarget target)
{
target.prependJavaScript(String.format("jQuery('%s').remove();", this.selector));
}

// Properties //

/**
Expand Down
Expand Up @@ -34,7 +34,7 @@ public class RequestCycleUtils
/**
* Gets the current {@link AjaxRequestTarget}
*
* @return the target or <tt>null<tt> if not in an ajax request
* @return the target or {@code null} if not in an ajax request
*/
public static AjaxRequestTarget getAjaxRequestTarget()
{
Expand Down
Expand Up @@ -7,14 +7,12 @@
</head>
<body>
<wicket:extend>
<div wicket:id="container">
<div id="menu-target">
Right click here to display the context menu
</div>
<div wicket:id="menu"></div>
<br/>
<div wicket:id="feedback"></div>
</div>
<div id="menu-target">
Right click here to display the context menu
</div>
<div wicket:id="menu"></div>
<br/>
<div wicket:id="feedback"></div>

<wicket:fragment wicket:id="moveToPosition">
<form wicket:id="form">
Expand Down
Expand Up @@ -18,15 +18,12 @@ public class KendoContextMenuPage extends AbstractMenuPage

public KendoContextMenuPage()
{
final WebMarkupContainer container = new WebMarkupContainer("container");
this.add(container.setOutputMarkupId(true));

// FeedbackPanel //
final KendoFeedbackPanel feedback = new KendoFeedbackPanel("feedback");
container.add(feedback);
this.add(feedback);

// Context Menu //
container.add(new ContextMenu("menu", KendoMenuPage.newMenuItemList()) {
this.add(new ContextMenu("menu", KendoMenuPage.newMenuItemList()) {

private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -69,10 +66,8 @@ public void onClick(AjaxRequestTarget target, IMenuItem item)
this.info("Clicked " + item.getTitle().getObject());
}

// this.destroy(target);
// target.prependJavaScript(String.format("kendo.destroy('%s');", JQueryWidget.getSelector(this.getPage())));
target.add(container);
// target.add(feedback);
this.reload(target);
target.add(feedback);
}
});
}
Expand Down
Expand Up @@ -27,13 +27,13 @@
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.util.ListModel;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.util.lang.Args;

import com.googlecode.wicket.jquery.core.JQueryBehavior;
import com.googlecode.wicket.jquery.core.JQueryGenericContainer;
import com.googlecode.wicket.jquery.core.Options;
import com.googlecode.wicket.jquery.core.utils.ListUtils;
import com.googlecode.wicket.jquery.core.utils.RequestCycleUtils;

/**
* Provides a jQuery UI sortable {@link JQueryGenericContainer}.<br/>
Expand Down Expand Up @@ -118,7 +118,7 @@ public void onEvent(IEvent<?> event)
{
if (event.getSource() instanceof Sortable<?>)
{
AjaxRequestTarget target = RequestCycle.get().find(AjaxRequestTarget.class);
AjaxRequestTarget target = RequestCycleUtils.getAjaxRequestTarget();

if (target != null)
{
Expand Down
Expand Up @@ -28,25 +28,30 @@
import org.apache.wicket.util.visit.Visits;

/**
* TODO javadoc
*
* INTERNAL USE<br/>
* Provides an {@link IListener} for {@link KendoUIBehavior}{@code s} that destroys widgets about to be repainted.
*
* @author Sebastien Briquet - sebfz1
*/
public class KendoDestroyListener implements IListener
{
/**
* TODO javadoc
*
* Specifies that a widgets can be automatically destroyed
*/
public static interface IDestroyable
{
/**
* Prepares the widget for safe removal from the DOM. Detaches all event handlers and removes jQuery.data attributes to avoid memory leaks. Calls destroy method of any child Kendo widgets.
* Prepares the widget for safe removal from the DOM.<br/>
* Detaches all event handlers and removes jQuery.data attributes to avoid memory leaks.<br/>
* Calls destroy method of any child Kendo widgets.
*
* @param target the {@link AjaxRequestTarget}
*/
void destroy(AjaxRequestTarget target);
}

// Events //

@Override
public void onBeforeRespond(Map<String, Component> map, AjaxRequestTarget target)
{
Expand All @@ -61,9 +66,12 @@ public void onAfterRespond(Map<String, Component> map, IJavaScriptResponse respo
{
}

// Factories //

/**
* TODO javadoc
* Gets a new {@link IVisitor} that will be used by {@link #onBeforeRespond(Map, AjaxRequestTarget)}
*
* @return the new {@code IVisitor}
*/
protected IVisitor<Component, Object> newBeforeRespondVisitor(final AjaxRequestTarget target)
{
Expand Down
Expand Up @@ -158,8 +158,11 @@ protected String widget(String method)
@Override
public void destroy(AjaxRequestTarget target)
{
target.prependJavaScript(String.format("console.info('destroying %s (%s)...')", this.method, this.selector)); //TODO remove log
// TODO: remove log
target.prependJavaScript(String.format("console.info('destroying %s (%s)...')", this.method, this.selector));
target.prependJavaScript(this.widget() + ".destroy();");

this.onDestroy(target);
}

// Events //
Expand All @@ -174,4 +177,15 @@ public void onConfigure(Component component)
LOG.warn("Application > MarkupSettings > StripWicketTags: setting is currently set to false. It is highly recommended to set it to true to prevent widget misbehaviors.");
}
}

/**
* Called when the widget is about to be destroyed
*
* @param target the {@link AjaxRequestTarget}
* @see #destroy(AjaxRequestTarget)
*/
protected void onDestroy(AjaxRequestTarget target)
{
// noop
}
}
Expand Up @@ -103,11 +103,11 @@ public void onRequest()
// IFilterStateLocator //
if (this.provider instanceof IFilterStateLocator<?>)
{
@SuppressWarnings("unused")
String logicPattern = "filter[logic]";
String fieldPattern = "filter[filters][%d][field]";
String valuePattern = "filter[filters][%d][value]";

@SuppressWarnings("unused")
String logicPattern = "filter[logic]";
@SuppressWarnings("unused")
String operatorPattern = "filter[filters][%d][operator]";
// TODO: implement logic & operator (new IFilterStateLocator interface?)
Expand Down
Expand Up @@ -36,10 +36,8 @@
* @author Sebastien Briquet - sebfz1
* @since 6.15.0
*/

public abstract class MenuBehavior extends KendoUIBehavior implements IJQueryAjaxAware, IMenuListener
{

private static final long serialVersionUID = 1L;
public static final String METHOD = "kendoMenu";

Expand Down
Expand Up @@ -18,6 +18,8 @@

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.feedback.ContainerFeedbackMessageFilter;
import org.apache.wicket.feedback.FeedbackMessagesModel;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
Expand Down Expand Up @@ -101,7 +103,7 @@ public InputWindow(String id, IModel<String> title, IModel<T> model, IModel<Stri
this.add(this.form);

// feedback //
this.feedback = new KendoFeedbackPanel("feedback");
this.feedback = this.newFeedbackPanel("feedback");
this.form.add(this.feedback);
}

Expand Down Expand Up @@ -222,6 +224,28 @@ public boolean wantSubmitOnParentFormSubmit()
};
}

/**
* Gets a new {@link KendoFeedbackPanel}
*
* @return a new {@code KendoFeedbackPanel}
*/
protected KendoFeedbackPanel newFeedbackPanel(String id)
{
return new KendoFeedbackPanel(id) {

private static final long serialVersionUID = 1L;

@Override
protected FeedbackMessagesModel newFeedbackMessagesModel()
{
FeedbackMessagesModel model = new FeedbackMessagesModel(this);
model.setFilter(new ContainerFeedbackMessageFilter(InputWindow.this));

return model;
}
};
}

/**
* Gets a new {@link Component} that will be used as a label in the window.<br/>
* Override this method when you need to show formatted label.
Expand Down

0 comments on commit d9dd12d

Please sign in to comment.