How extend select2 adaptor with options checking?

Viewed 12

I want to extend select2 default dropdown adapter. Find this - https://bojanv91.github.io/posts/2017/10/extending-select2-with-adapters-and-decorators - and try to write the same. But problem is this example just include AttachBody, AttachContainer adapters without check the options unlike select2 defaults - https://github.com/select2/select2/blob/develop/src/js/select2/defaults.js. I write the same but stuck into options check:

$.fn.select2.amd.define( 'customDropdownAdapter', [
    'select2/utils',

    'select2/dropdown',
    'select2/dropdown/search',
    'select2/dropdown/hidePlaceholder',
    'select2/dropdown/infiniteScroll',
    'select2/dropdown/attachBody',
    'select2/dropdown/minimumResultsForSearch',
    'select2/dropdown/selectOnClose',
    'select2/dropdown/closeOnSelect',
    'select2/dropdown/dropdownCss',
    'select2/dropdown/tagsSearchHighlight',

    // custom extension
    'select2/dropdown/newOption',
],
function(

        Utils,

        Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
        AttachBody, MinimumResultsForSearch, SelectOnClose, CloseOnSelect,
        DropdownCSS, TagsSearchHighlight,

        NewOption,

    ) {

    // *****************************************
    // *** THIS IS HOW SELECT2 DEFAULTS WORK ***
    // *****************************************
    //
    // options = $.extend(true, {}, this.defaults, options);

    // if (options.dropdownAdapter == null) {
    //     if (options.multiple) {
    //         options.dropdownAdapter = Dropdown;
    //     } else {
    //         var SearchableDropdown = Utils.Decorate(Dropdown, DropdownSearch);

    //         options.dropdownAdapter = SearchableDropdown;
    //     }

    //     if (options.minimumResultsForSearch !== 0) {
    //         options.dropdownAdapter = Utils.Decorate(
    //         options.dropdownAdapter,
    //         MinimumResultsForSearch
    //         );
    //     }

    //     ...
    // }
    //
    // *****************************************

    // *****************************************
    // *** HOW CAN I GET OPTIONS? ***
    // const options = ?
    // *****************************************

    let dropdownAdapter = Dropdown;

    // TO SET SELECT2 ADAPTERS
    if ( options.closeOnSelect ) {
        dropdownAdapter = Utils.Decorate(
            dropdownAdapter,
            CloseOnSelect,
        );
    };

    // TO SET MY CUSTOM ADAPTERS
    if ( options.newOption ) {
        dropdownAdapter = Utils.Decorate(
            dropdownAdapter,
            NewOption,
        );
    };

    return dropdownAdapter;
} );
0 Answers
Related