-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathICheck.php
194 lines (177 loc) · 6.83 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?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\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
$(document).ready(function(){
if ('$this->style' == 'line') {
if ('$this->color' == 'line') {
$('#$this->idItem > input').each(function(){
var self = $(this),
label = self.next(),
label_text = label.text();
label.remove();
self.iCheck({
checkboxClass: 'icheckbox_line',
radioClass: 'iradio_line',
insert: '<div class="icheck_line-icon"></div>'
});
});
} else {
$('#$this->idItem > input').each(function(){
var self = $(this),
label = self.next(),
label_text = label.text();
label.remove();
self.iCheck({
checkboxClass: 'icheckbox_line-$this->color',
radioClass: 'iradio_line-$this->color',
insert: '<div class="icheck_line-icon"></div>'
});
});
}
}
if ('$this->style' == 'minimal' || '$this->style' == 'square' || '$this->style' == 'flat' || '$this->style' == 'polaris' || '$this->style' == 'futurico') {
$(document).ready(function () {
$(".i-checks-$this->id").iCheck($this->checkOptions);
});
}
});
JS;
$view->registerJs($js);
}
private function getCheckOptions() {
switch ($this->style) {
case 'minimal':
if ($this->color == 'minimal') {
$this->checkOptions += [
'checkboxClass' => 'icheckbox_'.$this->style.'',
'radioClass' => 'iradio_'.$this->style.'',
];
} else {
$this->checkOptions += [
'checkboxClass' => 'icheckbox_'.$this->style.'-'.$this->color,
'radioClass' => 'iradio_'.$this->style.'-'.$this->color,
];
}
break;
case 'square':
if ($this->color == 'square') {
$this->checkOptions += [
'checkboxClass' => 'icheckbox_'.$this->style.'',
'radioClass' => 'iradio_'.$this->style.'',
];
} else {
$this->checkOptions += [
'checkboxClass' => 'icheckbox_'.$this->style.'-'.$this->color,
'radioClass' => 'iradio_'.$this->style.'-'.$this->color,
];
}
break;
case 'flat':
if ($this->color == 'flat') {
$this->checkOptions += [
'checkboxClass' => 'icheckbox_'.$this->style.'',
'radioClass' => 'iradio_'.$this->style.'',
];
} else {
$this->checkOptions += [
'checkboxClass' => 'icheckbox_'.$this->style.'-'.$this->color,
'radioClass' => 'iradio_'.$this->style.'-'.$this->color,
];
}
break;
case 'line':
if ($this->color == 'line') {
$this->checkOptions += [
'checkboxClass' => 'icheckbox_'.$this->style.'',
'radioClass' => 'iradio_'.$this->style.'',
];
} else {
$this->checkOptions += [
'checkboxClass' => 'icheckbox_'.$this->style.'-'.$this->color,
'radioClass' => 'iradio_'.$this->style.'-'.$this->color,
];
}
break;
case 'polaris':
$this->checkOptions += [
'checkboxClass' => 'icheckbox_'.$this->style.'',
'radioClass' => 'iradio_'.$this->style.'',
];
case 'futurico':
$this->checkOptions += [
'checkboxClass' => 'icheckbox_'.$this->style.'',
'radioClass' => 'iradio_'.$this->style.'',
];
break;
}
}
}