-
Notifications
You must be signed in to change notification settings - Fork 2
/
epiviz-chart-colors.html
268 lines (226 loc) · 8.91 KB
/
epiviz-chart-colors.html
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
<!-- Polymer dependency -->
<link rel="import" href="bower_components/polymer/polymer-element.html">
<!-- External Polymer Styles/elements dependency -->
<link rel="import" href="bower_components/paper-button/paper-button.html">
<link rel="import" href="bower_components/paper-dropdown-input/paper-dropdown-input.html">
<link rel="import" href="bower_components/paper-styles/color.html">
<link rel="import" href="bower_components/paper-styles/typography.html">
<link rel="import" href="bower_components/iron-form/iron-form.html">
<link rel="import" href="bower_components/paper-dialog/paper-dialog.html">
<link rel="import" href="bower_components/paper-dialog-scrollable/paper-dialog-scrollable.html">
<link rel="import" href="bower_components/paper-swatch-picker/paper-swatch-picker.html">
<link rel="import" href="bower_components/iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="bower_components/iron-label/iron-label.html">
<!--
`<epiviz-chart-colors>` is a view element that manages chart colors.
All charts implement the `epiviz.ChartColorsBehavior` that manages this element.
-->
<dom-module id="epiviz-chart-colors">
<template>
<style>
#formSettings {
min-width: 500px;
}
#scrollContent {
margin-bottom: 10px;
}
.color-item {
display: block;
text-align: center;
}
.container {
display: grid;
grid-auto-flow: column dense;
}
</style>
<!-- local DOM goes here -->
<div id="settings">
<!--no-overlap horizontal-align="left" vertical-align="top"-->
<paper-dialog id="modal" modal>
<h2>Chart Color Settings</h2>
<iron-form id="formSettings">
<form>
<div class="container">
<template id="colorPickers" is="dom-repeat" items="{{_getLabels(_measurements.*)}}">
<div class="color-item">
<paper-swatch-picker id="[[_getId(item)]]" name="{{_getName(item)}}" color="[[_getColor(item)]]"></paper-swatch-picker>
<iron-label for="{{_getId(item)}}">{{item}}</iron-label>
</div>
</template>
</div>
<div class="buttons container flex-end-justified">
<paper-button id="submit" on-tap="_submit" raised>Apply</paper-button>
<paper-button id="cancel" on-tap="_cancel" raised>Cancel</paper-button>
</div>
</form>
</iron-form>
</paper-dialog>
</div>
</template>
<script>
// Extend Polymer.Element base class
class EpivizChartColors extends Polymer.Element {
static get is() { return 'epiviz-chart-colors'; }
static get properties() {
return {
/**
* Default chart color labels.
*
* @type {Object<string, string>}
*/
labels: {
type: Object,
notify: true
},
/**
* Default chart color palettes.
*
* @type {Array<epiviz.ui.charts.ColorPalette>}
*/
palettes: {
type: Array,
notify: true
},
/**
* currently selected chart colors.
*
* @type {Object<string, string>}
*/
selected: {
type: Object,
notify: true
},
_measurements: {
type: Array,
notify: true
}
}
}
static get observers() {
return [
/* observer array just like 1.x */
]
}
constructor() {
super();
// this.addEventListener('submit.click', e => this._submit(e));
// this.addEventListener('cancel.click', e => this._cancel(e));
}
_sanitizeString(text) {
return text.replace(/[^a-zA-Z0-9]/g, '');
}
connectedCallback() {
var self = this;
super.connectedCallback();
this.$.formSettings.addEventListener('iron-form-submit', this._form_submit.bind(this));
var colorPickers = this.$.colorPickers;
colorPickers.addEventListener('dom-change', function(e) {
// auto-binding template is ready.
var labels = self.$.colorPickers.items;
var colorSwatch = self.shadowRoot.querySelectorAll("paper-swatch-picker");
colorSwatch.forEach(function(item, index) {
var defaultColors = item.defaultColors();
defaultColors[0] = self._getColor(labels[index]);
item.set('colorList', defaultColors);
item.set('color', self._getColor(labels[index]));
});
});
}
disconnectedCallback() {
super.connectedCallback();
}
/**
* Get Color for a given label/Key.
*
* @param {string} label label/key to search for.
*
* @return {Object} color set for the given label
*/
_getColor(label) {
return this.selected.getByKey(label);
}
/**
* UI Helper function to find group
*
* @param {string} value index number
* @param {number} expeced expected group
* @return {boolean} returns True if value in expected
*/
_isIndexGroup(value, expected) {
if (value % 3 == expected) {
return true;
}
return false;
}
/**
* UI Helper function to create Id for a given label (ignore spaces)
*
* @param {string} label string
*
* @return {string} return label ignoring white spaces
*/
_getId(label) {
return this._sanitizeString(label.split(' ').join(''));
}
_getName(item) {
return this._mList[item];
}
_getLabels(_measurements) {
var order = this._measurements.map(x => {return x.id});
this.labels.sort((a,b) => {
return order.indexOf(a) - order.indexOf(b);
});
return this.labels;
}
/**
* handles form submit action
*/
_submit(event) {
this.$.formSettings.submit();
}
/**
* handles form submit action
*/
_form_submit(event) {
var self = this;
//collect all colors set
var colors = [];
for (var i = 0; i < this.labels.length; i++) {
var swatch = self.shadowRoot.querySelector('#' + this._getId(this.labels[i]));
colors.push(swatch.color);
}
self.selected = new epiviz.ui.charts.ColorPalette(colors, undefined, undefined, self.selected.keyIndices());
// this.vals = event.detail;
this.callback(colors);
this.closeColors();
}
/**
* handles form cancel action
*/
_cancel(event) {
this.closeColors();
}
/**
* handles element modal show action
*/
showColors(target, callback) {
var self = this;
// this.$.scrollContent.dialogElement = this.$.modal;
this._mList = {}
this._measurements.forEach(function(m) {
self._mList[m.id] = m.name;
});
this.callback = callback;
this.$.modal.positionTarget = target;
this.$.modal.open();
}
/**
* handles element modal close action
*/
closeColors() {
this.$.modal.close();
}
};
customElements.define(EpivizChartColors.is, EpivizChartColors);
</script>
</dom-module>