forked from odpi/happi-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
happi-graph-legend.js
218 lines (190 loc) · 5.54 KB
/
happi-graph-legend.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
import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';
import '@polymer/iron-icons/iron-icons.js';
import '@polymer/paper-button/paper-button.js';
import { pluralize } from '@capaj/pluralize';
import {simpleSquareIcon} from './happi-graph-helpers';
class HappiGraphLegend extends PolymerElement {
static get properties() {
return {
iconsMap: {
type: Object,
value: {}
},
propertiesMap: {
type: Object,
value: {}
},
linksTypeIconMap: {
type: Object,
value: {}
},
legendData: {
type: Object,
value: {}
},
graphNodes: {
type: Object,
value: {},
observer: '_graphNodesUpdate'
},
graphLinks: {
type: Object,
value: {},
observer: '_graphLinksUpdate'
},
isMinimized: {
type: Boolean,
value: false
}
};
}
_graphLinksUpdate(newGraphLinks) {
let _links = newGraphLinks;
if (_links.length) {
_links.map(l => {
if (l.type && this.linksTypeIconMap[l.type]) {
let group = this.linksTypeIconMap[l.type].group
if (!this.legendData[group]) {
this.legendData[group] = [];
}
this.legendData[group][this.linksTypeIconMap[l.type].label] = l.type;
}
});
}
}
_graphNodesUpdate(newGraphNodes) {
let _nodes = newGraphNodes;
if (_nodes.length) {
_nodes.map(n => {
let group = this.propertiesMap[n.label].group
if (!this.legendData[group]) {
this.legendData[group] = [];
}
this.legendData[group][n.label] = n.label;
n.properties.map(p => {
if (this.propertiesMap[p.groupName]){
let propertiesGroup = this.propertiesMap[p.groupName].group
this.legendData[propertiesGroup][p.groupName] = p.groupName;
}
});
});
}
}
toggleMinimize() {
this.isMinimized = !this.isMinimized;
}
getIcon(type, label) {
let iconName = this.legendData[type][label];
if ((this.propertiesMap[iconName] && this.iconsMap[this.propertiesMap[iconName].icon])) {
return this.iconsMap[this.propertiesMap[iconName].icon];
} else if (this.linksTypeIconMap[iconName] && this.iconsMap[this.linksTypeIconMap[iconName].icon]) {
return this.iconsMap[this.linksTypeIconMap[iconName].icon];
} else {
return simpleSquareIcon;
}
}
getLabel(group) {
return pluralize(group);
}
getLegendCategories() {
return Object.keys(this.legendData);
}
getLegendLabels(legendKey) {
return [...new Set(Object.keys(this.legendData[legendKey]))];
}
static get template() {
return html`
<style>
:host {
display: block;
font-size:12px;
font-family: var(--happi-graph-font-family);
}
img {
height:20px;
vertical-align:middle;
margin-right:5px;
}
.svg-icons {
color: var(--happi-graph-secondary-color);
background: rgb(var(--happi-graph-primary-color-rgb), 0.9);
padding:10px;
max-width:400px;
display: flex;
flex-flow: row wrap;
justify-content: space-around;
border-bottom: 1px solid black;
}
.icon-title {
color: var(--happi-graph-secondary-color);
background: rgb(var(--happi-graph-primary-color-rgb), 0.9);
padding: 10px;
max-width: 400px;
font-weight: bold;
text-align: center;
}
.svg-icon {
display: flex;
align-items: center;
flex-grow: 4;
margin:5px;
}
.svg-icon span {
margin-top:1px;
width:120px;
}
.dropdown {
display:flex;
justify-content:flex-end;
cursor: pointer;
}
paper-button {
margin: 0;
padding: 0.2em 0.2em 0.4em 0.57em;
font-size:15px;
color: var(--happi-graph-primary-color);
background-color: #f4f5f7;
text-transform: none;
--paper-button: {
@apply(--layout-vertical);
@apply(--layout-center-center);
};
}
paper-button.keyboard-focus {
font-weight: normal;
}
.label {
margin-top:4px;
}
.label iron-icon {
margin-top:-3px;
}
</style>
<paper-button on-click="toggleMinimize" class="dropdown">
<div class="label">
Legend
<template is="dom-if" if="[[ !isMinimized ]]">
<iron-icon icon="icons:expand-more"></iron-icon>
</template>
<template is="dom-if" if="[[ isMinimized ]]">
<iron-icon icon="icons:chevron-right"></iron-icon>
</template>
</div>
</paper-button>
<template is="dom-if" if="[[ isMinimized ]]" restamp="true">
<template is="dom-repeat" items="{{ getLegendCategories() }}" as="legendKey">
<div class="icon-title">[[getLabel(legendKey)]]</div>
<div class="svg-icons">
<template is="dom-repeat" items="{{ getLegendLabels(legendKey) }}" as="label">
<div class="svg-icon">
<img src="data:image/svg+xml;utf8,[[ getIcon(legendKey, label) ]]"/>
<span>[[ label ]]</span>
</div>
</template>
</div>
</template>
</template>
`;
}
}
window.customElements.define('happi-graph-legend', HappiGraphLegend);