-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathHaspSlider.js
103 lines (88 loc) · 3.22 KB
/
HaspSlider.js
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
import Konva from 'konva';
import { v4 as uuidv4 } from 'uuid';
import HaspBar from './HaspBar';
// import { Rect } from 'konva/lib/shapes/Rect';
import mustache from 'mustache'
export default class HaspSlider extends HaspBar {
//styles
hasAction = false;
bg_color20 = "white";
minWidth = 20;
minHeight = 20;
constructor(config) {
config.entity_id ??= ''; //make sure this object can set its entityid
super(config);
this.knob = new Konva.Circle({
id: uuidv4(),
x: 0,
y: (this.height() / 2),
radius: (this.height()) * 0.8,
stroke: this.borderColor,
strokeWidth: this.borderWidth,
fill: this.theme.bg_color20,
type: "knob",
draggable: true,
});
//enables the button to change thye value
this.knob.on('dragmove', (e) => {
if (this.isHorizontal) {
this.knob.y((this.height() / 2))
if (this.knob.x() < 0) this.knob.x(0);
if (this.knob.x() > this.width()) this.knob.x(this.width());
const newVal = (this.knob.x() / this.width()) * (this.max - this.min);
this.val = Math.round(newVal);
const pos = this.adjust();
} else {
this.knob.x((this.width() / 2))
if (this.knob.y() < 0) this.knob.y(0);
if (this.knob.y() > this.height()) this.knob.y(this.height());
const newVal = this.max -( (this.knob.y() / this.height()) * (this.max - this.min));
this.val = Math.round(newVal);
const pos = this.adjust();
}
});
this.bg_color20 = this.theme.bg_color20;
this.updateSize()
this.add(this.knob);
}
updateSize() {
super.updateSize();
//vertical
if (this.width() < this.height()) {
const halfwidth = this.width() / 2;
const pos = (this.val / (this.max - this.min)) * this.width()
this.setRadius(this.width());
this.knob.radius(this.width() * 0.7);
this.knob.x(halfwidth);
this.knob.y(pos);
this.setBarVal(pos);
} else {
const halfheight = this.height() / 2;
const pos = (this.val / (this.max - this.min)) * this.height()
this.setRadius(this.height());
this.knob.radius(this.height() * 0.7);
this.knob.x(pos);
this.knob.y(halfheight);
this.setBarVal(pos);
}
}
adjust() {
const pos = super.adjust();
if (this.isHorizontal) {
this.knob.x(pos);
} else {
this.knob.y(pos);
}
}
async hassConfigExport(page, hassConfig) {
console.log('mustache')
mustache.tags = ["[[", "]]"];
await fetch('/media/templates/slider.mustache')
.then((response) => response.text())
.then((template) => {
const rendered = mustache.render(template, { page_id: page.haspid, haspid: this.haspid, hass_entityid: this.hass_entityid });
// console.log(rendered);
// hassConfig.push(rendered)
});
}
}