-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberControl.php
119 lines (107 loc) · 3.38 KB
/
NumberControl.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
<?php
namespace izisoft\number;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use izisoft\base\InputWidget;
class NumberControl extends InputWidget
{
/**
* @var array masked input plugin options
*/
public $maskedInputOptions = [];
/**
* @inheritdoc
*/
public $pluginName = 'numberControl';
/**
* @var array the HTML attributes for the base model input that will be saved typically to database. The following
* special options are recognized:
* - `type`: _string_, whether to generate a 'hidden' or 'text' input. Defaults to 'hidden'.
* - `label`: _string_, any label to be placed before the input. Will be only displayed if 'type' is 'text'.
*/
public $options = [];
/**
* @var array the HTML attributes for the displayed masked input
*/
public $displayOptions = [];
/**
* @var array the HTML attributes for the container in which the hidden save input will be rendered
*/
public $saveInputContainer = [];
/**
* @inheritdoc
*/
public function run()
{
$this->initWidget();
$this->registerAssets();
echo $this->getDisplayInput() . "\n" . $this->getSaveInput();
}
/**
* Registers the needed assets
*/
public function registerAssets()
{
$view = $this->getView();
NumberControlAsset::register($view);
$id = $this->options['id'];
$this->pluginOptions = [
'displayId' => $this->displayOptions['id'],
'maskedInputOptions' => $this->maskedInputOptions,
];
$this->registerPlugin($this->pluginName, "jQuery('#{$id}')");
}
/**
* Initializes the widget
*/
protected function initWidget()
{
if (!isset($this->displayOptions['id'])) {
$this->displayOptions['id'] = $this->options['id'] . '-disp';
}
if (!isset($this->displayOptions['class'])) {
$this->displayOptions['class'] = 'form-control';
}
if (isset($this->disabled) && $this->disabled) {
$this->displayOptions['disabled'] = true;
}
if (isset($this->readonly) && $this->readonly) {
$this->displayOptions['readonly'] = true;
}
$defaultOptions = [
'alias' => 'numeric',
'digits' => 2,
'groupSeparator' => ',',
'autoGroup' => true,
'autoUnmask' => false
];
$this->maskedInputOptions = array_replace_recursive($defaultOptions, $this->maskedInputOptions);
}
/**
* Generates the display input.
*
* @return string
*/
protected function getDisplayInput()
{
$name = ArrayHelper::remove($this->displayOptions, 'name', $this->displayOptions['id']);
return Html::textInput($name, $this->value, $this->displayOptions);
}
/**
* Generates the save input.
*
* @return string
*/
protected function getSaveInput()
{
$type = ArrayHelper::remove($this->options, 'type', 'hidden');
$out = $this->getInput($type . 'Input');
if ($type === 'text') {
$this->options['tabindex'] = 10000;
$out = ArrayHelper::remove($this->options, 'label', '') . $out;
} else {
Html::addCssStyle($this->saveInputContainer, 'display:none');
}
return Html::tag('div', $out, $this->saveInputContainer);
}
}