-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathnavigation.js
288 lines (234 loc) · 7.78 KB
/
navigation.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
/**
* --------------------------------------------------------------------------
* CoreUI navigation.js
* Licensed under MIT (https://github.com/coreui/coreui/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
import BaseComponent from './base-component.js'
import Data from './dom/data.js'
import EventHandler from './dom/event-handler.js'
import SelectorEngine from './dom/selector-engine.js'
import { defineJQueryPlugin } from './util/index.js'
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
const NAME = 'navigation'
const DATA_KEY = 'coreui.navigation'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
const Default = {
activeLinksExact: true,
groupsAutoCollapse: true
}
const DefaultType = {
activeLinksExact: 'boolean',
groupsAutoCollapse: '(string|boolean)'
}
const CLASS_NAME_ACTIVE = 'active'
const CLASS_NAME_SHOW = 'show'
const CLASS_NAME_NAV_GROUP = 'nav-group'
const CLASS_NAME_NAV_GROUP_TOGGLE = 'nav-group-toggle'
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`
const SELECTOR_NAV_GROUP = '.nav-group'
const SELECTOR_NAV_GROUP_ITEMS = '.nav-group-items'
const SELECTOR_NAV_GROUP_TOGGLE = '.nav-group-toggle'
const SELECTOR_NAV_LINK = '.nav-link'
const SELECTOR_DATA_NAVIGATION = '[data-coreui="navigation"]'
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
class Navigation extends BaseComponent {
constructor(element, config) {
super(element)
this._config = this._getConfig(config)
this._setActiveLink()
this._addEventListeners()
Data.set(element, DATA_KEY, this)
}
// Getters
static get Default() {
return Default
}
static get DATA_KEY() {
return DATA_KEY
}
static get DefaultType() {
return DefaultType
}
static get NAME() {
return NAME
}
// Private
_setActiveLink() {
for (const element of Array.from(this._element.querySelectorAll(SELECTOR_NAV_LINK))) {
if (element.classList.contains(CLASS_NAME_NAV_GROUP_TOGGLE)) {
continue
}
let currentUrl = String(window.location)
const urlHasParams = /\?.*=/
const urlHasQueryString = /\?./
const urlHasHash = /#./
if (urlHasParams.test(currentUrl) || urlHasQueryString.test(currentUrl)) {
currentUrl = currentUrl.split('?')[0]
}
if (urlHasHash.test(currentUrl)) {
currentUrl = currentUrl.split('#')[0]
}
if (this._config.activeLinksExact && element.href === currentUrl) {
element.classList.add(CLASS_NAME_ACTIVE)
// eslint-disable-next-line unicorn/no-array-for-each
Array.from(this._getParents(element, SELECTOR_NAV_GROUP)).forEach(element => {
element.classList.add(CLASS_NAME_SHOW)
element.setAttribute('aria-expanded', true)
})
}
if (!this._config.activeLinksExact && element.href.startsWith(currentUrl)) {
element.classList.add(CLASS_NAME_ACTIVE)
// eslint-disable-next-line unicorn/no-array-for-each
Array.from(this._getParents(element, SELECTOR_NAV_GROUP)).forEach(element => {
element.classList.add(CLASS_NAME_SHOW)
element.setAttribute('aria-expanded', true)
})
}
}
}
_getParents(element, selector) {
// Setup parents array
const parents = []
// Get matching parent elements
for (; element && element !== document; element = element.parentNode) {
// Add matching parents to array
if (selector) {
if (element.matches(selector)) {
parents.push(element)
}
} else {
parents.push(element)
}
}
return parents
}
_getAllSiblings(element, filter) {
const siblings = []
element = element.parentNode.firstChild
do {
if (element.nodeType === 3) {
continue // text node
}
if (element.nodeType === 8) {
continue // comment node
}
if (!filter || filter(element)) {
siblings.push(element)
}
// eslint-disable-next-line no-cond-assign
} while (element = element.nextSibling)
return siblings
}
_getChildren(n, skipMe) {
const children = []
for (; n; n = n.nextSibling) {
if (n.nodeType === 1 && n !== skipMe) {
children.push(n)
}
}
return children
}
_getSiblings(element, filter) {
const siblings = this._getChildren(element.parentNode.firstChild, element).filter(filter)
return siblings
}
_slideDown(element) {
element.style.height = 'auto'
const height = element.clientHeight
element.style.height = '0px'
setTimeout(() => {
element.style.height = `${height}px`
}, 0)
this._queueCallback(() => {
element.style.height = 'auto'
}, element, true)
}
_slideUp(element, callback) {
const height = element.clientHeight
element.style.height = `${height}px`
setTimeout(() => {
element.style.height = '0px'
}, 0)
this._queueCallback(() => {
if (typeof callback === 'function') {
callback()
}
}, element, true)
}
_toggleGroupItems(event) {
let toggler = event.target
if (!toggler.classList.contains(CLASS_NAME_NAV_GROUP_TOGGLE)) {
toggler = toggler.closest(SELECTOR_NAV_GROUP_TOGGLE)
}
const filter = element => Boolean(element.classList.contains(CLASS_NAME_NAV_GROUP) && element.classList.contains(CLASS_NAME_SHOW))
// Close other groups
if (this._config.groupsAutoCollapse === true) {
for (const element of this._getSiblings(toggler.parentNode, filter)) {
this._slideUp(SelectorEngine.findOne(SELECTOR_NAV_GROUP_ITEMS, element), () => {
element.classList.remove(CLASS_NAME_SHOW)
element.setAttribute('aria-expanded', false)
})
}
}
if (toggler.parentNode.classList.contains(CLASS_NAME_SHOW)) {
this._slideUp(SelectorEngine.findOne(SELECTOR_NAV_GROUP_ITEMS, toggler.parentNode), () => {
toggler.parentNode.classList.remove(CLASS_NAME_SHOW)
toggler.parentNode.setAttribute('aria-expanded', false)
})
return
}
toggler.parentNode.classList.add(CLASS_NAME_SHOW)
toggler.parentNode.setAttribute('aria-expanded', true)
this._slideDown(SelectorEngine.findOne(SELECTOR_NAV_GROUP_ITEMS, toggler.parentNode))
}
_addEventListeners() {
EventHandler.on(this._element, EVENT_CLICK_DATA_API, SELECTOR_NAV_GROUP_TOGGLE, event => {
event.preventDefault()
this._toggleGroupItems(event, this)
})
}
// Static
static navigationInterface(element, config) {
const data = Navigation.getOrCreateInstance(element, config)
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}
data[config]()
}
}
static jQueryInterface(config) {
return this.each(function () {
Navigation.navigationInterface(this, config)
})
}
}
/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
*/
EventHandler.on(window, EVENT_LOAD_DATA_API, () => {
for (const element of Array.from(document.querySelectorAll(SELECTOR_DATA_NAVIGATION))) {
Navigation.navigationInterface(element)
}
})
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
* add .Navigation to jQuery only if jQuery is present
*/
defineJQueryPlugin(Navigation)
export default Navigation