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.

119 lines
3.4 KiB

  1. <?php
  2. /*
  3. * The MIT License
  4. *
  5. * Copyright 2019 Blobt.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. namespace blobt\widgets;
  26. use yii\widgets\InputWidget;
  27. use Yii;
  28. use yii\base\InvalidConfigException;
  29. use yii\helpers\ArrayHelper;
  30. use yii\helpers\Html;
  31. use blobt\web\IcheckAsset;
  32. /**
  33. * Description of Icheck
  34. *
  35. * @author Blobt
  36. * @email 380255922@qq.com
  37. * @created Sep 12, 2019
  38. */
  39. class Icheck extends InputWidget {
  40. /**
  41. * @var array checkbox的选项
  42. */
  43. public $items = [];
  44. /**
  45. * @var array select 的html选择
  46. */
  47. public $options = [];
  48. /**
  49. * @var string 类型,支持checkbox或radio
  50. */
  51. public $type = 'checkbox';
  52. /**
  53. * @inheritdoc
  54. * @throws \ReflectionException
  55. * @throws \yii\base\InvalidConfigException
  56. */
  57. public function run() {
  58. $this->registerAsset();
  59. $this->registerJs();
  60. return $this->renderInput();
  61. }
  62. /**
  63. * 初始化
  64. */
  65. public function init() {
  66. parent::init();
  67. if ($this->hasModel() && !isset($this->options['name'])) {
  68. $this->name = $this->options['name'] = Html::getInputName($this->model, $this->attribute);
  69. if ($this->type == 'checkbox') {
  70. $this->name .= "[]";
  71. }
  72. }
  73. //表单默认会把css类form-control带过来,用来控制边框,这里不需边框所以去除
  74. $this->options["class"] = str_replace("form-control", '', $this->options["class"]);
  75. $this->options["class"] = $this->options["class"] . ' icheck-label-group';
  76. }
  77. /**
  78. * 注册js和css
  79. */
  80. protected function registerAsset() {
  81. $view = $this->getView();
  82. IcheckAsset::register($view);
  83. }
  84. /**
  85. * 注册js
  86. */
  87. protected function registerJs() {
  88. $js = <<<SCRIPT
  89. $("input[name='{$this->name}']").iCheck({
  90. checkboxClass: 'icheckbox_flat-blue',
  91. radioClass: 'iradio_flat-blue',
  92. });
  93. SCRIPT;
  94. $this->getView()->registerJs($js);
  95. }
  96. protected function renderInput() {
  97. if ($this->type == 'checkbox') {
  98. return Html::activeCheckboxList($this->model, $this->attribute, $this->items, $this->options);
  99. }
  100. if ($this->type == 'radio') {
  101. return Html::activeRadioList($this->model, $this->attribute, $this->items, $this->options);
  102. }
  103. return "";
  104. }
  105. }