-
Notifications
You must be signed in to change notification settings - Fork 84
/
site-outline-block.js
302 lines (300 loc) · 8.77 KB
/
site-outline-block.js
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
/**
* Copyright 2019 The Pennsylvania State University
* @license Apache-2.0, see License.md for full text.
*/
import { html, PolymerElement } from "@polymer/polymer/polymer-element.js";
import { store } from "@lrnwebcomponents/haxcms-elements/lib/core/haxcms-site-store.js";
import { autorun, toJS } from "mobx/lib/mobx.module.js";
import "@lrnwebcomponents/haxcms-elements/lib/ui-components/query/site-query.js";
import "@polymer/polymer/lib/elements/dom-repeat.js";
/**
* `site-outline-block`
* `Menu on top of the site typically a bar of options`
*
* @polymer
* @demo demo/index.html
*/
class SiteOutlineBlock extends PolymerElement {
/**
* Store the tag name to make it easier to obtain directly.
*/
static get tag() {
return "site-outline-block";
}
// render function
static get template() {
return html`
<style>
:host {
display: block;
--site-top-menu-bg: var(--haxcms-color, #ffffff);
--site-top-menu-indicator-arrow: 6px;
}
:host([sticky]) {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 1000;
@apply --site-top-menu-sticky;
}
.wrapper {
display: flex;
justify-content: center;
justify-items: space-evenly;
background-color: var(--site-top-menu-bg);
@apply --site-top-menu-wrapper;
}
:host .wrapper ::slotted(div.spacing) {
display: inline-flex;
@apply --site-top-menu-spacing;
}
.spacing {
display: inline-flex;
@apply --site-top-menu-spacing;
}
.link {
color: var(--site-top-menu-link-color, #444444);
@apply --site-top-menu-link;
}
paper-button {
text-transform: unset;
min-width: unset;
@apply --site-top-menu-button;
}
paper-button:hover,
paper-button:focus,
paper-button:active {
@apply --site-top-menu-button-hover;
}
.active {
color: var(--site-top-menu-link-active-color, #000000);
@apply --site-top-menu-link-active;
}
#indicator {
transition: 0.4s ease-in-out all;
transition-delay: 0.2s;
position: relative;
width: 0;
height: 0;
visibility: hidden;
}
:host([indicator="line"]) #indicator {
border-bottom: 2px solid var(--site-top-menu-indicator-color, #000000);
@apply --site-top-menu-indicator;
}
:host([indicator="arrow"]) #indicator {
border-left: var(--site-top-menu-indicator-arrow) solid transparent;
border-right: var(--site-top-menu-indicator-arrow) solid transparent;
border-bottom: var(--site-top-menu-indicator-arrow) solid
var(--site-top-menu-indicator-color, #000000);
@apply --site-top-menu-indicator;
}
#indicator.activated {
visibility: visible;
position: absolute;
@apply --site-top-menu-indicator-activated;
}
:host([notitle]) .spacing .link-title {
display: none;
}
.spacing .link-index {
display: none;
}
:host([showindex]) .spacing .link-index {
display: inline-flex;
}
</style>
<div class="wrapper">
<slot name="prefix"></slot>
<site-query
result="{{__items}}"
sort='{"order": "ASC"}'
conditions='{"parent": null}'
></site-query>
<dom-repeat items="[[__items]]" mutable-data>
<template>
<div class="spacing">
<a
data-id$="[[item.id]]"
class="link"
tabindex="-1"
title$="Go to [[item.title]]"
href$="[[item.location]]"
><paper-button noink="[[noink]]"
><span class="link-index">[[humanIndex(index)]]</span
><span class="link-title">[[item.title]]</span></paper-button
></a
>
</div>
</template>
</dom-repeat>
<slot name="suffix"></slot>
</div>
<div id="indicator"></div>
`;
}
/**
* Props
*/
static get properties() {
return {
/**
* manifest of everything, in case we need to check on children of parents
*/
manifest: {
type: Object
},
/**
* acitvely selected item
*/
activeId: {
type: String,
observer: "_activeIdChanged"
},
/**
* visually stick to top of interface at all times
*/
sticky: {
type: Boolean,
reflectToAttribute: true,
value: false
},
/**
* visualize the indicator as a a line, arrow, or not at all
*/
indicator: {
type: String,
reflectToAttribute: true,
value: "line"
},
/**
* ink on the buttons
*/
noink: {
type: Boolean,
reflectToAttribute: true,
value: false
},
/**
* hide title on the buttons
*/
notitle: {
type: Boolean,
reflectToAttribute: true,
value: false
},
/**
* ink on the buttons
*/
showindex: {
type: Boolean,
reflectToAttribute: true,
value: false
},
/**
* Stupid but faster then calculating on the fly for sure
*/
arrowSize: {
type: Number,
value: 6
}
};
}
humanIndex(index) {
return index + 1;
}
/**
* When active ID changes, see if we know what to highlight automatically
*/
_activeIdChanged(newValue) {
// as long as didn't disable the indicator, do this processing
if (this.indicator != "none") {
if (newValue) {
this.shadowRoot.querySelector("#indicator").classList.add("activated");
let el = null;
//ensure that this level is included
if (this.shadowRoot.querySelector('[data-id="' + newValue + '"]')) {
el = this.shadowRoot.querySelector('[data-id="' + newValue + '"]');
} else {
let tmpItem = this.manifest.items.find(i => i.id == newValue);
// fallback, maybe there's a child of this currently active
while (el === null && tmpItem && tmpItem.parent != null) {
// take the parent object of this current item
tmpItem = this.manifest.items.find(i => i.id == tmpItem.parent);
// see if IT lives in the dom, if not, keep going until we run out
if (
tmpItem &&
this.shadowRoot.querySelector('[data-id="' + tmpItem.id + '"]')
) {
el = this.shadowRoot.querySelector(
'[data-id="' + tmpItem.id + '"]'
);
}
}
}
if (this._prevEl) {
this._prevEl.classList.remove("active");
}
if (el) {
el.classList.add("active");
this._prevEl = el;
if (this.indicator == "arrow") {
this.shadowRoot.querySelector("#indicator").style.left =
el.offsetLeft + el.offsetWidth / 2 - this.arrowSize + "px";
this.shadowRoot.querySelector("#indicator").style.top =
el.offsetTop + el.offsetHeight - this.arrowSize + "px";
} else {
this.shadowRoot.querySelector("#indicator").style.left =
el.offsetLeft + "px";
this.shadowRoot.querySelector("#indicator").style.top =
el.offsetTop + el.offsetHeight + "px";
this.shadowRoot.querySelector("#indicator").style.width =
el.offsetWidth + "px";
}
}
} else {
// shouldn't be possible but might as well list
this.shadowRoot
.querySelector("#indicator")
.classList.remove("activated");
}
}
}
connectedCallback() {
super.connectedCallback();
this.__disposer = autorun(() => {
this.manifest = toJS(store.manifest);
});
// minor timing thing to ensure store has picked active
// needed if routes set on first paint or lifecycles miss
setTimeout(() => {
this.__disposer2 = autorun(() => {
this.activeId = toJS(store.activeId);
});
}, 50);
window.addEventListener(
"resize",
() => {
this._activeIdChanged(this.activeId);
},
true
);
}
disconnectedCallback() {
super.disconnectedCallback();
this.__disposer();
if (this.__disposer2) {
this.__disposer2();
}
window.removeEventListener(
"resize",
() => {
this._activeIdChanged(this.activeId);
},
true
);
}
}
window.customElements.define(SiteOutlineBlock.tag, SiteOutlineBlock);
export { SiteOutlineBlock };