-
Notifications
You must be signed in to change notification settings - Fork 24
/
paper-tree-node.html
executable file
·251 lines (205 loc) · 7.5 KB
/
paper-tree-node.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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<link rel="import" href="../iron-icon/iron-icon.html">
<link rel="import" href="../paper-menu-button/paper-menu-button.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="../paper-menu/paper-menu.html">
<link rel="import" href="../paper-item/paper-item.html">
<!--
`<paper-tree-node>` display a tree node with expandable/collapsible capabilities and actions menu.
A node is defined by a name, an icon and a list of actions.
Example:
<paper-tree-node></paper-tree-node>
### Styling
The following custom properties and mixins are available for styling:
Custom property | Description | Default
----------------|-------------|----------
`--paper-tree-selected-background-color` | Highlight color for selected node | `rgba(200, 200, 200, 0.5)`
`--paper-tree-selected-color` | Text and icon color for selected node | `inherit`
`--paper-tree-toggle-theme` | Change theme for node +/- toggle |
`--paper-tree-icon-theme` | Change theme for node icon |
@demo
-->
<link rel="import" href="../iron-icons/iron-icons.html">
<dom-module id="paper-tree-node">
<style>
:host(.selected) > .node-container > .node-row {
background-color: var(--paper-tree-selected-background-color, rgba(200, 200, 200, 0.5));
color: var(--paper-tree-selected-color, inherit);
}
:host(.selected) > .node-container > .node-row > #actions {
display: inline-block;
}
.node-container {
white-space: nowrap;
}
.node-row {
padding-left: 4px;
padding-right: 4px;
}
.node-preicon.collapsed,
.node-preicon.expanded {
padding-left: 0px;
}
.node-preicon {
padding-left: 18px;
}
.node-preicon:before {
margin-right: 5px;
@apply(--paper-tree-toggle-theme);
}
.node-preicon.collapsed:before {
content: '\002B';
}
.node-preicon.expanded:before {
content: '\2212';
}
.node-preicon, .node-name {
cursor: pointer;
}
.node-icon {
cursor: pointer;
width: 24px;
height: 24px;
@apply(--paper-tree-icon-theme);
}
#actions {
display: none;
float: right;
padding: 0;
top: -8px; /* cancel the padding of `paper-icon-button`. */
}
ul {
margin: 0;
padding-left: 20px;
}
li {
list-style-type: none;
}
[hidden] {
display: none;
}
</style>
<template>
<div class="node-container">
<div class="node-row">
<span class$="{{_computeClass(data.*)}}" on-click="toggleChildren"></span>
<iron-icon class="node-icon" icon$="{{_computeIcon(data.icon)}}" on-click="select"></iron-icon>
<span class="node-name" on-click="select">{{data.name}}</span>
<template is="dom-if" if="{{actions}}">
<paper-menu-button id="actions">
<paper-icon-button icon="more-vert" noink class="dropdown-trigger"></paper-icon-button>
<paper-menu class="dropdown-content">
<template is="dom-repeat" items="{{actions}}">
<paper-item on-click="_actionClicked">{{item.label}}</paper-item>
</template>
</paper-menu>
</paper-menu-button>
</template>
</div>
<template is="dom-if" if="{{data.open}}">
<ul>
<template is="dom-repeat" items="{{data.children}}">
<li>
<paper-tree-node data="{{item}}" actions="{{actions}}"></paper-tree-node>
</li>
</template>
</ul>
</template>
</div>
</template>
</dom-module>
<script>
Polymer({
is: 'paper-tree-node',
properties: {
/**
* Data hold by this node (contains the children).
*
* Specific data:
*
* - `data.name`: string representing the node name.
* - `data.icon`: string telling which icon to use (default to 'folder' icon).
* - `data.open`: boolean telling whether the node is expanded or not.
* - `data.children` array containing the children of the node.
*/
data: {
type: Object,
value: function() {
return null;
}
},
/**
* `actions` available for this node. Each action object has the following fields:
*
* - `action.label`: string representing the display name of the menu item.
* - `action.event`: string which is the event name to dispatch whenever the item is clicked.
*
*/
actions: {
type: Array,
value: function() {
return null;
}
}
},
/**
* The `select` event is fired whenever `select()` is called on the node.
*
* @event select
*/
/**
* The `toggle` event is fired whenever a tree node is expanded or collapsed.
*
* @event toggle
*/
/**
* Returns the necessary classes.
*
* @param {object} change An object containing the property that changed and its value.
* @return {string} The class name indicating whether the node is open or closed
*/
_computeClass: function(change) {
var open = change && change.base && change.base.open;
var children = change && change.base && change.base.children;
return 'node-preicon ' + ((open && children && children.length) ? 'expanded' : children && children.length ? 'collapsed' : '');
},
/**
* Compute the necessary node icon.
*
* @param {string=folder} an icon name.
* @return {string} the computed icon name.
*/
_computeIcon: function(icon) {
return icon ? icon : 'folder';
},
_actionClicked: function (event) {
this.fire(event.model.item.event, this);
},
/**
* Highlights node as the selected node.
*/
select: function() {
this.fire("select", this);
},
/**
* Returns the parent tree node. Returns `null` if root.
*/
getParent: function () {
return this.domHost.tagName === 'PAPER-TREE-NODE' ? this.domHost : null;
},
/**
* Returns the children tree nodes.
*/
getChildren: function () {
return Polymer.dom(this.root).querySelectorAll('paper-tree-node');
},
/**
* Display/Hide the children nodes.
*/
toggleChildren: function() {
this.set("data.open", !this.data.open && this.data.children && this.data.children.length);
setTimeout(this.fire.bind(this, "toggle", this));
}
});
</script>