1
1
import Popper from 'popper.js'
2
2
import PopOver from '../../utils/popover.class'
3
+ import { inBrowser } from '../../utils/env'
3
4
import { keys } from '../../utils/object'
4
5
import warn from '../../utils/warn'
5
6
6
- const inBrowser = typeof window !== 'undefined' && typeof document !== 'undefined'
7
-
8
7
// Key which we use to store tooltip object on element
9
- const BVPO = '__BV_PopOver__'
8
+ const BV_POPOVER = '__BV_PopOver__'
10
9
11
10
// Valid event triggers
12
11
const validTriggers = {
@@ -17,9 +16,9 @@ const validTriggers = {
17
16
}
18
17
19
18
// Build a PopOver config based on bindings (if any)
20
- // Arguments and modifiers take precedence over pased value config object
19
+ // Arguments and modifiers take precedence over passed value config object
21
20
/* istanbul ignore next: not easy to test */
22
- function parseBindings ( bindings ) {
21
+ const parseBindings = bindings => /* istanbul ignore next: not easy to test */ {
23
22
// We start out with a blank config
24
23
let config = { }
25
24
@@ -35,9 +34,10 @@ function parseBindings(bindings) {
35
34
config = { ...config , ...bindings . value }
36
35
}
37
36
38
- // If Argument , assume element ID of container element
37
+ // If argument , assume element ID of container element
39
38
if ( bindings . arg ) {
40
- // Element ID specified as arg. We must prepend '#' to become a CSS selector
39
+ // Element ID specified as arg
40
+ // We must prepend '#' to become a CSS selector
41
41
config . container = `#${ bindings . arg } `
42
42
}
43
43
@@ -55,35 +55,36 @@ function parseBindings(bindings) {
55
55
// placement of popover
56
56
config . placement = mod
57
57
} else if ( / ^ ( w i n d o w | v i e w p o r t ) $ / . test ( mod ) ) {
58
- // bounday of popover
58
+ // Boundary of popover
59
59
config . boundary = mod
60
60
} else if ( / ^ d \d + $ / . test ( mod ) ) {
61
- // delay value
61
+ // Delay value
62
62
const delay = parseInt ( mod . slice ( 1 ) , 10 ) || 0
63
63
if ( delay ) {
64
64
config . delay = delay
65
65
}
66
66
} else if ( / ^ o - ? \d + $ / . test ( mod ) ) {
67
- // offset value (negative allowed)
67
+ // Offset value (negative allowed)
68
68
const offset = parseInt ( mod . slice ( 1 ) , 10 ) || 0
69
69
if ( offset ) {
70
70
config . offset = offset
71
71
}
72
72
}
73
73
} )
74
74
75
- // Special handling of event trigger modifiers Trigger is a space separated list
75
+ // Special handling of event trigger modifiers trigger is
76
+ // a space separated list
76
77
const selectedTriggers = { }
77
78
78
- // parse current config object trigger
79
+ // Parse current config object trigger
79
80
let triggers = typeof config . trigger === 'string' ? config . trigger . trim ( ) . split ( / \s + / ) : [ ]
80
81
triggers . forEach ( trigger => {
81
82
if ( validTriggers [ trigger ] ) {
82
83
selectedTriggers [ trigger ] = true
83
84
}
84
85
} )
85
86
86
- // Parse Modifiers for triggers
87
+ // Parse modifiers for triggers
87
88
keys ( validTriggers ) . forEach ( trigger => {
88
89
if ( bindings . modifiers [ trigger ] ) {
89
90
selectedTriggers [ trigger ] = true
@@ -97,70 +98,64 @@ function parseBindings(bindings) {
97
98
config . trigger = 'focus'
98
99
}
99
100
if ( ! config . trigger ) {
100
- // remove trigger config
101
+ // Remove trigger config
101
102
delete config . trigger
102
103
}
103
104
104
105
return config
105
106
}
106
107
107
- //
108
- // Add or Update popover on our element
109
- //
110
- /* istanbul ignore next: not easy to test */
111
- function applyBVPO ( el , bindings , vnode ) {
108
+ // Add or update PopOver on our element
109
+ const applyPopover = ( el , bindings , vnode ) => {
112
110
if ( ! inBrowser ) {
111
+ /* istanbul ignore next */
113
112
return
114
113
}
114
+ // Popper is required for PopOvers to work
115
115
if ( ! Popper ) {
116
- // Popper is required for tooltips to work
117
- warn ( 'v-b-popover: Popper.js is required for popovers to work' )
116
+ /* istanbul ignore next */
117
+ warn ( 'v-b-popover: Popper.js is required for PopOvers to work' )
118
+ /* istanbul ignore next */
118
119
return
119
120
}
120
- if ( el [ BVPO ] ) {
121
- el [ BVPO ] . updateConfig ( parseBindings ( bindings ) )
121
+ const config = parseBindings ( bindings )
122
+ if ( el [ BV_POPOVER ] ) {
123
+ el [ BV_POPOVER ] . updateConfig ( config )
122
124
} else {
123
- el [ BVPO ] = new PopOver ( el , parseBindings ( bindings ) , vnode . context . $root )
125
+ el [ BV_POPOVER ] = new PopOver ( el , config , vnode . context . $root )
124
126
}
125
127
}
126
128
127
- //
128
- // Remove popover on our element
129
- //
130
- /* istanbul ignore next */
131
- function removeBVPO ( el ) {
132
- if ( ! inBrowser ) {
133
- return
134
- }
135
- if ( el [ BVPO ] ) {
136
- el [ BVPO ] . destroy ( )
137
- el [ BVPO ] = null
138
- delete el [ BVPO ]
129
+ // Remove PopOver on our element
130
+ const removePopover = el => {
131
+ if ( el [ BV_POPOVER ] ) {
132
+ el [ BV_POPOVER ] . destroy ( )
133
+ el [ BV_POPOVER ] = null
134
+ delete el [ BV_POPOVER ]
139
135
}
140
136
}
141
137
142
138
/*
143
139
* Export our directive
144
140
*/
145
- /* istanbul ignore next: not easy to test */
146
141
export default {
147
142
bind ( el , bindings , vnode ) {
148
- applyBVPO ( el , bindings , vnode )
143
+ applyPopover ( el , bindings , vnode )
149
144
} ,
150
145
inserted ( el , bindings , vnode ) {
151
- applyBVPO ( el , bindings , vnode )
146
+ applyPopover ( el , bindings , vnode )
152
147
} ,
153
- update ( el , bindings , vnode ) {
148
+ update ( el , bindings , vnode ) /* istanbul ignore next: not easy to test */ {
154
149
if ( bindings . value !== bindings . oldValue ) {
155
- applyBVPO ( el , bindings , vnode )
150
+ applyPopover ( el , bindings , vnode )
156
151
}
157
152
} ,
158
- componentUpdated ( el , bindings , vnode ) {
153
+ componentUpdated ( el , bindings , vnode ) /* istanbul ignore next: not easy to test */ {
159
154
if ( bindings . value !== bindings . oldValue ) {
160
- applyBVPO ( el , bindings , vnode )
155
+ applyPopover ( el , bindings , vnode )
161
156
}
162
157
} ,
163
158
unbind ( el ) {
164
- removeBVPO ( el )
159
+ removePopover ( el )
165
160
}
166
161
}
0 commit comments