forked from AmoebeLabs/flex-horseshoe-card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flex-horseshoe-card.js
1938 lines (1629 loc) · 56.3 KB
/
flex-horseshoe-card.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Card : flex-horseshoe-card.js
* Project : Home Assistant
* Repository: https://github.com/AmoebeLabs/
*
* Author : Mars @ AmoebeLabs.com
*
* License : MIT
*
* -----
* Description:
* The Flexible Horseshoe Card.
*
* Refs:
* - https://github.com/AmoebeLabs/flex-horseshoe-card
*
*******************************************************************************
*/
import {
LitElement, html, css, svg
} from "https://unpkg.com/[email protected]/lit-element.js?module";
//++ Consts ++++++++++
const FONT_SIZE = 12;
const SVG_VIEW_BOX = 200;
// Donut starts at -220 degrees and is 260 degrees in size.
// zero degrees is at 3 o'clock.
const HORSESHOE_RADIUS_SIZE = 0.45 * SVG_VIEW_BOX;
const TICKMARKS_RADIUS_SIZE = 0.43 * SVG_VIEW_BOX;
const HORSESHOE_PATH_LENGTH = 2 * 260/360 * Math.PI * HORSESHOE_RADIUS_SIZE;
const DEFAULT_SHOW = {
horseshoe: true,
scale_tickmarks: false,
horseshoe_style: 'fixed',
}
const DEFAULT_HORSESHOE_SCALE = {
min: 0,
max: 100,
width: 6,
color: 'var(--primary-background-color)',
}
const DEFAULT_HORSESHOE_STATE = {
width: 12,
color: 'var(--primary-color)',
}
//--
//++ Class ++++++++++
class FlexHorseshoeCard extends LitElement {
constructor() {
super();
// Get cardId for unique SVG gradient Id
this.cardId = Math.random().toString(36).substr(2, 9);
this.entities = [];
this.entitiesStr = [];
this.attributesStr = [];
this.viewBoxSize = SVG_VIEW_BOX;
this.colorStops = {};
this.animations = {};
this.animations.vlines = {};
this.animations.hlines = {};
this.animations.circles = {};
this.animations.icons = {};
this.animations.names = {};
this.animations.areas = {};
this.animations.states = {};
this.colorCache = {};
// http://jsfiddle.net/jlubean/dL5cLjxt/
this.isSafari = !!navigator.userAgent.match(/Version\/[\d\.]+.*Safari/);
this.iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
}
/*******************************************************************************
* Summary.
* Implements the properties method
*
*/
/*
static get properties() {
return {
hass: {},
config: {},
states: [],
statesStr: [],
dashArray: String,
color1_offset: String,
color0: String,
color1: String,
angleCoords: Object
}
}
*/
/*******************************************************************************
* styles()
*
* Summary.
* Returns the static CSS styles for the lit-element
*
* Note:
* - The BEM (http://getbem.com/naming/) naming style for CSS is used
* Of course, if no mistakes are made ;-)
*
*/
static get styles() {
return css`
:host {
cursor: pointer;
}
@media (print), (prefers-reduced-motion: reduce) {
.animated {
animation-duration: 1ms !important;
transition-duration: 1ms !important;
animation-iteration-count: 1 !important;
}
}
@keyframes zoomOut {
from {
opacity: 1;
}
50% {
opacity: 0;
transform: scale3d(0.3, 0.3, 0.3);
}
to {
opacity: 0;
}
}
@keyframes bounce {
from,
20%,
53%,
80%,
to {
animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
transform: translate3d(0, 0, 0);
}
40%,
43% {
animation-timing-function: cubic-bezier(0.755, 0.05, 0.855, 0.06);
transform: translate3d(0, -30px, 0);
}
70% {
animation-timing-function: cubic-bezier(0.755, 0.05, 0.855, 0.06);
transform: translate3d(0, -15px, 0);
}
90% {
transform: translate3d(0, -4px, 0);
}
}
@keyframes flash {
from,
50%,
to {
opacity: 1;
}
25%,
75% {
opacity: 0;
}
}
@keyframes headShake {
0% {
transform: translateX(0);
}
6.5% {
transform: translateX(-6px) rotateY(-9deg);
}
18.5% {
transform: translateX(5px) rotateY(7deg);
}
31.5% {
transform: translateX(-3px) rotateY(-5deg);
}
43.5% {
transform: translateX(2px) rotateY(3deg);
}
50% {
transform: translateX(0);
}
}
@keyframes heartBeat {
0% {
transform: scale(1);
}
14% {
transform: scale(1.3);
}
28% {
transform: scale(1);
}
42% {
transform: scale(1.3);
}
70% {
transform: scale(1);
}
}
@keyframes jello {
from,
11.1%,
to {
transform: translate3d(0, 0, 0);
}
22.2% {
transform: skewX(-12.5deg) skewY(-12.5deg);
}
33.3% {
transform: skewX(6.25deg) skewY(6.25deg);
}
44.4% {
transform: skewX(-3.125deg) skewY(-3.125deg);
}
55.5% {
transform: skewX(1.5625deg) skewY(1.5625deg);
}
66.6% {
transform: skewX(-0.78125deg) skewY(-0.78125deg);
}
77.7% {
transform: skewX(0.390625deg) skewY(0.390625deg);
}
88.8% {
transform: skewX(-0.1953125deg) skewY(-0.1953125deg);
}
}
@keyframes pulse {
from {
transform: scale3d(1, 1, 1);
}
50% {
transform: scale3d(1.05, 1.05, 1.05);
}
to {
transform: scale3d(1, 1, 1);
}
}
@keyframes rubberBand {
from {
transform: scale3d(1, 1, 1);
}
30% {
transform: scale3d(1.25, 0.75, 1);
}
40% {
transform: scale3d(0.75, 1.25, 1);
}
50% {
transform: scale3d(1.15, 0.85, 1);
}
65% {
transform: scale3d(0.95, 1.05, 1);
}
75% {
transform: scale3d(1.05, 0.95, 1);
}
to {
transform: scale3d(1, 1, 1);
}
}
@keyframes shake {
from,
to {
transform: translate3d(0, 0, 0);
}
10%,
30%,
50%,
70%,
90% {
transform: translate3d(-10px, 0, 0);
}
20%,
40%,
60%,
80% {
transform: translate3d(10px, 0, 0);
}
}
@keyframes swing {
20% {
transform: rotate3d(0, 0, 1, 15deg);
}
40% {
transform: rotate3d(0, 0, 1, -10deg);
}
60% {
transform: rotate3d(0, 0, 1, 5deg);
}
80% {
transform: rotate3d(0, 0, 1, -5deg);
}
to {
transform: rotate3d(0, 0, 1, 0deg);
}
}
@keyframes tada {
from {
transform: scale3d(1, 1, 1);
}
10%,
20% {
transform: scale3d(0.9, 0.9, 0.9) rotate3d(0, 0, 1, -3deg);
}
30%,
50%,
70%,
90% {
transform: scale3d(1.1, 1.1, 1.1) rotate3d(0, 0, 1, 3deg);
}
40%,
60%,
80% {
transform: scale3d(1.1, 1.1, 1.1) rotate3d(0, 0, 1, -3deg);
}
to {
transform: scale3d(1, 1, 1);
}
}
@keyframes wobble {
from {
transform: translate3d(0, 0, 0);
}
15% {
transform: translate3d(-25%, 0, 0) rotate3d(0, 0, 1, -5deg);
}
30% {
transform: translate3d(20%, 0, 0) rotate3d(0, 0, 1, 3deg);
}
45% {
transform: translate3d(-15%, 0, 0) rotate3d(0, 0, 1, -3deg);
}
60% {
transform: translate3d(10%, 0, 0) rotate3d(0, 0, 1, 2deg);
}
75% {
transform: translate3d(-5%, 0, 0) rotate3d(0, 0, 1, -1deg);
}
to {
transform: translate3d(0, 0, 0);
}
}
@media screen and (min-width: 467px) {
:host {
font-size: 12px;
}
}
@media screen and (max-width: 466px) {
:host {
font-size: 12px;
}
}
:host ha-card {
padding: 10px 10px 10px 10px;
}
.container {
position: relative;
height: 100%;
display: flex;
flex-direction: column;
}
.labelContainer {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 65%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-end;
}
.ellipsis {
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
.state {
position: relative;
display: flex;
flex-wrap: wrap;
max-width: 100%;
min-width: 0px;
}
#label {
display: flex;
line-height: 1;
}
#label.bold {
font-weight: bold;
}
#label, #name {
margin: 3% 0;
}
.text {
font-size: 100%;
}
#name {
font-size: 80%;
font-weight: 300;
}
.unit {
font-size: 65%;
font-weight: normal;
opacity: 0.6;
line-height: 2em;
vertical-align: bottom;
margin-left: 0.25rem;
}
.entity__area {
position: absolute;
top: 70%;
font-size: 120%;
opacity: 0.6;
display: flex;
line-height: 1;
align-items: center;
justify-content: center;
width: 100%;
height: 20%;
flex-direction: column;
}
.nam {
alignment-baseline: central;
fill: var(--primary-text-color);
}
.state__uom {
font-size: 20px;
opacity: 0.7;
margin: 0;
fill : var(--primary-text-color);
}
.state__value {
font-size: 3em;
opacity: 1;
fill : var(--primary-text-color);
text-anchor: middle;
}
.entity__name {
text-anchor: middle;
overflow: hidden;
opacity: 0.8;
fill : var(--primary-text-color);
font-size: 1.5em;
text-transform: uppercase;
letter-spacing: 0.1em;
}
.entity__area {
font-size: 12px;
opacity: 0.7;
overflow: hidden;
fill : var(--primary-text-color);
text-anchor: middle;
text-transform: uppercase;
letter-spacing: 0.1em;
}
.shadow {
font-size: 30px;
font-weight: 700;
text-anchor: middle;
}
.card--dropshadow-5 {
filter: drop-shadow(0 1px 0 #ccc)
drop-shadow(0 2px 0 #c9c9c9)
drop-shadow(0 3px 0 #bbb)
drop-shadow(0 4px 0 #b9b9b9)
drop-shadow(0 5px 0 #aaa)
drop-shadow(0 6px 1px rgba(0,0,0,.1))
drop-shadow(0 0 5px rgba(0,0,0,.1))
drop-shadow(0 1px 3px rgba(0,0,0,.3))
drop-shadow(0 3px 5px rgba(0,0,0,.2))
drop-shadow(0 5px 10px rgba(0,0,0,.25))
drop-shadow(0 10px 10px rgba(0,0,0,.2))
drop-shadow(0 20px 20px rgba(0,0,0,.15));
}
.card--dropshadow-medium--opaque--sepia90 {
filter: drop-shadow(0.0em 0.05em 0px #b2a98f22)
drop-shadow(0.0em 0.07em 0px #b2a98f55)
drop-shadow(0.0em 0.10em 0px #b2a98f88)
drop-shadow(0px 0.6em 0.9em rgba(0,0,0,0.15))
drop-shadow(0px 1.2em 0.15em rgba(0,0,0,0.1))
drop-shadow(0px 2.4em 2.5em rgba(0,0,0,0.1))
sepia(90%);
}
.card--dropshadow-heavy--sepia90 {
filter: drop-shadow(0.0em 0.05em 0px #b2a98f22)
drop-shadow(0.0em 0.07em 0px #b2a98f55)
drop-shadow(0.0em 0.10em 0px #b2a98f88)
drop-shadow(0px 0.3em 0.45em rgba(0,0,0,0.5))
drop-shadow(0px 0.6em 0.07em rgba(0,0,0,0.3))
drop-shadow(0px 1.2em 1.25em rgba(0,0,0,1))
drop-shadow(0px 1.8em 1.6em rgba(0,0,0,0.1))
drop-shadow(0px 2.4em 2.0em rgba(0,0,0,0.1))
drop-shadow(0px 3.0em 2.5em rgba(0,0,0,0.1))
sepia(90%);
}
.card--dropshadow-heavy {
filter: drop-shadow(0.0em 0.05em 0px #b2a98f22)
drop-shadow(0.0em 0.07em 0px #b2a98f55)
drop-shadow(0.0em 0.10em 0px #b2a98f88)
drop-shadow(0px 0.3em 0.45em rgba(0,0,0,0.5))
drop-shadow(0px 0.6em 0.07em rgba(0,0,0,0.3))
drop-shadow(0px 1.2em 1.25em rgba(0,0,0,1))
drop-shadow(0px 1.8em 1.6em rgba(0,0,0,0.1))
drop-shadow(0px 2.4em 2.0em rgba(0,0,0,0.1))
drop-shadow(0px 3.0em 2.5em rgba(0,0,0,0.1));
}
.card--dropshadow-medium--sepia90 {
filter: drop-shadow(0.0em 0.05em 0px #b2a98f)
drop-shadow(0.0em 0.15em 0px #b2a98f)
drop-shadow(0.0em 0.15em 0px #b2a98f)
drop-shadow(0px 0.6em 0.9em rgba(0,0,0,0.15))
drop-shadow(0px 1.2em 0.15em rgba(0,0,0,0.1))
drop-shadow(0px 2.4em 2.5em rgba(0,0,0,0.1))
sepia(90%);
}
.card--dropshadow-medium {
filter: drop-shadow(0.0em 0.05em 0px #b2a98f)
drop-shadow(0.0em 0.15em 0px #b2a98f)
drop-shadow(0.0em 0.15em 0px #b2a98f)
drop-shadow(0px 0.6em 0.9em rgba(0,0,0,0.15))
drop-shadow(0px 1.2em 0.15em rgba(0,0,0,0.1))
drop-shadow(0px 2.4em 2.5em rgba(0,0,0,0.1));
}
.card--dropshadow-light--sepia90 {
filter: drop-shadow(0px 0.10em 0px #b2a98f)
drop-shadow(0.1em 0.5em 0.2em rgba(0, 0, 0, .5))
sepia(90%);
}
.card--dropshadow-light {
filter: drop-shadow(0px 0.10em 0px #b2a98f)
drop-shadow(0.1em 0.5em 0.2em rgba(0, 0, 0, .5));
}
.card--dropshadow-down-and-distant {
filter: drop-shadow(0px 0.05em 0px #b2a98f)
drop-shadow(0px 14px 10px rgba(0,0,0,0.15)
drop-shadow(0px 24px 2px rgba(0,0,0,0.1))
drop-shadow(0px 34px 30px rgba(0,0,0,0.1));
}
.card--filter-none {
}
.horseshoe__svg__group {
transform: translateY(15%);
}
.line__horizontal {
stroke: var(--primary-text-color);
opacity: 0.3;
stroke-width: 2;
}
.line__vertical {
stroke: var(--primary-text-color);
opacity: 0.3;
stroke-width: 2;
}
.svg__dot {
fill: var(--primary-text-color);
opacity: 0.5;
align-self: center;
transform-origin: 50% 50%;
}
.icon {
align: center;
}
`;
}
/*******************************************************************************
* hass()
*
* Summary.
* Updates hass data for the card
*
*/
set hass(hass) {
// Set ref to hass, use "_"for the name ;-)
this._hass = hass;
var entityHasChanged = false;
// Update state strings and check for changes.
// Only if changed, continue and force render
var value;
var index = 0;
var attrSet = false;
var newStateStr;
for (value of this.config.entities) {
this.entities[index] = hass.states[this.config.entities[index].entity];
// Get attribute state if specified and available
if (this.config.entities[index].attribute) {
if (this.entities[index].attributes[this.config.entities[index].attribute]) {
newStateStr = this._buildState(this.entities[index].attributes[this.config.entities[index].attribute], this.config.entities[index]);
if (newStateStr != this.attributesStr[index]) {
this.attributesStr[index] = newStateStr;
entityHasChanged = true;
}
attrSet = true;
}
}
if (!attrSet) {
newStateStr = this._buildState(this.entities[index].state, this.config.entities[index]);
if (newStateStr != this.entitiesStr[index]) {
this.entitiesStr[index] = newStateStr;
entityHasChanged = true;
}
}
index++;
}
if (!entityHasChanged) {
return;
}
else {
}
// Use first state or attribute for displaying the horseshoe
// #TODO: only if state or attribute has changed.
var state = this.entities[0].state;
if ((this.config.entities[0].attribute)) {
if (this.entities[0].attributes[this.config.entities[0].attribute]) {
state = this.entities[0].attributes[this.config.entities[0].attribute];
}
}
// Calculate the size of the arc to fill the dasharray with this
// value. It will fill the horseshoe relative to the state and min/max
// values given in the configuration.
const min = this.config.horseshoe_scale.min || 0;
const max = this.config.horseshoe_scale.max || 100;
const val = Math.min(this._calculateValueBetween(min, max, state), 1);
const score = val * HORSESHOE_PATH_LENGTH;
const total = 10 * HORSESHOE_RADIUS_SIZE;
this.dashArray = `${score} ${total}`;
// We must draw the horseshoe. Depending on the stroke settings, we draw a fixed color, gradient, autominmax or colorstop
// #TODO: only if state or attribute has changed.
const strokeStyle = this.config.show.horseshoe_style;
if (strokeStyle == 'fixed') {
this.stroke_color = this.config.horseshoe_state.color;
this.color0 = this.config.horseshoe_state.color;
this.color1 = this.config.horseshoe_state.color;
this.color1_offset = '0%';
// We could set the circle attributes, but we do it with a variable as we are using a gradient
// to display the horseshoe circle .. <horseshoe circle>.setAttribute('stroke', stroke);
}
else if (strokeStyle == 'autominmax') {
// Use color0 and color1 for autoranging the color of the horseshoe
const stroke = this._calculateStrokeColor(state, this.colorStopsMinMax, true);
// We now use a gradient for the horseshoe, using two colors
// Set these colors to the colorstop color...
this.color0 = stroke;
this.color1 = stroke;
this.color1_offset = '0%';
}
else if (strokeStyle == 'colorstop' || strokeStyle == 'colorstopgradient') {
const stroke = this._calculateStrokeColor(state, this.colorStops, strokeStyle === 'colorstopgradient');
// We now use a gradient for the horseshoe, using two colors
// Set these colors to the colorstop color...
this.color0 = stroke;
this.color1 = stroke;
this.color1_offset = '0%';
}
else if (strokeStyle == 'lineargradient') {
// This has taken a lot of time to get a satisfying result, and it appeared much simpler than anticipated.
// I don't understand it, but for a circle, a gradient from left/right with adjusted stop is enough ?!?!?!
// No calculations to adjust the angle of the gradient, or rotating the gradient itself.
// Weird, but it works. Not a 100% match, but it is good enough for now...
// According to stackoverflow, these calculations / adjustments would be needed, but it isn't ;-)
// Added from https://stackoverflow.com/questions/9025678/how-to-get-a-rotated-linear-gradient-svg-for-use-as-a-background-image
const angleCoords = {'x1' : '0%', 'y1' : '0%', 'x2': '100%', 'y2' : '0%'};
this.color1_offset = `${Math.round((1-val)*100)}%`;
this.angleCoords = angleCoords;
}
// Check for animations linked to an entity or attribute.
// Set the dynamic animation depending on the state.
// If the card is rendered, the render() functions will take this dynamic animation into account.
//
// #TODO: Determine animation only if specific state or attribute has changed...
if (this.config.animations) Object.keys(this.config.animations).map(animation => {
const entityIndex = animation.substr(Number(animation.indexOf('.') + 1));
this.config.animations[animation].map(item => {
// if animation state not equals sensor state, return... Nothing to animate for this state...
if (this.entities[entityIndex].state.toLowerCase() != item.state.toLowerCase()) return;
if (item.vlines) {
item.vlines.map(item2 => {
if (!this.animations.vlines[item2.animation_id] || !item2.reuse) this.animations.vlines[item2.animation_id] = {};
this.animations.vlines[item2.animation_id] = Object.assign(this.animations.vlines[item2.animation_id], ...item2.styles);
})
}
if (item.hlines) {
item.hlines.map(item2 => {
if (!this.animations.hlines[item2.animation_id] || !item2.reuse) this.animations.hlines[item2.animation_id] = {};
this.animations.hlines[item2.animation_id] = Object.assign(this.animations.hlines[item2.animation_id], ...item2.styles);
})
}
if (item.circles) {
item.circles.map(item2 => {
if (!this.animations.circles[item2.animation_id] || !item2.reuse) this.animations.circles[item2.animation_id] = {};
this.animations.circles[item2.animation_id] = Object.assign(this.animations.circles[item2.animation_id], ...item2.styles);
})
}
if (item.icons) {
item.icons.map(item2 => {
if (!this.animations.icons[item2.animation_id] || !item2.reuse) this.animations.icons[item2.animation_id] = {};
this.animations.icons[item2.animation_id] = Object.assign(this.animations.icons[item2.animation_id], ...item2.styles);
})
}
if (item.states) {
item.states.map(item2 => {
if (!this.animations.states[item2.animation_id] || !item2.reuse) this.animations.states[item2.animation_id] = {};
this.animations.states[item2.animation_id] = Object.assign(this.animations.states[item2.animation_id], ...item2.styles);
})
}
});
});
// For now, always force update to render the card if any of the states or attributes have changed...
if (entityHasChanged) { this.requestUpdate();}
}
/*******************************************************************************
* setConfig()
*
* Summary.
* Sets/Updates the card configuration. Rarely called if the doc is right
*
*/
setConfig(config) {
if (!config.entities) {
throw Error('No entities defined');
}
if (!config.layout) {
throw Error('No layout defined');
}
if (!config.horseshoe_scale) {
throw Error('No horseshoe scale defined');
} else {
if ((!config.horseshoe_scale.min) && (!config.horseshoe_scale.min == 0) || (!config.horseshoe_scale.max)) {
throw Error('No horseshoe min/max for scale defined');
}
}
if ((!config.color_stops) || (config.color_stops.length < 2)) {
throw Error('No color_stops defined or not at least two colorstops');
}
// testing
if (config.entities) {
const newdomain = this._computeDomain(config.entities[0].entity);
if (newdomain != 'sensor') {
// If not a sensor, check if attribute is a number. If so, continue, otherwise Error...
if (config.entities[0].attribute && !isNaN(config.entities[0].attribute)) {
throw Error('First entity or attribute must be a numbered sensorvalue, but is NOT');
}
}
}
const newConfig = {
texts: [],
card_filter: 'card--filter-none',
...config,
show: { ...DEFAULT_SHOW, ...config.show },
horseshoe_scale: { ...DEFAULT_HORSESHOE_SCALE, ...config.horseshoe_scale },
horseshoe_state: { ...DEFAULT_HORSESHOE_STATE, ...config.horseshoe_state },
}
// Set default tap_action, if none given for an entity
newConfig.entities.forEach((entity, i) => {
if (!entity.tap_action) {
newConfig.entities[i].tap_action = {action: 'more-info'};
}
}
);
let colorStops = {};
// colorStops[newConfig.horseshoe_scale.min] = newConfig.horseshoe_state.color || '#03a9f4';
if (newConfig.color_stops) {
Object.keys(newConfig.color_stops).forEach((key) => {
colorStops[key] = newConfig.color_stops[key];
});
}
const sortedStops = Object.keys(colorStops).map(n => Number(n)).sort((a, b) => a - b);
this.colorStops = colorStops;
this.sortedStops = sortedStops;
// Create a colorStopsMinMax list for autominmax color determination
let colorStopsMinMax = {};
colorStopsMinMax[newConfig.horseshoe_scale.min] = colorStops[sortedStops[0]];
colorStopsMinMax[newConfig.horseshoe_scale.max] = colorStops[sortedStops[(sortedStops.length)-1]];
this.colorStopsMinMax = colorStopsMinMax;
// Now set the color0 and color1 for the gradient used in the horseshoe to the colors
// Use default for now!!
this.color0 = colorStops[sortedStops[0]];
this.color1 = colorStops[sortedStops[(sortedStops.length)-1]];
const angleCoords = {'x1' : '0%', 'y1' : '0%', 'x2': '100%', 'y2' : '0%'};
this.angleCoords = angleCoords;
this.color1_offset = '0%';
this.config = newConfig;
}
/*******************************************************************************
* connectedCallback()
*
* Summary.
*
*/
connectedCallback() {
super.connectedCallback();
}
/*******************************************************************************
* disconnectedCallback()
*
* Summary.
*
*/
disconnectedCallback() {
super.disconnectedCallback();
}
/*******************************************************************************
* render()
*
* Summary.
* Renders the complete SVG based card according to the specified layout in which
* the user can specify name, area, entities, lines and dots.
* The horseshoe is rendered on the full card. This one can be moved a bit via CSS.
*
*/
render({ config } = this) {
return html`
<ha-card
@click=${e => this.handlePopup(e, this.entities[0])}
>
<div class="container" id="container">
${this._renderSvg()}
</div>
<svg style="width:0;height:0;position:absolute;" aria-hidden="true" focusable="false">
<linearGradient gradientTransform="rotate(0)" id="horseshoe__gradient-${this.cardId}" x1="${this.angleCoords.x1}", y1="${this.angleCoords.y1}", x2="${this.angleCoords.x2}" y2="${this.angleCoords.y2}">
<stop offset="${this.color1_offset}" stop-color="${this.color1}" />
<stop offset="100%" stop-color="${this.color0}" />
</linearGradient>
</svg>
</ha-card>
`;
}
/*******************************************************************************
* renderTickMarks()
*
* Summary.
* Renders the tick marks on the scale.
*
*/
_renderTickMarks() {
const { config, } = this;
if (!config) return;
if (!config.show) return;
if (!config.show.scale_tickmarks) return;
const stroke = config.horseshoe_scale.color ? config.horseshoe_scale.color : 'var(--primary-background-color)';
const tickSize = config.horseshoe_scale.ticksize ? config.horseshoe_scale.ticksize
: (config.horseshoe_scale.max - config.horseshoe_scale.min) / 10;
// fullScale is 260 degrees. Hard coded for now...
const fullScale = 260;
const remainder = config.horseshoe_scale.min % tickSize;
const startTickValue = config.horseshoe_scale.min + (remainder == 0 ? 0 : (tickSize - remainder));
const startAngle = ((startTickValue - config.horseshoe_scale.min) /
(config.horseshoe_scale.max - config.horseshoe_scale.min)) * fullScale;
var tickSteps = ((config.horseshoe_scale.max - startTickValue) / tickSize);
// new
var steps = Math.floor(tickSteps);
const angleStepSize = (fullScale - startAngle) / tickSteps;
// If steps exactly match the max. value/range, add extra step for that max value.