-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GEOS-8571] Autocomplete support for dropdowns with long lists of values
- Loading branch information
Showing
12 changed files
with
268 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/web/core/src/main/java/org/geoserver/web/wicket/Select2DropDownChoice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,92 @@ | |||
/* (c) 2018 Open Source Geospatial Foundation - all rights reserved | |||
* This code is licensed under the GPL 2.0 license, available at the root | |||
* application directory. | |||
*/ | |||
package org.geoserver.web.wicket; | |||
|
|||
import org.apache.wicket.Component; | |||
import org.apache.wicket.behavior.Behavior; | |||
import org.apache.wicket.markup.head.IHeaderResponse; | |||
import org.apache.wicket.markup.head.JavaScriptHeaderItem; | |||
import org.apache.wicket.markup.head.OnLoadHeaderItem; | |||
import org.apache.wicket.markup.html.form.DropDownChoice; | |||
import org.apache.wicket.markup.html.form.IChoiceRenderer; | |||
import org.apache.wicket.model.IModel; | |||
import org.apache.wicket.request.resource.PackageResourceReference; | |||
import org.wicketstuff.select2.Select2Behavior; | |||
|
|||
import java.util.List; | |||
|
|||
/** | |||
* Auto-complete version of a {@link org.apache.wicket.markup.html.form.DropDownChoice} | |||
*/ | |||
public class Select2DropDownChoice<T> extends DropDownChoice<T> { | |||
|
|||
private static final PackageResourceReference SELECT2_KEYBOARD_JS = new PackageResourceReference( | |||
Select2DropDownChoice.class, "js/select2/select2-keyboard.js"); | |||
|
|||
|
|||
public Select2DropDownChoice(String id, IModel<T> model, IModel<List<T>> choices, IChoiceRenderer<T> renderer) { | |||
super(id, model, choices, renderer); | |||
initBehaviors(); | |||
} | |||
|
|||
public Select2DropDownChoice(String id, IModel<T> model, List<T> choices, IChoiceRenderer<T> renderer) { | |||
super(id, model, choices, renderer); | |||
initBehaviors(); | |||
} | |||
|
|||
public Select2DropDownChoice(String id, List<? extends T> choices) { | |||
super(id, choices); | |||
initBehaviors(); | |||
} | |||
|
|||
public Select2DropDownChoice(String id, List<? extends T> choices, IChoiceRenderer<? super T> renderer) { | |||
super(id, choices, renderer); | |||
initBehaviors(); | |||
} | |||
|
|||
public Select2DropDownChoice(String id, IModel<T> model, List<? extends T> choices) { | |||
super(id, model, choices); | |||
initBehaviors(); | |||
} | |||
|
|||
public Select2DropDownChoice(String id, IModel<? extends List<? extends T>> choices) { | |||
super(id, choices); | |||
initBehaviors(); | |||
} | |||
|
|||
public Select2DropDownChoice(String id, IModel<T> model, IModel<? extends List<? extends T>> choices) { | |||
super(id, model, choices); | |||
initBehaviors(); | |||
} | |||
|
|||
public Select2DropDownChoice(String id, IModel<? extends List<? extends T>> choices, IChoiceRenderer<? super T> | |||
renderer) { | |||
super(id, choices, renderer); | |||
initBehaviors(); | |||
} | |||
|
|||
private void initBehaviors() { | |||
add(new Select2Behavior()); | |||
add(new KeyboardBehavior()); | |||
} | |||
|
|||
|
|||
/** | |||
* Mimics keyboard behavior of native drop down choices | |||
*/ | |||
private static class KeyboardBehavior extends Behavior { | |||
|
|||
public KeyboardBehavior() { | |||
} | |||
|
|||
public void renderHead(Component component, IHeaderResponse response) { | |||
super.renderHead(component, response); | |||
response.render(JavaScriptHeaderItem.forReference(SELECT2_KEYBOARD_JS)); | |||
String enabler = "enableSelect2Keyboard('" + component.getMarkupId() + "');"; | |||
response.render(OnLoadHeaderItem.forScript(enabler)); | |||
} | |||
} | |||
|
|||
} |
105 changes: 105 additions & 0 deletions
105
src/web/core/src/main/java/org/geoserver/web/wicket/js/select2/select2-keyboard.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,105 @@ | |||
/* (c) 2018 Open Source Geospatial Foundation - all rights reserved | |||
* This code is licensed under the GPL 2.0 license, available at the root | |||
* application directory. | |||
*/ | |||
|
|||
function isCharacterKeyPress(evt) { | |||
if (typeof evt.which == "undefined") { | |||
// This is IE, which only fires keypress events for printable keys | |||
return true; | |||
} else if (typeof evt.which == "number" && evt.which > 0) { | |||
// In other browsers except old versions of WebKit, evt.which is | |||
// only greater than zero if the keypress is a printable key. | |||
// We need to filter out non printable chars and ctrl/alt/meta key combinations | |||
return !evt.ctrlKey && !evt.metaKey && !evt.altKey && evt.which > 32; | |||
} | |||
return false; | |||
} | |||
|
|||
function enableSelect2Keyboard(dropDownId) { | |||
var dropDown = $('#' + dropDownId); | |||
var s2Obj = dropDown.data('select2'); | |||
delete s2Obj.listeners.keypress; | |||
s2Obj.on('keypress', function (evt) { | |||
var key = evt.which; | |||
var KEYS = { | |||
BACKSPACE: 8, | |||
TAB: 9, | |||
ENTER: 13, | |||
SHIFT: 16, | |||
CTRL: 17, | |||
ALT: 18, | |||
ESC: 27, | |||
SPACE: 32, | |||
PAGE_UP: 33, | |||
PAGE_DOWN: 34, | |||
END: 35, | |||
HOME: 36, | |||
LEFT: 37, | |||
UP: 38, | |||
RIGHT: 39, | |||
DOWN: 40, | |||
DELETE: 46 | |||
}; | |||
if (this.isOpen()) { | |||
if (key === KEYS.ENTER) { | |||
this.trigger('results:select'); | |||
evt.preventDefault(); | |||
} else if ((key === KEYS.SPACE && evt.ctrlKey)) { | |||
this.trigger('results:toggle'); | |||
|
|||
evt.preventDefault(); | |||
} else if (key === KEYS.UP) { | |||
this.trigger('results:previous'); | |||
|
|||
evt.preventDefault(); | |||
} else if (key === KEYS.DOWN) { | |||
this.trigger('results:next'); | |||
|
|||
evt.preventDefault(); | |||
} else if (key === KEYS.ESC || key === KEYS.TAB) { | |||
this.close(); | |||
|
|||
evt.preventDefault(); | |||
} | |||
} else { | |||
if (key === KEYS.ENTER || key === KEYS.SPACE || | |||
((key === KEYS.DOWN || key === KEYS.UP) && evt.altKey)) { | |||
this.open(); | |||
|
|||
evt.preventDefault(); | |||
} else if (key === KEYS.DOWN) { | |||
var val = s2Obj.$element.find('option:selected').next().val(); | |||
if (undefined !== val) { | |||
s2Obj.$element.val(val); | |||
s2Obj.$element.trigger('change'); | |||
} | |||
evt.preventDefault(); | |||
} else if (key === KEYS.UP) { | |||
if (undefined != this.$element.find('option:selected').prev().val()) { | |||
this.$element.val(this.$element.find('option:selected').prev().val()); | |||
this.$element.trigger('change'); | |||
} | |||
evt.preventDefault(); | |||
} else if (isCharacterKeyPress(evt)) { | |||
// mimics typing in directly by matching the first letter typed (a browser collects | |||
// all typed chars and allows going beyond first letter) | |||
var option = this.$element.find('option').first(); | |||
var theChar = String.fromCharCode(key).toLowerCase() | |||
while (undefined != option.val() && option.text().toLowerCase().indexOf(theChar) !== 0) { | |||
option = option.next() | |||
} | |||
if (undefined != option.val()) { | |||
this.$element.val(option.val()); | |||
this.$element.trigger('change'); | |||
} | |||
evt.preventDefault(); | |||
} | |||
} | |||
} | |||
); | |||
|
|||
s2Obj.on('close', function () { | |||
this.$element.trigger('focus'); | |||
}); | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.