forked from phpnt/yii2-icheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ICheck.php
154 lines (139 loc) · 5.86 KB
/
ICheck.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* Created by PhpStorm.
* User: phpNT - http://phpnt.com
* Date: 29.04.2017
* Time: 11:59
*/
namespace phpnt\ICheck;
use yii\bootstrap\Html;
use yii\helpers\Json;
use yii\web\View;
use yii\widgets\InputWidget;
class ICheck extends InputWidget
{
const TYPE_CHECBOX = 'checkbox';
const TYPE_CHECBOX_LIST = 'checkbox-list';
const TYPE_RADIO = 'radio';
const TYPE_RADIO_LIST = 'radio-list';
const STYLE_MIMIMAL = 'minimal';
const STYLE_SQUARE = 'square';
const STYLE_FLAT = 'flat';
const STYLE_LINE = 'line';
const STYLE_POLARIS = 'polaris';
const STYLE_FUTURICO = 'futurico';
public $type;
public $style;
public $color = 'black';
public $items = [];
public $options = [];
public $checkOptions = [];
private $idItem;
public function init()
{
parent::init();
$this->type = $this->type ? $this->type : 'checkbox';
if ($this->style == 'polaris') {
$this->color = 'polaris';
}
if ($this->style == 'futurico') {
$this->color = 'futurico';
}
$this->color = ($this->color != 'black') ? $this->color : 'minimal';
$this->options['class'] = 'i-checks-'.$this->id;
$this->idItem = $this->options['id'];
}
public function run()
{
$this->registerScript();
switch ($this->type) {
case 'checkbox':
echo Html::activeCheckbox($this->model, $this->attribute, $this->options);
break;
case 'checkbox-list':
echo Html::activeCheckboxList($this->model, $this->attribute, $this->items, $this->options);
break;
case 'radio':
echo Html::activeRadio($this->model, $this->attribute, $this->options);
break;
case 'radio-list':
echo Html::activeRadioList($this->model, $this->attribute, $this->items, $this->options);
break;
}
}
public function registerScript()
{
$view = $this->getView();
$asset = ICheckAsset::register($view);
$this->getCheckOptions();
$view->registerCssFile($asset->baseUrl.'/skins/'.$this->style.'/'.$this->color.'.css');
$this->checkOptions = Json::encode($this->checkOptions);
$js = <<< JS
if ('$this->style' == 'line') {
$('#$this->idItem > input').each(function(){
var self = $(this),
label = self.next(),
label_text = label.text();
label.remove();
self.iCheck({
checkboxClass: ('$this->color' == 'line') ? 'icheckbox_line' : 'icheckbox_line-$this->color',
radioClass: ('$this->color' == 'line') ? 'iradio_line' : 'iradio_line-$this->color',
insert: '<div class="icheck_line-icon"></div>' + label_text
});
});
}
else if ('$this->style' == 'minimal' || '$this->style' == 'square' || '$this->style' == 'flat' || '$this->style' == 'polaris' || '$this->style' == 'futurico') {
$(".i-checks-$this->id").iCheck($this->checkOptions);
}
JS;
$view->registerJs($js, View::POS_READY);
}
private function getCheckOptions() {
switch ($this->style) {
case 'minimal':
if ($this->color == 'minimal') {
$this->checkOptions['checkboxClass'] = 'icheckbox_'.$this->style.'';
$this->checkOptions['radioClass'] = 'iradio_'.$this->style.'';
} else {
$this->checkOptions['checkboxClass'] = 'icheckbox_'.$this->style.'-'.$this->color;
$this->checkOptions['radioClass'] = 'iradio_'.$this->style.'-'.$this->color;
}
break;
case 'square':
if ($this->color == 'square') {
$this->checkOptions['checkboxClass'] = 'icheckbox_'.$this->style.'';
$this->checkOptions['radioClass'] = 'iradio_'.$this->style.'';
} else {
$this->checkOptions['checkboxClass'] = 'icheckbox_'.$this->style.'-'.$this->color;
$this->checkOptions['radioClass'] = 'iradio_'.$this->style.'-'.$this->color;
}
break;
case 'flat':
if ($this->color == 'flat') {
$this->checkOptions['checkboxClass'] = 'icheckbox_'.$this->style.'';
$this->checkOptions['radioClass'] = 'iradio_'.$this->style.'';
} else {
$this->checkOptions['checkboxClass'] = 'icheckbox_'.$this->style.'-'.$this->color;
$this->checkOptions['radioClass'] = 'iradio_'.$this->style.'-'.$this->color;
}
break;
case 'line':
if ($this->color == 'line') {
$this->checkOptions['checkboxClass'] = 'icheckbox_'.$this->style.'';
$this->checkOptions['radioClass'] = 'iradio_'.$this->style.'';
} else {
$this->checkOptions['checkboxClass'] = 'icheckbox_'.$this->style.'-'.$this->color;
$this->checkOptions['radioClass'] = 'iradio_'.$this->style.'-'.$this->color;
}
break;
case 'polaris':
$this->checkOptions['checkboxClass'] = 'icheckbox_'.$this->style.'';
$this->checkOptions['radioClass'] = 'iradio_'.$this->style.'';
break;
case 'futurico':
$this->checkOptions['checkboxClass'] = 'icheckbox_'.$this->style.'';
$this->checkOptions['radioClass'] = 'iradio_'.$this->style.'';
break;
}
}
}