-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtagcloud.js
114 lines (106 loc) · 3 KB
/
tagcloud.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
const html = `
<style>
body {
max-width: 300px;
}
ul.tc {
background: #fff;
color: #a33;
border-radius: 10px;
padding: 5px;
list-style: none;
padding-left: 0;
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
line-height: 2.5rem;
}
ul.tc a {
--size: attr(data-weight number, 2);
font-size: calc(var(--size) * 1rem);
color: #a33;
display: block;
padding: 0.125rem 0.25rem;
text-decoration: none;
position: relative;
border-radius: 0.4rem;
vertical-align: middle;
}
ul.tc li.active a {
background: #a33;
color: #fff;
}
ul.tc a::after {
content: " (" attr(data-count) ")";
font-size: 1rem;
display: inline-block;
vertical-align: middle;
}
ul.tc a[data-weight="1"] { --size: 1; }
ul.tc a[data-weight="2"] { --size: 2; }
ul.tc a[data-weight="3"] { --size: 3; }
ul.tc a[data-weight="4"] { --size: 4; }
ul.tc a[data-weight="5"] { --size: 5; }
ul.tc a[data-weight="6"] { --size: 6; }
ul.tc a[data-weight="7"] { --size: 7; }
ul.tc a[data-weight="8"] { --size: 8; }
ul.tc a[data-weight="9"] { --size: 9; }
</style>
<ul id="tc" class="tc"></ul>
<script>
const tc = document.getElementById("tc");
window.addEventListener("message", e => {
if (e.source !== parent || !e.data.reearth) return;
tc.innerHTML = "";
e.data.tags.map(tag => {
const li = document.createElement("li");
li.setAttribute("data-tagid", tag.id);
const a = document.createElement("a");
a.textContent = tag.label;
a.addEventListener("click", e => {
e.preventDefault();
li.classList.toggle("active");
parent.postMessage({
tags: Array.from(document.querySelectorAll("ul.tc li.active")).map(tag => tag.dataset.tagid)
}, "*");
});
a.setAttribute("href", "#");
a.setAttribute("data-count", tag.count.toString());
a.setAttribute("data-weight", Math.min(tag.count + 1, 9));
li.appendChild(a);
return li;
}).forEach(e => {
tc.appendChild(e);
});
});
</script>
`;
let hidden = [];
reearth.on("message", data => {
if (!data.tags) return;
const displayed = reearth.layers.findByTags(...data.tags).map(l => l.id);
const newHidden = data.tags.length ? reearth.layers.findAll(l => !displayed.includes(l.id)).map(l => l.id) : [];
reearth.layers.show(...hidden);
reearth.layers.hide(...newHidden);
hidden = newHidden;
});
reearth.ui.show(html);
reearth.on("update", send);
send();
function tags() {
return reearth.layers.tags.flatMap(tg => (tg.tags || [tg]).map(t => ({
id: t.id,
label: t.label ?? "",
count: reearth.layers.findByTags(t.id).length,
}))).filter(t => !!t);
}
function send() {
reearth.ui.postMessage({
// This is necessary to determine if the message is caused by a plugin,
// since the browser may send a message to all iframes
// depending on the browser and operating environment, such as Chrome on Android.
reearth: true,
tags: tags(),
});
}