| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 (function() { | 5 (function() { |
| 6 /** | 6 /** |
| 7 * Toggles the display of nodes given the status of their associated controls. | 7 * Toggles the display of nodes given the status of their associated controls. |
| 8 * | 8 * |
| 9 * For each node passed to this function, check to see if a toggle has been | 9 * For each node passed to this function, check to see if a toggle has been |
| 10 * inserted into the node's parent. If yes, change the state of the toggle | 10 * inserted into the node's parent. If yes, change the state of the toggle |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 * toggle control is clicked, any <ul> elements who are siblings of the | 51 * toggle control is clicked, any <ul> elements who are siblings of the |
| 52 * control are hidden/revealed as appropriate given the control's state. | 52 * control are hidden/revealed as appropriate given the control's state. |
| 53 * | 53 * |
| 54 * If a list item possesses the class "leftNavSelected" its ancestor <ul> is | 54 * If a list item possesses the class "leftNavSelected" its ancestor <ul> is |
| 55 * revealed by default (it represents the current page). | 55 * revealed by default (it represents the current page). |
| 56 */ | 56 */ |
| 57 function initSidebar() { | 57 function initSidebar() { |
| 58 var toc = document.getElementById('gc-toc'); | 58 var toc = document.getElementById('gc-toc'); |
| 59 if (!toc) | 59 if (!toc) |
| 60 return; | 60 return; |
| 61 |
| 62 // Figure out which link matches the current page so it can be styled |
| 63 // differently. |
| 64 var links = toc.getElementsByTagName('a'); |
| 65 var selectedNode = null; |
| 66 var path_parts = document.location.pathname.replace(/\/+$/, '').split('/') |
| 67 for (var i = 0; i < links.length; i++) { |
| 68 if (links[i].getAttribute('href') != path_parts[path_parts.length - 1]) |
| 69 continue; |
| 70 links[i].className = 'leftNavSelected'; |
| 71 links[i].removeAttribute('href'); |
| 72 selectedNode = links[i]; |
| 73 } |
| 74 |
| 75 // Go through the items on the sidebar and add toggles. |
| 61 var items = toc.getElementsByTagName('li'); | 76 var items = toc.getElementsByTagName('li'); |
| 62 var selectedNode = null; | |
| 63 for (var i = 0; i < items.length; i++) { | 77 for (var i = 0; i < items.length; i++) { |
| 64 var item = items[i]; | 78 var item = items[i]; |
| 65 if (item.className == 'leftNavSelected') { | 79 if (!item.firstChild || item.firstChild.tagName != 'SPAN') |
| 66 selectedNode = item; | 80 continue; |
| 67 } else if (item.firstChild && | 81 // Only assign toggles to text nodes in the sidebar. |
| 68 item.firstChild.tagName == 'SPAN') { | 82 var a = document.createElement('a'); |
| 69 // Only assign toggles to text nodes in the sidebar. | 83 a.className = 'toggle selected'; |
| 70 var a = document.createElement('a'); | 84 a.appendChild(document.createTextNode(' ')); |
| 71 a.className = 'toggle selected'; | 85 a.onclick = function() { |
| 72 a.appendChild(document.createTextNode(' ')); | 86 toggleList(this.parentNode.getElementsByTagName('ul')); |
| 73 a.onclick = function() { | 87 }; |
| 74 toggleList(this.parentNode.getElementsByTagName('ul')); | 88 item.firstChild.onclick = function() { |
| 75 }; | 89 toggleList(this.parentNode.getElementsByTagName('ul')); |
| 76 item.firstChild.onclick = function() { | 90 }; |
| 77 toggleList(this.parentNode.getElementsByTagName('ul')); | 91 item.insertBefore(a, item.firstChild); |
| 78 }; | 92 toggleList(item.getElementsByTagName('ul')); |
| 79 item.insertBefore(a, item.firstChild); | |
| 80 toggleList(item.getElementsByTagName('ul')); | |
| 81 } | |
| 82 } | 93 } |
| 83 if (selectedNode) { | 94 |
| 95 // Reveal the selected link. |
| 96 if (selectedNode) |
| 84 revealAncestor(selectedNode); | 97 revealAncestor(selectedNode); |
| 85 } | |
| 86 }; | 98 }; |
| 87 | 99 |
| 88 initSidebar(); | 100 initSidebar(); |
| 89 })() | 101 })() |
| OLD | NEW |