Fix select2 library dependency checks
- Added safety checks for select2 library availability - Prevents TypeError when select2 library not loaded - Falls back to regular input behavior when select2 unavailable - Fixed getSeparator function to handle missing select2 defaults
This commit is contained in:
23
src/inputs/select2/select2.js
vendored
23
src/inputs/select2/select2.js
vendored
@@ -147,6 +147,12 @@ $(function(){
|
||||
render: function() {
|
||||
this.setClass();
|
||||
|
||||
// Check if Select2 is available
|
||||
if (typeof $.fn.select2 === 'undefined') {
|
||||
this.error = 'Select2 library is not included. Please include Select2 CSS and JS files.';
|
||||
return;
|
||||
}
|
||||
|
||||
//can not apply select2 here as it calls initSelection
|
||||
//over input that does not have correct value yet.
|
||||
//apply select2 only in value2input
|
||||
@@ -209,6 +215,12 @@ $(function(){
|
||||
value = value.join(this.getSeparator());
|
||||
}
|
||||
|
||||
// Check if Select2 is available before using it
|
||||
if (typeof $.fn.select2 === 'undefined') {
|
||||
this.$input.val(value);
|
||||
return;
|
||||
}
|
||||
|
||||
//for remote source just set value, text is updated by initSelection
|
||||
if(!this.$input.data('select2')) {
|
||||
this.$input.val(value);
|
||||
@@ -235,13 +247,18 @@ $(function(){
|
||||
var $el = $(this.options.scope);
|
||||
if (!$el.data('editable').isEmpty) {
|
||||
var data = {id: value, text: $el.text()};
|
||||
this.$input.select2('data', data);
|
||||
if (typeof $.fn.select2 !== 'undefined' && this.$input.data('select2')) {
|
||||
this.$input.select2('data', data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
input2value: function() {
|
||||
if (typeof $.fn.select2 === 'undefined' || !this.$input.data('select2')) {
|
||||
return this.$input.val();
|
||||
}
|
||||
return this.$input.select2('val');
|
||||
},
|
||||
|
||||
@@ -274,7 +291,7 @@ $(function(){
|
||||
},
|
||||
|
||||
getSeparator: function() {
|
||||
return this.options.select2.separator || $.fn.select2.defaults.separator;
|
||||
return this.options.select2.separator || ($.fn.select2 && $.fn.select2.defaults ? $.fn.select2.defaults.separator : ',');
|
||||
},
|
||||
|
||||
/*
|
||||
@@ -294,7 +311,7 @@ $(function(){
|
||||
},
|
||||
|
||||
destroy: function() {
|
||||
if(this.$input) {
|
||||
if(this.$input && typeof $.fn.select2 !== 'undefined') {
|
||||
if(this.$input.data('select2')) {
|
||||
this.$input.select2('destroy');
|
||||
}
|
||||
|
Reference in New Issue
Block a user