• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 $(document).ready(function() {
18   prettyPrint();
19 
20   var sluggify_ = function(s) {
21     return (s || '').replace(/ /g, '-').replace(/[^\w-]/g, '').toLowerCase();
22   };
23 
24   $('h2, h3, h4.includetoc').each(function() {
25     $(this).attr('id', 'toc_' + sluggify_($(this).data('toctitle') || $(this).text()));
26     $(this).click(function() {
27       smoothScrollToId($(this).attr('id'));
28     });
29   });
30 
31   var buildNav_ = function(queries, $contentRoot, $navRoot) {
32     if (!queries || !queries.length) {
33       return;
34     }
35 
36     $contentRoot.find(queries[0]).each(function() {
37       var $navNode = $('<div>')
38           .text($(this).html())
39           .appendTo($navRoot);
40       buildNav_(queries.splice(1), $(this), $navNode);
41     });
42   };
43 
44   buildNav();
45 });
46 
47 function buildNav() {
48   var currentLevel = 2;
49   var $currentParent = $('nav');
50   var $currentNode = null;
51 
52   $('#page-content').find('h2, h3, h4.includetoc').each(function() {
53     var level = $(this).get(0).tagName.substring(1);
54 
55     if (level < currentLevel) {
56       // ascend
57       for (var i = 0; i < (currentLevel - level); i++) {
58         $currentParent = $currentParent.parents('div.children, nav').first();
59       }
60 
61     } else if (level > currentLevel) {
62       // descend
63       $currentParent = $('<div>')
64           .addClass('children')
65           .appendTo($currentNode);
66     }
67 
68     var tocId = $(this).attr('id');
69     var navId = tocId.replace(/toc_/, 'nav_');
70 
71     $interactionNode = $('<span>')
72         .html($(this).data('toctitle') || $(this).html())
73         .data('target', tocId)
74         .click(function() {
75           smoothScrollToId($(this).data('target'));
76         });
77 
78     $currentNode = $('<div>')
79         .attr('id', navId)
80         .addClass('item')
81         .append($interactionNode)
82         .appendTo($currentParent);
83 
84     currentLevel = level;
85   });
86 
87   var headerPositionCache = [];
88   var rebuildHeaderPositionCache_ = function() {
89     headerPositionCache = [];
90     $('#page-content').find('h2, h3, h4.includetoc').each(function() {
91       headerPositionCache.push({
92         id: $(this).attr('id').replace(/toc_/, 'nav_'),
93         top: $(this).offset().top
94       });
95     });
96   };
97 
98   var updateSelectedNavPosition_ = function() {
99     $('nav .item').removeClass('selected');
100     var scrollTop = $(window).scrollTop();
101     for (var i = headerPositionCache.length - 1; i >= 0; i--) {
102       if (scrollTop >= headerPositionCache[i].top) {
103         $('#' + headerPositionCache[i].id).addClass('selected');
104         console.log($('#' + headerPositionCache[i].id));
105         break;
106       }
107     }
108   };
109 
110   rebuildHeaderPositionCache_();
111   $(window).resize(function() {
112     rebuildHeaderPositionCache_();
113     updateSelectedNavPosition_();
114   });
115 
116   $(window).scroll(function() {
117     updateSelectedNavPosition_();
118   });
119 }
120 
121 function smoothScrollToId(id) {
122   var $target = $('#' + id);
123   $('body').animate({ scrollTop: $target.offset().top }, 200, 'swing', function() {
124     document.location.hash = id;
125   });
126 }
127