diff --git a/assets/src/js/functions/select.js b/assets/src/js/functions/select.js index 8e31ece9f..cf369ea4e 100755 --- a/assets/src/js/functions/select.js +++ b/assets/src/js/functions/select.js @@ -20,41 +20,25 @@ function start_superselect() { // Statico $('.superselect').each(function () { - $this = $(this); + let $this = $(this); + $(this).select2({ theme: "bootstrap", language: "it", width: '100%', maximumSelectionLength: $this.data('maximum') ? $this.data('maximum') : -1, minimumResultsForSearch: $this.hasClass('no-search') ? -1 : 0, - allowClear: $this.hasClass('no-search') ? false : true, - templateResult: function (data, container) { - var bg; // templateSelection - - if (data._bgcolor_) { - bg = data._bgcolor_; - } else if ($(data.element).attr("_bgcolor_")) { - bg = $(data.element).attr("_bgcolor_"); - } else if ($(data.element).data("_bgcolor_")) { - bg = $(data.element).data("_bgcolor_"); - } - - if (bg) { - $(container).css("background-color", bg); - $(container).css("color", setContrast(bg)); - } - - return data.text; - }, + allowClear: !$this.hasClass('no-search'), escapeMarkup: function (text) { return text; - } + }, + templateResult: selectBackground, }); }); // Dinamico (AJAX, per tabelle con molti record) $('.superselectajax').each(function () { - $this = $(this); + let $this = $(this); $(this).select2({ theme: "bootstrap", @@ -65,24 +49,7 @@ function start_superselect() { escapeMarkup: function (text) { return text; }, - templateResult: function (data, container) { - var bg; // templateSelection - - if (data._bgcolor_) { - bg = data._bgcolor_; - } else if ($(data.element).attr("_bgcolor_")) { - bg = $(data.element).attr("_bgcolor_"); - } else if ($(data.element).data("_bgcolor_")) { - bg = $(data.element).data("_bgcolor_"); - } - - if (bg && !$("head").find('#' + data._resultId + '_style').length) { - $(container).css("background-color", bg); - $(container).css("color", setContrast(bg)); - } - - return data.text; - }, + templateResult: selectBackground, ajax: { url: globals.rootdir + "/ajax_select.php?op=" + $this.data('source'), dataType: 'json', @@ -133,6 +100,24 @@ function start_superselect() { }); } +function selectBackground(data, container) { + let bg; + + if (data._bgcolor_) { + bg = data._bgcolor_; + } else if ($(data.element).attr("_bgcolor_")) { + bg = $(data.element).attr("_bgcolor_"); + } else if ($(data.element).data("_bgcolor_")) { + bg = $(data.element).data("_bgcolor_"); + } + + if (bg && !$("head").find('#' + data._resultId + '_style').length) { + $(container).css("background-color", bg); + $(container).css("color", setContrast(bg)); + } + + return data.text; +} /** * Reimposta i contenuti di un creato con select2 impostando un valore di default */ jQuery.fn.selectAdd = function (values) { - $this = this; + let $this = this; values.forEach(function (item) { if (item.data) { @@ -207,8 +192,7 @@ jQuery.fn.selectAdd = function (values) { delete item.data; - var option = $('