Skip to content

Commit

Permalink
Updates to release v2.1.9 fix #325 fix #328 fix #330
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-v committed Sep 25, 2020
1 parent 1f095ab commit 6a6840c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
5 changes: 4 additions & 1 deletion CHANGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 13 additions & 5 deletions src/Select2.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace kartik\select2;

use Exception;
use kartik\base\AddonTrait;
use kartik\base\InputWidget;
use ReflectionException;
Expand All @@ -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
Expand All @@ -33,6 +34,7 @@
class Select2 extends InputWidget
{
use AddonTrait;

/**
* Select2 large input size
*/
Expand Down Expand Up @@ -210,6 +212,7 @@ public function run()
* Initializes and renders the widget
* @throws ReflectionException
* @throws InvalidConfigException
* @throws Exception
*/
public function renderWidget()
{
Expand Down Expand Up @@ -311,6 +314,7 @@ protected function renderToggleAll()

/**
* Initializes the placeholder for Select2
* @throws Exception
*/
protected function initPlaceholder()
{
Expand Down Expand Up @@ -338,6 +342,7 @@ protected function initPlaceholder()
*
* @return string
* @throws InvalidConfigException
* @throws Exception
*/
protected function embedAddon($input)
{
Expand Down Expand Up @@ -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'])) {
Expand All @@ -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;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/assets/css/select2-krajee-bs4.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
20 changes: 13 additions & 7 deletions src/assets/js/select2-krajee.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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);
};
Expand Down
2 changes: 1 addition & 1 deletion src/assets/js/select2-krajee.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6a6840c

Please sign in to comment.