-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInputfieldFloatRange.module
214 lines (174 loc) · 6.53 KB
/
InputfieldFloatRange.module
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
namespace ProcessWire;
/**
* ProcessWire FloatRange Inputfield
* By @eelkenet, https://eelke.net
*/
class InputfieldFloatRange extends InputfieldFloat
{
public static function getModuleInfo()
{
return array(
"title" => __("Float (Range)", __FILE__),
"summary" => __("Range slider inputfield, with precision, step values & optional manual override", __FILE__), // Module Summary
"version" => 9,
"permanent" => false,
"icon" => "sliders",
"href" => "https://github.com/eelke/InputfieldFloatRange",
"author" => "Eelke Feenstra, eelke.net",
"installs" => ["FieldtypeFloatRange"],
"requires" => ["ProcessWire>=3.0.83"]
);
}
public function init()
{
parent::init();
$this->set("displayValueField", 0);
$this->set("inputType", "range");
$this->set("roundingMethod", "round");
$this->set("defaultValue", ""); // blank means not set
$this->attr("step", ""); // blank means not set
$this->attr("min", "0");
$this->attr("max", "100");
}
public function renderReady(Inputfield $parent = null, $renderValueMode = false)
{
parent::renderReady();
$otherFields = $this->config->js("InputfieldFloatRange") ? $this->config->js("InputfieldFloatRange") : [];
$this->config->js("InputfieldFloatRange", array_merge($otherFields, [
"Inputfield_" . $this->attr("name") => [
"precision" => $this->get("precision"),
"rounding" => $this->get("roundingMethod"),
"displayValueField" => $this->get("displayValueField"),
"defaultValue" => $this->attr("defaultValue"),
"min" => $this->attr("min"),
"max" => $this->attr("max"),
"step" => (float) $this->attr("step")
]
]));
}
public function render()
{
$attrs = $this->getAttributes();
if ($this->attr("step") == "" || ($this->get("displayValueField") && $this->get("roundingMethod") == "disabled")) {
$this->attr("step", "any");
}
$this->addClass($this->get("displayValueField") ? "InputfieldFloatRange--with-value" : "InputfieldFloatRange--without-value");
// If there is no value:
if (!strlen($attrs["value"])) {
$min = (float) $this->get("min");
$max = (float) $this->get("max");
// Add optional default value
if (strlen($this->get("defaultValue"))) {
// Make sure to fit defaultValue between min and max values
$defaultValue = max($min, min((float) $this->get("defaultValue"), $max));
// Round according to settings
$this->value = $this->roundValue($defaultValue);
} else {
// Else set value to halfway point
$val = ($min + $max) / 2;
// Round according to settings
$this->value = $this->roundValue($val);
}
$attrs["value"] = $this->value;
}
$out = parent::render();
if ($this->get("displayValueField")) {
$out .= "<input type='text' class='InputfieldFloatRange__display' value='$this->value' />";
}
return $out;
}
/** Round a value to the match the current step, rouding method and precision settings
* @return float
*/
private function roundValue(float $val)
{
$step = $this->get("step");
$rounding = $this->get("roundingMethod");
$precision = $this->get("precision");
$value = $val;
if ($step !== "" && $step !== 0 && $step !== "any") {
if ($rounding === "round") $value = $step * round($value / $step);
else if ($rounding === "floor") $value = $step * floor($value / $step);
else if ($rounding === "ceil") $value = $step * ceil($value / $step);
}
if ($precision > -1) {
return number_format($value, $precision, ".", "");
} else return $value;
}
public function set($key, $value)
{
if ($key == "inputType") {
$this->attr("type", $value);
} else if ($key == "min" || $key == "max" || $key == "step" || $key == "defaultValue") {
if (strlen("$value")) {
$value = strpos($value, ",") !== false ? str_replace(",", ".", $value) : $value;
$value = strpos($value, ".") !== false ? (float) $value : (int) $value;
}
}
return parent::set($key, $value);
}
/**
* Extend InputfieldFloat"s inputType option with "range"
*/
public function getConfigInputfields()
{
$inputfields = parent::getConfigInputfields();
$f = $inputfields->get("inputType");
$f->addOption("range", $this->_("Range"));
$f->attr("value", $this->inputType);
$f = $inputfields->get("min");
$f->description = $this->_("The minimum allowed value for this field. Default value = 0.");
$f->required = true;
$f->columnWidth = 25;
$f = $inputfields->get("max");
$f->description = $this->_("The maximum allowed value for this field. Default value = 100.");
$f->required = true;
$f->columnWidth = 25;
/** @var InputfieldText $f */
$f = $this->modules->get("InputfieldText");
$f->attr("name", "defaultValue");
$f->attr("value", $this->defaultValue);
$f->label = $this->_("Default value");
$f->description =
$this->_("The (optional) default value for this field. ");
$f->notes = $this->_("If kept empty, the slider will initially be set halfway min and max values");
$f->required = false;
$f->columnWidth = 25;
$inputfields->add($f);
/** @var InputfieldText $f */
$f = $this->modules->get("InputfieldText");
$f->attr("name", "step");
$f->attr("value", $this->attr("step"));
$f->label = $this->_("Step");
$f->columnWidth = 25;
$f->description = $this->_("Step increment (float). Leave blank to ignore");
$inputfields->add($f);
/** @var InputfieldCheckbox $f */
$f = $this->modules->get("InputfieldCheckbox");
$f->attr("name", "displayValueField");
$f->set("value", 1);
$f->attr("checked", $this->get("displayValueField") == 1 ? "checked" : "");
$f->label = $this->_("Show value next to slider");
$f->label2 = $this->_("Show");
$f->description = $this->_("Display the input of the field next to the slider");
$f->columnWidth = 25;
$inputfields->add($f);
/** @var InputfieldSelect $f */
$f = $this->modules->get("InputfieldSelect");
$f->attr("name", "roundingMethod");
$f->label = $this->_("Step rounding method");
$f->description = $this->_("Select the rounding method for manually entered values (N). ");
$f->notes = $this->_("Unless disabled, manually entered values are rounded (using this function) when a user leaves the input field");
$f->required = true;
$f->showIf("displayValueField=1,step!=''");
$f->addOption("round", "Round (Default; Round N to the nearest step)");
$f->addOption("floor", "Floor (Round N to the previous step)");
$f->addOption("ceil", "Ceil (Round N to next step)");
$f->addOption("disabled", "Disabled (WARNING: This also disables the Step setting!)");
$f->attr("value", $this->get("roundingMethod"));
$f->columnWidth = 75;
$inputfields->add($f);
return $inputfields;
}
}