core icon indicating copy to clipboard operation
core copied to clipboard

[SELECT2] Allow initialization JS overrides

Open andruhon opened this issue 6 years ago • 9 comments

The JS initialization script was moved from renderHead to Select2Behavior which is burned into the AbstractSelect2Choice as a private final added in the constructor.

I would add a protected method, something like getSelect2Behavior which would return the select2Behavior, but will allow us to override it (maybe restrict to the Select2Behavior).

The JS inside could also be made customisable. Alternatively the string can be amended without overrides, though Select2Behavior settings.

Currently we have to do some hacking to get rid of default behavior, to add our slightly customized JS:

Optional<? extends Behavior> select2Behavour = getBehaviors().stream().filter(behaviour -> behaviour instanceof Select2Behavior).findFirst();
       if (select2Behavour.isPresent()) {
           remove(select2Behavour.get());
       }

andruhon avatar Jun 19 '19 04:06 andruhon

The functionality you are requesting is already implemented :)

You can override JS/CSS scripts Please take a look at ApplicationSettings class

:)

solomax avatar Jun 19 '19 04:06 solomax

Thank you @solomax ! The intent is not to replace the JS file reference, but to change this string: "$('#%s').select2(%s);"

andruhon avatar Jun 19 '19 04:06 andruhon

So you would like select2 drop-down will not be select2? Maybe you can provide sort of real-life-example?

solomax avatar Jun 19 '19 04:06 solomax

@solomax Kind of. Unfortunately some things are not available through options and it's needed to "override" the constructor like this:

    $.fn.select2.amd.define("select2/myMagicSelect2",
            ['select2/core'],
            function (Select2) {
                return function ($el, options) {
                    // my magic stuff
                    var select = new Select2($el, options);
                    // more magic stuff
                }
            }
    );

and later call something like

$.fn.select2.amd.require('select2/myMagicSelect2')($select, options);

So the $.fn.select2.amd.require('select2/myMagicSelect2')($('#%s'), %s) would replace $('#%s').select2(%s);

andruhon avatar Jun 19 '19 04:06 andruhon

I believe you can create PR :)

solomax avatar Jun 19 '19 04:06 solomax

I will

andruhon avatar Jun 19 '19 05:06 andruhon

@solomax is JIRA also needed?

andruhon avatar Jun 19 '19 05:06 andruhon

@andruhon no JIRA is required, this is not part of Apache Wicket :)

solomax avatar Jun 19 '19 05:06 solomax

@andruhon I believe the easiest way to implement this will be one more setting in ApplicationSettings class :)

solomax avatar Jun 20 '19 01:06 solomax