📄 toc.js
字号:
//=============================================================================
// System : Sandcastle Help File Builder
// File : TOC.js
// Author : Eric Woodruff (Eric@EWoodruff.us)
// Updated : 07/03/2007
// Note : Copyright 2006, Eric Woodruff, All rights reserved
// Compiler: JavaScript
//
// This file contains the methods necessary to implement a simple tree view
// for the table of content with a resizable splitter and Ajax support to
// load tree nodes on demand. It also contains the script necessary to do
// full-text searches.
//
// This code may be used in compiled form in any way you desire. This file
// may be redistributed unmodified by any means PROVIDING it is not sold
// for profit without the author's written consent. This notice, the
// author's name, and all copyright notices must remain intact in all
// applications, documentation, and source files.
//
// This code is provided "as is" with no warranty either express or implied.
// The author accepts no liability for any damage or loss of business that
// this product may cause.
//
// Version Date Who Comments
// ============================================================================
// 1.3.0.0 09/12/2006 EFW Created the code
// 1.4.0.2 06/15/2007 EFW Reworked to get rid of frame set and to add
// support for Ajax to load tree nodes on demand.
// 1.5.0.0 06/24/2007 EFW Added full-text search capabilities
//=============================================================================
// IE flag
var isIE = (navigator.userAgent.indexOf("MSIE") >= 0);
// Minimum width of the TOC div
var minWidth = 100;
// Elements and sizing info
var divTOC, divSizer, topicContent, divNavOpts, divSearchOpts, divSearchResults,
divTree, docBody, maxWidth, offset, txtSearchText, chkSortByTitle;
// Last node selected
var lastNode, lastSearchNode;
//============================================================================
// Initialize the tree view and resize the content
function Initialize()
{
docBody = document.getElementsByTagName("body")[0];
divTOC = document.getElementById("TOCDiv");
divSizer = document.getElementById("TOCSizer");
topicContent = document.getElementById("TopicContent");
divNavOpts = document.getElementById("divNavOpts");
divSearchOpts = document.getElementById("divSearchOpts");
divSearchResults = document.getElementById("divSearchResults");
divTree = document.getElementById("divTree");
txtSearchText = document.getElementById("txtSearchText");
chkSortByTitle = document.getElementById("chkSortByTitle");
// The sizes are bit off in FireFox
if(!isIE)
divNavOpts.style.width = divSearchOpts.style.width = 292;
ResizeTree();
SyncTOC();
}
//============================================================================
// Navigation and expand/collaps code
// Synchronize the table of content with the selected page if possible
function SyncTOC()
{
var idx, anchor, base, href, url, anchors, treeNode, saveNode;
base = window.location.href;
base = base.substr(0, base.lastIndexOf("/") + 1);
if(base.substr(0, 5) == "file:" && base.substr(0, 8) != "file:///")
base = base.replace("file://", "file:///");
url = GetCurrentUrl();
if(url == "")
return false;
if(url.substr(0, 5) == "file:" && url.substr(0, 8) != "file:///")
url = url.replace("file://", "file:///");
while(true)
{
anchors = divTree.getElementsByTagName("A");
anchor = null;
for(idx = 0; idx < anchors.length; idx++)
{
href = anchors[idx].href;
if(href.substring(0, 7) != 'http://' &&
href.substring(0, 8) != 'https://' &&
href.substring(0, 7) != 'file://')
href = base + href;
if(href == url)
{
anchor = anchors[idx];
break;
}
}
if(anchor == null)
{
// If it contains a "#", strip anything after that and try again
if(url.indexOf("#") != -1)
{
url = url.substr(0, url.indexOf("#"));
continue;
}
return;
}
break;
}
// If found, select it and find the parent tree node
SelectNode(anchor);
saveNode = anchor;
lastNode = null;
while(anchor != null)
{
if(anchor.className == "TreeNode")
{
treeNode = anchor;
break;
}
anchor = anchor.parentNode;
}
// Expand it and all of its parents
while(anchor != null)
{
Expand(anchor);
anchor = anchor.parentNode;
while(anchor != null)
{
if(anchor.className == "TreeNode")
break;
anchor = anchor.parentNode;
}
}
lastNode = saveNode;
// Scroll the node into view
var windowTop = lastNode.offsetTop - divTree.offsetTop - divTree.scrollTop;
var windowBottom = divTree.clientHeight - windowTop - lastNode.offsetHeight;
if(windowTop < 0)
divTree.scrollTop += windowTop - 30;
else
if(windowBottom < 0)
divTree.scrollTop -= windowBottom - 30;
}
// Get the currently loaded URL from the IFRAME
function GetCurrentUrl()
{
var base, url = "";
try
{
url = window.frames["TopicContent"].document.URL.replace(/\\/g, "/");
}
catch(e)
{
// If this happens the user probably navigated to another frameset
// that didn't make itself the topmost frameset and we don't have
// control of the other frame anymore. In that case, just reload
// our index page.
base = window.location.href;
base = base.substr(0, base.lastIndexOf("/") + 1);
if(base.substr(0, 5) == "file:" && base.substr(0, 8) != "file:///")
base = base.replace("file://", "file:///");
if(base.substr(0, 5) == "file:")
top.location.href = base + "Index.html";
else
top.location.href = base + "Index.aspx";
}
return url;
}
// Expand or collapse all nodes
function ExpandOrCollapseAll(expandNodes)
{
var divIdx, childIdx, img, divs = document.getElementsByTagName("DIV");
var childNodes, child, div, link, img;
for(divIdx = 0; divIdx < divs.length; divIdx++)
if(divs[divIdx].className == "Hidden" ||
divs[divIdx].className == "Visible")
{
childNodes = divs[divIdx].parentNode.childNodes;
for(childIdx = 0; childIdx < childNodes.length; childIdx++)
{
child = childNodes[childIdx];
if(child.className == "TreeNodeImg")
img = child;
if(child.className == "Hidden" || child.className == "Visible")
{
div = child;
break;
}
}
if(div.className == "Visible" && !expandNodes)
{
div.className = "Hidden";
img.src = "Collapsed.gif";
}
else
if(div.className == "Hidden" && expandNodes)
{
div.className = "Visible";
img.src = "Expanded.gif";
if(div.innerHTML == "")
FillNode(div, true)
}
}
}
// Toggle the state of the specified node
function Toggle(node)
{
var i, childNodes, child, div, link;
childNodes = node.parentNode.childNodes;
for(i = 0; i < childNodes.length; i++)
{
child = childNodes[i];
if(child.className == "Hidden" || child.className == "Visible")
{
div = child;
break;
}
}
if(div.className == "Visible")
{
div.className = "Hidden";
node.src = "Collapsed.gif";
}
else
{
div.className = "Visible";
node.src = "Expanded.gif";
if(div.innerHTML == "")
FillNode(div, false)
}
}
// Expand the selected node
function Expand(node)
{
var i, childNodes, child, div, img;
// If not valid, don't bother
if(GetCurrentUrl() == "")
return false;
if(node.tagName == "A")
childNodes = node.parentNode.childNodes;
else
childNodes = node.childNodes;
for(i = 0; i < childNodes.length; i++)
{
child = childNodes[i];
if(child.className == "TreeNodeImg")
img = child;
if(child.className == "Hidden" || child.className == "Visible")
{
div = child;
break;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -