-
Notifications
You must be signed in to change notification settings - Fork 0
/
cork-popup-button.js
142 lines (117 loc) · 2.9 KB
/
cork-popup-button.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
import {PolymerElement, html} from "@polymer/polymer"
import template from "./cork-popup-button.html"
import "@polymer/iron-icons"
import "@polymer/iron-icon"
import "@ucd-lib/cork-icons"
export class CorkPopupButton extends PolymerElement {
static get properties() {
return {
label: {
type: String,
value: ''
},
icon: {
type : String,
value : ''
},
open: {
type: Boolean,
value: false,
notify: true
},
noBtnArrow : {
type : Boolean,
value : false
},
tabindex : {
type : Number,
value : 1,
reflectToAttribute : true
}
};
}
static get template() {
return html([template]);
}
constructor() {
super();
this.hide = this.hide.bind(this);
this._resetTriangle = this._resetTriangle.bind(this);
this.addEventListener('keyup', this._onKeyUp.bind(this));
}
connectedCallback() {
super.connectedCallback();
this.updateStyles();
window.addEventListener('click', this.hide);
window.addEventListener('resize', this._resetTriangle);
}
disconnectedCallback() {
super.disconnectedCallback();
window.removeEventListener('click', this.hide);
window.removeEventListener('resize', this._resetTriangle);
}
toggle() {
this.open = !this.open;
this._resetTriangle();
}
_onKeyUp(e) {
if( e.which !== 13 ) return;
this._onClick(e);
}
_onClick(e) {
this._prevent(e);
this.toggle();
}
_prevent(e) {
// stop original event
e.preventDefault();
e.stopPropagation();
// send new click event, tagging this element as src
var event = new CustomEvent('click', {
bubbles : true,
composed : true,
detail : {
corkSrc : this
}
});
window.dispatchEvent(event);
}
focus() {
this.$.btn.focus();
}
unfocus() {
this.$.btn.unfocus();
}
hide(e) {
if( e && e.detail && e.detail.corkSrc === this ) {
return;
}
this.open = false;
}
_resetTriangle(noRetry) {
if( !this.open ) return;
var ww = window.innerWidth;
var pw = this.$.popup.offsetWidth;
var offset = this.$.btn.getBoundingClientRect();
var bl = offset.left;
var bw = offset.width;
var pw = this.$.popup.offsetWidth;
if( ww <= 768 ) {
this.$.popup.style.left = (-1 * bl) + 'px';
this.$.popup.style.right = (-1 * (ww - bl - bw)) + 'px';
} else if( bl + pw > ww + 20 ) {
this.$.popup.style.left = 'auto';
this.$.popup.style.right = 0;
} else {
this.$.popup.style.left = 0;
this.$.popup.style.right = 'auto';
}
var l = bw - 36;
var max = pw - 40;
if( l > max ) l = max;
this.$.triangle.style.left = l+'px';
if( !noRetry ) return;
setTimeout(() => this._resetTriangle(true), 0);
}
}
window.customElements.define('cork-popup-button', CorkPopupButton);