websemantics / Image-Select

Image Select is an extension of Chosen, a jQuery plugin that makes long, unwieldy select boxes much more user-friendly. It provides image support for Single and Multi select HTML tags.
http://websemantics.github.io/Image-Select
MIT License
147 stars 44 forks source link

Doesn't work with chosen('destroy') #13

Closed dertuxmalwieder closed 9 years ago

dertuxmalwieder commented 9 years ago

chosen('destroy') seems to trigger chosen:hiding_dropdown which causes imageSelect to run in an error.

While I don't quite understand what the line span.find('img').remove() - does it lack a semicolon, maybe? - is intended to do, here's my quick fix:

                $this.on("chosen:hiding_dropdown", function(e, _c){

                chosen = $this.data('chosen');
                if (chosen) {
                    var options  = chosen.form_field.options;

                    var selected = $(chosen.form_field).find(':selected');

                    if(!selected) return;

                    var img_src  = selected.attr('data-img-src');

                    if(!img_src) return;

                    var template = html_template.replace('{url}',img_src);

                    // For multiple selection
                    span = $(chosen.container).find('.chosen-choices span').last()
                    span.find('img').remove()
                    span.prepend(template.replace('{class_name}','chose-image'));

                    // For single select
                    span = $(chosen.container).find('.chosen-single span')
                    span.find('img').remove()
                    span.prepend(template.replace('{class_name}','chose-image-small'));
                }
            })