1
- ( function ( ) {
2
- angular
3
- . module ( 'docsApp' )
4
- . directive ( 'h4' , MdAnchorDirective )
5
- . directive ( 'h3' , MdAnchorDirective )
6
- . directive ( 'h2' , MdAnchorDirective )
7
- . directive ( 'h1' , MdAnchorDirective ) ;
8
-
9
- function MdAnchorDirective ( $mdUtil , $compile ) {
10
-
11
- /** @const @type {RegExp } */
12
- var unsafeCharRegex = / [ & \s + $ , : ; = ? @ " # { } | ^ ~ [ ` % ! ' \] . / ( ) * \\ ] / g;
13
-
14
- return {
15
- restrict : 'E' ,
16
- scope : { } ,
17
- require : '^?mdContent' ,
18
- link : postLink
19
- } ;
20
-
21
- function postLink ( scope , element , attr , ctrl ) {
22
-
23
- // Only create anchors when being inside of a md-content.
24
- if ( ! ctrl ) {
25
- return ;
26
- }
27
-
28
- var anchorEl = $compile ( '<a class="docs-anchor" ng-href="#{{ name }}" name="{{ name }}"></a>' ) ( scope ) ;
29
-
30
- // Wrap contents inside of the anchor element.
31
- anchorEl . append ( element . contents ( ) ) ;
32
-
33
- // Append the anchor element to the directive element.
34
- element . append ( anchorEl ) ;
35
-
36
- // Delay the URL creation, because the inner text might be not interpolated yet.
37
- $mdUtil . nextTick ( createContentURL ) ;
38
-
39
- /**
40
- * Creates URL from the text content of the element and writes it into the scope.
41
- */
42
- function createContentURL ( ) {
43
- scope . name = element . text ( )
44
- . trim ( ) // Trim text due to browsers extra whitespace.
45
- . replace ( / ' / g, '' ) // Transform apostrophes words to a single one.
46
- . replace ( unsafeCharRegex , '-' ) // Replace unsafe chars with a dash symbol.
47
- . replace ( / - { 2 , } / g, '-' ) // Remove repeating dash symbols.
48
- . replace ( / ^ - | - $ / g, '' ) // Remove preceding or ending dashes.
49
- . toLowerCase ( ) ; // Link should be lower-case for accessible URL.
50
- }
51
- }
52
- }
53
-
54
- // Manually specify $inject because Strict DI is enabled.
55
- MdAnchorDirective . $inject = [ '$mdUtil' , '$compile' ] ;
56
-
57
- } ) ( ) ;
1
+ ( function ( ) {
2
+ angular
3
+ . module ( 'docsApp' )
4
+ . directive ( 'h4' , MdAnchorDirective )
5
+ . directive ( 'h3' , MdAnchorDirective )
6
+ . directive ( 'h2' , MdAnchorDirective )
7
+ . directive ( 'h1' , MdAnchorDirective ) ;
8
+
9
+ function MdAnchorDirective ( $mdUtil , $compile , $rootScope ) {
10
+
11
+ /** @const @type {RegExp } */
12
+ var unsafeCharRegex = / [ & \s + $ , : ; = ? @ " # { } | ^ ~ [ ` % ! ' \] . / ( ) * \\ ] / g;
13
+
14
+ return {
15
+ restrict : 'E' ,
16
+ scope : { } ,
17
+ require : '^?mdContent' ,
18
+ link : postLink
19
+ } ;
20
+
21
+ function postLink ( scope , element , attr , ctrl ) {
22
+
23
+ // Only create anchors when being inside of a md-content.
24
+ if ( ! ctrl ) {
25
+ return ;
26
+ }
27
+
28
+ var anchorEl = $compile ( '<a class="docs-anchor" ng-href="{{ name }}" name="{{ name }}"></a>' ) ( scope ) ;
29
+
30
+ // Wrap contents inside of the anchor element.
31
+ anchorEl . append ( element . contents ( ) ) ;
32
+
33
+ // Append the anchor element to the directive element.
34
+ element . append ( anchorEl ) ;
35
+
36
+ // Delay the URL creation, because the inner text might be not interpolated yet.
37
+ $mdUtil . nextTick ( createContentURL ) ;
38
+
39
+ /**
40
+ * Creates URL from the text content of the element and writes it into the scope.
41
+ */
42
+ function createContentURL ( ) {
43
+ var path = scope . $root . $location ? scope . $root . $location . path ( ) : '' ;
44
+ var name = element . text ( ) ;
45
+ name = name
46
+ . trim ( ) // Trim text due to browsers extra whitespace.
47
+ . replace ( / ' / g, '' ) // Transform apostrophes words to a single one.
48
+ . replace ( unsafeCharRegex , '-' ) // Replace unsafe chars with a dash symbol.
49
+ . replace ( / - { 2 , } / g, '-' ) // Remove repeating dash symbols.
50
+ . replace ( / ^ - | - $ / g, '' ) // Remove preceding or ending dashes.
51
+ . toLowerCase ( ) ; // Link should be lower-case for accessible URL.
52
+ scope . name = path + '#' + name ;
53
+ }
54
+ }
55
+ }
56
+
57
+ // Manually specify $inject because Strict DI is enabled.
58
+ MdAnchorDirective . $inject = [ '$mdUtil' , '$compile' ] ;
59
+
60
+ } ) ( ) ;
0 commit comments