You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

161 lines
4.6 KiB

<?php
/*
* The MIT License
*
* Copyright 2019 Blobt.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace linyao\widgets;
use yii\base\InvalidArgumentException;
use yii\widgets\InputWidget;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use linyao\web\Select2Asset;
/**
* Description of Select2
*
* @author Blobt
* @email 380255922@qq.com
* @created Aug 20, 2019
*/
class Select2 extends InputWidget {
/**
* @var array select的选项
*/
public $items = [];
/**
* @var array select 的html选择
*/
public $options;
/**
* @var string|bool 默认显示的提示内容,如果不想显示可以设置成false
*/
public $promptText = '请选择';
/**
* @var string Regular expression used for attribute name validation.
* @since 2.0.12
* 属性匹配规则
*/
private static $attributeRegex = '/(^|.*\])([\w\.\+]+)(\[.*|$)/u';
/**
* @inheritdoc
* @throws \ReflectionException
* @throws \yii\base\InvalidConfigException
*/
public function run() {
$this->registerAsset();
$this->registerSelect2Js();
return $this->renderSelect();
}
/**
* 初始化
*/
public function init() {
parent::init();
if ($this->hasModel() && !isset($this->options['name'])) {
$this->name = $this->options['name'] = Html::getInputName($this->model, $this->attribute);
}
if (!empty($this->promptText)) {
$this->options = ArrayHelper::merge($this->options, ['prompt' => $this->promptText]);
}
}
/**
* 注册js和css
*/
protected function registerAsset() {
$view = $this->getView();
Select2Asset::register($view);
}
/**
* 注册js
*/
protected function registerSelect2Js() {
$inputId = self::getInputId($this->model, $this->attribute);
$js = <<<SCRIPT
$('#{$inputId}').select2();
SCRIPT;
$this->getView()->registerJs($js);
}
/**
* 渲染一个select
* @return string
*/
protected function renderSelect() {
$value = null;
if ($this->hasModel()) {
$value = ArrayHelper::getValue($this->model, $this->attribute);
}
return Html::dropDownList($this->name, $value, $this->items, $this->options);
}
/**
* @param $model
* @param $attribute
* @return string
* 获取要填入属性的输入框name
*/
private static function getInputName($model, $attribute)
{
$formName = $model->formName();
if (!preg_match(static::$attributeRegex, $attribute, $matches)) {
throw new InvalidArgumentException('Attribute name must contain word characters only.');
}
$prefix = $matches[1];
$attribute = $matches[2];
$suffix = $matches[3];
if ($formName === '' && $prefix === '') {
return $attribute . $suffix;
} elseif ($formName !== '') {
return $formName . $prefix . "[$attribute]" . $suffix;
}
throw new InvalidArgumentException(get_class($model) . '::formName() cannot be empty for tabular inputs.');
}
/**
* @param $model
* @param $attribute
* @return mixed
* 获取要填入属性的输入框id
*/
private static function getInputId($model, $attribute)
{
$charset = Yii::$app ? Yii::$app->charset : 'UTF-8';
$name = mb_strtolower(static::getInputName($model, $attribute), $charset);
return str_replace(['[]', '][', '[', ']', ' ', '.'], ['', '-', '-', '', '-', '-'], $name);
}
}