Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions wicketstuff-jquery-ui-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<name>Martin Grigorov</name>
<email>mgrigorov@apache.org</email>
</developer>
<developer>
<id>reiern70</id>
<name>Ernesto Reinaldo Barreiro</name>
<email>reiern70@apache.org</email>
</developer>
</developers>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ public void renderHead(Component component, IHeaderResponse response)
* @return the {@link JQueryTemplateResourceStream}
*/
protected abstract JQueryTemplateResourceStream newResourceStream();

/**
* @return The JavaScript code rendering the template.
*/
public String getTemplateRenderingCode()
{
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.wicketstuff.jquery.core.renderer.ITextRenderer;
import org.wicketstuff.jquery.core.renderer.TextRenderer;
import org.wicketstuff.jquery.core.template.IJQueryTemplate;
import org.wicketstuff.jquery.core.template.JQueryAbstractTemplateBehavior;
import org.wicketstuff.jquery.core.utils.RequestCycleUtils;
import org.wicketstuff.jquery.ui.template.JQueryTemplateBehavior;

Expand All @@ -52,7 +53,7 @@ public abstract class AutoCompleteTextField<T extends Serializable> extends Text
private final IConverter<T> converter;

private final IJQueryTemplate template;
private JQueryTemplateBehavior templateBehavior = null;
private JQueryAbstractTemplateBehavior templateBehavior = null;

/**
* Cache of current choices, needed to retrieve the user selected object
Expand Down Expand Up @@ -246,11 +247,22 @@ protected void onInitialize()

if (this.template != null)
{
this.templateBehavior = new JQueryTemplateBehavior(this.template);
this.templateBehavior = createTemplateBehavior(this.template);
this.add(this.templateBehavior);
}
}

/**
* Factory method for {@link JQueryAbstractTemplateBehavior}
*
* @param template {@link IJQueryTemplate}
* @return
*/
protected JQueryAbstractTemplateBehavior createTemplateBehavior(IJQueryTemplate template)
{
return new JQueryTemplateBehavior(this.template);
}

@Override
public void onConfigure(JQueryBehavior behavior)
{
Expand Down Expand Up @@ -313,7 +325,7 @@ protected CharSequence getChoiceCallbackUrl()
{
// warning, the template text should be of the form <a>...</a> in order to work
String render = "jQuery('%s').data('ui-autocomplete')._renderItem = function( ul, item ) { " // lf
+ "var content = jQuery.tmpl(jQuery('#%s').html(), item);" // lf
+ "var content = " + templateBehavior.getTemplateRenderingCode() // lf
+ "return jQuery('<li/>').data('ui-autocomplete-item', item).append(content).appendTo(ul);" // lf
+ "}";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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 org.wicketstuff.jquery.ui.template;

import org.apache.wicket.Component;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.markup.head.PriorityHeaderItem;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.resource.JQueryPluginResourceReference;
import org.wicketstuff.jquery.core.template.IJQueryTemplate;
import org.wicketstuff.jquery.core.template.JQueryAbstractTemplateBehavior;
import org.wicketstuff.jquery.core.template.JQueryTemplateResourceStream;

/**
* Templates machinery based on <a href="https://github.com/BorisMoore/jsrender">jsrender</a>
*
* @author reiern70
*/
public class JQueryJsRenderTemplateBehavior extends JQueryAbstractTemplateBehavior
{
private static final long serialVersionUID = 1L;
public static final PackageResourceReference TMPL_JS = new JQueryPluginResourceReference(JQueryJsRenderTemplateBehavior.class, "jsrender.min.js");

private String token = null;
private final IJQueryTemplate template;

/**
* Constructor
*
* @param template the {@link IJQueryTemplate} that this behavior should render via the resource stream
*/
public JQueryJsRenderTemplateBehavior(IJQueryTemplate template)
{
super();

this.template = template;
}

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

response.render(new PriorityHeaderItem(JavaScriptHeaderItem.forReference(JQueryJsRenderTemplateBehavior.TMPL_JS)));
}

// Methods //

@Override
public void bind(Component component)
{
super.bind(component);

this.token = String.format("%s-template", component.getMarkupId());
}

// Properties //

@Override
public String getToken()
{
return this.token;
}

// Factories //

@Override
protected JQueryTemplateResourceStream newResourceStream()
{
return new JQueryTemplateResourceStream(this.template.getText(), this.getToken());
}

@Override
public String getTemplateRenderingCode()
{
return "jQuery.templates(jQuery('#%s').html()).render(item);";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@ protected JQueryTemplateResourceStream newResourceStream()
{
return new JQueryTemplateResourceStream(this.template.getText(), this.getToken());
}

@Override
public String getTemplateRenderingCode()
{
return "jQuery.tmpl(jQuery('#%s').html(), item);";
}
}

Large diffs are not rendered by default.