AbstractNumericControl.php
3.74 KB
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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Controls;
use YahnisElsts\AdminMenuEditor\Customizable\HtmlHelper;
use YahnisElsts\AdminMenuEditor\Customizable\Settings;
abstract class AbstractNumericControl extends ClassicControl {
const NUMBER_VALIDATION_PATTERN = '\\s*-?[0-9]+(?:[.,]\\d*)?\s*';
protected $min = null;
protected $max = null;
protected $step = null;
protected $rangeByUnit = [];
protected $spinButtonsAllowed = false;
public function __construct($settings = [], $params = []) {
parent::__construct($settings, $params);
//Range.
if ( array_key_exists('min', $params) ) {
$this->min = $params['min'];
} else if ( $this->mainSetting instanceof Settings\NumericSetting ) {
$this->min = $this->mainSetting->getMinValue();
}
if ( array_key_exists('max', $params) ) {
$this->max = $params['max'];
} else if ( $this->mainSetting instanceof Settings\NumericSetting ) {
$this->max = $this->mainSetting->getMaxValue();
}
//Step.
if ( array_key_exists('step', $params) ) {
//Step must be a positive number, null, or the special value "any".
if ( is_numeric($params['step']) ) {
$this->step = abs($params['step']);
} else if ( ($params['step'] === null) || ($params['step'] === 'any') ) {
$this->step = $params['step'];
} else {
throw new \InvalidArgumentException("Invalid step value: {$params['step']}");
}
}
//Each unit can have a different range.
if ( array_key_exists('rangeByUnit', $params) ) {
$this->rangeByUnit = $params['rangeByUnit'];
}
}
protected function getSliderRanges() {
$sliderRanges = [];
if ( ($this->min !== null) && ($this->max !== null) ) {
$sliderRanges['_default'] = [
'min' => $this->min,
'max' => $this->max,
'step' => $this->getDefaultStep(),
];
}
return array_merge($sliderRanges, $this->rangeByUnit);
}
/**
* @return float|int
*/
protected function getDefaultStep() {
if ( is_numeric($this->step) ) {
$step = (float)$this->step;
} else {
if ( $this->mainSetting instanceof Settings\FloatSetting ) {
$step = ($this->max - $this->min) / 100;
} else {
$step = 1;
}
}
return $step;
}
protected function getBasicInputAttributes() {
$attributes = [
'min' => $this->min,
'max' => $this->max,
'step' => $this->step,
];
if ( $this->spinButtonsAllowed ) {
$attributes['type'] = 'number';
} else {
$attributes['type'] = 'text';
$attributes['inputmode'] = 'numeric';
$attributes['pattern'] = self::NUMBER_VALIDATION_PATTERN;
$attributes['maxlength'] = 20;
}
return $attributes;
}
protected function renderUnitDropdown(
Settings\StringEnumSetting $unitSetting,
$elementAttributes = [],
$includeKoBindings = true
) {
//Display a dropdown list of units.
$units = $unitSetting->generateChoiceOptions();
$selectedUnit = $unitSetting->getValue();
list($optionHtml, $optionBindings) = ChoiceControlOption::generateSelectOptions(
$units,
$selectedUnit,
$unitSetting
);
if ( $includeKoBindings ) {
$elementAttributes['data-bind'] = $this->makeKoDataBind(array_merge(
$optionBindings,
['value' => $this->getKoObservableExpression($selectedUnit, $unitSetting)],
$this->getKoEnableBinding()
));
}
echo HtmlHelper::tag('select', $elementAttributes);
//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $optionHtml;
echo '</select>';
}
protected function getKoComponentParams() {
$params = parent::getKoComponentParams();
$params['min'] = $this->min;
$params['max'] = $this->max;
$params['step'] = $this->getDefaultStep();
$sliderRanges = $this->getSliderRanges();
if ( !empty($sliderRanges) ) {
$params['sliderRanges'] = $sliderRanges;
}
return $params;
}
}