7
7
8
8
import {
9
9
defineJQueryPlugin ,
10
+ getElement ,
10
11
getSelectorFromElement ,
11
- getUID ,
12
- isElement ,
13
12
typeCheckConfig
14
13
} from './util/index'
15
14
import EventHandler from './dom/event-handler'
@@ -52,6 +51,7 @@ const SELECTOR_NAV_LIST_GROUP = '.nav, .list-group'
52
51
const SELECTOR_NAV_LINKS = '.nav-link'
53
52
const SELECTOR_NAV_ITEMS = '.nav-item'
54
53
const SELECTOR_LIST_ITEMS = '.list-group-item'
54
+ const SELECTOR_LINK_ITEMS = `${ SELECTOR_NAV_LINKS } , ${ SELECTOR_LIST_ITEMS } , .${ CLASS_NAME_DROPDOWN_ITEM } `
55
55
const SELECTOR_DROPDOWN = '.dropdown'
56
56
const SELECTOR_DROPDOWN_TOGGLE = '.dropdown-toggle'
57
57
@@ -69,7 +69,6 @@ class ScrollSpy extends BaseComponent {
69
69
super ( element )
70
70
this . _scrollElement = this . _element . tagName === 'BODY' ? window : this . _element
71
71
this . _config = this . _getConfig ( config )
72
- this . _selector = `${ this . _config . target } ${ SELECTOR_NAV_LINKS } , ${ this . _config . target } ${ SELECTOR_LIST_ITEMS } , ${ this . _config . target } .${ CLASS_NAME_DROPDOWN_ITEM } `
73
72
this . _offsets = [ ]
74
73
this . _targets = [ ]
75
74
this . _activeTarget = null
@@ -110,7 +109,7 @@ class ScrollSpy extends BaseComponent {
110
109
this . _targets = [ ]
111
110
this . _scrollHeight = this . _getScrollHeight ( )
112
111
113
- const targets = SelectorEngine . find ( this . _selector )
112
+ const targets = SelectorEngine . find ( SELECTOR_LINK_ITEMS , this . _config . target )
114
113
115
114
targets . map ( element => {
116
115
const targetSelector = getSelectorFromElement ( element )
@@ -150,15 +149,7 @@ class ScrollSpy extends BaseComponent {
150
149
...( typeof config === 'object' && config ? config : { } )
151
150
}
152
151
153
- if ( typeof config . target !== 'string' && isElement ( config . target ) ) {
154
- let { id } = config . target
155
- if ( ! id ) {
156
- id = getUID ( NAME )
157
- config . target . id = id
158
- }
159
-
160
- config . target = `#${ id } `
161
- }
152
+ config . target = getElement ( config . target ) || document . documentElement
162
153
163
154
typeCheckConfig ( NAME , config , DefaultType )
164
155
@@ -225,20 +216,16 @@ class ScrollSpy extends BaseComponent {
225
216
226
217
this . _clear ( )
227
218
228
- const queries = this . _selector . split ( ',' )
219
+ const queries = SELECTOR_LINK_ITEMS . split ( ',' )
229
220
. map ( selector => `${ selector } [data-bs-target="${ target } "],${ selector } [href="${ target } "]` )
230
221
231
- const link = SelectorEngine . findOne ( queries . join ( ',' ) )
222
+ const link = SelectorEngine . findOne ( queries . join ( ',' ) , this . _config . target )
232
223
224
+ link . classList . add ( CLASS_NAME_ACTIVE )
233
225
if ( link . classList . contains ( CLASS_NAME_DROPDOWN_ITEM ) ) {
234
226
SelectorEngine . findOne ( SELECTOR_DROPDOWN_TOGGLE , link . closest ( SELECTOR_DROPDOWN ) )
235
227
. classList . add ( CLASS_NAME_ACTIVE )
236
-
237
- link . classList . add ( CLASS_NAME_ACTIVE )
238
228
} else {
239
- // Set triggered link as active
240
- link . classList . add ( CLASS_NAME_ACTIVE )
241
-
242
229
SelectorEngine . parents ( link , SELECTOR_NAV_LIST_GROUP )
243
230
. forEach ( listGroup => {
244
231
// Set triggered links parents as active
@@ -261,7 +248,7 @@ class ScrollSpy extends BaseComponent {
261
248
}
262
249
263
250
_clear ( ) {
264
- SelectorEngine . find ( this . _selector )
251
+ SelectorEngine . find ( SELECTOR_LINK_ITEMS , this . _config . target )
265
252
. filter ( node => node . classList . contains ( CLASS_NAME_ACTIVE ) )
266
253
. forEach ( node => node . classList . remove ( CLASS_NAME_ACTIVE ) )
267
254
}
0 commit comments