Example #1
0
  private void setDefaultText() {

    String dataPlaceHolder = selectElement.getAttribute("data-placeholder");

    if (dataPlaceHolder != null && dataPlaceHolder.length() > 0) {
      defaultText = dataPlaceHolder;
    } else if (isMultiple) {
      if (options.getPlaceholderTextMultiple() != null) {
        defaultText = options.getPlaceholderTextMultiple();
      } else if (options.getPlaceholderText() != null) {
        defaultText = options.getPlaceholderText();
      } else {
        defaultText = "Select Some Options";
      }
    } else {
      if (options.getPlaceholderTextSingle() != null) {
        defaultText = options.getPlaceholderTextSingle();
      } else if (options.getPlaceholderText() != null) {
        defaultText = options.getPlaceholderText();
      } else {
        defaultText = "Select an Option";
      }
    }

    String dataNoResultsText = selectElement.getAttribute("data-no_results_text");
    if (dataNoResultsText != null && dataNoResultsText.length() > 0) {
      resultsNoneFound = dataNoResultsText;
    } else if (options.getNoResultsText() != null) {
      resultsNoneFound = options.getNoResultsText();
    } else {
      resultsNoneFound = "No results match";
    }
  }
 @Override
 public void onBrowserEvent(
     Context context,
     Element parent,
     String value,
     NativeEvent event,
     ValueUpdater<String> valueUpdater) {
   super.onBrowserEvent(context, parent, value, event, valueUpdater);
   String type = event.getType();
   if ("change".equals(type)) {
     HasOptions key = (HasOptions) context.getKey();
     SelectElement select = parent.getFirstChild().cast();
     String newValue = null;
     if (key.hasOptions()) {
       List<String> options = key.getOptions();
       int optionsPreambleSize =
           key.getOptionsPreamble() == null ? 0 : key.getOptionsPreamble().size();
       int selectedOptionIndex = select.getSelectedIndex() - optionsPreambleSize;
       if (selectedOptionIndex >= 0) newValue = options.get(selectedOptionIndex);
     } else newValue = key.getNoOptionsString();
     setViewData(key, newValue);
     finishEditing(parent, newValue, key, valueUpdater);
     if (valueUpdater != null) {
       valueUpdater.update(newValue);
     }
   }
 }
Example #3
0
 public void addItemGroup(ItemGroup itemGroup) {
   SelectElement select = getSelectElement();
   OptGroupElement optGroup = createOptGroupElement(itemGroup.getItem(), itemGroup.getClassName());
   for (Item item : itemGroup.getItems()) {
     optGroup.appendChild(createOption(item));
   }
   select.appendChild(optGroup);
 }
Example #4
0
 private void insertItem(String item, Direction dir, String value, String optionStyle) {
   SelectElement select = getSelectElement();
   OptionElement option = createOption(item, dir, value);
   if (optionStyle != null) {
     option.setClassName(optionStyle);
   }
   select.appendChild(option);
 }
Example #5
0
  private String buildContainerId() {
    String id = null;
    String selectElementId = selectElement.getId();

    if (selectElementId != null && selectElementId.length() > 0) {
      id = containerIdRegExp.replace(selectElementId, "_");
    } else {
      id = generateContainerId();
      selectElement.setId(id);
    }

    id += "_chzn";

    return id;
  }
Example #6
0
  /**
   * Creates an <code>&lt;option&gt;</code> element and inserts it as a child of the specified
   * <code>&lt;select&gt;</code> element. If the index is less than zero, or greater than or equal
   * to the length of the list, then the option element will be appended to the end of the list.
   *
   * @param selectElem the <code>&lt;select&gt;</code> element
   * @param item the text of the new item; cannot be <code>null</code>
   * @param value the <code>value</code> attribute for the new <code>&lt;option&gt;</code>; cannot
   *     be <code>null</code>
   * @param index the index at which to insert the child
   */
  public static void insertListItem(Element selectElem, String item, String value, int index) {
    assert !PotentialElement.isPotential(selectElem) : "Cannot insert into a PotentialElement";

    SelectElement select = selectElem.<SelectElement>cast();
    OptionElement option = Document.get().createOptionElement();
    option.setText(item);
    option.setValue(value);

    if ((index == -1) || (index == select.getLength())) {
      select.add(option, null);
    } else {
      OptionElement before = select.getOptions().getItem(index);
      select.add(option, before);
    }
  }
Example #7
0
  private void setDefaultValues() {
    clickTestAction =
        new Function() {
          @Override
          public boolean f(Event e) {
            return testActiveClick(e);
          }
        };

    activateAction =
        new Function() {
          @Override
          public boolean f(Event e) {
            return activateField(e);
          }
        };

    activeField = false;
    mouseOnContainer = false;
    resultsShowing = false;

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

    choices = 0;

    css = Resources.INSTANCE.css();
  }
Example #8
0
  private void resultSelect(Event e) {
    if (resultHighlight != null) {
      GQuery high = resultHighlight;
      String highId = high.attr("id");

      resultClearHighlight();

      if (isMultiple) {
        resultDeactivate(high);
      } else {
        searchResults.find("." + css.resultSelected()).removeClass(css.resultSelected());
        resultSingleSelected = high;
        selectedItem.removeClass(css.chznDefault());
      }

      high.addClass(css.resultSelected());

      int position = Integer.parseInt(highId.substring(highId.lastIndexOf("_") + 1));
      OptionItem item = (OptionItem) selectItems.get(position);
      item.setSelected(true);
      selectElement.getOptions().getItem(item.getOptionsIndex()).setSelected(true);

      if (isMultiple) {
        choiceBuild(item);
      } else {
        selectedItem.find("span").text(item.getText());
        if (allowSingleDeselect) {
          singleDeselectControlBuild();
        }
      }

      if (!e.getMetaKey() || !isMultiple) {
        resultsHide();
      }

      searchField.val("");

      if (isMultiple || currentValue == null || !currentValue.equals($selectElement.val())) {
        String value = selectElement.getOptions().getItem(item.getOptionsIndex()).getValue();
        fireEvent(new ChosenChangeEvent(value, this));
      }

      currentValue = $selectElement.val();

      searchFieldScale();
    }
  }
Example #9
0
  private void resultsBuild() {

    selectItems = new SelectParser().parse(selectElement);

    if (isMultiple && choices > 0) {
      searchChoices.find("li." + css.searchChoice()).remove();
      choices = 0;
    } else if (!isMultiple) {
      selectedItem.addClass(css.chznDefault()).find("span").text(defaultText);

      if (selectElement.getOptions().getLength() <= options.getDisableSearchThreshold()) {
        container.addClass(css.chznContainerSingleNoSearch());
      } else {
        container.removeClass(css.chznContainerSingleNoSearch());
      }
    }

    SafeHtmlBuilder content = new SafeHtmlBuilder();

    for (int i = 0; i < selectItems.length(); i++) {
      SelectItem item = selectItems.get(i);

      if (item.isGroup()) {
        SafeHtml result = resultAddGroup((GroupItem) item);
        if (result != null) {
          content.append(result);
        }
      } else {
        OptionItem optionItem = (OptionItem) item;

        if (optionItem.isEmpty()) {
          continue;
        }

        SafeHtml optionHtml = resultAddOption(optionItem);
        if (optionHtml != null) {
          content.append(optionHtml);
        }

        if (optionItem.isSelected() && isMultiple) {
          choiceBuild(optionItem);
        } else if (optionItem.isSelected() && !isMultiple) {
          selectedItem.removeClass(css.chznDefault()).find("span").text(optionItem.getText());
          if (allowSingleDeselect) {
            singleDeselectControlBuild();
          }
        }
      }
    }
    searchFieldDisabled();
    showSearchFieldDefault();
    searchFieldScale();

    searchResults.html(content.toSafeHtml().asString());
  }
 @Override
 public void onBrowserEvent(
     final Context context,
     final Element parent,
     final String value,
     final NativeEvent event,
     final ValueUpdater<String> valueUpdater) {
   super.onBrowserEvent(context, parent, value, event, valueUpdater);
   final String type = event.getType();
   if ("change".equals(type)) {
     final Object key = context.getKey();
     final SelectElement select = parent.getFirstChild().cast();
     final String newValue = options.get(select.getSelectedIndex());
     setViewData(key, newValue);
     finishEditing(parent, newValue, key, valueUpdater);
     if (valueUpdater != null) {
       valueUpdater.update(newValue);
     }
   }
 }
Example #11
0
  private void searchFieldDisabled() {
    isDisabled = selectElement.isDisabled();
    if (isDisabled) {
      container.addClass(css.chznDisabled());
      InputElement.as(searchField.get(0)).setDisabled(true);
      if (!isMultiple) {
        selectedItem.unbind("focus", activateAction);
      }

    } else {
      container.removeClass(css.chznDisabled());
      InputElement.as(searchField.get(0)).setDisabled(false);
      if (!isMultiple) {
        selectedItem.bind("focus", activateAction);
      }
    }
  }
Example #12
0
  private void resultsReset(Event e) {
    OptionElement firstoption = selectElement.getOptions().getItem(0);
    if (firstoption != null) {
      firstoption.setSelected(true);
    }

    selectedItem.find("span").text(defaultText);
    if (!isMultiple) {
      selectedItem.addClass(css.chznDefault());
    }

    showSearchFieldDefault();
    resultsResetCleanup();

    fireEvent(new ChosenChangeEvent(null, this));
    if (activeField) {
      resultsHide();
    }
  }
Example #13
0
  protected void init(SelectElement element, ChosenOptions options, EventBus eventBus) {
    this.selectElement = element;
    this.options = options;
    this.eventBus = eventBus;

    this.$selectElement = $(selectElement);

    setDefaultValues();

    this.isMultiple = selectElement.isMultiple();

    setDefaultText();

    setup();

    bind();

    finishSetup();
  }
Example #14
0
  private void resultDeselect(int index) {
    OptionItem item = (OptionItem) selectItems.get(index);

    item.setSelected(false);

    // select option in original element
    OptionElement option = selectElement.getOptions().getItem(item.getOptionsIndex());
    option.setSelected(false);

    $("#" + containerId + "_o_" + index)
        .removeClass(css.resultSelected())
        .addClass(css.activeResult())
        .show();

    resultClearHighlight();
    winnowResults();

    fireEvent(new ChosenChangeEvent(option.getValue(), false, this));

    searchFieldScale();
  }
Example #15
0
 /**
  * This constructor may be used by subclasses to explicitly use an existing element. This element
  * must be a &lt;select&gt; element.
  *
  * @param element the element to be used
  */
 protected ListBox(Element element) {
   super(element);
   SelectElement.as(element);
 }