6
6
7
7
function AnchorJS ( options ) {
8
8
'use strict' ;
9
-
10
9
this . options = options || { } ;
11
10
12
11
this . _applyRemainingDefaultOptions = function ( opts ) {
13
- this . options . icon = this . options . hasOwnProperty ( 'icon' ) ? opts . icon : '\ue9cb' ; // Accepts characters (and also URLs?), like '#', '¶', '❡', or '§'.
14
- this . options . visible = this . options . hasOwnProperty ( 'visible' ) ? opts . visible : 'hover' ; // Also accepts 'always'
15
- this . options . placement = this . options . hasOwnProperty ( 'placement' ) ? opts . placement : 'right' ; // Also accepts 'left'
12
+ this . options . icon = this . options . hasOwnProperty ( 'icon' )
13
+ ? opts . icon
14
+ : '\ue9cb' ; // Accepts characters (and also URLs?), like '#', '¶', '❡', or '§'.
15
+ this . options . visible = this . options . hasOwnProperty ( 'visible' )
16
+ ? opts . visible
17
+ : 'hover' ; // Also accepts 'always'
18
+ this . options . placement = this . options . hasOwnProperty ( 'placement' )
19
+ ? opts . placement
20
+ : 'right' ; // Also accepts 'left'
16
21
this . options . class = this . options . hasOwnProperty ( 'class' ) ? opts . class : '' ; // Accepts any class name.
17
22
} ;
18
23
19
24
this . _applyRemainingDefaultOptions ( options ) ;
20
25
21
26
this . add = function ( selector ) {
22
27
var elements ,
23
- elsWithIds ,
24
- idList ,
25
- elementID ,
26
- i ,
27
- roughText ,
28
- tidyText ,
29
- index ,
30
- count ,
31
- newTidyText ,
32
- readableID ,
33
- anchor ;
28
+ elsWithIds ,
29
+ idList ,
30
+ elementID ,
31
+ i ,
32
+ roughText ,
33
+ tidyText ,
34
+ index ,
35
+ count ,
36
+ newTidyText ,
37
+ readableID ,
38
+ anchor ;
34
39
35
40
this . _applyRemainingDefaultOptions ( this . options ) ;
36
41
@@ -55,7 +60,6 @@ function AnchorJS(options) {
55
60
} ) ;
56
61
57
62
for ( i = 0 ; i < elements . length ; i ++ ) {
58
-
59
63
if ( elements [ i ] . hasAttribute ( 'id' ) ) {
60
64
elementID = elements [ i ] . getAttribute ( 'id' ) ;
61
65
} else {
@@ -65,12 +69,13 @@ function AnchorJS(options) {
65
69
// spaces with hyphens, truncate to 32 characters, and make toLowerCase.
66
70
//
67
71
// Example string: // '⚡⚡⚡ Unicode icons are cool--but they definitely don't belong in a URL fragment.'
68
- tidyText = roughText . replace ( / [ ^ \w \s - ] / gi, '' ) // ' Unicode icons are cool--but they definitely dont belong in a URL fragment'
69
- . replace ( / \s + / g, '-' ) // '-Unicode-icons-are-cool--but-they-definitely-dont-belong-in-a-URL-fragment'
70
- . replace ( / - { 2 , } / g, '-' ) // '-Unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-URL-fragment'
71
- . substring ( 0 , 64 ) // '-Unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-URL'
72
- . replace ( / ^ - + | - + $ / gm, '' ) // 'Unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-URL'
73
- . toLowerCase ( ) ; // 'unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-url'
72
+ tidyText = roughText
73
+ . replace ( / [ ^ \w \s - ] / gi, '' ) // ' Unicode icons are cool--but they definitely dont belong in a URL fragment'
74
+ . replace ( / \s + / g, '-' ) // '-Unicode-icons-are-cool--but-they-definitely-dont-belong-in-a-URL-fragment'
75
+ . replace ( / - { 2 , } / g, '-' ) // '-Unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-URL-fragment'
76
+ . substring ( 0 , 64 ) // '-Unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-URL'
77
+ . replace ( / ^ - + | - + $ / gm, '' ) // 'Unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-URL'
78
+ . toLowerCase ( ) ; // 'unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-url'
74
79
75
80
// Compare our generated ID to existing IDs (and increment it if needed)
76
81
// before we add it to the page.
@@ -121,7 +126,8 @@ function AnchorJS(options) {
121
126
anchor . style . marginLeft = '-1em' ;
122
127
anchor . style . paddingRight = '0.5em' ;
123
128
elements [ i ] . insertBefore ( anchor , elements [ i ] . firstChild ) ;
124
- } else { // if the option provided is `right` (or anything else).
129
+ } else {
130
+ // if the option provided is `right` (or anything else).
125
131
anchor . style . paddingLeft = '0.375em' ;
126
132
elements [ i ] . appendChild ( anchor ) ;
127
133
}
@@ -131,8 +137,7 @@ function AnchorJS(options) {
131
137
} ;
132
138
133
139
this . remove = function ( selector ) {
134
- var domAnchor ,
135
- elements = document . querySelectorAll ( selector ) ;
140
+ var domAnchor , elements = document . querySelectorAll ( selector ) ;
136
141
for ( var i = 0 ; i < elements . length ; i ++ ) {
137
142
domAnchor = elements [ i ] . querySelector ( '.anchorjs-link' ) ;
138
143
if ( domAnchor ) {
@@ -149,30 +154,26 @@ function AnchorJS(options) {
149
154
}
150
155
151
156
var style = document . createElement ( 'style' ) ,
152
- linkRule =
153
- ' .anchorjs-link {' +
154
- ' opacity: 0;' +
155
- ' text-decoration: none;' +
157
+ linkRule = ' .anchorjs-link {' +
158
+ ' opacity: 0;' +
159
+ ' text-decoration: none;' +
156
160
' -webkit-font-smoothing: antialiased;' +
157
- ' -moz-osx-font-smoothing: grayscale;' +
161
+ ' -moz-osx-font-smoothing: grayscale;' +
158
162
' }' ,
159
- hoverRule =
160
- ' *:hover > .anchorjs-link,' +
161
- ' .anchorjs-link:focus {' +
162
- ' opacity: 1;' +
163
+ hoverRule = ' *:hover > .anchorjs-link,' +
164
+ ' .anchorjs-link:focus {' +
165
+ ' opacity: 1;' +
163
166
' }' ,
164
- anchorjsLinkFontFace =
165
- ' @font-face {' +
166
- ' font-family: "anchorjs-icons";' +
167
- ' font-style: normal;' +
168
- ' font-weight: normal;' + // Icon from icomoon; 10px wide & 10px tall; 2 empty below & 4 above
167
+ anchorjsLinkFontFace = ' @font-face {' +
168
+ ' font-family: "anchorjs-icons";' +
169
+ ' font-style: normal;' +
170
+ ' font-weight: normal;' + // Icon from icomoon; 10px wide & 10px tall; 2 empty below & 4 above
169
171
' src: url(data:application/x-font-ttf;charset=utf-8;base64,AAEAAAALAIAAAwAwT1MvMg8SBTUA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AAAAAAADgCuAAEAAAAAAAEADgAAAAEAAAAAAAIABwCfAAEAAAAAAAMADgBLAAEAAAAAAAQADgC0AAEAAAAAAAUACwAqAAEAAAAAAAYADgB1AAEAAAAAAAoAGgDeAAMAAQQJAAEAHAAOAAMAAQQJAAIADgCmAAMAAQQJAAMAHABZAAMAAQQJAAQAHADCAAMAAQQJAAUAFgA1AAMAAQQJAAYAHACDAAMAAQQJAAoANAD4YW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzVmVyc2lvbiAxLjAAVgBlAHIAcwBpAG8AbgAgADEALgAwYW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzYW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzUmVndWxhcgBSAGUAZwB1AGwAYQByYW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzRm9udCBnZW5lcmF0ZWQgYnkgSWNvTW9vbi4ARgBvAG4AdAAgAGcAZQBuAGUAcgBhAHQAZQBkACAAYgB5ACAASQBjAG8ATQBvAG8AbgAuAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==) format("truetype");' +
170
172
' }' ,
171
- pseudoElContent =
172
- ' [data-anchorjs-icon]::after {' +
173
- ' content: attr(data-anchorjs-icon);' +
173
+ pseudoElContent = ' [data-anchorjs-icon]::after {' +
174
+ ' content: attr(data-anchorjs-icon);' +
174
175
' }' ,
175
- firstStyleEl ;
176
+ firstStyleEl ;
176
177
177
178
style . className = 'anchorjs' ;
178
179
style . appendChild ( document . createTextNode ( '' ) ) ; // Necessary for Webkit.
0 commit comments