Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement stateless use of select2 choices #45

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ <h1>Multi-Select <small>Ajax result loading. Paging with infinite scrolling. Min
</div>
</div>
</section>

<section>
<div class="page-header">
<h1>Multi-Select <small>Ajax result loading (stateless). Paging with infinite scrolling. Minimum input length. Drag &amp; Drop</small></h1>
</div>
<div class="row">
<div class="span12">
<p>
Countries currently selected: <strong><span
wicket:id="countriesStateless"></span> </strong>
</p>
<form wicket:id="multiStateless">
<input wicket:id="countriesStateless"
type="hidden" style="width: 300px;" />
<div class="form-actions">
<button type="submit" class="btn">Submit</button>
</div>
</form>
</div>
</div>
</section>

</div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

/**
* Example page.
Expand All @@ -35,6 +34,9 @@ public class HomePage extends WebPage {

private Country country = Country.US;
private List<Country> countries = new ArrayList<Country>(Arrays.asList(new Country[] { Country.US, Country.CA }));

private List<Country> countriesStateless = new ArrayList<Country>(Arrays.asList(new Country[] { Country.US, Country.CA }));


public HomePage() {

Expand All @@ -60,9 +62,21 @@ public HomePage() {
Select2MultiChoice<Country> countries = new Select2MultiChoice<Country>("countries",
new PropertyModel<Collection<Country>>(this, "countries"), new CountriesProvider());
countries.getSettings().setMinimumInputLength(1);
countries.add(new DragAndDropBehavior());
countries.add(new DragAndDropBehavior());
multi.add(countries);


add(new Label("countriesStateless", new PropertyModel<Object>(this, "countriesStateless")));

Form<?> multiStateless = new Form<Void>("multiStateless");
add(multiStateless);

Select2MultiChoice<Country> countriesStateless = new Select2MultiChoice<Country>("countriesStateless",
new PropertyModel<Collection<Country>>(this, "countriesStateless"), new CountriesProvider());
countriesStateless.getSettings().setMinimumInputLength(1);
countriesStateless.getSettings().setStateless(true);
countriesStateless.getSettings().setMountPath(WicketApplication.COUNTRIES_MOUNT_PATH);
multiStateless.add(countriesStateless);
}

/**
Expand Down Expand Up @@ -108,7 +122,7 @@ private static List<Country> queryMatches(String term, int page, int pageSize) {
* @author igor
*
*/
public class CountriesProvider extends TextChoiceProvider<Country> {
public static class CountriesProvider extends TextChoiceProvider<Country> {

@Override
protected String getDisplayText(Country choice) {
Expand Down Expand Up @@ -137,4 +151,12 @@ public Collection<Country> toChoices(Collection<String> ids) {
}

}

public List<Country> getCountriesStateless() {
return countriesStateless;
}

public void setCountriesStateless(List<Country> countriesStateless) {
this.countriesStateless = countriesStateless;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,26 @@
* Application object
*/
public class WicketApplication extends WebApplication {

public static final String COUNTRIES_MOUNT_PATH = "/countries/";

@Override
public Class<HomePage> getHomePage() {
return HomePage.class;
}

@Override
protected void init() {
super.init();

mountResource(COUNTRIES_MOUNT_PATH, new JsonResourceReference<Country>() {

private static final long serialVersionUID = 1L;

@Override
protected ChoiceProvider<Country> getChoiceProvider() {
return new HomePage.CountriesProvider();
}
});
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.vaynberg.wicket.select2;

import junit.framework.Assert;

import org.apache.wicket.util.tester.WicketTester;
import org.junit.Before;
import org.junit.Test;

/**
* A bogus test to keep bamboo happy since its looking for test output
*
* @author igor
*
*/
public class SelectTest {

private WicketTester tester;

@Before
public void bogus() {
tester = new WicketTester();
}

@Test
public void testMultiSelect() {
TestMultiSelectPage testPage =tester.startPage(TestMultiSelectPage.class);
tester.assertRenderedPage(TestMultiSelectPage.class);
tester.getRequest().setParameter("countries", Country.CU.name()+","+Country.CA.name());
tester.submitForm(testPage.getForm());
Assert.assertTrue(testPage.getCountries().contains(Country.CU));
Assert.assertTrue(testPage.getCountries().contains(Country.CA));
Assert.assertTrue(!testPage.getCountries().contains(Country.US));

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
<meta charset="utf-8" />
<title>Apache Wicket Select2 Examples</title>
<wicket:link>
<link rel="stylesheet" href="bootstrap/css/bootstrap.css"
type="text/css" media="screen" />
<link rel="stylesheet" href="bootstrap/css/bootstrap-responsive.css"
type="text/css" media="screen" />
</wicket:link>

</head>
<body>

<a href="https://github.com/ivaynberg/wicket-select2"><img style="position: absolute; top: 0; right: 0; border: 0; z-index: 2000" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"></a>
<div class="container">
<section>
<div class="page-header">
<h1>Single-Select <small>Ajax result loading. Paging with infinite scrolling. Minimum input length</small></h1>
</div>
<div class="row">
<div class="span12">
<form wicket:id="multi">
<input wicket:id="countries" type="hidden"
style="width: 300px;" />
<div class="form-actions">
<button type="submit" class="btn">Submit</button>
</div>
</form>
</div>
</div>
</section>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright 2012 Igor Vaynberg
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with
* the License. You may obtain a copy of the License in the LICENSE file, or 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.vaynberg.wicket.select2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.PropertyModel;

/**
* Example page.
*
* @author igor
*
*/
@SuppressWarnings("unused")
public class TestMultiSelectPage extends WebPage {

private static final long serialVersionUID = 1L;

private static final int PAGE_SIZE = 20;

private Country country = Country.US;
private List<Country> countries = new ArrayList<Country>(Arrays.asList(new Country[] { Country.US, Country.CA }));

private Form<?> form;

private Select2MultiChoice<Country> countriesChoice;

public TestMultiSelectPage() {

// single-select example


form = new Form<Void>("multi");
add(form);

countriesChoice = new Select2MultiChoice<Country>("countries",
new PropertyModel<Collection<Country>>(this, "countries"), new CountriesProvider());
countriesChoice.getSettings().setMinimumInputLength(1);
countriesChoice.add(new DragAndDropBehavior());
form.add(countriesChoice);

}

/**
* Queries {@code pageSize} worth of countries from the {@link Country} enum, starting with {@code page * pageSize}
* offset. Countries are matched on their {@code displayName} containing {@code term}
*
* @param term
* search term
* @param page
* starting page
* @param pageSize
* items per page
* @return list of matches
*/
private static List<Country> queryMatches(String term, int page, int pageSize) {

List<Country> result = new ArrayList<Country>();

term = term.toUpperCase();

final int offset = page * pageSize;

int matched = 0;
for (Country country : Country.values()) {
if (result.size() == pageSize) {
break;
}

if (country.getDisplayName().toUpperCase().contains(term)) {
matched++;
if (matched > offset) {
result.add(country);
}
}
}
return result;
}

/**
* {@link Country} based choice provider for Select2 components. Demonstrates integration between Select2 components
* and a domain object (in this case an enum).
*
* @author igor
*
*/
public static class CountriesProvider extends TextChoiceProvider<Country> {

@Override
protected String getDisplayText(Country choice) {
return choice.getDisplayName();
}

@Override
protected Object getId(Country choice) {
return choice.name();
}

@Override
public void query(String term, int page, Response<Country> response) {
response.addAll(queryMatches(term, page, 10));
response.setHasMore(response.size() == 10);

}

@Override
public Collection<Country> toChoices(Collection<String> ids) {
ArrayList<Country> countries = new ArrayList<Country>();
for (String id : ids) {
countries.add(Country.valueOf(id));
}
return countries;
}

}

public Country getCountry() {
return country;
}

public void setCountry(Country country) {
this.country = country;
}

public List<Country> getCountries() {
return countries;
}

public void setCountries(List<Country> countries) {
this.countries = countries;
}

public Form<?> getForm() {
return form;
}

public void setForm(Form<?> form) {
this.form = form;
}

public Select2MultiChoice<Country> getCountriesChoice() {
return countriesChoice;
}

public void setCountriesChoice(Select2MultiChoice<Country> countriesChoice) {
this.countriesChoice = countriesChoice;
}

}
Loading