This repository was archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap-menu.html
More file actions
executable file
·290 lines (265 loc) · 8.33 KB
/
map-menu.html
File metadata and controls
executable file
·290 lines (265 loc) · 8.33 KB
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../smooth-scroll/smooth-scroll.html">
<link rel="import" href="map-menu-container.html">
<link rel="import" href="map-menu-builder.html">
<dom-module id="map-menu">
<template>
<style>
:host {
--map-menu-active-color: rgba(0,0,0, .1);
display: block;
overflow-y: scroll;
position: relative;
height: 100%;
}
#activeIndicator {
background: var(--map-menu-active-color);
transition: all .3s ease-in-out;
position: absolute
}
map-menu-container {
padding: 2em;
@apply(--map-menu-container)
}
/* turn default active color if indicator is on */
:host[active-indicator] map-menu-builder {
--map-menu-active-color: transparent;
}
</style>
<div id="itemslist">
<map-menu-container>
<div id="activeIndicator"></div>
<map-menu-builder id="builder" items="[[items]]"></map-menu-builder>
</map-menu-container>
</div>
<smooth-scroll id="smoothScroll"></smooth-scroll>
</template>
<script>
Polymer({
is: 'map-menu',
properties: {
title: {
type: String,
value: 'Content Outline',
},
data: {
type: Array,
value: null,
},
items: {
type: Array,
value: null,
notify: true,
},
/**
* Current selected item.
*/
selected: {
type: String,
notify: true,
observer: '__selectedChanged'
},
/**
* Auto scroll an active element if not in view
*/
autoScroll: {
type: Boolean,
value: false
},
/**
* Show active indicator animation
*/
activeIndicator: {
type: Boolean,
value: false
}
},
observers: [
'_dataChanged(data)'
],
listeners: {
'link-clicked': '__linkClickedHandler',
'child-attached': '__childAttached',
'toggle-updated': '__toggleUpdated'
},
__selectedChanged: function (newSelected, oldSelected) {
if (newSelected !== oldSelected) {
this.refreshActiveChildren(newSelected);
};
},
/**
* Set and unset active properties on children
* @param {string} activeItem
* @param {number} timeoutTime
*/
refreshActiveChildren: function (activeItem, timeoutTime = 100) {
// find active items and unactivate them
const oldActiveItem = this.querySelector('[active]');
if (activeItem && activeItem !== '') {
const newActiveItem = this.querySelector(`#${activeItem}`);
if (newActiveItem) {
// set the new active attribute to the item
newActiveItem.setAttribute('active', true);
// move the highlight thingy
if (this.activeIndicator) {
this.__updateActiveIndicator(newActiveItem, timeoutTime)
}
// if auto scroll enabled then scroll element into view
if (this.autoScroll) {
// kick off smooth scroll
this.$.smoothScroll.scroll(newActiveItem, {
duration: 300,
scrollElement: this
})
}
}
}
if (oldActiveItem) {
oldActiveItem.removeAttribute('active');
}
},
/**
* Set data property
*/
setData: function (data) {
this.set('data', []);
this.set('data', data)
},
/**
* Convert data from a linear array
* to a nested array for template rendering
*/
_dataChanged: function (data) {
const items = [];
if (!data) return;
// find parents
data.forEach(element => {
// find top level parents
if (!element.parent) {
items.push(element);
}
});
// Recursively find and set children
items.forEach((item, i) => {
this._setChildren(item, data);
});
// Update items array
this.set('items', []);
this.set('items', items);
},
/**
* Recursively search through a data to find children
* of a specified item.
* @param {object} item item of an array to search on. Passed by reference.
* @param {array} data linear array of the data set.
* @return {void}
*/
_setChildren: function (item, data) {
// find all children
const children = data.filter(d => item.id === d.parent);
item.children = children;
if (item.children.length > 0) {
item.children.forEach(child => {
// recursively call itself
this._setChildren(child, data);
});
}
},
/**
* Determine if a menu item has children
*/
__hasChildren: function (item) {
return item.children.length > 0;
},
/**
* asdf
*/
__linkClickedHandler: function (e) {
this.selected = e.detail.id;
this.fire('selected', e.detail.id);
},
/**
* When the children are attached find out if they
* have the same id as a selected and
*/
__childAttached: function (e) {
const childId = e.detail.id;
if (childId === this.selected) {
this.refreshActiveChildren(this.selected);
}
},
/**
* When a user clicks the toggle button to collapse or
* expand a submenu, this event gets triggered after
* the animation has been triggered
*/
__toggleUpdated: function (e) {
this.refreshActiveChildren(this.selected, 300)
},
/**
* Find out if
*/
__isInViewport: function (element) {
const scrollParent = this.__getScrollParent(element)
if (!scrollParent) return false
var elementTop = element.offsetTop;
var elementBottom = elementTop + element.offsetHeight;
var viewportTop = scrollParent.offsetTop;
var viewportBottom = viewportTop + scrollParent.offsetHeight;
return elementBottom > viewportTop && elementTop < viewportBottom;
},
/**
* Get scroll parent
*/
__getScrollParent: function (node) {
if (node == null) {
return null;
}
if (node.scrollHeight > node.clientHeight) {
return node;
} else {
return this.__getScrollParent(node.parentNode);
}
},
/**
* Move the highlight widget over active element
*/
__updateActiveIndicator: function (element, timeoutTime = 100) {
// run it through to set time just to let stuff set up
setTimeout(() => {
const activeIndicator = this.$.activeIndicator
const elementIsHidden = this.__parentsHidden(element)
const left = element.offsetLeft
const bottom = element.offsetBottom
const top = element.offsetTop
const width = element.offsetWidth
// if the element is hidden the set the indicator height to zero to make it disapear
const height = (!elementIsHidden) ? element.offsetHeight : 0
// if the height is zero then make the timeoutTime faster
timeoutTime = (height > 0) ? timeoutTime : 10
activeIndicator.setAttribute('style', `width:${width}px;height:${height}px;top:${top}px;left:${left}px`)
}, timeoutTime)
},
/**
* Find out if any parents of the item are collapsed
*/
__parentsHidden: function (node) {
// get the parent node
const parent = node.parentNode
// bail if we have no node to work with
if (parent == null) return null
// if we found a submenu check if it is hidden
if (parent.tagName === 'MAP-MENU-SUBMENU') {
// if open is set to false then we have
// found a hidden parent
if (!parent.opened) return true
}
// wrap up and exit if we came all the way back to map-menu
if (parent.tagName === 'MAP-MENU') return false
// if we got all the way here then we need recursively run this
// against the parent node
return this.__parentsHidden(parent)
},
});
</script>
</dom-module>