forked from somatonic/FieldtypeRangeSlider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputfieldRangeSlider.module
152 lines (117 loc) · 4.38 KB
/
InputfieldRangeSlider.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
<?php
/**
* ProcessWire Range Slider Inputfieldtype
* by "Soma" Philipp Urlich
*
* ProcessWire 2.x
* Copyright (C) 2010 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class InputfieldRangeSlider extends Inputfield implements InputfieldHasArrayValue{
public static function getModuleInfo() {
return array(
'title' => __('Integer Range Slider', __FILE__), // Module Title
'summary' => __('Simple jQuery UI integer range slider that can be configured.', __FILE__), // Module Summary
'version' => 104,
'requires' => array("FieldtypeRangeSlider")
);
}
public function __construct() {
parent::__construct();
}
public function init() {
return parent::init();
}
public function setAttribute($key, $value) {
if($key == 'value'){
if(!is_array($value)) {
throw new WireException("This input only accepts an array for it's value");
}
}
return parent::setAttribute($key, $value);
}
public function ___render() {
$out = '';
// config
$min = $this->minValue;
$max = $this->maxValue;
$range = $this->isrange ? 'range: true,' : '';
$value = $this->attr('value');
if($this->isrange) $display = "<span id='RangeSlider_{$this->name}_display1'></span> – <span id='RangeSlider_{$this->name}_display2'>0</span>";
else $display = "<span id='RangeSlider_{$this->name}_display1'>0</span>";
$out .= "<div class='RangeSlider RangeSlider_$this->name' id='RangeSlider_$this->name'>$display<div></div></div>";
if($this->isrange) {
$out .= "<input type='hidden' name='{$this->name}[min]' id='Inputfield_" . $this->name . "1' value='" . $value['min'] . "'/>";
$out .= "<input type='hidden' name='{$this->name}[max]' id='Inputfield_" . $this->name . "2' value='" . $value['max'] . "'/>";
$display = "$( '#RangeSlider_{$this->name}_display1' ).text( '$this->prefix' + ui.values[0] + '$this->suffix');";
$display .= "$( '#RangeSlider_{$this->name}_display2' ).text( '$this->prefix' + ui.values[1] + '$this->suffix');";
$constrain = "$( '#Inputfield_{$this->name}1' ).val( ui.values[0] );";
$constrain .= "$( '#Inputfield_{$this->name}2' ).val( ui.values[1] );";
// for change event checks
$constrain .= "$( '#Inputfield_{$this->name}1' ).trigger( 'change' );";
$val = "values: [ $( '#Inputfield_{$this->name}1' ).val(), $( '#Inputfield_{$this->name}2' ).val() ]";
$set = "$( '#RangeSlider_{$this->name}_display1' ).text( '$this->prefix' + $( '#RangeSlider_$this->name div' ).slider( 'values', 0 ) + '$this->suffix' );";
$set .= "$( '#RangeSlider_{$this->name}_display2' ).text( '$this->prefix' + $( '#RangeSlider_$this->name div' ).slider( 'values', 1 ) + '$this->suffix' );";
} else {
$out .= "<input type='hidden' name='{$this->name}[min]' id='Inputfield_" . $this->name . "1' value='" . $value['min'] . "'/>";
$display = "$( '#RangeSlider_{$this->name}_display1' ).text( '$this->prefix' + ui.value + '$this->suffix');";
$constrain = "$( '#Inputfield_{$this->name}1' ).val( ui.value );";
// for change event checks
$constrain .= "$( '#Inputfield_{$this->name}1' ).trigger( 'change' );";
$val = "value: $( '#Inputfield_{$this->name}1' ).val()";
$set = "$( '#RangeSlider_{$this->name}_display1' ).text( '$this->prefix' + $( '#RangeSlider_$this->name div' ).slider( 'value' ) + '$this->suffix' );";
}
$width = $this->width;
$step = $this->step;
$out .= <<<_END
<script>
$(function(){
$( '#RangeSlider_$this->name div' ).slider({
$range
$val,
animate: true,
min: $min,
max: $max,
step: $step,
slide: function( event, ui ) {
$display
$constrain
}
});
$set
});
</script>
<style>
#RangeSlider_$this->name > div { width:$width%!important; }
</style>
_END;
return $out;
}
public function ___processInput(WireInputData $input) {
$name = $this->attr('name');
$range = $this->attr('value');
$inputValue = $input->$name;
if(isset($inputValue)) {
// check for changes in min value
if($range['min'] != $inputValue['min']){
$range['min'] = $inputValue['min'];
$this->trackChange('value');
}
// only if range is enabled
if($this->isrange){
if($range['max'] != $inputValue['max']){
$range['max'] = $inputValue['max'];
$this->trackChange('value');
}
}
}
// set new value
$this->attr("value", $range);
// echo "json:" . wireEncodeJSON((array) $this->attr("value"), true);
return $this;
}
}