-
Notifications
You must be signed in to change notification settings - Fork 0
/
TouchSpinInput.php
95 lines (77 loc) · 2.76 KB
/
TouchSpinInput.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
<?php
namespace dowlatow\widgets;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\MaskedInput;
class TouchSpinInput extends MaskedInput
{
/** @var int */
public $min = 0;
/** @var int */
public $max = 999;
/** @var int */
public $default = 100;
/** @var string */
public $wrapperOptions;
public function run()
{
$this->registerClientScript();
Html::addCssClass($this->wrapperOptions, 'input-touchspin');
echo Html::beginTag('div', $this->wrapperOptions);
if ($this->hasModel()) {
if ($this->default !== false) {
$value = $this->model->{$this->attribute};
if (!is_numeric($value) || $value < $this->min || $value > $this->max) {
$this->model->{$this->attribute} = $this->default;
}
}
echo Html::activeInput($this->type, $this->model, $this->attribute, $this->options);
} else {
if ($this->default !== false) {
if (!is_numeric($this->value) || $this->value < $this->min || $this->value > $this->max) {
$this->value = $this->default;
}
}
echo Html::input($this->type, $this->name, $this->value, $this->options);
}
echo Html::tag('div', null, ['class' => 'caret step-up']);
echo Html::tag('div', null, ['class' => 'caret step-down']);
echo Html::endTag('div');
}
public function init()
{
$this->mask = '9{*}';
parent::init();
}
public function registerClientScript()
{
$this->registerAsset();
$this->getView()->registerJs($this->getInitScript());
}
public function registerAsset()
{
TouchSpinInputAsset::register($this->getView());
}
public function getInitScript()
{
$js = '';
$this->initClientOptions();
if (!empty($this->mask)) {
$this->clientOptions['mask'] = $this->mask;
}
$this->hashPluginOptions($this->getView());
if (is_array($this->definitions) && !empty($this->definitions)) {
$js .= '$.extend($.' . self::PLUGIN_NAME . '.defaults.definitions, ' . Json::htmlEncode($this->definitions) . ");\n";
}
if (is_array($this->aliases) && !empty($this->aliases)) {
$js .= '$.extend($.' . self::PLUGIN_NAME . '.defaults.aliases, ' . Json::htmlEncode($this->aliases) . ");\n";
}
$id = $this->options['id'];
$js .= '$("#' . $id . '").' . self::PLUGIN_NAME . "(" . $this->_hashVar . ");\n";
$js .= '$("#' . $this->options['id'] . '").touchSpinInput({
minValue: ' . $this->min . ',
maxValue: ' . $this->max . '
});' . "\n";
return $js;
}
}