@@ -361,12 +361,28 @@ export default Vue.extend({
361
361
}
362
362
} ,
363
363
methods : {
364
+ // Private method to update the v-model
364
365
updateModel ( val ) {
365
366
if ( val !== this . visible ) {
366
367
this . $emit ( 'change' , val )
367
368
}
368
369
} ,
369
- // Public Methods
370
+ // Private method to create a BvModalEvent object
371
+ buildEvent ( type , opts = { } ) {
372
+ return new BvModalEvent ( type , {
373
+ // Default options
374
+ cancelable : false ,
375
+ target : this . $refs . modal || this . $el || null ,
376
+ relatedTarget : null ,
377
+ trigger : null ,
378
+ // Supplied options
379
+ ...opts ,
380
+ // Options that can't be overridden
381
+ vueTarget : this ,
382
+ componentId : this . safeId ( )
383
+ } )
384
+ } ,
385
+ // Public method to show modal
370
386
show ( ) {
371
387
if ( this . isVisible || this . isOpening ) {
372
388
// If already open, on in the process of opening, do nothing
@@ -383,12 +399,8 @@ export default Vue.extend({
383
399
this . isOpening = true
384
400
// Set the element to return focus to when closed
385
401
this . return_focus = this . return_focus || this . getActiveElement ( )
386
- const showEvt = new BvModalEvent ( 'show' , {
387
- cancelable : true ,
388
- vueTarget : this ,
389
- target : this . $refs . modal ,
390
- relatedTarget : null ,
391
- componentId : this . safeId ( )
402
+ const showEvt = this . buildEvent ( 'show' , {
403
+ cancelable : true
392
404
} )
393
405
this . emitEvent ( showEvt )
394
406
// Don't show if canceled
@@ -401,18 +413,15 @@ export default Vue.extend({
401
413
// Show the modal
402
414
this . doShow ( )
403
415
} ,
416
+ // Public method to hide modal
404
417
hide ( trigger = '' ) {
405
418
if ( ! this . isVisible || this . isClosing ) {
406
419
/* istanbul ignore next */
407
420
return
408
421
}
409
422
this . isClosing = true
410
- const hideEvt = new BvModalEvent ( 'hide' , {
423
+ const hideEvt = this . buildEvent ( 'hide' , {
411
424
cancelable : trigger !== 'FORCE' ,
412
- vueTarget : this ,
413
- target : this . $refs . modal ,
414
- relatedTarget : null ,
415
- componentId : this . safeId ( ) ,
416
425
trigger : trigger || null
417
426
} )
418
427
// We emit specific event for one of the three built-in buttons
@@ -513,14 +522,7 @@ export default Vue.extend({
513
522
this . isShow = true
514
523
this . isTransitioning = false
515
524
this . $nextTick ( ( ) => {
516
- const shownEvt = new BvModalEvent ( 'shown' , {
517
- cancelable : false ,
518
- vueTarget : this ,
519
- target : this . $refs . modal ,
520
- relatedTarget : null ,
521
- componentId : this . safeId ( )
522
- } )
523
- this . emitEvent ( shownEvt )
525
+ this . emitEvent ( this . buildEvent ( 'shown' ) )
524
526
this . focusFirst ( )
525
527
this . setEnforceFocus ( true )
526
528
} )
@@ -546,14 +548,7 @@ export default Vue.extend({
546
548
modalManager . unregisterModal ( this )
547
549
// TODO: Need to find a way to pass the `trigger` property
548
550
// to the `hidden` event, not just only the `hide` event
549
- const hiddenEvt = new BvModalEvent ( 'hidden' , {
550
- cancelable : false ,
551
- vueTarget : this ,
552
- target : this . $el ,
553
- relatedTarget : null ,
554
- componentId : this . safeId ( )
555
- } )
556
- this . emitEvent ( hiddenEvt )
551
+ this . emitEvent ( this . buildEvent ( 'hidden' ) )
557
552
} )
558
553
} ,
559
554
// Event emitter
@@ -639,18 +634,18 @@ export default Vue.extend({
639
634
} ,
640
635
// Root listener handlers
641
636
showHandler ( id , triggerEl ) {
642
- if ( id === this . id ) {
637
+ if ( id === this . safeId ( ) ) {
643
638
this . return_focus = triggerEl || this . getActiveElement ( )
644
639
this . show ( )
645
640
}
646
641
} ,
647
642
hideHandler ( id ) {
648
- if ( id === this . id ) {
643
+ if ( id === this . safeId ( ) ) {
649
644
this . hide ( 'event' )
650
645
}
651
646
} ,
652
647
toggleHandler ( id , triggerEl ) {
653
- if ( id === this . id ) {
648
+ if ( id === this . safeId ( ) ) {
654
649
this . toggle ( triggerEl )
655
650
}
656
651
} ,
@@ -662,8 +657,6 @@ export default Vue.extend({
662
657
} ,
663
658
// Focus control handlers
664
659
focusFirst ( ) {
665
- // TODO: Add support for finding input element with 'autofocus'
666
- // attribute set and focus that element
667
660
// Don't try and focus if we are SSR
668
661
if ( isBrowser ) {
669
662
const modal = this . $refs . modal
0 commit comments