diff --git a/CHANGE.md b/CHANGE.md index 66cb508..ad000b1 100755 --- a/CHANGE.md +++ b/CHANGE.md @@ -3,9 +3,12 @@ Change Log: `yii2-widget-select2` ## Version 2.1.9 -**Date:** _under development_ +**Date:** 25-Sep-2020 +- (enh #330): Fix select all for option values containing hyphen. - (enh #329): Minor fixes to select2 krajee theme. +- (enh #328): Fix undefined options in select all. +- (enh #325): Use `hashVarLoadPosition` to initialize Select2 hash variable. ## Version 2.1.8 diff --git a/src/Select2.php b/src/Select2.php index 8dd335a..90cf487 100644 --- a/src/Select2.php +++ b/src/Select2.php @@ -9,6 +9,7 @@ namespace kartik\select2; +use Exception; use kartik\base\AddonTrait; use kartik\base\InputWidget; use ReflectionException; @@ -18,8 +19,8 @@ use yii\helpers\Inflector; use yii\helpers\ArrayHelper; use yii\helpers\Json; +use Yii\validators\RequiredValidator; use yii\web\JsExpression; -use yii\web\View; /** * Select2 widget is a Yii2 wrapper for the Select2 jQuery plugin. This input widget is a jQuery based replacement for @@ -33,6 +34,7 @@ class Select2 extends InputWidget { use AddonTrait; + /** * Select2 large input size */ @@ -210,6 +212,7 @@ public function run() * Initializes and renders the widget * @throws ReflectionException * @throws InvalidConfigException + * @throws Exception */ public function renderWidget() { @@ -311,6 +314,7 @@ protected function renderToggleAll() /** * Initializes the placeholder for Select2 + * @throws Exception */ protected function initPlaceholder() { @@ -338,6 +342,7 @@ protected function initPlaceholder() * * @return string * @throws InvalidConfigException + * @throws Exception */ protected function embedAddon($input) { @@ -432,14 +437,17 @@ public function registerAssets() $this->_s2OptionsVar = 's2options_' . hash('crc32', $options); $this->options['data-s2-options'] = $this->_s2OptionsVar; $view = $this->getView(); - $view->registerJs("var {$this->_s2OptionsVar} = {$options};", View::POS_HEAD); + $view->registerJs("var {$this->_s2OptionsVar} = {$options};", $this->hashVarLoadPosition); if ($this->maintainOrder) { $val = Json::encode(is_array($this->value) ? $this->value : [$this->value]); $view->registerJs("initS2Order('{$id}',{$val});"); } $this->registerPlugin($this->pluginName, "jQuery('#{$id}')", "initS2Loading('{$id}','{$this->_s2OptionsVar}')"); } - + + /** + * @return bool + */ protected function isRequired() { if (!empty($this->options['required'])) { @@ -448,9 +456,9 @@ protected function isRequired() if (!$this->hasModel()) { return false; } - $validators = $this->model->getValidators($this->attribute); + $validators = $this->model->getActiveValidators($this->attribute); foreach ($validators as $validator) { - if ($validator instanceof yii\validators\RequiredValidator) { + if ($validator instanceof RequiredValidator) { return true; } } diff --git a/src/assets/css/select2-krajee-bs4.css b/src/assets/css/select2-krajee-bs4.css index 62088b1..23299f6 100644 --- a/src/assets/css/select2-krajee-bs4.css +++ b/src/assets/css/select2-krajee-bs4.css @@ -384,6 +384,7 @@ /** * Bootstrap validation states */ +select.is-invalid + .select2-container--krajee-bs4 .select2-selection--single, .has-error.select2-container--krajee-bs4 .select2-dropdown, .has-error .select2-container--krajee-bs4 .select2-selection { border-color: #dc3545; @@ -395,6 +396,7 @@ border-color: #dc3545; } +select.is-valid + .select2-container--krajee-bs4 .select2-selection--single, .has-success.select2-container--krajee-bs4 .select2-dropdown, .has-success .select2-container--krajee-bs4 .select2-selection { border-color: #28a745; diff --git a/src/assets/js/select2-krajee.js b/src/assets/js/select2-krajee.js index 7b53fbd..1ab2b75 100644 --- a/src/assets/js/select2-krajee.js +++ b/src/assets/js/select2-krajee.js @@ -38,13 +38,16 @@ var initS2ToggleAll = function () { } listenTogAll = function () { $tog.off('.krajees2').on('click.krajees2', function () { - var isSelect = $tog.hasClass('s2-togall-select'), ev = 'selectall', val; + var isSelect = $tog.hasClass('s2-togall-select'), ev = 'selectall', opts, val; if (!isSelect) { ev = 'unselectall'; } $('#select2-' + id + '-results .select2-results__option[role="option"]').each(function () { - val = $(this).attr('id').split('-').pop(); - $el.find('option:not([disabled])[value="' + val + '"]').prop('selected', !!isSelect); + opts = $(this).attr('id').match(/^select2-[^-]*-result-.{4}-(.*)$/); + if (opts.length && opts[1]) { + val = opts[1]; + $el.find('option:not([disabled])[value="' + val + '"]').prop('selected', !!isSelect); + } }); $el.select2('close').trigger('krajeeselect2:' + ev).trigger('change'); }); @@ -102,10 +105,13 @@ var initS2ToggleAll = function () { } }; initS2Unselect = function () { - var $el = $(this), opts = $el.data('select2').options; - opts.set('disabled', true); + var $el = $(this), select2 = $el.data('select2'); + if (!select2 || !select2.options) { + return; + } + select2.options.set('disabled', true); setTimeout(function () { - opts.set('disabled', false); + select2.options.set('disabled', false); $el.trigger('krajeeselect2:cleared'); }, 1); }; @@ -169,7 +175,7 @@ var initS2ToggleAll = function () { setTimeout(function () { if ($el.attr('multiple') && $el.attr('dir') === 'rtl') { $el.parent().find('.select2-search__field').css({width: '100%', direction: 'rtl'}); - $el.parent().find('.select2-search--inline').css({float: 'none'}); + $el.parent().find('.select2-search--inline').css({"float": "none"}); } }, 100); }; diff --git a/src/assets/js/select2-krajee.min.js b/src/assets/js/select2-krajee.min.js index e8f84c8..af32212 100644 --- a/src/assets/js/select2-krajee.min.js +++ b/src/assets/js/select2-krajee.min.js @@ -7,4 +7,4 @@ * Author: Kartik Visweswaran * For more JQuery plugins visit http://plugins.krajee.com * For more Yii related demos visit http://demos.krajee.com - */var initS2ToggleAll=function(){},initS2Order=function(){},initS2Loading=function(){},initS2Change=function(){},initS2Unselect=function(){};!function(e){"use strict";"function"==typeof define&&define.amd?define(["jquery"],e):"object"==typeof module&&module.exports?module.exports=e(require("jquery")):e(window.jQuery)}(function(e){"use strict";initS2ToggleAll=function(t){var n,s=e("#"+t),l="#s2-togall-"+t,o=e(l);s.attr("multiple")&&(n=function(){o.off(".krajees2").on("click.krajees2",function(){var n,l=o.hasClass("s2-togall-select"),i="selectall";l||(i="unselectall"),e("#select2-"+t+'-results .select2-results__option[role="option"]').each(function(){n=e(this).attr("id").split("-").pop(),s.find('option:not([disabled])[value="'+n+'"]').prop("selected",!!l)}),s.select2("close").trigger("krajeeselect2:"+i).trigger("change")})},s.on("select2:open.krajees2",function(){var i="input.krajees2 keyup.krajees2";o.parent().attr("id")!=="parent-"+l&&s.attr("multiple")&&(e("#select2-"+t+"-results").closest(".select2-dropdown").prepend(o),e("#parent-"+l).remove(),e(this).parent().find(".select2-search__field").off(i).on(i,function(){setTimeout(function(){var s="#select2-"+t+'-results .select2-results__option[role="option"]',l=s+'[aria-selected="true"]',i=e(s).length;o.removeClass("s2-togall-select s2-togall-unselect"),i>0&&e(l).length===i?o.addClass("s2-togall-unselect"):o.addClass("s2-togall-select"),n()},100)}))}).on("change.krajees2",function(){if(s.attr("multiple")){var t=0,l=s.val()?s.val().length:0;o.removeClass("s2-togall-select s2-togall-unselect"),s.find("option:enabled").each(function(){e(this).val().length&&t++}),0===t||l!==t?o.addClass("s2-togall-select"):o.addClass("s2-togall-unselect"),n()}}),n())},initS2Change=function(t){t=t||e(this);var n,s,l=e(".select2-container--open"),o=t.parents("[class*='has-']");if(o.length)for(n=o[0].className.split(/\s+/),s=0;s0&&e(l).length===i?o.addClass("s2-togall-unselect"):o.addClass("s2-togall-select"),n()},100)}))}).on("change.krajees2",function(){if(s.attr("multiple")){var t=0,l=s.val()?s.val().length:0;o.removeClass("s2-togall-select s2-togall-unselect"),s.find("option:enabled").each(function(){e(this).val().length&&t++}),0===t||l!==t?o.addClass("s2-togall-select"):o.addClass("s2-togall-unselect"),n()}}),n())},initS2Change=function(t){t=t||e(this);var n,s,l=e(".select2-container--open"),o=t.parents("[class*='has-']");if(o.length)for(n=o[0].className.split(/\s+/),s=0;s