1
1
import { isMobile } from '../util/env'
2
- import { getNode , on , body , findAll , toggleClass } from '../util/dom'
2
+ import * as dom from '../util/dom'
3
3
import { getHash } from '../route/hash'
4
4
5
+ const title = dom . $ . title
5
6
/**
6
7
* Toggle button
7
8
*/
8
9
export function btn ( el ) {
9
- const toggle = ( ) => body . classList . toggle ( 'close' )
10
+ const toggle = ( ) => dom . body . classList . toggle ( 'close' )
10
11
11
- el = getNode ( el )
12
- on ( el , 'click' , toggle )
12
+ el = dom . getNode ( el )
13
+ dom . on ( el , 'click' , toggle )
13
14
14
15
if ( isMobile ) {
15
- const sidebar = getNode ( '.sidebar' )
16
+ const sidebar = dom . getNode ( '.sidebar' )
16
17
17
- on ( sidebar , 'click' , ( ) => {
18
+ dom . on ( sidebar , 'click' , ( ) => {
18
19
toggle ( )
19
- setTimeout ( ( ) => getAndActive ( true ) , 0 )
20
+ setTimeout ( ( ) => getAndActive ( sidebar , true , true ) , 0 )
20
21
} )
21
22
}
22
23
}
23
24
24
25
export function sticky ( ) {
25
- const cover = getNode ( 'section.cover' )
26
+ const cover = dom . getNode ( 'section.cover' )
26
27
if ( ! cover ) return
27
28
const coverHeight = cover . getBoundingClientRect ( ) . height
28
29
29
30
if ( window . pageYOffset >= coverHeight || cover . classList . contains ( 'hidden' ) ) {
30
- toggleClass ( body , 'add' , 'sticky' )
31
+ dom . toggleClass ( dom . body , 'add' , 'sticky' )
31
32
} else {
32
- toggleClass ( body , 'remove' , 'sticky' )
33
+ dom . toggleClass ( dom . body , 'remove' , 'sticky' )
33
34
}
34
35
}
35
36
36
- export function getAndActive ( el , isParent ) {
37
- const dom = getNode ( el )
38
- const links = findAll ( dom , 'a' )
39
- const hash = '#' + getHash ( )
37
+ /**
38
+ * Get and active link
39
+ * @param {string|element } el
40
+ * @param {Boolean } isParent acitve parent
41
+ * @param {Boolean } autoTitle auto set title
42
+ * @return {element }
43
+ */
44
+ export function getAndActive ( el , isParent , autoTitle ) {
45
+ el = dom . getNode ( el )
40
46
47
+ const links = dom . findAll ( el , 'a' )
48
+ const hash = '#' + getHash ( )
41
49
let target
42
50
43
51
links
@@ -48,11 +56,15 @@ export function getAndActive (el, isParent) {
48
56
49
57
if ( hash . indexOf ( href ) === 0 && ! target ) {
50
58
target = a
51
- toggleClass ( node , 'add' , 'active' )
59
+ dom . toggleClass ( node , 'add' , 'active' )
52
60
} else {
53
- toggleClass ( node , 'remove' , 'active' )
61
+ dom . toggleClass ( node , 'remove' , 'active' )
54
62
}
55
63
} )
56
64
65
+ if ( autoTitle ) {
66
+ dom . $ . title = target ? `${ target . innerText } - ${ title } ` : title
67
+ }
68
+
57
69
return target
58
70
}
0 commit comments