author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Tue, 20 Oct 2020 00:23:52 +0200 | |
branch | svghmi |
changeset 3063 | 466c3df67835 |
parent 3056 | 827bf284feec |
child 3059 | e0db3f6a5f39 |
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; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
9 |
scroll_size = 10; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
10 |
min_size = 0.07; |
3012 | 11 |
fi = undefined; |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
12 |
curr_value = 0; |
3012 | 13 |
drag = false; |
14 |
enTimer = false; |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
15 |
handle_click = undefined; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
16 |
last_drag = false; |
3012 | 17 |
|
18 |
dispatch(value) { |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
19 |
//save current value inside widget |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
20 |
this.curr_value = value; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
21 |
|
3020
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
22 |
if(this.value_elt) |
895bbeced72d
SVGHMI: Update Slider widget so that it doesn't display future value but actual value only.
Edouard Tisserant
parents:
3018
diff
changeset
|
23 |
this.value_elt.textContent = String(value); |
3012 | 24 |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
25 |
//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
|
26 |
if(!this.drag || (this.setpoint_elt != undefined)){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
27 |
this.update_DOM(value, this.handle_elt); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
28 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
29 |
} |
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
30 |
|
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
31 |
update_DOM(value, elt){ |
3012 | 32 |
let [min,max,start,totallength] = this.range; |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
33 |
// check if handle is resizeable |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
34 |
if (this.scroll_size != undefined){ //size changes |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
35 |
//get parameters |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
36 |
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
|
37 |
let tip = this.range_elt.getPointAtLength(length); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
38 |
let handle_min = totallength*this.min_size; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
39 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
40 |
let step = 1; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
41 |
//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
|
42 |
if ((totallength/handle_min) < (max-min+1)){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
43 |
step = (max-min+1)/(totallength/handle_min-1); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
44 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
45 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
46 |
let kx,ky,offseY,offseX = undefined; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
47 |
//scale on x or y axes |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
48 |
if (this.fi > 0.75){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
49 |
//get scale factor |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
50 |
if(step > 1){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
51 |
ky = handle_min/this.handle_orig.height; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
52 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
53 |
else{ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
54 |
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
|
55 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
56 |
kx = 1; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
57 |
//get 0 offset to stay inside range |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
58 |
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
|
59 |
offseX = 0; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
60 |
//get distance from value |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
61 |
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
|
62 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
63 |
else{ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
64 |
//get scale factor |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
65 |
if(step > 1){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
66 |
kx = handle_min/this.handle_orig.width; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
67 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
68 |
else{ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
69 |
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
|
70 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
71 |
ky = 1; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
72 |
//get 0 offset to stay inside range |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
73 |
offseX = start.x - (this.handle_orig.x * kx); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
74 |
offseY = 0; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
75 |
//get distance from value |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
76 |
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
|
77 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
78 |
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
|
79 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
80 |
else{ //size stays the same |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
81 |
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
|
82 |
let tip = this.range_elt.getPointAtLength(length); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
83 |
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
|
84 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
85 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
86 |
// 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
|
87 |
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
|
88 |
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
|
89 |
if(this.drag){ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
90 |
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
|
91 |
}else{ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
92 |
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
|
93 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
94 |
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
|
95 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
96 |
} |
3012 | 97 |
} |
98 |
||
99 |
on_release(evt) { |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
100 |
//unbind events |
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
101 |
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
|
102 |
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
|
103 |
|
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
104 |
window.removeEventListener("mouseup", 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
|
105 |
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
|
106 |
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
|
107 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
108 |
//reset drag flag |
3012 | 109 |
if(this.drag){ |
110 |
this.drag = false; |
|
111 |
} |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
112 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
113 |
// get final position |
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
114 |
this.update_position(evt); |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
115 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
116 |
} |
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
117 |
|
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
118 |
on_drag(evt){ |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
119 |
//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
|
120 |
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
|
121 |
this.update_position(evt); |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
122 |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
123 |
//reset timer |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
124 |
this.enTimer = false; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
125 |
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
|
126 |
} |
3012 | 127 |
} |
128 |
||
129 |
update_position(evt){ |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
130 |
var html_dist = 0; |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
131 |
let [min,max,start,totallength] = this.range; |
3012 | 132 |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
133 |
//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
|
134 |
var range_borders = this.range_elt.getBoundingClientRect(); |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
135 |
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
|
136 |
var range_length = Math.sqrt( range_borders.height*range_borders.height + range_borders.width*range_borders.width ); |
3012 | 137 |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
138 |
//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
|
139 |
var mouseX = undefined; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
140 |
var mouseY = undefined; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
141 |
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
|
142 |
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
|
143 |
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
|
144 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
145 |
else{ |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
146 |
mouseX = evt.pageX; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
147 |
mouseY = evt.pageY; |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
148 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
149 |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
150 |
// calculate position |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
151 |
if (this.handle_click){ //if clicked on handle |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
152 |
let moveDist = 0, resizeAdd = 0; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
153 |
let range_percent = 1; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
154 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
155 |
//set paramters for resizeable handle |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
156 |
if (this.scroll_size != undefined){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
157 |
// add one more object to stay inside range |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
158 |
resizeAdd = 1; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
159 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
160 |
//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
|
161 |
// calculate percent of range with out handle |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
162 |
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
|
163 |
range_percent = 1-this.min_size; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
164 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
165 |
else{ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
166 |
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
|
167 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
168 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
169 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
170 |
//calculate value difference on x or y axis |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
171 |
if(this.fi > 0.7){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
172 |
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
|
173 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
174 |
else{ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
175 |
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
|
176 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
177 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
178 |
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
|
179 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
180 |
else{ //if clicked on widget |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
181 |
//get handle distance from mouse position |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
182 |
if (minX > mouseX && minY < mouseY){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
183 |
html_dist = 0; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
184 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
185 |
else if (maxX < mouseX && maxY > mouseY){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
186 |
html_dist = range_length; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
187 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
188 |
else{ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
189 |
if(this.fi > 0.7){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
190 |
html_dist = (minY - mouseY)/Math.sin(this.fi); |
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 |
else{ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
193 |
html_dist = (mouseX - minX)/Math.cos(this.fi); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
194 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
195 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
196 |
//calculate distance |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
197 |
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
|
198 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
199 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
200 |
//check if in range |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
201 |
if (this.curr_value > max){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
202 |
this.curr_value = max; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
203 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
204 |
else if (this.curr_value < min){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
205 |
this.curr_value = min; |
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 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
208 |
this.apply_hmi_value(0, this.curr_value); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
209 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
210 |
//redraw handle |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
211 |
this.request_animate(); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
212 |
|
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 |
animate(){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
216 |
// redraw handle on screen refresh |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
217 |
// 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
|
218 |
if(this.setpoint_elt != undefined){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
219 |
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
|
220 |
} |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
221 |
else{ |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
222 |
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
|
223 |
} |
3012 | 224 |
} |
225 |
||
226 |
on_select(evt){ |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
227 |
//enable drag flag and timer |
3012 | 228 |
this.drag = true; |
229 |
this.enTimer = true; |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
230 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
231 |
//bind events |
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
232 |
window.addEventListener("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
|
233 |
window.addEventListener("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
|
234 |
|
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
235 |
window.addEventListener("mouseup", 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
|
236 |
window.addEventListener("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
|
237 |
window.addEventListener("touchcancel", this.bound_on_release, true); |
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
238 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
239 |
// check if handle was pressed |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
240 |
if (evt.currentTarget == this.handle_elt){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
241 |
//get mouse position on the handle |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
242 |
let mouseX = undefined; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
243 |
let mouseY = undefined; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
244 |
if (evt.type.startsWith("touch")){ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
245 |
mouseX = Math.ceil(evt.touches[0].clientX); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
246 |
mouseY = Math.ceil(evt.touches[0].clientY); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
247 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
248 |
else{ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
249 |
mouseX = evt.pageX; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
250 |
mouseY = evt.pageY; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
251 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
252 |
//save coordinates and orig value |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
253 |
this.handle_click = [mouseX,mouseY,this.curr_value]; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
254 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
255 |
else{ |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
256 |
// get new handle position and reset if handle was not pressed |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
257 |
this.handle_click = undefined; |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
258 |
this.update_position(evt); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
259 |
} |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
260 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
261 |
//prevent next events |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
262 |
evt.stopPropagation(); |
3012 | 263 |
} |
264 |
||
265 |
init() { |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
266 |
//set min max value if not defined |
3012 | 267 |
let min = this.min_elt ? |
268 |
Number(this.min_elt.textContent) : |
|
269 |
this.args.length >= 1 ? this.args[0] : 0; |
|
270 |
let max = this.max_elt ? |
|
271 |
Number(this.max_elt.textContent) : |
|
272 |
this.args.length >= 2 ? this.args[1] : 100; |
|
273 |
||
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
274 |
// save initial parameters |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
275 |
this.range_elt.style.strokeMiterlimit="0"; |
3012 | 276 |
this.range = [min, max, this.range_elt.getPointAtLength(0),this.range_elt.getTotalLength()]; |
277 |
let start = this.range_elt.getPointAtLength(0); |
|
278 |
let end = this.range_elt.getPointAtLength(this.range_elt.getTotalLength()); |
|
279 |
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
|
280 |
this.handle_orig = this.handle_elt.getBBox(); |
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
281 |
|
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
282 |
//bind functions |
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
283 |
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
|
284 |
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
|
285 |
this.on_bound_drag = this.on_drag.bind(this); |
3012 | 286 |
|
3056
827bf284feec
Button, ToggleButton and slider updated. Error to warning when building
usveticic
parents:
3045
diff
changeset
|
287 |
this.handle_elt.addEventListener("mousedown", this.bound_on_select); |
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
288 |
this.element.addEventListener("mousedown", this.bound_on_select); |
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
289 |
this.element.addEventListener("touchstart", this.bound_on_select); |
3012 | 290 |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
291 |
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
|
292 |
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
|
293 |
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
|
294 |
} |
3012 | 295 |
|
296 |
} |
|
297 |
} |
|
298 |
|| |
|
299 |
||
300 |
template "widget[@type='Slider']", mode="widget_defs" { |
|
301 |
param "hmi_element"; |
|
302 |
labels("handle range"); |
|
3021
49799de67540
SVGHMI: add a SetPoint to Slider Widget, visible only when operating the slider.
Edouard Tisserant
parents:
3020
diff
changeset
|
303 |
optional_labels("value min max setpoint"); |
3012 | 304 |
|, |
3018 | 305 |
} |