All Downloads are FREE. Search and download functionalities are using the official Maven repository.

META-INF.resources.butterfaces-js.butterfaces-combobox.jquery.js Maven / Gradle / Ivy

(function ($) {
    // extend jQuery --------------------------------------------------------------------
    $.fn.butterCombobox = function () {
        return this.each(function () {
            new FilterableCombobox(this);
        });
    };

    // define objects --------------------------------------------------------------------

    /**
     * @class
     */
    var FilterableComboboxMouseEventListener = Class.extend({
        /**
         * Is called when a mouse down event is triggered on a result list option. Intended to be overridden.
         * @param {String} value the value of the result list option
         */
        onResultItemMouseDown: function (value) {
        },

        /**
         * Is called when a mouse enter event is triggered on a result list option. Intended to be overridden.
         * @param {String} value the value of the result list option
         */
        onResultItemMouseEnter: function (value) {
        }
    });

    /**
     * @class
     */
    var FilterableComboboxOption = Class.extend({
        /**
         * @constructs
         * @param {HTMLElement} optionElement
         * @param {FilterableComboboxMouseEventListener} mouseEventListener
         * @param {jQuery} $template
         */
        init: function (optionElement, mouseEventListener, $template) {
            this._$optionElement = $(optionElement);
            this._mouseEventListener = mouseEventListener;
            this._$resultElement;
            this._initResultItem($template);
        },

        _initResultItem: function ($template) {
            var self = this;
            var resultItemHtml = self._$optionElement.text();
            if ($template !== undefined) {
                var dataFields = self._$optionElement.data();
                resultItemHtml = $template;
                $template.replace(/\{\{(.*?)\}\}/g, function (group0, group1) {
                    resultItemHtml = resultItemHtml.replace("{{" + group1 + "}}", dataFields[group1.toLowerCase()])
                    return;
                })
            }

            self._$resultElement = $("
  • ") .html(resultItemHtml) .addClass("butter-dropdownlist-resultItem"); if (self._mouseEventListener !== undefined) { self._$resultElement. on("mousedown", function () { self._mouseEventListener.onResultItemMouseDown(self.getValue()); }) .on("mouseenter", function () { self._mouseEventListener.onResultItemMouseEnter(self.getValue()); }); } }, /** * @returns {String} the option's value */ getValue: function () { return this._$optionElement.val(); }, /** * @returns {String} the option's label text. This is the text between the option tags. */ getLabel: function () { return this._$optionElement.text(); }, /** * @returns {jQuery} a jQuery extended HTML element */ getResultElement: function () { return this._$resultElement; } }); /** * @class */ var FilterableComboboxEmtpyOption = FilterableComboboxOption.extend({ /** * @constructs */ init: function () { this._$optionElement = $("
    ") .addClass("butter-dropdownlist-container") .addClass("butter-component-combobox-dropdownlist-container") .css({ position: "absolute", left: inputGroupOffset.left, top: inputGroupOffset.top + $inputGroup.outerHeight(), width: $inputGroup.innerWidth() }); self.$resultListContainer = $("
      ") .addClass("butter-dropdownlist-resultList") .on("mouseleave", function () { self._optionList.unselect(); }) .appendTo(self.$resultContainer); $("body").append(self.$resultContainer); } self._detachResultListElements(); self.$selectedOption = null; $.each(self._optionList.getFilteredOptions(searchText), function (index, element) { self.$resultListContainer.append(element.getResultElement()); }); }, _hideOptionResultList: function () { var self = this; if (self._isResultContainerShown()) { self._detachResultListElements(); self.$resultContainer.remove(); self.$resultContainer = null; self.$resultListContainer = null; this._optionList.unselect(); } }, _detachResultListElements: function () { this._optionList.unselect(); this.$resultListContainer.children().detach(); }, /** * @inheritdoc */ onResultItemMouseDown: function (value) { this._setSelectedValue(); }, /** * @inheritdoc */ onResultItemMouseEnter: function (value) { if (!this._isMouseEnterBlocked) { this._optionList.select(value); } }, _moveResultOptionElementSelectionCursor: function (direction) { var self = this; if (direction > 0) { self._optionList.selectNext(); } else { self._optionList.selectPrevious(); } self._isMouseEnterBlocked = true; self._scrollToSelectedOptionElementIfNecessary(); // this is necessary because if the mouse cursor is over a result item after the list has been scrolled a // mouse enter event will be triggered and the result item under the mouse cursor will be selected unintentionally. window.setTimeout(function () { self._isMouseEnterBlocked = false; }, 100); }, _setSelectedValue: function () { if (this._optionList.hasSelected()) { var selectOption = this._optionList.getSelected(); this.$select .val(selectOption.getValue()) .change(); this.$ghostInput.val(selectOption.getLabel()); this._hideOptionResultList(); } }, _resetDisplayValue: function () { // set old value this.$ghostInput.val(this.$select.find("option:selected").text()); }, _stopEvent: function (event) { event.stopPropagation(); event.preventDefault(); }, _selectCompleteTextInGhostInput: function () { this.$ghostInput[0].setSelectionRange(0, this.$ghostInput.val().length); }, _isResultContainerShown: function () { return this.$resultContainer !== null; }, _scrollToSelectedOptionElementIfNecessary: function () { if (this._optionList.hasSelected()) { var $item = this._optionList.getSelected().getResultElement(); var containerHeight = this.$resultContainer.innerHeight(); var containerScrollTop = this.$resultContainer.scrollTop(); //console.log("FilterableCombobox - containerHeight: %s", containerHeight); //console.log("FilterableCombobox - containerScrollTop: %s", containerScrollTop); var itemHeight = $item.outerHeight(); //console.log("FilterableCombobox - itemHeight: %s", itemHeight); var itemTop = $item.position().top; //console.log("FilterableCombobox - itemTop: %s", itemTop); if (itemTop < 0) { // resultItem is scrolled out to top this.$resultContainer.scrollTop(containerScrollTop + itemTop); } else if (itemTop + itemHeight > containerHeight) { // resultItem is scrolled out to bottom this.$resultContainer.scrollTop(containerScrollTop + ((itemTop + itemHeight) - containerHeight)); } } } }); }(jQuery));




  • © 2015 - 2025 Weber Informatics LLC | Privacy Policy