forked from Hackception/hc-tree-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhc-tree-view-behavior.html
266 lines (243 loc) · 8.81 KB
/
hc-tree-view-behavior.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
<link rel="import" href="../polymer/polymer.html">
<script>
/* global hc */
// Ensure our namespace exists
window.hc = window.hc || {};
/**
* Use `hc.TreeViewBehavior` to implement a tree based around a flat data structure and built on top of
* `hc-nested-list`.
*
* @polymerBehavior hc.TreeViewBehavior
*/
hc.TreeViewBehavior = {
properties: {
/* Used to allow changing what the tree node's id is */
itemIdName: {
type: String,
value: 'id'
},
/* Used to allow changing what the tree node's parent id is */
itemParentIdName: {
type: String,
value: 'parentId'
},
/* The list of items to display in the tree, using a flat structure */
items: Array,
/* The list of all selected items without duplicates */
selectedItems: {
type: Array,
value: () => [],
notify: true
},
/**
* The parts object used by the list element
*
* @private
*/
_parts: Object
},
listeners: {
'item-selected-changed': 'itemSelectedChanged',
'item-state-changed': 'itemStateChanged'
},
/**
* Collapses the tree while maintaining the current selection states
*
* @param {Array.<Object>} [items = this.items]
* @param {Object} [parts = this._parts]
* @param {Boolean} [preventUpdate] - blocks the update to parts and blocks template updating
*/
collapse(items = this.items, parts = this._parts, preventUpdate = false) {
if (!items.length) return;
for(let i = 0; i < items.length; i++) {
const item = items[i];
item.open = false;
if (!item[this.itemParentIdName])
parts.root[item[this.itemIdName]].open = false;
const itemParts = parts[items[i][this.itemIdName]];
if (itemParts)
this.collapse(itemParts, parts, true);
}
if (!preventUpdate)
this.debounce('collapseUpdate', () => {
this.set('_parts', Object.assign({}, this._parts));
});
},
/**
* Takes an array of one or more items and removes them from the selectedItems array.
*
* @param {Array.<Object>} [items = this.selectedItems]
* @param {Boolean} [preventUpdate] - blocks the call to _updateState to block template updating
*/
deselectItems(items = this.selectedItems, preventUpdate) {
let selectedItems = [];
const getNodeId = (i) => i[this.itemIdName];
// Map to Ids for perf
selectedItems = this.selectedItems.map(getNodeId);
const passedItems = items.map(getNodeId);
// Filter by Ids for perf
selectedItems = selectedItems.filter((i) => passedItems.indexOf(i) === -1);
// Update selectedItems if there was an item removed
if (this.selectedItems.length !== selectedItems.length) {
const getItemById = (id) => this.selectedItems.find((i) => getNodeId(i) === id);
// Re-map ids to items if not empty
if (selectedItems.length > 0) selectedItems = selectedItems.map(getItemById);
this.set('selectedItems', selectedItems);
if (!preventUpdate) this._updateState(items, 'off');
}
},
/**
* Fetches the parent for the given item.
*
* @param {Object} item
* @returns {Object|undefined}
*/
getParent(item) {
return this.$.list.getParent(item);
},
/**
* Makes calls to select or deselect the item based on the selected in the event.
*
* @listens hc-tree-view-behavior#item-selected-changed
* @param {CustomEvent} [event]
* @param {Object} detail
*/
itemSelectedChanged(event, detail) {
this[detail.selected ? 'selectItems' : 'deselectItems']([detail.item], true);
},
/**
* Updates the parents and children of this item that it's state was changed, update thiers as well if needed.
*
* @listens hc-tree-view-behavior#item-state-changed
* @param {CustomEvent} event
* @param {Object} detail
*/
itemStateChanged(event, detail) {
const {item, state} = detail;
// Reject duplicated events
if (item.state === state) return;
item.state = state;
// Change selected status for parents up the chain
const parent = this._updateParents(item, state);
if (parent)
this[parent.state === 'on' ? 'selectItems' : 'deselectItems']([parent], true);
// Change selected status for children, even if they are hidden.
this[state === 'on' ? 'selectItems' : 'deselectItems'](this._updateChildren(item, state), true);
this.debounce('itemStateChanged', () => {
this.set('_parts', Object.assign({}, this._parts));
});
},
/**
* Clears all state from the tree, resetting back to the original tree.
*/
reset() {
this.deselectItems();
this.set('items', Object.assign([], this.items));
},
/**
* Takes an array of one or more items adds them to the selectedItems array, preventing duplicates.
*
* @param {Array.<Object>} items
* @param {Boolean} [preventUpdate] - blocks the call to _updateState to block template updating
*/
selectItems(items, preventUpdate) {
// Add all of the items to the selectedItems as a set
const selectedSet = new Set(this.selectedItems);
items.forEach((item) => selectedSet.add(item));
// Update selectedItems with an array version if an item was added
if (this.selectedItems.length !== selectedSet.size) {
this.set('selectedItems', Array.from(selectedSet));
if (!preventUpdate) this._updateState(items, 'on');
}
},
/**
* Updates the state on all children for the given item, returning the items that were updated.
*
* @private
* @param {Object} item
* @param {Boolean} state
* @param {Object} [parts = this._parts]
* @returns {Array.<Object>}
*/
_updateChildren(item, state, parts = this._parts) {
const children = parts[item[this.itemIdName]];
if (!children || !state) return [];
const updatedChildren = [];
for (let i = 0; i < children.length; i++) {
const child = children[i];
// Only update the children if the state actually changes.
if (child.state !== state) {
child.state = state;
updatedChildren.push(children[i]);
updatedChildren.push(...this._updateChildren(child, state, parts));
}
}
return updatedChildren;
},
/**
* Updates the state on the parent for the given item and all of it's parents.
*
* @private
* @param {Object} item
* @param {Boolean} state
* @param {Object} [parts = this._parts]
* @return {Object|undefined} - Parent of the item if it exists
*/
_updateParents(item, state, parts = this._parts) {
// Update up the chain (parents)
const parent = this.getParent(item);
if (parent && parent.state !== state) {
const siblings = parts[parent[this.itemIdName]];
const prevState = parent.state;
// If all of the siblings share the same state then set it for the parent, otherwise it's indeterminate
const newState = siblings.every((i) => i.state === state) ? state : null;
// If there's a change, update
if (prevState !== newState) {
parent.state = newState;
this._updateParents(parent, state, parts);
}
}
return parent;
},
/**
* Takes a list of items and updates their state in parts.
*
* @private
* @param {Array.<Object>} items
* @param {String} state
* @param {Object} [parts = this._parts]
*/
_updateState(items, state, parts = this._parts) {
if (!items || !parts) return;
// Filter items to remove children that have a parent in the array
items.filter((item) => !items.some((i) => i[this.itemIdName] === item[this.itemParentIdName])).
// Pass the filtered items into _updateChildren
forEach((item) => {
// Find the item in the parts object
const parentId = this.$.list._getParentId(item);
const partItem = parts[parentId].find((i) => i[this.itemIdName] === item[this.itemIdName]);
// Trigger a state change that will propagate
this.itemStateChanged(null, {
item: partItem,
state
});
});
}
};
/**
* Event for changing the selection status of a tree node
*
* @event hc-tree-view-behavior#item-selected-changed
* @type {Object}
* @property {Object} item - the item to change selection status for
* @property {Boolean} selection - the updated selection status
*/
/**
* Event for changing the state of a tree node
*
* @event hc-tree-view-behavior#item-state-changed
* @type {Object}
* @property {Object} item - the item to change state for
* @property {Boolean} state - the updated state
*/
</script>