-
Notifications
You must be signed in to change notification settings - Fork 337
/
Copy pathPopover.vue
717 lines (607 loc) · 17.2 KB
/
Popover.vue
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
<template>
<div
class="v-popover"
:class="cssClass"
>
<div
ref="trigger"
class="trigger"
style="display: inline-block;"
:aria-describedby="isOpen ? popoverId : undefined"
:tabindex="trigger.indexOf('focus') !== -1 ? 0 : undefined"
>
<slot />
</div>
<div
:id="popoverId"
ref="popover"
:class="[popoverBaseClass, popoverClass, cssClass]"
:style="{
visibility: isOpen ? 'visible' : 'hidden',
}"
:aria-hidden="isOpen ? 'false' : 'true'"
:tabindex="autoHide ? 0 : undefined"
@keyup.esc="autoHide && hide()"
>
<div :class="popoverWrapperClass">
<div
ref="inner"
:class="popoverInnerClass"
style="position: relative;"
>
<div>
<slot
name="popover"
:is-open="isOpen"
/>
</div>
<ResizeObserver
v-if="handleResize"
@notify="$_handleResize"
/>
</div>
<div
ref="arrow"
:class="popoverArrowClass"
/>
</div>
</div>
</div>
</template>
<script>
import { directive } from '../directives/v-tooltip'
import Popper from 'popper.js'
import { ResizeObserver } from 'vue-resize'
import { supportsPassive } from '../utils'
function getDefault (key) {
const value = directive.options.popover[key]
if (typeof value === 'undefined') {
return directive.options[key]
}
return value
}
let isIOS = false
if (typeof window !== 'undefined' && typeof navigator !== 'undefined') {
isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream
}
const openPopovers = []
let Element = function () {}
if (typeof window !== 'undefined') {
Element = window.Element
}
export default {
name: 'VPopover',
components: {
ResizeObserver,
},
props: {
open: {
type: Boolean,
default: false,
},
disabled: {
type: Boolean,
default: false,
},
placement: {
type: String,
default: () => getDefault('defaultPlacement'),
},
delay: {
type: [String, Number, Object],
default: () => getDefault('defaultDelay'),
},
offset: {
type: [String, Number],
default: () => getDefault('defaultOffset'),
},
trigger: {
type: String,
default: () => getDefault('defaultTrigger'),
},
container: {
type: [String, Object, Element, Boolean],
default: () => getDefault('defaultContainer'),
},
boundariesElement: {
type: [String, Element],
default: () => getDefault('defaultBoundariesElement'),
},
popperOptions: {
type: Object,
default: () => getDefault('defaultPopperOptions'),
},
popoverClass: {
type: [String, Array],
default: () => getDefault('defaultClass'),
},
popoverBaseClass: {
type: [String, Array],
default: () => directive.options.popover.defaultBaseClass,
},
popoverInnerClass: {
type: [String, Array],
default: () => directive.options.popover.defaultInnerClass,
},
popoverWrapperClass: {
type: [String, Array],
default: () => directive.options.popover.defaultWrapperClass,
},
popoverArrowClass: {
type: [String, Array],
default: () => directive.options.popover.defaultArrowClass,
},
autoHide: {
type: Boolean,
default: () => directive.options.popover.defaultAutoHide,
},
handleResize: {
type: Boolean,
default: () => directive.options.popover.defaultHandleResize,
},
openGroup: {
type: String,
default: null,
},
openClass: {
type: [String, Array],
default: () => directive.options.popover.defaultOpenClass,
},
ariaId: {
default: null,
},
},
data () {
return {
isOpen: false,
id: Math.random().toString(36).substr(2, 10),
}
},
computed: {
cssClass () {
return {
[this.openClass]: this.isOpen,
}
},
popoverId () {
return `popover_${this.ariaId != null ? this.ariaId : this.id}`
},
},
watch: {
open (val) {
if (val) {
this.show()
} else {
this.hide()
}
},
disabled (val, oldVal) {
if (val !== oldVal) {
if (val) {
this.hide()
} else if (this.open) {
this.show()
}
}
},
container (val) {
if (this.isOpen && this.popperInstance) {
const popoverNode = this.$refs.popover
const reference = this.$refs.trigger
const container = this.$_findContainer(this.container, reference)
if (!container) {
console.warn('No container for popover', this)
return
}
container.appendChild(popoverNode)
this.popperInstance.scheduleUpdate()
}
},
trigger (val) {
this.$_removeEventListeners()
this.$_addEventListeners()
},
placement (val) {
this.$_updatePopper(() => {
this.popperInstance.options.placement = val
})
},
offset: '$_restartPopper',
boundariesElement: '$_restartPopper',
popperOptions: {
handler: '$_restartPopper',
deep: true,
},
},
created () {
this.$_isDisposed = false
this.$_mounted = false
this.$_events = []
this.$_preventOpen = false
},
mounted () {
const popoverNode = this.$refs.popover
popoverNode.parentNode && popoverNode.parentNode.removeChild(popoverNode)
this.$_init()
if (this.open) {
this.show()
}
},
deactivated () {
this.hide()
},
beforeDestroy () {
this.dispose()
},
methods: {
show ({ event, skipDelay = false, force = false } = {}) {
if (force || !this.disabled) {
this.$_scheduleShow(event)
this.$emit('show')
}
this.$emit('update:open', true)
this.$_beingShowed = true
requestAnimationFrame(() => {
this.$_beingShowed = false
})
},
hide ({ event, skipDelay = false } = {}) {
this.$_scheduleHide(event)
this.$emit('hide')
this.$emit('update:open', false)
},
dispose () {
this.$_isDisposed = true
this.$_removeEventListeners()
this.hide({ skipDelay: true })
if (this.popperInstance) {
this.popperInstance.destroy()
// destroy tooltipNode if removeOnDestroy is not set, as popperInstance.destroy() already removes the element
if (!this.popperInstance.options.removeOnDestroy) {
const popoverNode = this.$refs.popover
popoverNode.parentNode && popoverNode.parentNode.removeChild(popoverNode)
}
}
this.$_mounted = false
this.popperInstance = null
this.isOpen = false
this.$emit('dispose')
},
$_init () {
if (this.trigger.indexOf('manual') === -1) {
this.$_addEventListeners()
}
},
$_show () {
const reference = this.$refs.trigger
const popoverNode = this.$refs.popover
clearTimeout(this.$_disposeTimer)
// Already open
if (this.isOpen) {
return
}
// Popper is already initialized
if (this.popperInstance) {
this.isOpen = true
this.popperInstance.enableEventListeners()
this.popperInstance.scheduleUpdate()
}
if (!this.$_mounted) {
const container = this.$_findContainer(this.container, reference)
if (!container) {
console.warn('No container for popover', this)
return
}
container.appendChild(popoverNode)
this.$_mounted = true
this.isOpen = false
if (this.popperInstance) {
requestAnimationFrame(() => {
if (!this.hidden) {
this.isOpen = true
}
})
}
}
if (!this.popperInstance) {
const popperOptions = {
...this.popperOptions,
placement: this.placement,
}
popperOptions.modifiers = {
...popperOptions.modifiers,
arrow: {
...popperOptions.modifiers && popperOptions.modifiers.arrow,
element: this.$refs.arrow,
},
}
if (this.offset) {
const offset = this.$_getOffset()
popperOptions.modifiers.offset = {
...popperOptions.modifiers && popperOptions.modifiers.offset,
offset,
}
}
if (this.boundariesElement) {
popperOptions.modifiers.preventOverflow = {
...popperOptions.modifiers && popperOptions.modifiers.preventOverflow,
boundariesElement: this.boundariesElement,
}
}
this.popperInstance = new Popper(reference, popoverNode, popperOptions)
// Fix position
requestAnimationFrame(() => {
if (this.hidden) {
this.hidden = false
this.$_hide()
return
}
if (!this.$_isDisposed && this.popperInstance) {
this.popperInstance.scheduleUpdate()
// Show the tooltip
requestAnimationFrame(() => {
if (this.hidden) {
this.hidden = false
this.$_hide()
return
}
if (!this.$_isDisposed) {
this.isOpen = true
} else {
this.dispose()
}
})
} else {
this.dispose()
}
})
}
const openGroup = this.openGroup
if (openGroup) {
let popover
for (let i = 0; i < openPopovers.length; i++) {
popover = openPopovers[i]
if (popover.openGroup !== openGroup) {
popover.hide()
popover.$emit('close-group')
}
}
}
openPopovers.push(this)
this.$emit('apply-show')
},
$_hide () {
// Already hidden
if (!this.isOpen) {
return
}
const index = openPopovers.indexOf(this)
if (index !== -1) {
openPopovers.splice(index, 1)
}
this.isOpen = false
if (this.popperInstance) {
this.popperInstance.disableEventListeners()
}
clearTimeout(this.$_disposeTimer)
const disposeTime = directive.options.popover.disposeTimeout || directive.options.disposeTimeout
if (disposeTime !== null) {
this.$_disposeTimer = setTimeout(() => {
const popoverNode = this.$refs.popover
if (popoverNode) {
// Don't remove popper instance, just the HTML element
popoverNode.parentNode && popoverNode.parentNode.removeChild(popoverNode)
this.$_mounted = false
}
}, disposeTime)
}
this.$emit('apply-hide')
},
$_findContainer (container, reference) {
// if container is a query, get the relative element
if (typeof container === 'string') {
container = window.document.querySelector(container)
} else if (container === false) {
// if container is `false`, set it to reference parent
container = reference.parentNode
}
return container
},
$_getOffset () {
const typeofOffset = typeof this.offset
let offset = this.offset
// One value -> switch
if (typeofOffset === 'number' || (typeofOffset === 'string' && offset.indexOf(',') === -1)) {
offset = `0, ${offset}`
}
return offset
},
$_addEventListeners () {
const reference = this.$refs.trigger
const directEvents = []
const oppositeEvents = []
const events = typeof this.trigger === 'string'
? this.trigger
.split(' ')
.filter(
trigger => ['click', 'hover', 'focus'].indexOf(trigger) !== -1,
)
: []
events.forEach(event => {
switch (event) {
case 'hover':
directEvents.push('mouseenter')
oppositeEvents.push('mouseleave')
break
case 'focus':
directEvents.push('focus')
oppositeEvents.push('blur')
break
case 'click':
directEvents.push('click')
oppositeEvents.push('click')
break
}
})
// schedule show tooltip
directEvents.forEach(event => {
const func = event => {
if (this.isOpen) {
return
}
event.usedByTooltip = true
!this.$_preventOpen && this.show({ event: event })
this.hidden = false
}
this.$_events.push({ event, func })
reference.addEventListener(event, func)
})
// schedule hide tooltip
oppositeEvents.forEach(event => {
const func = event => {
if (event.usedByTooltip) {
return
}
this.hide({ event: event })
this.hidden = true
}
this.$_events.push({ event, func })
reference.addEventListener(event, func)
})
},
$_scheduleShow (event = null, skipDelay = false) {
clearTimeout(this.$_scheduleTimer)
if (skipDelay) {
this.$_show()
} else {
// defaults to 0
const computedDelay = parseInt((this.delay && this.delay.show) || this.delay || 0)
this.$_scheduleTimer = setTimeout(this.$_show.bind(this), computedDelay)
}
},
$_scheduleHide (event = null, skipDelay = false) {
clearTimeout(this.$_scheduleTimer)
if (skipDelay) {
this.$_hide()
} else {
// defaults to 0
const computedDelay = parseInt((this.delay && this.delay.hide) || this.delay || 0)
this.$_scheduleTimer = setTimeout(() => {
if (!this.isOpen) {
return
}
// if we are hiding because of a mouseleave, we must check that the new
// reference isn't the tooltip, because in this case we don't want to hide it
if (event && event.type === 'mouseleave') {
const isSet = this.$_setTooltipNodeEvent(event)
// if we set the new event, don't hide the tooltip yet
// the new event will take care to hide it if necessary
if (isSet) {
return
}
}
this.$_hide()
}, computedDelay)
}
},
$_setTooltipNodeEvent (event) {
const reference = this.$refs.trigger
const popoverNode = this.$refs.popover
const relatedreference = event.relatedreference || event.toElement || event.relatedTarget
const callback = event2 => {
const relatedreference2 = event2.relatedreference || event2.toElement || event2.relatedTarget
// Remove event listener after call
popoverNode.removeEventListener(event.type, callback)
// If the new reference is not the reference element
if (!reference.contains(relatedreference2)) {
// Schedule to hide tooltip
this.hide({ event: event2 })
}
}
if (popoverNode.contains(relatedreference)) {
// listen to mouseleave on the tooltip element to be able to hide the tooltip
popoverNode.addEventListener(event.type, callback)
return true
}
return false
},
$_removeEventListeners () {
const reference = this.$refs.trigger
this.$_events.forEach(({ func, event }) => {
reference.removeEventListener(event, func)
})
this.$_events = []
},
$_updatePopper (cb) {
if (this.popperInstance) {
cb()
if (this.isOpen) this.popperInstance.scheduleUpdate()
}
},
$_restartPopper () {
if (this.popperInstance) {
const isOpen = this.isOpen
this.dispose()
this.$_isDisposed = false
this.$_init()
if (isOpen) {
this.show({ skipDelay: true, force: true })
}
}
},
$_handleGlobalClose (event, touch = false) {
if (this.$_beingShowed) return
this.hide({ event: event })
if (event.closePopover) {
this.$emit('close-directive')
} else {
this.$emit('auto-hide')
}
if (touch) {
this.$_preventOpen = true
setTimeout(() => {
this.$_preventOpen = false
}, 300)
}
},
$_handleResize () {
if (this.isOpen && this.popperInstance) {
this.popperInstance.scheduleUpdate()
this.$emit('resize')
}
},
},
}
if (typeof document !== 'undefined' && typeof window !== 'undefined') {
if (isIOS) {
document.addEventListener('touchend', handleGlobalTouchend, supportsPassive
? {
passive: true,
capture: true,
}
: true)
} else {
window.addEventListener('click', handleGlobalClick, true)
}
}
function handleGlobalClick (event) {
handleGlobalClose(event)
}
function handleGlobalTouchend (event) {
handleGlobalClose(event, true)
}
function handleGlobalClose (event, touch = false) {
// Delay so that close directive has time to set values
for (let i = 0; i < openPopovers.length; i++) {
const popover = openPopovers[i]
if (popover.$refs.popover) {
const contains = popover.$refs.popover.contains(event.target)
requestAnimationFrame(() => {
if (event.closeAllPopover || (event.closePopover && contains) || (popover.autoHide && !contains)) {
popover.$_handleGlobalClose(event, touch)
}
})
}
}
}
</script>