Skip to content

Commit 6a6840c

Browse files
committed
Updates to release v2.1.9 fix kartik-v#325 fix kartik-v#328 fix kartik-v#330
1 parent 1f095ab commit 6a6840c

File tree

5 files changed

+33
-14
lines changed

5 files changed

+33
-14
lines changed

CHANGE.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ Change Log: `yii2-widget-select2`
33

44
## Version 2.1.9
55

6-
**Date:** _under development_
6+
**Date:** 25-Sep-2020
77

8+
- (enh #330): Fix select all for option values containing hyphen.
89
- (enh #329): Minor fixes to select2 krajee theme.
10+
- (enh #328): Fix undefined options in select all.
11+
- (enh #325): Use `hashVarLoadPosition` to initialize Select2 hash variable.
912

1013
## Version 2.1.8
1114

src/Select2.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace kartik\select2;
1111

12+
use Exception;
1213
use kartik\base\AddonTrait;
1314
use kartik\base\InputWidget;
1415
use ReflectionException;
@@ -18,8 +19,8 @@
1819
use yii\helpers\Inflector;
1920
use yii\helpers\ArrayHelper;
2021
use yii\helpers\Json;
22+
use Yii\validators\RequiredValidator;
2123
use yii\web\JsExpression;
22-
use yii\web\View;
2324

2425
/**
2526
* Select2 widget is a Yii2 wrapper for the Select2 jQuery plugin. This input widget is a jQuery based replacement for
@@ -33,6 +34,7 @@
3334
class Select2 extends InputWidget
3435
{
3536
use AddonTrait;
37+
3638
/**
3739
* Select2 large input size
3840
*/
@@ -210,6 +212,7 @@ public function run()
210212
* Initializes and renders the widget
211213
* @throws ReflectionException
212214
* @throws InvalidConfigException
215+
* @throws Exception
213216
*/
214217
public function renderWidget()
215218
{
@@ -311,6 +314,7 @@ protected function renderToggleAll()
311314

312315
/**
313316
* Initializes the placeholder for Select2
317+
* @throws Exception
314318
*/
315319
protected function initPlaceholder()
316320
{
@@ -338,6 +342,7 @@ protected function initPlaceholder()
338342
*
339343
* @return string
340344
* @throws InvalidConfigException
345+
* @throws Exception
341346
*/
342347
protected function embedAddon($input)
343348
{
@@ -432,14 +437,17 @@ public function registerAssets()
432437
$this->_s2OptionsVar = 's2options_' . hash('crc32', $options);
433438
$this->options['data-s2-options'] = $this->_s2OptionsVar;
434439
$view = $this->getView();
435-
$view->registerJs("var {$this->_s2OptionsVar} = {$options};", View::POS_HEAD);
440+
$view->registerJs("var {$this->_s2OptionsVar} = {$options};", $this->hashVarLoadPosition);
436441
if ($this->maintainOrder) {
437442
$val = Json::encode(is_array($this->value) ? $this->value : [$this->value]);
438443
$view->registerJs("initS2Order('{$id}',{$val});");
439444
}
440445
$this->registerPlugin($this->pluginName, "jQuery('#{$id}')", "initS2Loading('{$id}','{$this->_s2OptionsVar}')");
441446
}
442-
447+
448+
/**
449+
* @return bool
450+
*/
443451
protected function isRequired()
444452
{
445453
if (!empty($this->options['required'])) {
@@ -448,9 +456,9 @@ protected function isRequired()
448456
if (!$this->hasModel()) {
449457
return false;
450458
}
451-
$validators = $this->model->getValidators($this->attribute);
459+
$validators = $this->model->getActiveValidators($this->attribute);
452460
foreach ($validators as $validator) {
453-
if ($validator instanceof yii\validators\RequiredValidator) {
461+
if ($validator instanceof RequiredValidator) {
454462
return true;
455463
}
456464
}

src/assets/css/select2-krajee-bs4.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@
384384
/**
385385
* Bootstrap validation states
386386
*/
387+
select.is-invalid + .select2-container--krajee-bs4 .select2-selection--single,
387388
.has-error.select2-container--krajee-bs4 .select2-dropdown,
388389
.has-error .select2-container--krajee-bs4 .select2-selection {
389390
border-color: #dc3545;
@@ -395,6 +396,7 @@
395396
border-color: #dc3545;
396397
}
397398

399+
select.is-valid + .select2-container--krajee-bs4 .select2-selection--single,
398400
.has-success.select2-container--krajee-bs4 .select2-dropdown,
399401
.has-success .select2-container--krajee-bs4 .select2-selection {
400402
border-color: #28a745;

src/assets/js/select2-krajee.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@ var initS2ToggleAll = function () {
3838
}
3939
listenTogAll = function () {
4040
$tog.off('.krajees2').on('click.krajees2', function () {
41-
var isSelect = $tog.hasClass('s2-togall-select'), ev = 'selectall', val;
41+
var isSelect = $tog.hasClass('s2-togall-select'), ev = 'selectall', opts, val;
4242
if (!isSelect) {
4343
ev = 'unselectall';
4444
}
4545
$('#select2-' + id + '-results .select2-results__option[role="option"]').each(function () {
46-
val = $(this).attr('id').split('-').pop();
47-
$el.find('option:not([disabled])[value="' + val + '"]').prop('selected', !!isSelect);
46+
opts = $(this).attr('id').match(/^select2-[^-]*-result-.{4}-(.*)$/);
47+
if (opts.length && opts[1]) {
48+
val = opts[1];
49+
$el.find('option:not([disabled])[value="' + val + '"]').prop('selected', !!isSelect);
50+
}
4851
});
4952
$el.select2('close').trigger('krajeeselect2:' + ev).trigger('change');
5053
});
@@ -102,10 +105,13 @@ var initS2ToggleAll = function () {
102105
}
103106
};
104107
initS2Unselect = function () {
105-
var $el = $(this), opts = $el.data('select2').options;
106-
opts.set('disabled', true);
108+
var $el = $(this), select2 = $el.data('select2');
109+
if (!select2 || !select2.options) {
110+
return;
111+
}
112+
select2.options.set('disabled', true);
107113
setTimeout(function () {
108-
opts.set('disabled', false);
114+
select2.options.set('disabled', false);
109115
$el.trigger('krajeeselect2:cleared');
110116
}, 1);
111117
};
@@ -169,7 +175,7 @@ var initS2ToggleAll = function () {
169175
setTimeout(function () {
170176
if ($el.attr('multiple') && $el.attr('dir') === 'rtl') {
171177
$el.parent().find('.select2-search__field').css({width: '100%', direction: 'rtl'});
172-
$el.parent().find('.select2-search--inline').css({float: 'none'});
178+
$el.parent().find('.select2-search--inline').css({"float": "none"});
173179
}
174180
}, 100);
175181
};

src/assets/js/select2-krajee.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)