Skip to content

Commit

Permalink
Update to release v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-v committed May 8, 2015
1 parent 92b2d62 commit d2b0200
Show file tree
Hide file tree
Showing 132 changed files with 8,196 additions and 6,367 deletions.
18 changes: 18 additions & 0 deletions CHANGE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
version 2.0.0
=============
**Date:** 09-May-2015

Major revamp to the widget. Version bumped to release v2.0.0.

- (enh #27): Updates Select2 plugin to release v4.0. The following additional functionalities and changes should be expected with this new release:
- New `theme` property that allows you to set themes in Select2 to style your widget.
- A brand new theme by Krajee `Select2::THEME_KRAJEE` is specially styled for Select2. This will help achieve various new yii2-widget-select2 features. This theme matches the bootstrap 3 styling with enhancements.
- Additional themes provided in form of `Select2::THEME_DEFAULT`, `Select2::THEME_CLASSIC`, and `Select2::THEME_BOOTSTRAP`. One can add their own custom theme and configure the widget.
- No more `query` plugin property needed. It is also not mandatory to configure `data` even if you have not set `tags` or `query` or `ajax`. Widget will intelligently evaluate the properties and default list values.
- Enhanced tagging support. Use it just like a multiple select but with taggable values. In addition, one can create tags on the fly.
- Enhanced ajax support. Refer the [demos](http://demos.krajee.com/widget-details/select2) for details.
- The `initSelection` method of Select2 3.5.x plugin is obsolete/removed. New `initValueText` property is been provided with the Select2 widget to cater to this (e.g. for ajax based loading).
- Ability to disable selective option values in the Select2 dropdown OR add HTML attributes to selective options.
- Enhancement by Krajee to disable the search box to use like a normal select.
- Enhancements to locales and translations. Allow multiple language Select2 widgets on the same page.

version 1.0.1
=============
**Date:** 03-May-2015
Expand Down
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,22 @@ to the ```require``` section of your `composer.json` file.

## Latest Release

> NOTE: The latest version of the module is v1.0.1. Refer the [CHANGE LOG](https://github.com/kartik-v/yii2-widget-select2/blob/master/CHANGE.md) for details.
> NOTE: The latest version of the module is v2.0 Refer the [CHANGE LOG](https://github.com/kartik-v/yii2-widget-select2/blob/master/CHANGE.md) for details.
The widget has a major version revamp with v2.0. This release includes updates to use Select2 plugin release v4.0. Select2 release v4.0 is a major rewrite over Select2 v3.x and hence quite a few enhancements or changes should be expected. To use the earlier plugin release v3.5.2, you must point it to the [v1.0.1 release](https://github.com/kartik-v/yii2-widget-select2/releases/tag/v1.0.1) of the widget.

Enhancements with release v2.0:

- New `theme` property that allows you to set themes in Select2 to style your widget.
- A brand new theme by Krajee `Select2::THEME_KRAJEE` is specially styled for Select2. This will help achieve various new yii2-widget-select2 features. This theme matches the bootstrap 3 styling with enhancements.
- Additional themes provided in form of `Select2::THEME_DEFAULT`, `Select2::THEME_CLASSIC`, and `Select2::THEME_BOOTSTRAP`. One can add their own custom theme and configure the widget.
- No more `query` plugin property needed. It is also not mandatory to configure `data` even if you have not set `tags` or `query` or `ajax`. Widget will intelligently evaluate the properties and default list values.
- Enhanced tagging support. Use it just like a multiple select but with taggable values. In addition, one can create tags on the fly.
- Enhanced ajax support. Refer the [demos](http://demos.krajee.com/widget-details/select2) for details.
- The `initSelection` method of Select2 3.5.x plugin is obsolete/removed. New `initValueText` property is been provided with the Select2 widget to cater to this (e.g. for ajax based loading).
- Ability to disable selective option values in the Select2 dropdown OR add HTML attributes to selective options.
- Enhancement by Krajee to disable the search box to use like a normal select.
- Enhancements to locales and translations. Allow multiple language Select2 widgets on the same page.

## Demo

Expand All @@ -44,7 +59,7 @@ use kartik\select2\Select2;

// Normal select with ActiveForm & model
echo $form->field($model, 'state_1')->widget(Select2::classname(), [
'data' => array_merge(["" => ""], $data),
'data' => $data,
'language' => 'de',
'options' => ['placeholder' => 'Select a state ...'],
'pluginOptions' => [
Expand Down
156 changes: 98 additions & 58 deletions Select2.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/**
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2015
* @package yii2-widgets
* @subpackage yii2-widget-select2
* @version 1.0.1
* @subpackage yii2-widget-select2
* @version 2.0.0
*/

namespace kartik\select2;
Expand All @@ -22,21 +22,45 @@
*
* @author Kartik Visweswaran <[email protected]>
* @since 1.0
* @see http://ivaynberg.github.com/select2/
* @see https://github.com/select2/select2
*/
class Select2 extends \kartik\base\InputWidget
{

const LARGE = 'lg';
const MEDIUM = 'md';
const SMALL = 'sm';

const THEME_DEFAULT = 'default';
const THEME_CLASSIC = 'classic';
const THEME_BOOTSTRAP = 'bootstrap';
const THEME_KRAJEE = 'krajee';

/**
* @var array $data the option data items. The array keys are option values, and the array values
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
* For each sub-array, an option group will be generated whose label is the key associated with the sub-array.
* If you have a list of data models, you may convert them into the format described above using
* [[\yii\helpers\ArrayHelper::map()]].
*/
public $data;

/**
* @var string the locale ID (e.g. 'fr', 'de') for the language to be used by the Select2 Widget.
* If this property not set, then the current application language will be used.
*/
public $language;


/**
* @var string the theme name to be used for styling the Select2
*/
public $theme = self::THEME_KRAJEE;

/**
* @var string, the displayed text in the dropdown for the initial
* value when you do not set or provide `data` (e.g. using with ajax).
*/
public $initValueText;

/**
* @var bool whether to hide the search control and render it as a simple select. Defaults to `false`.
*/
Expand All @@ -62,59 +86,70 @@ class Select2 extends \kartik\base\InputWidget
*/
public $size = self::MEDIUM;

/**
* @var array $data the option data items. The array keys are option values, and the array values
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
* For each sub-array, an option group will be generated whose label is the key associated with the sub-array.
* If you have a list of data models, you may convert them into the format described above using
* [[\yii\helpers\ArrayHelper::map()]].
*/
public $data;

/**
* @var array the HTML attributes for the input tag. The following options are important:
* - multiple: boolean whether multiple or single item should be selected. Defaults to false.
* - placeholder: string placeholder for the select item.
*/
public $options = [];

/**
* @var boolean indicator for displaying text inputs
* instead of select fields
*/
private $_hidden = false;
protected static $_inbuiltThemes = [
self::THEME_DEFAULT,
self::THEME_CLASSIC,
self::THEME_BOOTSTRAP,
self::THEME_KRAJEE,
];

/**
* Initializes the widget
*
* @throw InvalidConfigException
* @inheritdoc
*/
public function init()
{
$this->pluginOptions['theme'] = $this->theme;
parent::init();
$this->_hidden = !empty($this->pluginOptions['data']) ||
!empty($this->pluginOptions['query']) ||
!empty($this->pluginOptions['ajax']) ||
isset($this->pluginOptions['tags']);
if (!isset($this->data) && !$this->_hidden) {
throw new InvalidConfigException("No 'data' source found for Select2. Either the 'data' property must be set OR one of 'data', 'query', 'ajax', or 'tags' must be set within 'pluginOptions'.");
if (ArrayHelper::getValue($this->pluginOptions, 'tags', false)) {
$this->options['multiple'] = true;
}
if ($this->hideSearch) {
$css = ArrayHelper::getValue($this->pluginOptions, 'dropdownCssClass', '');
$css .= ' kv-hide';
$css .= ' kv-hide-search';
$this->pluginOptions['dropdownCssClass'] = $css;
}
if (!empty($this->options['placeholder']) && !$this->_hidden &&
(empty($this->options['multiple']) || $this->options['multiple'] == false)
) {
$this->data = ["" => ""] + $this->data;
$this->initPlaceholder();
if (!isset($this->data)) {
$key = empty($this->value) ? '' : $this->value;
$val = empty($this->initValueText) ? $key : $this->initValueText;
$this->data = [$key => $val];
}
Html::addCssClass($this->options, 'form-control');
Html::addCssStyle($this->options, 'width:100%', false);
$this->initLanguage();
$this->registerAssets();
$this->renderInput();
}

/**
* Initializes the placeholder for Select2
*/
protected function initPlaceholder()
{
$isMultiple = ArrayHelper::getValue($this->options, 'multiple', false);
if (!empty($this->options['prompt']) && empty($this->pluginOptions['placeholder'])) {
$this->pluginOptions['placeholder'] = $this->options['prompt'];
if ($isMultiple) {
unset($this->options['prompt']);
}
return;
}
if (!empty($this->options['placeholder'])) {
$this->pluginOptions['placeholder'] = $this->options['placeholder'];
unset($this->options['placeholder']);
}
if (!empty($this->pluginOptions['placeholder']) && is_string($this->pluginOptions['placeholder']) && !$isMultiple) {
$this->options['prompt'] = $this->pluginOptions['placeholder'];
}
}

/**
* Embeds the input group addon
*
Expand All @@ -124,15 +159,20 @@ public function init()
*/
protected function embedAddon($input)
{
if (empty($this->addon)) {
if (!isset($this->size) && empty($this->addon)) {
return $input;
}
$prepend = ArrayHelper::getValue($this->addon, 'prepend', '');
$append = ArrayHelper::getValue($this->addon, 'append', '');
$group = ArrayHelper::getValue($this->addon, 'groupOptions', []);
$size = isset($this->size) ? ' input-group-' . $this->size : '';
Html::addCssClass($group, 'input-group' . $size);
if (empty($this->addon)) {
return Html::tag('div', $input, $group);
}
$prepend = ArrayHelper::getValue($this->addon, 'prepend', '');
$append = ArrayHelper::getValue($this->addon, 'append', '');
if ($this->pluginLoading) {
Html::addCssClass($group, 'kv-hide group-' . $this->options['id']);
Html::addCssClass($group, 'kv-input-group-hide');
Html::addCssClass($group, 'group-' . $this->options['id']);
}
if (is_array($prepend)) {
$content = ArrayHelper::getValue($prepend, 'content', '');
Expand All @@ -141,7 +181,7 @@ protected function embedAddon($input)
} else {
$prepend = Html::tag('span', $content, ['class' => 'input-group-addon']);
}
Html::addCssClass($group, 'input-group' . $size . ' select2-bootstrap-prepend');
Html::addCssClass($group, 'select2-bootstrap-prepend');
}
if (is_array($append)) {
$content = ArrayHelper::getValue($append, 'content', '');
Expand All @@ -150,7 +190,7 @@ protected function embedAddon($input)
} else {
$append = Html::tag('span', $content, ['class' => 'input-group-addon']);
}
Html::addCssClass($group, 'input-group' . $size . ' select2-bootstrap-append');
Html::addCssClass($group, 'select2-bootstrap-append');
}
$addonText = $prepend . $input . $append;
$contentBefore = ArrayHelper::getValue($this->addon, 'contentBefore', '');
Expand All @@ -166,48 +206,48 @@ protected function embedAddon($input)
*/
protected function renderInput()
{
$class = $this->pluginLoading ? 'kv-hide ' : '';
if (empty($this->addon) && isset($this->size)) {
$class .= 'input-' . $this->size;
}
if ($this->pluginLoading) {
$this->_loadIndicator = '<div class="kv-plugin-loading loading-' . $this->options['id'] . '">&nbsp;</div>';
Html::addCssStyle($this->options, 'display:none');
}
Html::addCssClass($this->options, $class);
if ($this->_hidden) {
$input = $this->getInput('textInput');
} else {
$input = $this->getInput('dropDownList', true);
}
$input = $this->getInput('dropDownList', true);
echo $this->_loadIndicator . $this->embedAddon($input);
}

/**
* Registers the asset bundle and locale
*/
public function registerAssetBundle() {
public function registerAssetBundle()
{
$view = $this->getView();
Select2Asset::register($view)->addLanguage($this->language, 'select2_locale_', '/');
$lang = isset($this->language) ? $this->language : '';
Select2Asset::register($view)->addLanguage($lang, '', 'js/i18n');
if (in_array($this->theme, self::$_inbuiltThemes)) {
$bundleClass = __NAMESPACE__ . '\Theme' . ucfirst($this->theme) . 'Asset';
$bundleClass::register($view);
}
}

/**
* Registers the needed assets
*/
public function registerAssets()
{
$id = $this->options['id'];
$this->registerAssetBundle();
// set default width
$this->pluginOptions['width'] = 'resolve';
// validate bootstrap has-success & has-error states
$this->pluginEvents += ['select2-open' => "function(){initSelect2DropStyle('{$id}')}"];

$clear = 'kv_close_' . str_replace('-', '_', $id);
$this->pluginEvents += [
'select2:opening' => "function(event){initSelect2DropStyle('{$id}', '{$clear}', event);}",
'select2:unselect' => "function(){window.{$clear} = true;}"
];
// register plugin
if ($this->pluginLoading) {
$this->registerPlugin('select2', "jQuery('#{$id}')", "initSelect2Loading('{$id}')");
$this->registerPlugin('select2', "jQuery('#{$id}')",
"initSelect2Loading('{$id}', '.select2-container--{$this->theme}')");
} else {
$this->registerPlugin('select2');
}

}
}
9 changes: 6 additions & 3 deletions Select2Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2014 - 2015
* @package yii2-widgets
* @subpackage yii2-widget-select2
* @version 1.0.1
* @version 2.0.0
*/

namespace kartik\select2;
Expand All @@ -20,11 +20,14 @@
class Select2Asset extends \kartik\base\AssetBundle
{

/**
* @inheritdoc
*/
public function init()
{
$this->setSourcePath(__DIR__ . '/lib');
$this->setupAssets('css', ['select2', 'select2-kv']);
$this->setupAssets('js', ['select2', 'select2-kv']);
$this->setupAssets('css', ['css/select2']);
$this->setupAssets('js', ['js/select2.full', 'js/select2-krajee']);
parent::init();
}

Expand Down
33 changes: 33 additions & 0 deletions ThemeBootstrapAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2014 - 2015
* @package yii2-widgets
* @subpackage yii2-widget-select2
* @version 2.0.0
*/

namespace kartik\select2;

use Yii;

/**
* Bootstrap Select2 theme
*
* @author Kartik Visweswaran <[email protected]>
* @since 1.0
*/
class ThemeBootstrapAsset extends \kartik\base\AssetBundle
{

/**
* @inheritdoc
*/
public function init()
{
$this->setSourcePath(__DIR__ . '/lib');
$this->setupAssets('css', ['css/select2-bootstrap']);
parent::init();
}

}
Loading

0 comments on commit d2b0200

Please sign in to comment.