/
toolpop.js
312 lines (309 loc) · 8.62 KB
/
toolpop.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
303
304
305
306
307
308
309
310
311
312
/*
* Tooltip/Popover component mixin
* Common props
*/
import { isArray } from '../utils/array'
import { assign } from '../utils/object'
import { isElement, getById } from '../utils/dom'
import observeDom from '../utils/observe-dom'
const PLACEMENTS = {
top: 'top',
topleft: 'topleft',
topright: 'topright',
right: 'right',
righttop: 'righttop',
rightbottom: 'rightbottom',
bottom: 'bottom',
bottomleft: 'bottomleft',
bottomright: 'bottomright',
left: 'left',
lefttop: 'lefttop',
leftbottom: 'leftbottom',
auto: 'auto'
}
const OBSERVER_CONFIG = {
subtree: true,
childList: true,
characterData: true,
attributes: true,
attributeFilter: ['class', 'style']
}
export default {
props: {
target: {
// String ID of element, or element/component reference
type: [String, Object, HTMLElement]
},
delay: {
type: [Number, Object, String],
default: 0
},
offset: {
type: [Number, String],
default: 0
},
noFade: {
type: Boolean,
default: false
},
container: {
// String ID of container, if null body is used (default)
type: String,
default: null
},
boundary: {
// String: scrollParent, window, or viewport
// Element: element reference
type: [String, Object],
default: 'scrollParent'
},
show: {
type: Boolean,
default: false
},
disabled: {
type: Boolean,
default: false
}
},
watch: {
show (show, old) {
if (show === old) {
return
}
show ? this.onOpen() : this.onClose()
},
disabled (disabled, old) {
if (disabled === old) {
return
}
disabled ? this.onDisable() : this.onEnable()
}
},
created () {
// Create non-reactive property
this._toolpop = null
this._obs_title = null
this._obs_content = null
},
mounted () {
// We do this in a next tick to ensure DOM has rendered first
this.$nextTick(() => {
// Instantiate ToolTip/PopOver on target
// The createToolpop method must exist in main component
if (this.createToolpop()) {
if (this.disabled) {
// Initially disabled
this.onDisable()
}
// Listen to open signals from others
this.$on('open', this.onOpen)
// Listen to close signals from others
this.$on('close', this.onClose)
// Listen to disable signals from others
this.$on('disable', this.onDisable)
// Listen to disable signals from others
this.$on('enable', this.onEnable)
// Observe content Child changes so we can notify popper of possible size change
this.setObservers(true)
// Set intially open state
if (this.show) {
this.onOpen()
}
}
})
},
updated () {
// If content/props changes, etc
if (this._toolpop) {
this._toolpop.updateConfig(this.getConfig())
}
},
/* istanbul ignore next: not easy to test */
activated () {
// Called when component is inside a <keep-alive> and component brought offline
this.setObservers(true)
},
/* istanbul ignore next: not easy to test */
deactivated () {
// Called when component is inside a <keep-alive> and component taken offline
if (this._toolpop) {
this.setObservers(false)
this._toolpop.hide()
}
},
/* istanbul ignore next: not easy to test */
beforeDestroy () {
// Shutdown our local event listeners
this.$off('open', this.onOpen)
this.$off('close', this.onClose)
this.$off('disable', this.onDisable)
this.$off('enable', this.onEnable)
this.setObservers(false)
// bring our content back if needed
this.bringItBack()
if (this._toolpop) {
this._toolpop.destroy()
this._toolpop = null
}
},
computed: {
baseConfig () {
const cont = this.container
let delay = (typeof this.delay === 'object') ? this.delay : (parseInt(this.delay, 10) || 0)
return {
// Title prop
title: (this.title || '').trim() || '',
// Contnt prop (if popover)
content: (this.content || '').trim() || '',
// Tooltip/Popover placement
placement: PLACEMENTS[this.placement] || 'auto',
// Container curently needs to be an ID with '#' prepended, if null then body is used
container: cont ? (/^#/.test(cont) ? cont : `#${cont}`) : false,
// boundariesElement passed to popper
boundary: this.boundary,
// Show/Hide delay
delay: delay || 0,
// Offset can be css distance. if no units, pixels are assumed
offset: this.offset || 0,
// Disable fade Animation?
animation: !this.noFade,
// Open/Close Trigger(s)
trigger: isArray(this.triggers) ? this.triggers.join(' ') : this.triggers,
// Callbacks so we can trigger events on component
callbacks: {
show: this.onShow,
shown: this.onShown,
hide: this.onHide,
hidden: this.onHidden,
enabled: this.onEnabled,
disabled: this.onDisabled
}
}
}
},
methods: {
getConfig () {
const cfg = assign({}, this.baseConfig)
if (this.$refs.title && this.$refs.title.innerHTML.trim()) {
// If slot has content, it overrides 'title' prop
// We use the DOM node as content to allow components!
cfg.title = this.$refs.title
cfg.html = true
}
if (this.$refs.content && this.$refs.content.innerHTML.trim()) {
// If slot has content, it overrides 'content' prop
// We use the DOM node as content to allow components!
cfg.content = this.$refs.content
cfg.html = true
}
return cfg
},
onOpen () {
if (this._toolpop) {
this._toolpop.show()
}
},
onClose (callback) {
if (this._toolpop) {
this._toolpop.hide(callback)
} else if (typeof callback === 'function') {
callback()
}
},
onDisable () {
if (this._toolpop) {
this._toolpop.disable()
}
},
onEnable () {
if (this._toolpop) {
this._toolpop.enable()
}
},
updatePosition () {
if (this._toolpop) {
// Instruct popper to reposition popover if necessary
this._toolpop.update()
}
},
getTarget () {
const target = this.target
if (typeof target === 'string') {
// Assume ID of element
return getById(target)
} else if (typeof target === 'object' && isElement(target.$el)) {
// Component reference
return target.$el
} else if (typeof target === 'object' && isElement(target)) {
// Element reference
return target
}
return null
},
onShow (evt) {
this.$emit('show', evt)
},
onShown (evt) {
this.setObservers(true)
this.$emit('update:show', true)
this.$emit('shown', evt)
},
onHide (evt) {
this.$emit('hide', evt)
},
onHidden (evt) {
this.setObservers(false)
// bring our content back if needed to keep Vue happy
// Tooltip class will move it back to tip when shown again
this.bringItBack()
this.$emit('update:show', false)
this.$emit('hidden', evt)
},
onEnabled (evt) {
if (!evt || evt.type !== 'enabled') {
// Prevent possible endless loop if user mistakienly fires enabled instead of enable
return
}
this.$emit('update:disabled', false)
this.$emit('disabled')
},
onDisabled (evt) {
if (!evt || evt.type !== 'disabled') {
// Prevent possible endless loop if user mistakienly fires disabled instead of disable
return
}
this.$emit('update:disabled', true)
this.$emit('enabled')
},
bringItBack () {
// bring our content back if needed to keep Vue happy
if (this.$el && this.$refs.title) {
this.$el.appendChild(this.$refs.title)
}
if (this.$el && this.$refs.content) {
this.$el.appendChild(this.$refs.content)
}
},
/* istanbul ignore next: not easy to test */
setObservers (on) {
if (on) {
if (this.$refs.title) {
this._obs_title = observeDom(this.$refs.title, this.updatePosition.bind(this), OBSERVER_CONFIG)
}
if (this.$refs.content) {
this._obs_content = observeDom(this.$refs.content, this.updatePosition.bind(this), OBSERVER_CONFIG)
}
} else {
if (this._obs_title) {
this._obs_title.disconnect()
this._obs_title = null
}
if (this._obs_content) {
this._obs_content.disconnect()
this._obs_content = null
}
}
}
}
}