-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHalo.js
67 lines (58 loc) · 2.05 KB
/
Halo.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
const haloTemplateString = `
<style>
:host {
--halo-padding: 4px;
--halo-border-color-start: rgba(150, 150, 150, 1.0);
--halo-border-color-end: rgba(210, 210, 210, 1.0);
display: block;
position: absolute;
width: calc(100% + (var(--halo-padding) * 2));
height: calc(100% + (var(--halo-padding) * 2));
top: calc(-1 * var(--halo-padding));
left: calc(-1 * var(--halo-padding));
border: 1px solid var(--halo-border-color-start);
z-index: 1000;
}
</style>
`;
class Halo extends HTMLElement {
constructor() {
super();
this.template = document.createElement('template');
this.template.innerHTML = haloTemplateString;
this.attachShadow({mode: 'open'});
this.shadowRoot.append(
this.template.content.cloneNode(true)
);
// Let's cheat! Store the delta values
// on the element itself
this.deltaX = 0;
this.deltaY = 0;
// Bound component methods
this.handleMouseDown = this.handleMouseDown.bind(this);
this.handleMouseMove = this.handleMouseMove.bind(this);
this.handleMouseUp = this.handleMouseUp.bind(this);
}
connectedCallback() {
this.parentElement.addEventListener('mousedown', this.handleMouseDown);
}
disconnectedCallback(){
this.parentElement.removeEventListener('mousedown', this.handleMouseDown);
}
handleMouseDown(event){
document.addEventListener('mouseup', this.handleMouseUp);
document.addEventListener('mousemove', this.handleMouseMove);
}
handleMouseUp(event){
event.stopPropagation();
event.preventDefault();
document.removeEventListener('mouseup', this.handleMouseUp);
document.removeEventListener('mousemove', this.handleMouseMove);
}
handleMouseMove(event){
const nextX = this.deltaX + event.movementX;
const nextY = this.deltaY + event.movementY;
this.parentElement.style.tranform = `translate(${nextX}px, ${nextY}px)`;
}
}
window.customElements.define('tts-halo', Halo);