forked from FrontendMatter/material-design-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtooltip.js
More file actions
168 lines (144 loc) · 3.76 KB
/
Copy pathtooltip.js
File metadata and controls
168 lines (144 loc) · 3.76 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
import { handler } from 'dom-factory'
import { scrollTargetBehavior } from '../scroll-target-behavior'
/**
* @param {HTMLElement} element
* @return {Object}
*/
export const tooltipComponent = (element) => ({
/**
* Public properties.
* @type {Object}
*/
properties: {
/**
* Attaches the tooltip to an element.
* @type {Object}
*/
for: {
readOnly: true,
value () {
var target = this.element.getAttribute('for')
return document.querySelector('#' + target)
}
},
position: {
reflectToAttribute: true,
value: 'bottom'
},
opened: {
type: Boolean,
reflectToAttribute: true
}
},
/**
* Event listeners.
* @type {Array}
*/
listeners: [
'for.show(mouseenter, touchstart)',
'for.hide(mouseleave, touchend)',
'window._debounceResize(resize)'
],
observers: [
'_reset(position)'
],
mixins: [
scrollTargetBehavior(element)
],
/**
* A reference to the drawer layout.
* @return {Object}
*/
get drawerLayout () {
const layoutNode = document.querySelector('.mdk-js-drawer-layout')
if (layoutNode) {
return layoutNode.mdkDrawerLayout
}
},
_reset () {
this.element.removeAttribute('style')
var props = this.for.getBoundingClientRect()
var left = props.left + (props.width / 2)
var top = props.top + (props.height / 2)
var marginLeft = -1 * (this.element.offsetWidth / 2)
var marginTop = -1 * (this.element.offsetHeight / 2)
if (this.position === 'left' || this.position === 'right') {
if (top + marginTop < 0) {
this.element.style.top = '0'
this.element.style.marginTop = '0'
}
else {
this.element.style.top = top + 'px'
this.element.style.marginTop = marginTop + 'px'
}
}
else if (left + marginLeft < 0) {
this.element.style.left = '0'
this.element.style.marginLeft = '0'
}
else {
this.element.style.left = left + 'px'
this.element.style.marginLeft = marginLeft + 'px'
}
if (this.position === 'top') {
this.element.style.top = props.top - this.element.offsetHeight - 10 + 'px'
}
else if (this.position === 'right') {
this.element.style.left = props.left + props.width + 10 + 'px'
}
else if (this.position === 'left') {
this.element.style.left = props.left - this.element.offsetWidth - 10 + 'px'
}
else {
this.element.style.top = props.top + props.height + 10 + 'px'
}
},
_debounceResize () {
clearTimeout(this._debounceResizeTimer)
this._debounceResizeTimer = setTimeout(() => {
if (window.innerWidth !== this._debounceResizeWidth) {
this._debounceResizeWidth = window.innerWidth
this._reset()
}
}, 50)
},
/**
* Overrides `scrollTargetBehavior._scrollHandler`
*/
_scrollHandler () {
clearTimeout(this._debounceScrollTimer)
this._debounceScrollTimer = setTimeout(this._reset.bind(this), 50)
},
show () {
// this.element.style.transform = 'scale(1)'
this.opened = true
},
hide () {
// this.element.style.transform = 'scale(0)'
this.opened = false
},
toggle () {
this.opened = !this.opened
},
/**
* Initialize component.
*/
init () {
document.body.appendChild(this.element)
this._debounceResizeWidth = window.innerWidth
this.attachToScrollTarget()
this._reset()
if (this.drawerLayout && this.drawerLayout.hasScrollingRegion) {
this.scrollTargetSelector = this.drawerLayout.contentContainer
}
},
/**
* Destroy component.
*/
destroy () {
clearTimeout(this._debounceResizeTimer)
clearTimeout(this._debounceScrollTimer)
this.detachFromScrollTarget()
}
})
handler.register('mdk-tooltip', tooltipComponent)