forked from predixdesignsystem/px-vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpx-vis-toolbar.html
362 lines (331 loc) · 11 KB
/
px-vis-toolbar.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
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
<link rel="import" href="../polymer/polymer.html"/>
<link rel="import" href="../px-tooltip/px-tooltip.html"/>
<link rel="import" href="px-vis-behavior-common.html" />
<link rel="import" href="px-vis-behavior-d3.html" />
<!--
Element providing an on-chart zoom.
##### Usage
<px-vis-toolbar
additional-detail="[[item]]"
dynamic-menu-config="[[dynamicMenuConfig]]">
</px-vis-toolbar>
@element px-vis-toolbar
@blurb Element providing a menu dynamically built depending on options
@homepage index.html
@demo demo.html
-->
<link rel="import" href="css/px-vis-toolbar-styles.html">
<dom-module id="px-vis-toolbar">
<template>
<style include="px-vis-toolbar-styles"></style>
<!--main row-->
<span id="mainRow" class$="flex [[_getRowDirection(horizontalAlignment)]]">
<template id="mainRepeat" is="dom-repeat" items={{_internalConfig}} strip-whitespace>
<span class$="u-p-- flex__item main-item [[_getSelectedClass(item.selected)]]" on-tap="_clickItem" value="{{index}}">
<i class$="fa [[_getIcon(item)]] [[_getSelectedClass(item.selected)]]" value="{{index}}">
</i>{{item.title}}
<template is="dom-if" if="[[_hasTooltip(item)]]">
<px-tooltip
tooltip-message="[[item.tooltipLabel]]"
orientation="top"
smart-orientation>
</px-tooltip>
</template>
</span>
</template>
</span>
<!--sub row-->
<template id="templateSubRow" is="dom-if" if="[[_isWithinChart(withinChart)]]">
<span id="subRow" class$="flex [[_getRowDirection(horizontalAlignment)]] [[_getSubRowClass(withinChart)]]">
<template id="subRepeat" is="dom-repeat" items={{currentSubConfig}} strip-whitespace>
<span class$="u-p-- flex__item main-item [[_getSelectedClass(item.selected)]]" on-tap="_clickSubItem" value="{{index}}">
<i class$="fa [[_getIcon(item)]] [[_getSelectedClass(item.selected)]]" value="{{index}}">
</i>{{item.title}}
<template is="dom-if" if="[[_hasTooltip(item)]]">
<px-tooltip
tooltip-message="[[item.tooltipLabel]]"
orientation="top"
smart-orientation>
</px-tooltip>
</template>
</span>
</template>
</span>
</template>
</template>
</dom-module>
<script>
Polymer({
is: 'px-vis-toolbar',
behaviors: [
PxVisBehavior.actionConfig,
PxVisBehavior.toolbarConfig
],
/**
* Properties block, expose attribute values to the DOM via 'reflect'
*
* @property properties
* @type Object
*/
properties: {
/**
* Whether the toolbar is within a chart. drives where the secondary toolbar is displayed
*/
withinChart: {
type: Boolean,
value: false
},
/**
* The charts margin. Used to position the secondary toolbar if withinChart is true
*/
chartMargin: {
type: Object
},
/**
* Current action config determined by what has been selected on the toolbar. Needs
* to be used by interaction space
*/
actionConfig: {
type: Object
},
/**
* Config for default actions: tooltip/pan/zoom
*/
_defaultActionsMapping: {
type: Object,
value: function() {
return {
'tooltip': {
'tooltipLabel': 'tooltip',
'icon': 'fa-crosshairs',
'buttonGroup': 1,
'actionConfig': {
'mouseout': 'resetTooltip',
'mousemove': 'calcTooltipData',
'mousedown': 'null',
'mouseup': 'null'
}
},
'pan': {
'tooltipLabel': 'pan',
'icon': 'fa-arrows',
'buttonGroup': 1,
'actionConfig': {
'mousedown': 'startPanning',
'mouseup': 'stopPanning',
},
'subConfig': {
'resetZoom': {
'icon': 'fa-expand',
'tooltipLabel': 'reset zoom to inital value',
'eventName': 'px-vis-toolbar-reset-zoom'
}
}
},
'zoom': {
'tooltipLabel': 'zoom',
'icon': 'fa-search',
'buttonGroup': 1,
'selected': true,
'actionConfig': {
'mousedown': 'startZooming',
'mouseup': 'stopZooming',
},
'subConfig': {
'zoomIn': {
'icon': 'fa-plus',
'tooltipLabel': 'zoom in',
'eventName': 'px-vis-toolbar-zoom-in'
},
'zoomOut': {
'icon': 'fa-minus',
'tooltipLabel': 'zoom out',
'eventName': 'px-vis-toolbar-zoom-out'
},
'undoZoom': {
'icon': 'fa-undo',
'tooltipLabel': 'undo zoom',
'eventName': 'px-vis-toolbar-undo-zoom'
},
'resetZoom': {
'icon': 'fa-expand',
'tooltipLabel': 'reset zoom to inital value',
'eventName': 'px-vis-toolbar-reset-zoom'
}
}
}
};
}
},
/**
* Current subconfiguration. Can be the result of the toolbarConfig or passed in from another toolbar
*/
currentSubConfig: {
type: Object,
notify: true
},
/**
* Current config used to build the toolbar
*/
_internalConfig: {
type: Object
},
/**
* Horizontal alignment of the toolbar
*/
horizontalAlignment: {
type: String,
value: 'right'
}
},
observers: [
'_configChanged(toolbarConfig.*, horizontalAlignment)',
'_positionSubRow(withinChart, chartMargin, horizontalAlignment)'
],
listeners: {
},
_configChanged: function() {
var internalConf = [],
keys = Object.keys(this.toolbarConfig);
for(var i=0; i<keys.length; i++) {
var toPush;
//if this is a default action use it
if(this._defaultActionsMapping[keys[i]]) {
toPush = this._defaultActionsMapping[keys[i]];
} else {
toPush = this.toolbarConfig[keys[i]];
}
internalConf.push(toPush);
if(toPush.selected) {
this._tryProcessSubConfig(toPush);
}
}
//reverse array to take flex row rev into account
if(this.horizontalAlignment === 'right') {
internalConf.reverse();
}
this.set('_internalConfig', internalConf);
},
/**
* Called when clicking an item of the main toolbar.
*/
_clickItem: function(evt) {
evt = Polymer.dom(evt);
var index = evt.localTarget.value,
conf = this._internalConfig[index];
//make it the current selection if it is in a buttonGroup
if(conf.buttonGroup) {
//ensure we remember selection, deselect others and select this one
for(var i =0; i<this._internalConfig.length; i++) {
if(index !== i && this._internalConfig[i].buttonGroup === conf.buttonGroup) {
this.set('_internalConfig.#' + i + '.selected', false);
}
}
this.set('_internalConfig.#' + index + '.selected', true);
}
this._tryProcessSubConfig(conf);
this._processItem(conf);
},
_tryProcessSubConfig: function(conf) {
//process sub config if existing
if(conf.subConfig) {
var subConf = [],
keys = Object.keys(conf.subConfig);
//reverse order to take flex-row-reverse into account
if(this.horizontalAlignment === 'right') {
for(var i=keys.length-1; i>-1; i--) {
subConf.push(conf.subConfig[keys[i]]);
}
} else {
for(var i=0; i<keys.length; i++) {
subConf.push(conf.subConfig[keys[i]]);
}
}
this.set('currentSubConfig', subConf);
} else {
this.set('currentSubConfig', []);
}
},
/**
* Called when clicking an item of the secondary toolbar.
*/
_clickSubItem: function(evt) {
evt = Polymer.dom(evt);
var index = evt.localTarget.value,
conf = this.currentSubConfig[index];
//make it the current selection if it is selectable
if(conf.buttonGroup) {
//ensure we remember selection, deselect others and select this one
for(var i =0; i<this.currentSubConfig.length; i++) {
if(i !== index && this.currentSubConfig[i].buttonGroup === conf.buttonGroup) {
this.set('currentSubConfig.#' + i + '.selected', false);
}
}
this.set('currentSubConfig.#' + index + '.selected', true);
}
this._processItem(conf);
},
/**
* Processes an item click: fire event if needed and set action config appropriately
*/
_processItem: function(conf) {
if(conf.actionConfig) {
if(typeof conf.actionConfig === 'string') {
this.set('actionConfig', JSON.parse(conf.actionConfig));
} else {
this.set('actionConfig', conf.actionConfig);
}
}
if(conf.eventName) {
//fire the event fromt the chart's context
this.fire('px-vis-event-request', {'eventName': conf.eventName, 'data': conf});
}
if(conf.onClick) {
var fn;
if(typeof conf.onClick === 'function') {
fn = conf.onClick;
} else if (typeof conf.onClick === 'string') {
fn = eval('var f = function() { return ' + conf.onClick + ';}; f();');
}
//run it from the chart's context
this.fire('px-vis-action-request', {'function': fn, 'data': conf});
}
},
_getIcon: function(item) {
return item.icon ? (item.icon + ' u-mr--') : '';
},
_getSelectedClass: function(selected) {
return selected ? 'selected ' : '';
},
_hasTooltip: function(item) {
return item.tooltipLabel && item.tooltipLabel.length;
},
_positionSubRow: function(withinChart, chartMargin) {
if(withinChart) {
//make sure dom if is up to date
this.$.templateSubRow.render();
this.$$('#subRow').style['top'] = Number(chartMargin.top) + 'px';
if(this.horizontalAlignment === 'left') {
this.$$('#subRow').style['left'] = Number(chartMargin.left) + 'px';
}
}
},
_getSubRowClass: function(withinChart) {
if(withinChart) {
return 'subrow--chart ';
} else {
return '';
}
},
_isWithinChart: function(withinChart) {
return withinChart;
},
_getRowDirection: function(horizontalAlignment) {
if(horizontalAlignment === 'right') {
return 'flex--row--rev ';
} else {
return 'flex--row ';
}
}
});
</script>