author | Edouard Tisserant |
Fri, 19 Feb 2021 14:56:14 +0100 | |
branch | svghmi |
changeset 3159 | 1d7c3d13a4df |
parent 3062 | 9ec338a99a18 |
child 3232 | 7bdb766c2a4d |
permissions | -rw-r--r-- |
3012 | 1 |
// widget_slider.ysl2 |
2 |
||
3 |
template "widget[@type='Slider']", mode="widget_class" |
|
4 |
|| |
|
5 |
class SliderWidget extends Widget{ |
|
6 |
frequency = 5; |
|
7 |
range = undefined; |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
8 |
handle_orig = undefined; |
3062 | 9 |
scroll_size = undefined; |
10 |
scroll_range = 0; |
|
11 |
scroll_visible = 7; |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
12 |
min_size = 0.07; |
3012 | 13 |
fi = undefined; |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
14 |
curr_value = 0; |
3012 | 15 |
drag = false; |
16 |
enTimer = false; |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
17 |
handle_click = undefined; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
18 |
last_drag = false; |
3012 | 19 |
|
3062 | 20 |
dispatch(value,oldval, index) { |
21 |
if (index == 0){ |
|
22 |
let [min,max,start,totallength] = this.range; |
|
23 |
//save current value inside widget |
|
24 |
this.curr_value = value; |
|
25 |
||
26 |
//check if in range |
|
27 |
if (this.curr_value > max){ |
|
28 |
this.curr_value = max; |
|
29 |
this.apply_hmi_value(0, this.curr_value); |
|
30 |
} |
|
31 |
else if (this.curr_value < min){ |
|
32 |
this.curr_value = min; |
|
33 |
this.apply_hmi_value(0, this.curr_value); |
|
34 |
} |
|
35 |
||
36 |
if(this.value_elt) |
|
37 |
this.value_elt.textContent = String(value); |
|
38 |
} |
|
39 |
else if(index == 1){ |
|
40 |
this.scroll_range = value; |
|
41 |
this.set_scroll(); |
|
42 |
} |
|
43 |
else if(index == 2){ |
|
44 |
this.scroll_visible = value; |
|
45 |
this.set_scroll(); |
|
46 |
} |
|
3012 | 47 |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
48 |
//don't update if draging and setpoint ghost doesn't exist |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
49 |
if(!this.drag || (this.setpoint_elt != undefined)){ |
3062 | 50 |
this.update_DOM(this.curr_value, this.handle_elt); |
51 |
} |
|
52 |
} |
|
53 |
||
54 |
set_scroll(){ |
|
55 |
//check if range is bigger than visible and set scroll size |
|
56 |
if(this.scroll_range > this.scroll_visible){ |
|
57 |
this.scroll_size = this.scroll_range - this.scroll_visible; |
|
58 |
this.range[0] = 0; |
|
59 |
this.range[1] = this.scroll_size; |
|
60 |
} |
|
61 |
else{ |
|
62 |
this.scroll_size = 1; |
|
63 |
this.range[0] = 0; |
|
64 |
this.range[1] = 1; |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
65 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
66 |
} |
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
67 |
|
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
68 |
update_DOM(value, elt){ |
3012 | 69 |
let [min,max,start,totallength] = this.range; |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
70 |
// check if handle is resizeable |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
71 |
if (this.scroll_size != undefined){ //size changes |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
72 |
//get parameters |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
73 |
let length = Math.max(min,Math.min(max,(Number(value)-min)*max/(max-min))); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
74 |
let tip = this.range_elt.getPointAtLength(length); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
75 |
let handle_min = totallength*this.min_size; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
76 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
77 |
let step = 1; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
78 |
//check if range is bigger than max displayed and recalculate step |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
79 |
if ((totallength/handle_min) < (max-min+1)){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
80 |
step = (max-min+1)/(totallength/handle_min-1); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
81 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
82 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
83 |
let kx,ky,offseY,offseX = undefined; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
84 |
//scale on x or y axes |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
85 |
if (this.fi > 0.75){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
86 |
//get scale factor |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
87 |
if(step > 1){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
88 |
ky = handle_min/this.handle_orig.height; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
89 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
90 |
else{ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
91 |
ky = (totallength-handle_min*(max-min))/this.handle_orig.height; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
92 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
93 |
kx = 1; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
94 |
//get 0 offset to stay inside range |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
95 |
offseY = start.y - (this.handle_orig.height + this.handle_orig.y) * ky; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
96 |
offseX = 0; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
97 |
//get distance from value |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
98 |
tip.y =this.range_elt.getPointAtLength(0).y - length/step *handle_min; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
99 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
100 |
else{ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
101 |
//get scale factor |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
102 |
if(step > 1){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
103 |
kx = handle_min/this.handle_orig.width; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
104 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
105 |
else{ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
106 |
kx = (totallength-handle_min*(max-min))/this.handle_orig.width; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
107 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
108 |
ky = 1; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
109 |
//get 0 offset to stay inside range |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
110 |
offseX = start.x - (this.handle_orig.x * kx); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
111 |
offseY = 0; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
112 |
//get distance from value |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
113 |
tip.x =this.range_elt.getPointAtLength(0).x + length/step *handle_min; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
114 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
115 |
elt.setAttribute('transform',"matrix("+(kx)+" 0 0 "+(ky)+" "+(tip.x-start.x+offseX)+" "+(tip.y-start.y+offseY)+")"); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
116 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
117 |
else{ //size stays the same |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
118 |
let length = Math.max(0,Math.min(totallength,(Number(value)-min)*totallength/(max-min))); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
119 |
let tip = this.range_elt.getPointAtLength(length); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
120 |
elt.setAttribute('transform',"translate("+(tip.x-start.x)+","+(tip.y-start.y)+")"); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
121 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
122 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
123 |
// show or hide ghost if exists |
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
124 |
if(this.setpoint_elt != undefined){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
125 |
if(this.last_drag!= this.drag){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
126 |
if(this.drag){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
127 |
this.setpoint_elt.setAttribute("style", this.setpoint_style); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
128 |
}else{ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
129 |
this.setpoint_elt.setAttribute("style", "display:none"); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
130 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
131 |
this.last_drag = this.drag; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
132 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
133 |
} |
3012 | 134 |
} |
135 |
||
136 |
on_release(evt) { |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
137 |
//unbind events |
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
138 |
window.removeEventListener("touchmove", this.on_bound_drag, true); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
139 |
window.removeEventListener("mousemove", this.on_bound_drag, true); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
140 |
|
3059
e0db3f6a5f39
Button and toggle reworked to use animate and dispatch
usveticic
parents:
3056
diff
changeset
|
141 |
window.removeEventListener("mouseup", this.bound_on_release, true); |
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
142 |
window.removeEventListener("touchend", this.bound_on_release, true); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
143 |
window.removeEventListener("touchcancel", this.bound_on_release, true); |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
144 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
145 |
//reset drag flag |
3012 | 146 |
if(this.drag){ |
147 |
this.drag = false; |
|
148 |
} |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
149 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
150 |
// get final position |
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
151 |
this.update_position(evt); |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
152 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
153 |
} |
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
154 |
|
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
155 |
on_drag(evt){ |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
156 |
//ignore drag event for X amount of time and if not selected |
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
157 |
if(this.enTimer && this.drag){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
158 |
this.update_position(evt); |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
159 |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
160 |
//reset timer |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
161 |
this.enTimer = false; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
162 |
setTimeout("{hmi_widgets['"+this.element_id+"'].enTimer = true;}", 100); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
163 |
} |
3012 | 164 |
} |
165 |
||
166 |
update_position(evt){ |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
167 |
var html_dist = 0; |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
168 |
let [min,max,start,totallength] = this.range; |
3012 | 169 |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
170 |
//calculate size of widget in html |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
171 |
var range_borders = this.range_elt.getBoundingClientRect(); |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
172 |
var [minX,minY,maxX,maxY] = [range_borders.left,range_borders.bottom,range_borders.right,range_borders.top]; |
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
173 |
var range_length = Math.sqrt( range_borders.height*range_borders.height + range_borders.width*range_borders.width ); |
3012 | 174 |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
175 |
//get range and mouse coordinates |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
176 |
var mouseX = undefined; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
177 |
var mouseY = undefined; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
178 |
if (evt.type.startsWith("touch")){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
179 |
mouseX = Math.ceil(evt.touches[0].clientX); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
180 |
mouseY = Math.ceil(evt.touches[0].clientY); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
181 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
182 |
else{ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
183 |
mouseX = evt.pageX; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
184 |
mouseY = evt.pageY; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
185 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
186 |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
187 |
// calculate position |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
188 |
if (this.handle_click){ //if clicked on handle |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
189 |
let moveDist = 0, resizeAdd = 0; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
190 |
let range_percent = 1; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
191 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
192 |
//set paramters for resizeable handle |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
193 |
if (this.scroll_size != undefined){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
194 |
// add one more object to stay inside range |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
195 |
resizeAdd = 1; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
196 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
197 |
//chack if range is bigger than display option and |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
198 |
// calculate percent of range with out handle |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
199 |
if(((max/(max*this.min_size)) < (max-min+1))){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
200 |
range_percent = 1-this.min_size; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
201 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
202 |
else{ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
203 |
range_percent = 1-(max-max*this.min_size*(max-min))/max; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
204 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
205 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
206 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
207 |
//calculate value difference on x or y axis |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
208 |
if(this.fi > 0.7){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
209 |
moveDist = ((max-min+resizeAdd)/(range_length*range_percent))*((this.handle_click[1]-mouseY)/Math.sin(this.fi)); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
210 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
211 |
else{ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
212 |
moveDist = ((max-min+resizeAdd)/(range_length*range_percent))*((mouseX-this.handle_click[0])/Math.cos(this.fi)); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
213 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
214 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
215 |
this.curr_value = Math.ceil(this.handle_click[2] + moveDist); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
216 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
217 |
else{ //if clicked on widget |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
218 |
//get handle distance from mouse position |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
219 |
if (minX > mouseX && minY < mouseY){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
220 |
html_dist = 0; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
221 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
222 |
else if (maxX < mouseX && maxY > mouseY){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
223 |
html_dist = range_length; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
224 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
225 |
else{ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
226 |
if(this.fi > 0.7){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
227 |
html_dist = (minY - mouseY)/Math.sin(this.fi); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
228 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
229 |
else{ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
230 |
html_dist = (mouseX - minX)/Math.cos(this.fi); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
231 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
232 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
233 |
//calculate distance |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
234 |
this.curr_value=Math.ceil((html_dist/range_length)*(this.range[1]-this.range[0])+this.range[0]); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
235 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
236 |
|
3062 | 237 |
//check if in range and apply |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
238 |
if (this.curr_value > max){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
239 |
this.curr_value = max; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
240 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
241 |
else if (this.curr_value < min){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
242 |
this.curr_value = min; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
243 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
244 |
this.apply_hmi_value(0, this.curr_value); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
245 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
246 |
//redraw handle |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
247 |
this.request_animate(); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
248 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
249 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
250 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
251 |
animate(){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
252 |
// redraw handle on screen refresh |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
253 |
// check if setpoint(ghost) handle exsist otherwise update main handle |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
254 |
if(this.setpoint_elt != undefined){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
255 |
this.update_DOM(this.curr_value, this.setpoint_elt); |
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
256 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
257 |
else{ |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
258 |
this.update_DOM(this.curr_value, this.handle_elt); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
259 |
} |
3012 | 260 |
} |
261 |
||
262 |
on_select(evt){ |
|
3062 | 263 |
//enable drag flag and timer |
264 |
this.drag = true; |
|
265 |
this.enTimer = true; |
|
266 |
||
267 |
//bind events |
|
268 |
window.addEventListener("touchmove", this.on_bound_drag, true); |
|
269 |
window.addEventListener("mousemove", this.on_bound_drag, true); |
|
270 |
||
271 |
window.addEventListener("mouseup", this.bound_on_release, true); |
|
272 |
window.addEventListener("touchend", this.bound_on_release, true); |
|
273 |
window.addEventListener("touchcancel", this.bound_on_release, true); |
|
274 |
||
275 |
// check if handle was pressed |
|
276 |
if (evt.currentTarget == this.handle_elt){ |
|
277 |
//get mouse position on the handle |
|
278 |
let mouseX = undefined; |
|
279 |
let mouseY = undefined; |
|
280 |
if (evt.type.startsWith("touch")){ |
|
281 |
mouseX = Math.ceil(evt.touches[0].clientX); |
|
282 |
mouseY = Math.ceil(evt.touches[0].clientY); |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
283 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
284 |
else{ |
3062 | 285 |
mouseX = evt.pageX; |
286 |
mouseY = evt.pageY; |
|
287 |
} |
|
288 |
//save coordinates and orig value |
|
289 |
this.handle_click = [mouseX,mouseY,this.curr_value]; |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
290 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
291 |
else{ |
3062 | 292 |
// get new handle position and reset if handle was not pressed |
293 |
this.handle_click = undefined; |
|
294 |
this.update_position(evt); |
|
295 |
} |
|
296 |
||
297 |
//prevent next events |
|
298 |
evt.stopPropagation(); |
|
299 |
||
300 |
} |
|
301 |
||
3012 | 302 |
|
303 |
init() { |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
304 |
//set min max value if not defined |
3012 | 305 |
let min = this.min_elt ? |
306 |
Number(this.min_elt.textContent) : |
|
307 |
this.args.length >= 1 ? this.args[0] : 0; |
|
308 |
let max = this.max_elt ? |
|
309 |
Number(this.max_elt.textContent) : |
|
310 |
this.args.length >= 2 ? this.args[1] : 100; |
|
311 |
||
3059
e0db3f6a5f39
Button and toggle reworked to use animate and dispatch
usveticic
parents:
3056
diff
changeset
|
312 |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
313 |
// save initial parameters |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
314 |
this.range_elt.style.strokeMiterlimit="0"; |
3012 | 315 |
this.range = [min, max, this.range_elt.getPointAtLength(0),this.range_elt.getTotalLength()]; |
316 |
let start = this.range_elt.getPointAtLength(0); |
|
317 |
let end = this.range_elt.getPointAtLength(this.range_elt.getTotalLength()); |
|
318 |
this.fi = Math.atan2(start.y-end.y, end.x-start.x); |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
319 |
this.handle_orig = this.handle_elt.getBBox(); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
320 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
321 |
//bind functions |
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
322 |
this.bound_on_select = this.on_select.bind(this); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
323 |
this.bound_on_release = this.on_release.bind(this); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
324 |
this.on_bound_drag = this.on_drag.bind(this); |
3012 | 325 |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
326 |
this.handle_elt.addEventListener("mousedown", this.bound_on_select); |
3062 | 327 |
this.element.addEventListener("mousedown", this.bound_on_select); |
328 |
this.element.addEventListener("touchstart", this.bound_on_select); |
|
329 |
//touch recognised as page drag without next command |
|
330 |
document.body.addEventListener("touchstart", function(e){}, false); |
|
331 |
||
332 |
//save ghost style |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
333 |
if(this.setpoint_elt != undefined){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
334 |
this.setpoint_style = this.setpoint_elt.getAttribute("style"); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
335 |
this.setpoint_elt.setAttribute("style", "display:none"); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
336 |
} |
3012 | 337 |
|
338 |
} |
|
339 |
} |
|
340 |
|| |
|
341 |
||
342 |
template "widget[@type='Slider']", mode="widget_defs" { |
|
343 |
param "hmi_element"; |
|
344 |
labels("handle range"); |
|
3062 | 345 |
optional_labels("value min max setpoint"); |
3012 | 346 |
|, |
3018 | 347 |
} |