-
Notifications
You must be signed in to change notification settings - Fork 46
/
px-vis-dynamic-menu.html
175 lines (147 loc) · 4.62 KB
/
px-vis-dynamic-menu.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
<!--
Copyright (c) 2018, General Electric
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../polymer/polymer.html"/>
<link rel="import" href="../iron-dropdown/iron-dropdown.html"/>
<link rel="import" href="../px-icon-set/px-icon-set.html"/>
<link rel="import" href="../px-icon-set/px-icon.html"/>
<link rel="import" href="px-vis-behavior-common.html" />
<link rel="import" href="px-vis-behavior-d3.html" />
<link rel="import" href="css/px-vis-dynamic-menu-styles.html">
<!--
### Usage
<px-vis-dynamic-menu
dynamic-menu-config="[[dynamicMenuConfig]]">
</px-vis-dynamic-menu>
@element px-vis-dynamic-menu
@blurb a configurable, dynamically built dropdown menu
@homepage index.html
@demo demo.html
-->
<dom-module id="px-vis-dynamic-menu">
<template>
<style include="px-vis-dynamic-menu-styles"></style>
<button id="button" class="btn btn--bare--primary btn--icon" slot="dropdown-trigger" on-tap="_buttonClicked">
<px-icon icon="[[menuIcon]]" class="icon"></px-icon>
</button>
<iron-dropdown
id="dropdown"
opened="[[_opened]]"
always-on-top
no-animations
dynamic-align
allow-outside-scroll
no-overlap>
<div slot="dropdown-content" class="menu-wrapper">
<ul class="menu-list">
<template is="dom-repeat" items="{{dynamicMenuConfig}}">
<li class="menu-wrapper--item u-p--" on-tap="_clickItem" value="[[index]]">
<px-icon class="u-mr-- menu-icon" value="[[index]]" icon="[[item.icon]]"></px-icon>
<span>{{item.name}}</span>
</li>
</template>
</ul>
</div>
</iron-dropdown>
</template>
</dom-module>
<script>
Polymer({
is: 'px-vis-dynamic-menu',
behaviors: [
PxVisBehavior.dynamicMenuConfig
],
/**
* Properties block, expose attribute values to the DOM via 'reflect'
*
* @property properties
* @type Object
*/
properties: {
menuIcon: {
type: String,
value: 'px-utl:app-settings'
},
/**
* Whether the menu is currently shown.
*/
_shown: {
type: Boolean,
value: false
},
/**
* Whether the menu is currently opened.
*/
_opened: {
type: Boolean,
value: false
},
/**
* Class to use when showing the element.
*/
displayClass: {
type: String,
value: 'inline-block'
},
/**
* Data to be passed back out when running an item's action or firing the associated event.
*/
additionalDetail: {
type: Object
}
},
attached: function() {
this.$.dropdown.set('positionTarget', this.$.button);
},
_buttonClicked: function() {
this.set('_opened', !this._opened);
},
_clickItem: function(evt) {
var target = evt.currentTarget,
item = this.dynamicMenuConfig[target.value];
//if a function has been defined for this entry then run it
if(item.action) {
var action = item.action;
//parse function if it's a string
if(typeof item.action === 'string') {
var fn = eval('var f = function() { return ' + action + ';}; f();');
item.action = fn;
}
//make sure we use a specific context if it exists
if(item.actionContext) {
var actionBound = item.action.bind(item.actionContext);
actionBound({
'menuItem': item,
'additionalDetail': this.additionalDetail
});
} else {
//use chart context
this.fire('px-vis-action-request', {
'function': item.action,
'data': {
'menuItem': item,
'additionalDetail': this.additionalDetail
}});
}
}
//fire an event if required
if(item.eventName) {
var detail = {};
//include context if it exists
detail.menuItem = item;
detail.additionalDetail = this.additionalDetail;
this.fire('px-vis-event-request', {'eventName': item.eventName, 'data': detail})
}
this.set('_opened', false);
}
});
</script>