forked from fineemb/lovelace-cn-map-card
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cn-map-card.js
1061 lines (986 loc) · 34.3 KB
/
cn-map-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
console.info("%c GAODE MAP CARD \n%c Version 1.2.7 ",
"color: orange; font-weight: bold; background: black",
"color: white; font-weight: bold; background: dimgray");
window._AMapSecurityConfig = { securityJsCode:'6dfe05bc031ab44411012f73d00208c2', }
import 'https://webapi.amap.com/loader.js';
import './w3color.js';
Date.prototype.format = function(fmt) {
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt)) {
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
}
function controlArrayLength(arr) {
if (arr.length <= 1000) {
// 如果数组长度小于等于 1000,则无需操作
return arr;
} else {
// 计算需要移除的元素个数
const removeCount = arr.length - 1000;
// 计算平均每个元素需要移除的个数
const averageRemoveCount = Math.ceil(removeCount / arr.length);
const step = Math.ceil(arr.length / 1000);
const controlledArray = [];
// 循环添加均匀分布的元素到新数组
for (let i = 0; i < arr.length; i += step) {
controlledArray.push(arr[i]);
}
return controlledArray;
}
}
function convertUTCTimeToLocalTime(UTCDateString) {
if(!UTCDateString){
return '-';
}
function formatFunc(str) { //格式化显示
return str > 9 ? str : '0' + str
}
var date2 = new Date(UTCDateString); //这步是关
var year = date2.getFullYear();
var mon = formatFunc(date2.getMonth() + 1);
var day = formatFunc(date2.getDate());
var hour = date2.getHours();
var seconds = date2.getSeconds();
var noon = hour >= 12 ? '' : 'AM';
hour = formatFunc(hour);
var min = formatFunc(date2.getMinutes());
var dateStr = year+'-'+mon+'-'+day+' '+hour+':'+min+':'+seconds;
return dateStr;
}
function getRadius(idx, t1, t2) {
if (t1 === undefined) {
var date1 = new Date();
} else {
var date1 = new Date(t1);
}
if (t2 === undefined) {
var date2 = new Date();
} else {
var date2 = new Date(t2);
}
var seconds = Math.floor((date2.getTime() - date1.getTime())/1000)
var minute = Math.floor(seconds/60)
var hour = Math.floor(seconds/60/60)
var waitTime = '0'
if (hour > 0) {
waitTime = hour.toString() + '小时 ' + (minute - hour * 60).toString() + '分钟 ' + (seconds % 60).toString() + '秒'
} else if (minute > 0) {
waitTime = minute.toString() + '分钟 ' + (seconds - minute * 60).toString() + '秒'
} else {
waitTime = seconds.toString() + '秒'
}
var radius = Math.floor(minute/5)
if (radius < 5) {
return [5, waitTime]
} else {
return [radius, waitTime]
}
}
const preloadCard = type => window.loadCardHelpers()
.then(({ createCardElement }) => createCardElement({type}));
const LitElement = Object.getPrototypeOf(
customElements.get("ha-panel-lovelace")
);
const html = LitElement.prototype.html;
const css = LitElement.prototype.css;
const includeDomains = ["device_tracker","person"];
class GaodeMapCard extends HTMLElement {
constructor() {
super();
this.markers = {};
this.paths = {};
this.circle = {}
this.persons = [];
this.fit = 0;
this.trace = false;
this.historyPath = {};
this.loaded = false;
this.loadst = false;
this.oldentities = []
this.old_mode;
this.theme;
this.positions = {};
this._colors = [
"#0288D1",
"#00AA00",
"#984ea3",
"#00d2d5",
"#ff7f00",
"#af8d00",
"#7f80cd",
"#b3e900",
"#c42e60",
"#a65628",
"#f781bf",
"#8dd3c7",
];
this.root = this.attachShadow({ mode: 'open' });
if (this.root.lastChild) this.root.removeChild(root.lastChild);
const style = document.createElement('style');
style.textContent = this._cssData();
this.root.appendChild(style);
const hacard = document.createElement('ha-card');
this.card = hacard;
hacard.className = 'gaode-map-card';
hacard.innerHTML = `
<div id="root">
<div id="map">
<div id="container"></div>
<div class="info" id="info">
移动到圆点查看
</div>
<div class="entity" id="entity">
</div>
<div class="time" id="time">
<label for="fname">开始:</label>
<input type="text" id="start_time">
<label for="lname">结束:</label>
<input type="text" id="end_time">
<button type="button" id="refresh">确定</button>
</div>
<ha-icon-button id="fitbutton" icon="hass:image-filter-center-focus" title="Reset focus" role="button" tabindex="0" aria-disabled="false"></ha-icon-button>
</div>
</div>
`;
this.root.appendChild(hacard);
let fitButton = this.root.querySelector("#fitbutton")
fitButton.addEventListener('click', () => {
if(this.trace){
this.trace=false
this.root.querySelector("#fitbutton").classList.remove("active")
this.map.setPitch(0)
}else{
this.trace=true
this.root.querySelector("#fitbutton").classList.add("active")
this.map.setPitch(80)
}
});
}
connectedCallback(){
// console.log(this.config);
this._loadMap({
key: this.config.key||"ce3b1a3a7e67fc75810ce1ba1f83c01a", // 申请好的Web端开发者Key,首次调用 load 时必填 f87e0c9c4f3e1e78f963075d142979f0
version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
plugins: ['AMap.MoveAnimation'] //插件列表
});
}
static getConfigElement() {
return document.createElement("gaode-map-card-editor");
}
static getStubConfig() {
return {aspect_ratio: '1',
dark_mode: "auto",
traffic: false,
entities: ["zone.home"] }
}
set isPanel(isPanel){
this._isPanel = isPanel;
}
set editMode(editMode){
this._editMode = editMode ;
}
set hass(hass) {
this._hass = hass;
this.entities = this.config.entities;
this.card.header=this.config.title;
if(!this.loaded || this.config.entities.length<1)return;
if(this._isPanel){
this.root.querySelector("#root").style.paddingBottom = 0;
this.setAttribute("is-panel","");
}
var oc = JSON.stringify(this.oldentities);
var nc = JSON.stringify(this.entities);
if(oc!=nc){
//更新标记点
this.map.clearMap();
this.markers = {};
this.paths = {};
this.historyPath = {};
this.entities.forEach(function(entity,index) {
let entityt = typeof entity === "string"?entity:entity.entity;
let type = entity.type?entity.type:"gps";
this._addMarker(entityt,index,type);
},this);
this.oldentities = deepClone(this.entities);
}else{
//仅更新位置
for(var i in this.entities) {
let entityt = typeof this.entities[i] === "string"?this.entities[i]:this.entities[i].entity;
let type = this.entities[i].type?this.entities[i].type:"gps";
this._updateMarker(entityt,type);
}
//实时追踪
if(this.trace){
let angle = this.config.angle?hass.states[this.config.angle].state:0;
if(angle)this.map.setRotation(360-angle);
this.map.setFitView(this.persons, false, [40, 40, 40, 40]);
}
}
//更新式样
let dark_mode = this.config.dark_mode;
let newTheme = hass.themes.default_theme;
let style = dark_mode;
if(this.old_mode!=dark_mode){
if(dark_mode!="auto"){
this.map.setMapStyle("amap://styles/"+style);
this.root.querySelector("#map").className = style;
this.old_mode = dark_mode;
}else{
let cardColor = hass.themes.themes[newTheme]["primary-background-color"] || "#FFFFF";
let lightness = cardColor?w3color(cardColor).lightness:1;
let colorDark = lightness<0.5?true:false;
style = colorDark?'dark':'normal';
this.map.setMapStyle("amap://styles/"+style);
this.root.querySelector("#map").className = style;
this.old_mode = dark_mode;
this.theme=hass.themes.default_theme;
}
}
if(dark_mode==="auto"){
if(this.theme!=newTheme){
let cardColor = hass.themes.themes[newTheme]["primary-background-color"] || "#FFFFF";
let lightness = cardColor?w3color(cardColor).lightness:1;
let colorDark = lightness<0.5?true:false;
style = colorDark?'dark':'normal';
this.map.setMapStyle("amap://styles/"+style);
this.root.querySelector("#map").className = style;
this.old_mode = dark_mode;
this.theme=hass.themes.default_theme;
}
}
//实时路况图层
if(this.config.traffic){
this.trafficLayer.show();
}else{
this.trafficLayer.hide();
}
//更新视界
// console.info(this.fit)
if(this.fit >= this.entities.length){
this.map.setFitView(this.persons, false, [40, 40, 40, 40]);
this.fit = 0;
}
}
setConfig(config) {
preloadCard('map');
customElements.get("hui-map-card");
this.config = deepClone(config);
let d = this.root.querySelector("#root")
d.style.paddingBottom = 100*(this.config.aspect_ratio||1)+"%";
}
_loadMap(config){
AMapLoader.load(config).then(()=>{
let mapContainer = this.root.querySelector("#container");
this.map = new AMap.Map(mapContainer,{
viewMode: '3D',
zoom: this.config.default_zoom || 9
});
let mode = this.config.dark_mode;
let style = (mode==="auto")?"normal":mode;
this.old_mode = mode;
this.map.setMapStyle("amap://styles/"+style);
this.root.querySelector("#map").className = style;
//实时路况图层
this.trafficLayer = new AMap.TileLayer.Traffic({
zIndex: 10
});
this.trafficLayer.setMap(this.map);
this.loaded = true;
}).catch(e => {
console.log(e);
})
const endTime = new Date();
const startTime = new Date();
let hours_to_show =this.config.hours_to_show||0;
//startTime.setHours(endTime.getHours() - hours_to_show);
startTime.setHours(0, 0, 0, 0);
this.root.querySelector('#start_time').value = startTime.format("yyyy-MM-dd hh:mm:ss")
this.root.querySelector('#end_time').value = endTime.format("yyyy-MM-dd hh:mm:ss")
var entityhtml = '<button type="button" id="entity_all">全部</button>'
this.entities.forEach(function(entity,index) {
let entityt = typeof entity === "string"?entity:entity.entity;
if (entityt != 'zone.home') {
let objstates = this._hass.states[entityt];
let entityName =objstates.attributes.friendly_name?objstates.attributes.friendly_name.split(' ').map(function (part) { return part.substr(0, 1); }).join('') : '';
entityhtml += '<button type="button" id="' + entityt.replace('.', '_') + '">'+entityName+'</button>'
}
},this);
this.root.querySelector("#entity").innerHTML = entityhtml;
this.entities.forEach(function(entity,index) {
let entityt = typeof entity === "string"?entity:entity.entity;
if (entityt != 'zone.home') {
this.root.querySelector('#'+entityt.replace('.', '_')).addEventListener('click', function(entityt) {
this._entity(entityt);
}.bind(this, entityt));
}
},this);
var entityt = 'entity_all'
this.root.querySelector('#' + entityt).addEventListener('click', function(entityt) {
this._entity(undefined);
}.bind(this, entityt));
var args = undefined
this.root.querySelector('#refresh').addEventListener('click', function(args) {
this.oldentities = []
}.bind(this, args));
}
_updateMarker(entity,type){
let objstates = this._hass.states[entity];
if(!objstates || !objstates.attributes.longitude){
return
}
let gps = [objstates.attributes.longitude, objstates.attributes.latitude];
let hours_to_show =this.config.hours_to_show||0;
let newLngLat = new AMap.LngLat(gps[0],gps[1])
let oldLngLat = new AMap.LngLat(gps[0],gps[1])
if(this.positions[entity]){
let oldGPS = this.positions[entity]
oldLngLat = new AMap.LngLat(oldGPS[0],oldGPS[1])
}
let distance = newLngLat.distance(oldLngLat)
// 过滤太小的距离
// console.log(distance);
if(distance>5){
const that = this;
AMap.convertFrom(gps, type, function (status, result) {
if (result.info === 'ok' && that.markers[entity]) {
that.markers[entity].moveTo(result.locations[0], {
autoRotation: false
})
if(hours_to_show>0 && that.trace){
that._gethistory(hours_to_show, entity, "")
}
}
});
}
this.positions[entity] = gps;
}
_addMarker(entity,index,type){
let color = this._colors[index%this._colors.length];
let objstates = this._hass.states[entity];
if(!objstates || !objstates.attributes.longitude){
this.fit++;
return
}
let gps = new AMap.LngLat(objstates.attributes.longitude, objstates.attributes.latitude);
let that = this;
if(type=='gaode'){
that._showMarker(gps,entity,color,type);
}else{
AMap.convertFrom(gps, type, function (status, result) {
// console.info(result.locations[0])
if (result.info === 'ok') {
that._showMarker(result.locations[0],entity,color,type);
}
});
}
}
_showMarker(result,entity,color,type){
let domain = entity.split('.')[0];
let hours_to_show =this.config.hours_to_show||0;
let objstates = this._hass.states[entity];
let entityPicture = objstates.attributes.entity_picture || '';
let entityName =objstates.attributes.friendly_name?objstates.attributes.friendly_name.split(' ').map(function (part) { return part.substr(0, 1); }).join('') : '';
let markerContent = `<ha-entity-marker width="20" height="20" entity-id="`+entity+`" entity-name="`+entityName+`" entity-picture="`+entityPicture+`" entity-color="`+color+`"></ha-entity-marker>`
//区域
var circle = new AMap.Circle({
center: result, // 圆心位置
radius: objstates.attributes.radius || objstates.attributes.gps_accuracy, // 圆半径
fillColor: domain==='zone'?'rgb(255, 152, 0)':color, // 圆形填充颜色
fillOpacity: 0.2,
zIndex: 101,
strokeColor: domain==='zone'?'rgb(255, 152, 0)':color, // 描边颜色
strokeWeight: 3, // 描边宽度
});
this.map.add(circle);
//标记点
let marker = new AMap.Marker({
map: this.map,
position: result,
content: domain==='zone'?`<ha-icon icon="`+objstates.attributes.icon+`"></ha-icon>`:markerContent,
zIndex: domain==='zone'?102:103,
anchor: 'center'
});
if(domain==='person'||domain==='device_tracker'){
this.persons.push(marker);
//历史路径
if(hours_to_show>0){
this._gethistory(hours_to_show, entity, color, type)
}
}
this.markers[entity] = marker;
this.fit++;
if(this.fit === this.entities.length)this.loaded = true;
}
_entity(entity) {
const that = this;
for (var i=0; i < that.entities.length ; ++i){
var ientity = typeof that.entities[i] === "string"?that.entities[i]:that.entities[i].entity;
if (entity === undefined) {
if( that.paths[ientity]){
that.paths[ientity].show();
}
if( that.circle[ientity]){
for (var j=0; j < that.circle[ientity].length ; ++j){
that.circle[ientity][j].show();
}
}
continue
}
if (entity == ientity) {
if( that.paths[entity]){
that.paths[entity].show();
}
if( that.circle[entity]){
for (var j=0; j < that.circle[entity].length ; ++j){
that.circle[entity][j].show();
}
}
} else {
if( that.paths[ientity]){
that.paths[ientity].hide();
}
if( that.circle[ientity]){
for (var j=0; j < that.circle[ientity].length ; ++j){
that.circle[ientity][j].hide();
}
}
}
}
}
_gethistory(hours, entity, color, type){
//const endTime = new Date();
//const startTime = new Date();
//startTime.setHours(endTime.getHours() - hours);
const that = this;
//alert(that.root.querySelector('#start_time').value)
const startTime = new Date(that.root.querySelector('#start_time').value)
const endTime = new Date(that.root.querySelector('#end_time').value)
this._hass.callApi("GET", "history/period/"+startTime.toISOString()+"?filter_entity_id="+entity+"&significant_changes_only=0&end_time="+endTime.toISOString())
.then(function(res) {
let arr = controlArrayLength(res[0])
if (arr.length > 1 && that.historyPath[entity] != arr.length) {
that.historyPath[entity] = arr.length;
var lineArr = []
var infoArr = []
var waitArr = []
for(var i in arr) {
let p = arr[i].attributes;
if(p.longitude)lineArr.push(new AMap.LngLat(p.longitude,p.latitude));
let radius = [0, 0]
if (parseInt(i) + 1 < arr.length) {
radius = getRadius(i, arr[i].last_updated, arr[(parseInt(i)+1).toString()].last_updated);
if(p.longitude)waitArr.push(radius[0]);
} else {
radius = getRadius(i, arr[i].last_updated, undefined);
if(p.longitude)waitArr.push(radius[0]);
}
let lu = arr[i].last_updated;
if(p.longitude) {
if (p.speed) {
infoArr.push(p.friendly_name + '> 速度:' + p.speed.toString() + 'km/h 到达时间:' + convertUTCTimeToLocalTime(lu) + ' 停留时间:' + radius[1]);
} else {
infoArr.push(p.friendly_name + '> 到达时间:' + convertUTCTimeToLocalTime(lu) + ' 停留时间:' + radius[1]);
}
}
}
if(type=='gaode'){
var path2 = lineArr;
var info2 = infoArr;
var wait2 = waitArr;
if( that.paths[entity]){
that.paths[entity].setPath(path2);
}else{
that.paths[entity] = new AMap.Polyline({
map: that.map,
path: path2,
zIndex: 200,
strokeWeight: 6,
strokeColor: color,
strokeOpacity: 0.5,
showDir: true,
lineJoin: 'round'
});
var circleArr = []
for(var i=0;i<path2.length;i+=1){
var center = path2[i];
var circle = new AMap.CircleMarker({
map: that.map,
center:center,
strokeWeight:0,
radius: wait2[i],
fillColor:color,
fillOpacity:0.5,
zIndex:200,
bubble:true
});
circleArr.push(circle)
const t = info2[i]
circle.on('mouseover', function (e) {
this.setOptions({strokeWeight:3})
var text = '' + t
that.root.querySelector("#info").innerText = text;
that.root.querySelector("#info").style.display = "block"
});
circle.on('mouseout', function (e) {
this.setOptions({strokeWeight:0})
var text = '移动到圆点查看'
that.root.querySelector("#info").innerText = text;
that.root.querySelector("#info").style.display = "none"
});
}
that.circle[entity] = circleArr
}
}else{
var info2 = infoArr;
var wait2 = waitArr;
AMap.convertFrom(lineArr, type, function (status, result) {
if (result.info === 'ok') {
var path2 = result.locations;
if( that.paths[entity]){
that.paths[entity].setPath(path2);
}else{
that.paths[entity] = new AMap.Polyline({
map: that.map,
path: path2,
zIndex: 200,
strokeWeight: 6,
strokeColor: color,
strokeOpacity: 0.5,
showDir: true,
lineJoin: 'round'
});
var circleArr = []
for(var i=0;i<path2.length;i+=1){
var center = path2[i];
var circle = new AMap.CircleMarker({
map: that.map,
center:center,
strokeWeight:0,
radius: wait2[i],
fillColor:color,
fillOpacity:0.5,
zIndex:200,
bubble:true
});
circleArr.push(circle)
const t = info2[i]
circle.on('mouseover', function (e) {
var text = '' + t
this.setOptions({strokeWeight:3})
that.root.querySelector("#info").innerText = text;
that.root.querySelector("#info").style.display = "block"
});
circle.on('mouseout', function (e) {
var text = '移动到圆点查看'
this.setOptions({strokeWeight:0})
that.root.querySelector("#info").innerText = text;
that.root.querySelector("#info").style.display = "none"
});
}
that.circle[entity] = circleArr
}
}
});
}
}
})
}
_cssData(){
var css = `
:host([is-panel]) ha-card {
left: 0;
top: 0;
width: 100%;
/**
* In panel mode we want a full height map. Since parent #view
* only sets min-height, we need absolute positioning here
*/
height: 100%;
position: absolute;
}
ha-card {
overflow: hidden;
}
#map {
z-index: 0;
border: none;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: inherit;
}
.amap-container {
z-index: 0;
border: none;
position: relative;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.amap-logo{
position: absolute;
bottom: 0;
left: 10px;
}
.amap-marker ha-icon{
position: absolute;
bottom: calc(50% - 12px);
left: calc(50% - 12px);
}
ha-icon-button {
position: absolute;
top: 7px;
left: 7px;
}
ha-entity-marker {
height: 24px!important;
width: 24px!important;
}
#root {
position: relative;
}
#container > iframe{
visibility: hidden;
}
:host([is-panel]) #root {
height: 100%;
}
.normal #fitbutton {
color:#000;
}
.dark #fitbutton {
color:#fff;
}
#fitbutton.active {
color:var(--paper-item-icon-active-color);
}
.info {
padding: 0.75rem 1.25rem;
margin-bottom: 1rem;
border-radius: 0.25rem;
position: fixed;
top: 5rem;
background-color: white;
width: auto;
min-width: 22rem;
border-width: 0;
right: 1rem;
display: none;
box-shadow: 0 2px 6px 0 rgb(114 124 245 / 50%);
}
.entity {
padding: 0.75rem 1.25rem;
margin-bottom: 1rem;
border-radius: 0.25rem;
position: fixed;
top: 5rem;
background-color: white;
width: auto;
border-width: 0;
left: 1rem;
box-shadow: 0 2px 6px 0 rgb(114 124 245 / 50%);
}
.time {
padding: 0.75rem 1.25rem;
margin-bottom: 1rem;
border-radius: 0.25rem;
position: fixed;
top: 5rem;
background-color: white;
width: auto;
border-width: 0;
left: 18rem;
box-shadow: 0 2px 6px 0 rgb(114 124 245 / 50%);
}
.marker {
position: absolute;
top: -20px;
right: -118px;
color: #fff;
padding: 4px 10px;
box-shadow: 1px 1px 1px rgba(10, 10, 10, .2);
white-space: nowrap;
font-size: 12px;
font-family: "";
background-color: #25A5F7;
border-radius: 3px;
}
`
return css;
}
}
function deepClone(value) {
if (!(!!value && typeof value == 'object')) {
return value;
}
if (Object.prototype.toString.call(value) == '[object Date]') {
return new Date(value.getTime());
}
if (Array.isArray(value)) {
return value.map(deepClone);
}
var result = {};
Object.keys(value).forEach(
function(key) { result[key] = deepClone(value[key]); });
return result;
}
customElements.define("gaode-map-card", GaodeMapCard);
export class GaodeMapCardEditor extends LitElement {
setConfig(config) {
this.config = deepClone(config);
this._configEntities = config.entities
? this._processEditorEntities(config.entities)
: [];
}
static get properties() {
return {
hass: {},
config: {}
};
}
render() {
var patt = new RegExp("device_tracker|zone|person")
if (!this.hass) {
return html``;
}
let dark_mode = this.config.dark_mode
return html`
<div class="card-config">
<paper-input
label="${this.hass.localize("ui.panel.lovelace.editor.card.generic.title")} (${this.hass.localize("ui.panel.lovelace.editor.card.config.optional")})"
.value="${this.config.title}"
.configValue="${"title"}"
@value-changed="${this._valueChanged}"
></paper-input>
<div class="side-by-side">
<paper-input
label="${this.hass.localize("ui.panel.lovelace.editor.card.generic.aspect_ratio")} (${this.hass.localize("ui.panel.lovelace.editor.card.config.optional")})"
.value="${this.config.aspect_ratio}"
.configValue="${"aspect_ratio"}"
@value-changed="${this._valueChanged}"
></paper-input>
<paper-input
label="${this.hass.localize("ui.panel.lovelace.editor.card.map.default_zoom")} (${this.hass.localize("ui.panel.lovelace.editor.card.config.optional")})"
type="number"
.value="${this.config.default_zoom}"
.configValue="${"default_zoom"}"
@value-changed="${this._valueChanged}"
></paper-input>
</div>
<div class="side-by-side">
<mwc-formfield label="实时路况">
<ha-switch
?checked="${this.config.traffic !== false}"
.configValue="${"traffic"}"
@change="${this._valueChanged}"
></ha-switch>
</mwc-formfield>
<paper-input
label="${this.hass.localize("ui.panel.lovelace.editor.card.map.hours_to_show")} (${this.hass.localize("ui.panel.lovelace.editor.card.config.optional")})"
type="number"
.value="${this.config.hours_to_show}"
.configValue="${"hours_to_show"}"
@change="${this._valueChanged}"
></paper-input>
</div>
<div class="side-by-side">
<mwc-formfield label="白天模式">
<mwc-radio id="b1" ?checked=${(dark_mode==='normal')} value="normal" name="style_mode" .configValue="${"dark_mode"}" @change="${this._valueChanged}"></mwc-radio>
</mwc-formfield>
<mwc-formfield label="夜间模式">
<mwc-radio id="b2" ?checked=${(dark_mode==='dark')} value="dark" name="style_mode" .configValue="${"dark_mode"}" @change="${this._valueChanged}"></mwc-radio>
</mwc-formfield>
<mwc-formfield label="跟随主题">
<mwc-radio id="b3" ?checked=${(dark_mode==='auto')} value="auto" name="style_mode" .configValue="${"dark_mode"}" @change="${this._valueChanged}"></mwc-radio>
</mwc-formfield>
</div>
<hui-entity-editor
.hass="${this.hass}"
.entities="${this._configEntities}"
.includeDomains=${includeDomains}
@entities-changed="${this._entitiesValueChanged}"
></hui-entity-editor>
<h3>API KEY
<a href="//lbs.amap.com/dev/id/newuser" class="" target="_blank">获取KEY</a>
</h3>
<div class="gaode_key">
<paper-input
label="${this.hass.localize("component.airvisual.config.step.user.data.api_key")}"
.value="${this.config.key}"
.configValue="${"key"}"
@value-changed="${this._valueChanged}"
></paper-input>
</div>
</div>
<datalist id="browsers">
${Object.keys(this.hass.states).filter(a => patt.test(a) ).map(entId => html`
<option value=${entId}>${this.hass.states[entId].attributes.friendly_name || entId}</option>
`)}
</datalist>
`;
}
static get styles() {
return css `
a{
color: var(--accent-color);
}
.side-by-side {
display: flex;
}
.side-by-side > * {
flex: 1;
padding-right: 4px;
}
ha-switch{
margin-right: 10px;
}
.entities > * {
width: 100%;
padding-right: 4px;
}
paper-dropdown-menu{
width: 100%;
padding-right: 4px;
}
paper-input-container ha-icon{
margin-right: 10px;
}
`
}
_focusEntity(e){
e.target.value = ''
}
_delEntity(ev){
const target = ev.target.previousElementSibling;
if (!this.config || !this.hass ) {
return;
}
const entities = this.config.entities
let id = -1 ;
for (var i=0; i < entities.length ; ++i){
if(entities[i]===target.value){
id = i
}
}
if(id>-1)entities.splice(id, 1);
this.configChanged(this.config)
}
_addEntity(ev){
const target = ev.target.value || ev.target.previousElementSibling.value;
if (!this.config || !this.hass || !target) {
return;
}
const entities = this.config.entities
let flag = true;
entities.forEach(item=>{
if(target===item){
flag = false;
ev.target.value = ''
}
})
if(flag){
entities.push(target)
this.config = {
...this.config,
"entities": entities
};
this.configChanged(this.config)
ev.target.value = ''
}
}
_changeEntity(ev){
const target = ev.target;
if (!this.config || !this.hass || !target) {
return;
}
const entities = this.config.entities
let id = -1 ;
for (var i=0; i < entities.length ; ++i){
if(entities[i]===target.defaultValue){
id = i
}
}
if(id>-1){
delete entities[id];
entities[id] = target.value
}
this.configChanged(this.config)
}
_entitiesValueChanged(ev){
if (!this.config || !this.hass) {
return;
}
if (ev.detail && ev.detail.entities) {