-
Notifications
You must be signed in to change notification settings - Fork 77
/
active-when-visible.js
136 lines (135 loc) · 3.09 KB
/
active-when-visible.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
/**
* Copyright 2019 The Pennsylvania State University
* @license Apache-2.0, see License.md for full text.
*/
import { LitElement, html, css } from "lit-element/lit-element.js";
import { store } from "@lrnwebcomponents/haxcms-elements/lib/core/haxcms-site-store.js";
/**
* `active-when-visible`
* `Title of the site`
*
* @polymer
* @demo demo/index.html
*/
class ActiveWhenVisible extends LitElement {
static get styles() {
return [
css`
:host {
display: block;
}
a {
height: 10px;
width: 10px;
float: left;
pointer-events: none;
background-color: transparent;
}
`
];
}
/**
* Store the tag name to make it easier to obtain directly.
*/
static get tag() {
return "active-when-visible";
}
// render function
render() {
return html`
<div>
<a
id="a"
.href="${this._a}"
.name="#${this.itemId}"
aria-hidden="true"
></a>
<slot></slot>
</div>
`;
}
constructor() {
super();
this.thresholds = [0.0, 0.25, 0.5, 0.75, 1.0];
this.rootMargin = "0px";
this.visibleLimit = 0.5;
this.isVisible = false;
}
updated(changedProperties) {
changedProperties.forEach((oldValue, propName) => {
if (propName == "isVisible") {
// fire an event that this is a core piece of the system
this.dispatchEvent(
new CustomEvent("is-visible-changed", {
detail: this[propName]
})
);
}
});
}
/**
* LitElement / popular convention
*/
static get properties() {
return {
itemId: {
type: String,
attribute: "item-id"
},
_a: {
type: String
},
thresholds: {
type: Array
},
rootMargin: {
type: String,
attribute: "root-margin"
},
visibleLimit: {
type: Number,
reflect: true,
attribute: "visible-limit"
},
isVisible: {
type: Boolean,
attribute: "is-visible"
}
};
}
connectedCallback() {
super.connectedCallback();
// setup the intersection observer
this.observer = new IntersectionObserver(
this.handleIntersectionCallback.bind(this),
{
root: document.rootElement,
rootMargin: this.rootMargin,
threshold: this.thresholds
}
);
this.observer.observe(this);
}
/**
* Handle this being visible
*/
handleIntersectionCallback(entries) {
for (let entry of entries) {
if (Number(entry.intersectionRatio).toFixed(2) >= this.visibleLimit) {
// now we care
if (this.itemId) {
let item = store.findItem(this.itemId);
this._a = item.location
.replace("pages/", "")
.replace("/index.html", "");
this.isVisible = true;
setTimeout(() => {
this.shadowRoot.querySelector("#a").click();
}, 25);
}
}
}
}
}
window.customElements.define(ActiveWhenVisible.tag, ActiveWhenVisible);
export { ActiveWhenVisible };