Skip to content

Commit

Permalink
Merge pull request #294 from ArcBees/mmc_fix_285
Browse files Browse the repository at this point in the history
Fix for #285 : ChosenValueListBox allowSingleDeselect not working as expected
  • Loading branch information
Olivier Lafleur committed Mar 3, 2016
2 parents e2f5515 + 2a2436e commit 28b7157
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
26 changes: 16 additions & 10 deletions plugin/src/main/java/com/arcbees/chosen/client/ChosenImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public abstract class ChosenImpl {
protected GQuery searchContainer;
protected String defaultText;
private GQuery $selectElement;
private boolean allowSingleDeselect;
private GQuery container;
private String containerId;
private ChosenCss css;
Expand Down Expand Up @@ -594,9 +593,7 @@ protected void fireChosenChangeEventIfNotEqual(OptionItem item, String newValue,

protected void addChoice(OptionItem item) {
selectedItem.find("span").text(item.getText());
if (allowSingleDeselect) {
singleDeselectControlBuild();
}
singleDeselectControlBuild();

selectedValues.clear();
}
Expand Down Expand Up @@ -859,6 +856,13 @@ private boolean containerMouseUp(Event e) {
return true;
}

private SafeHtml createEmptyOption() {
SafeHtmlBuilder builder = new SafeHtmlBuilder();
builder.append(fromTrustedString("<option value=''></option>"));

return builder.toSafeHtml();
}

private SafeHtml createOption(OptionItem item) {
SafeHtmlBuilder builder = new SafeHtmlBuilder();
builder.append(fromTrustedString("<option value='")).appendEscaped(item.getValue())
Expand Down Expand Up @@ -1086,6 +1090,7 @@ private void rebuildResultItems(boolean init) {
OptionItem optionItem = (OptionItem) item;

if (optionItem.isEmpty()) {
optionsHtml.append(createEmptyOption());
continue;
}

Expand Down Expand Up @@ -1283,11 +1288,6 @@ public boolean f(Event e) {
mouseOnContainer = false;
resultsShowing = false;

NodeList<OptionElement> optionsList = selectElement.getOptions();
allowSingleDeselect =
options.isAllowSingleDeselect() && optionsList.getLength() > 0
&& "".equals(optionsList.getItem(0).getText());

choices = 0;

if (options.getResources() != null) {
Expand All @@ -1312,6 +1312,12 @@ public boolean f(Event e) {
}
}

private boolean isAllowSingleDeselect() {
NodeList<OptionElement> optionsList = selectElement.getOptions();
return options.isAllowSingleDeselect() && optionsList.getLength() > 0
&& "".equals(optionsList.getItem(0).getText());
}

private void setTabIndex() {
String tabIndexProperty = $selectElement.attr(TABINDEX_PROPERTY);
if (tabIndexProperty != null && tabIndexProperty.length() > 0) {
Expand Down Expand Up @@ -1385,7 +1391,7 @@ private void setup() {
}

private void singleDeselectControlBuild() {
if (allowSingleDeselect && selectedItem.find("abbr").isEmpty()) {
if (isAllowSingleDeselect() && selectedItem.find("abbr").isEmpty()) {
selectedItem.find("span").first().after(
"<abbr class=\"" + css.searchChoiceClose() + " " + css.iconCross() + "\"></abbr>");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,17 @@ public void forceRedraw() {
}

public GQuery getChosenElement() {
ChosenImpl impl = $(getElement()).data(CHOSEN_DATA_KEY,
ChosenImpl.class);
ChosenImpl impl = getChosenImpl();
if (impl != null) {
return impl.getContainer();
}
return $();
}

protected ChosenImpl getChosenImpl() {
return $(getElement()).data(CHOSEN_DATA_KEY, ChosenImpl.class);
}

public int getDisableSearchThreshold() {
return options.getDisableSearchThreshold();
}
Expand Down Expand Up @@ -405,7 +408,7 @@ public String getValue() {
* @return the values of all selected options in an array
*/
public String[] getValues() {
ChosenImpl impl = $(getElement()).data(CHOSEN_DATA_KEY, ChosenImpl.class);
ChosenImpl impl = getChosenImpl();

if (impl != null) {
List<String> selectedValues = impl.getSelectedValues();
Expand Down

0 comments on commit 28b7157

Please sign in to comment.