This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAutoNumeric.php
120 lines (108 loc) · 3.03 KB
/
AutoNumeric.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
<?php
/**
* @copyright Maxim Perfilev, 2015
* @package yii2-autonumeric
* @version 1.0.0
*/
namespace extead\autonumeric;
use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
/**
* Class AutoNumeric
* @package autonumeric
* @author Maxim Perfilev <[email protected]>
* @since 1.0.0
*/
class AutoNumeric extends \yii\widgets\InputWidget {
/**
* @var string
*/
protected $_pluginName = 'autoNumeric';
/**
* @var array
*/
private $_inputOptions = [];
/**
* @var array
*/
public $pluginOptions = [];
/**
* @throws \yii\base\InvalidConfigException
*/
public function init()
{
parent::init();
$this->initPluginOptions();
$this->_inputOptions = $this->options;
$this->_inputOptions['id'] .= '-disp';
if (isset($this->_inputOptions['name'])) {
unset($this->_inputOptions['name']);
}
$this->registerAssets();
$this->renderInput();
}
/**
* Render hidden "real" input and visible input for formatted value
*/
protected function renderInput()
{
$name = $this->_inputOptions['id'];
Html::addCssClass($this->_inputOptions, 'form-control');
$input = Html::textInput($name, $this->value, $this->_inputOptions);
$input .= $this->hasModel() ?
Html::activeHiddenInput($this->model, $this->attribute, $this->options) :
Html::hiddenInput($this->name, $this->value, $this->options);
echo $input;
}
/**
* Load options from Yii::$app->formatter
*
* @param $paramFrom
* @param $paramTo
*/
protected function setDefaultFormat($paramFrom, $paramTo)
{
$formatter = Yii::$app->formatter;
if (empty($this->pluginOptions[$paramTo]) && !empty($formatter->$paramFrom)) {
$this->pluginOptions[$paramTo] = $formatter->$paramFrom;
}
}
/**
* Load default plugin options from params autoNumeric section
*/
protected function initPluginOptions()
{
if (!empty(Yii::$app->params['autoNumericOptions'])) {
$this->pluginOptions += Yii::$app->params['autoNumericOptions'];
} else {
$this->setDefaultFormat('decimalSeparator', 'aDec');
$this->setDefaultFormat('thousandSeparator', 'aSep');
}
}
/**
* Register assets.
*/
public function registerAssets()
{
$view = $this->getView();
AutoNumericAsset::register($view);
$id = 'jQuery("#' . $this->_inputOptions['id'] . '")';
$idSave = 'jQuery("#' . $this->options['id'] . '")';
$plugin = $this->_pluginName;
$pluginOptions = Json::encode($this->pluginOptions);
$js = <<< JS
var val = parseFloat({$idSave}.val());
{$id}.{$plugin}('init', {$pluginOptions});
if(!isNaN(val)) {
{$id}.{$plugin}('set', val);
}
{$id}.on('change', function () {
var unformatted = {$id}.{$plugin}('get');
{$idSave}.val(unformatted);
{$idSave}.trigger('change');
});
JS;
$view->registerJs($js);
}
}