📄 jscooktree.js
字号:
/*
JSCookTree v2.01. (c) Copyright 2002 by Heng Yuan
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
// data structures
//
// ctTreeInfo stores information about the current tree
//
function ctTreeInfo (nodeProperties, prefix, hideType, expandLevel)
{
// default node properties 节点属性
this.nodeProperties = nodeProperties;
// current selected item in this tree 树型当前节点所选项
this.currentItem = null;
// theme prefix
this.prefix = prefix;
// open tree type
// 0: just open the current tree 打开当前节点不关闭其他节点
// 1: close other branches in the same tree
// 2: close other branches in other trees as well
this.hideType = hideType;
// the deepest level of the tree is the always expand
this.expandLevel = expandLevel;
// beginIndex is the first index of the tree item
this.beginIndex = 0;
// endIndex is same as beginIndex + # of items in the tree
this.endIndex = 0;
}
function ctMenuInfo (id, idSub)
{
// id of the menu item that owns the sub menu
this.id = id;
// the id of the sub menu
this.idSub = idSub;
}
// Globals
var _ctIDSubMenuCount = 0;
var _ctIDSubMenu = 'ctSubTreeID'; // for creating submenu id
var _ctCurrentItem = null; // the current menu item being selected;
var _ctNoAction = new Object (); // indicate that the item cannot be hovered.
var _ctItemList = new Array (); // a simple list of items
var _ctTreeList = new Array (); // a list of ctTreeInfo.
var _ctMenuList = new Array (); // a list of ctMenuInfo
var _ctMenuInitStr = ''; // initiation command that initiate menu items
// default node properties
var _ctNodeProperties =
{
// tree attributes
//
// except themeLevel, all other attributes can be specified
// for each level of depth of the tree.
// HTML code to the left of a folder item
// first one is for folder closed, second one is for folder opened
folderLeft: [['', '']],
// HTML code to the right of a folder item
// first one is for folder closed, second one is for folder opened
folderRight: [['', '']],
// HTML code to the left of a regular item
itemLeft: [''],
// HTML code to the right of a regular item
itemRight: [''],
// HTML code for the connector
// first one is for w/ having next sibling, second one is for no next sibling
folderConnect: [[['',''],['','']]],
itemConnect: [['',''],['','']],
// HTML code for spacers
// first one connects next, second one doesn"t
spacer: [[' ', ' ']],
// deepest level of theme specified
themeLevel: 1
// tells JSCookTree to use <A> ancher tag to open links
// if this field is set to false, then JSCookTree would hand it.
};
//////////////////////////////////////////////////////////////////////
//
// Drawing Functions and Utility Functions
//
//////////////////////////////////////////////////////////////////////
//
// produce a new unique submenu id
//
function ctNewSubMenuID ()
{
return _ctIDSubMenu + (++_ctIDSubMenuCount);
}
//
// return the property string for the menu item
//
function ctActionItem ()
{
return ' onmouseover="ctItemMouseOver (this.parentNode)" onmouseout="ctItemMouseOut (this.parentNode)" onmousedown="ctItemMouseDown (this.parentNode)" onmouseup="ctItemMouseUp (this.parentNode)"';
}
//
// return the property string for the menu item
//
function ctNoActionItem (item)
{
return item[1];
}
//
// used to determine the property string
//
function ctGetPropertyLevel (level, property)
{
return (level >= property.length) ? (property.length - 1) : level;
}
function ctCollapseTree (id)
{
var menu = ctGetObject (id).firstChild;
var i;
for (i = 0; i < menu.ctItems.length; ++i)
ctCloseFolder (menu.ctItems[i]);
}
//
// expand a tree such that upto level is exposed
//
function ctExpandTree (id, expandLevel)
{
if (expandLevel <= 0)
return;
var obj = ctGetObject (id);
if (!obj)
return;
var thisMenu = obj.firstChild;
if (!thisMenu)
return;
ctExpandTreeSub (thisMenu, expandLevel)
}
function ctExpandTreeSub (subMenu, expandLevel)
{
if (subMenu.ctLevel >= expandLevel)
return;
var i;
var item;
for (i = 0; i < subMenu.ctItems.length; ++i)
{
item = subMenu.ctItems[i];
if (item.ctIdSub)
{
ctOpenFolder (item);
ctExpandTreeSub (ctGetObject (item.ctIdSub), expandLevel);
}
}
}
//
// expose a particular menu item use its link as the search value
//
function ctExposeItem (treeIndex, link)
{
if (treeIndex < 0 || treeIndex >= _ctTreeList.length)
return;
var tree = _ctTreeList[treeIndex];
var endIndex = tree.endIndex;
var i;
for (i = tree.beginIndex; i < endIndex; ++i)
{
if (_ctItemList[i].length > 2 &&
_ctItemList[i][2] == link)
{
return ctExposeTreeIndex (treeIndex, i);
}
}
}
//
// expose a particular menu item using its index
// 展开详细
function ctExposeTreeIndex (treeIndex, index)
{
var item = ctGetObject ('ctItemID' + (_ctTreeList[treeIndex].beginIndex + index)).parentNode;
if (!item)
return null;
var parentItem = ctGetThisMenu (item).ctParent;
if (parentItem)
ctExposeTreeIndexSub (parentItem);
ctSetSelectedItem (item);
return item;
}
function ctExposeTreeIndexSub (item)
{
var parentItem = ctGetThisMenu (item).ctParent;
if (parentItem)
ctExposeTreeIndexSub (parentItem);
ctOpenFolder (item);
}
//
// mark a particular menu item with id using its link
//
function ctMarkItem (treeIndex, link)
{
if (treeIndex < 0 || treeIndex >= _ctTreeList.length)
return;
var tree = _ctTreeList[treeIndex];
var endIndex = tree.endIndex;
var i;
for (i = tree.beginIndex; i < endIndex; ++i)
{
if (_ctItemList[i].length > 2 &&
_ctItemList[i][2] == link)
{
var item = ctGetObject ('ctItemID' + (_ctTreeList[treeIndex].beginIndex + i)).parentNode;
if (!item)
return null;
if (item.id == "JSCookTreeItem")
item.id = 'JSCookTreeMarked';
return item;
}
}
}
//
// mark a particular menu item with id using index
//
function ctMarkTreeIndex (treeIndex, index)
{
var item = ctGetObject ('ctItemID' + (_ctTreeList[treeIndex].beginIndex + index)).parentNode;
if (!item)
return null;
if (item.id == "JSCookTreeItem")
item.id = 'JSCookTreeMarked';
return item;
}
//
// return the current selected node for the current tree
//
// treeItem treeItem is the table row of where the tree item is located
//
function ctGetSelectedItem (treeIndex)
{
if (_ctTreeList[treeIndex].hideType <= 1)
return _ctTreeList[treeIndex].currentItem;
else
return _ctCurrentItem;
}
//
// The function that builds the menu inside the specified element id.
//
function ctDraw (id, tree, nodeProperties, prefix, hideType, expandLevel)
{
var obj = ctGetObject (id);
if (!nodeProperties)
nodeProperties = _ctNodeProperties;
if (!prefix)
prefix = '';
if (!hideType)
hideType = 0;
if (!expandLevel)
expandLevel = 0;
//var treeIndex = _ctTreeList.push (new ctTreeInfo (nodeProperties, prefix, hideType, expandLevel)) - 1;
_ctTreeList[_ctTreeList.length] = new ctTreeInfo (nodeProperties, prefix, hideType, expandLevel);
var treeIndex = _ctTreeList.length - 1;
var beginIndex = _ctItemList.length;
_ctMenuInitStr = '';
var str = ctDrawSub (tree, true, null, treeIndex, 0, nodeProperties, prefix, '');
obj.innerHTML = str;
eval (_ctMenuInitStr);
_ctMenuInitStr = '';
var endIndex = _ctItemList.length;
_ctTreeList[treeIndex].beginIndex = beginIndex;
_ctTreeList[treeIndex].endIndex = endIndex;
if (expandLevel){
ctExpandTree (id, expandLevel);
}
ctExpandTree('myMenuID',1);
//document.write ('<textarea wrap="off" rows="15" cols="80">' + str + '</textarea><br>');
return treeIndex;
}
//
// draw the sub menu recursively
//
function ctDrawSub (subMenu, isMain, id, treeIndex, level, nodeProperties, prefix, indent)
{
var lvl = level;
if (lvl > nodeProperties.themeLevel)
lvl = nodeProperties.themeLevel;
var str = '<div class="' + prefix + 'TreeLevel' + lvl + '"';
if (!isMain)
str += ' id="' + id + '"';
str += '>';
var strSub = '';
var item;
var idSub;
var hasChild;
var classStr;
var connectSelect;
var childIndent;
var index;
var actionStr;
var itemID;
var markerStr;
var themeLevel = nodeProperties.themeLevel;
var i;
if (isMain)
i = 0;
else
i = 5;
var className = ' class="' + prefix + 'Row"';
for (; i < subMenu.length; ++i)
{
item = subMenu[i];
if (!item)
continue;
//index = _ctItemList.push (item) - 1;
_ctItemList[_ctItemList.length] = item;
index = _ctItemList.length - 1;
hasChild = (item.length > 5);
idSub = hasChild ? ctNewSubMenuID () : null;
str += '<table cellspacing="0" class="' + prefix + 'Table">';
//
// #JSCookTreeFolderClose & #JSCookTreeFolderOpen
// are used in style sheet to control the animation of folder open/close
// Also, it tells status of the submenu
//
str += '<tr' + className;
if (hasChild)
str += ' id="JSCookTreeFolderClosed">';
else
str += ' id="JSCookTreeItem">';
classStr = prefix + (hasChild ? 'Folder' : 'Item');
//
// markerStr is used to mark Spacer cell such that the item (<tr> tag)
// could be tracked in an alternative way
// _ctMenuInitStr is used to initate the menu item
//
itemID = 'ctItemID' + index;
markerStr = ' id="' + itemID + '"';
_ctMenuInitStr += 'ctSetupItem (ctGetObject ("' + itemID + '").parentNode,' + index + ',' + treeIndex + ',' + level + ',' + (idSub ? ('"' + idSub + '"') : 'null') + ');';
str += '<td class="' + classStr + 'Spacer"' + markerStr + '>' + indent;
str += '</td>';
if (item[0] == _ctNoAction)
{
str += ctNoActionItem (item, prefix);
str += '</tr></table>';
continue;
}
actionStr = ctActionItem ();
str += '<td class="' + classStr + 'Left"' + actionStr + '>';
// add connect part
if (hasChild)
{
connectSelect = ctHasNextItem (i, subMenu) ? 0 : 1;
lvl = ctGetPropertyLevel (level, nodeProperties.folderConnect);
str += '<span class="JSCookTreeFolderClosed">' + nodeProperties.folderConnect[lvl][connectSelect][0] + '</span>' +
'<span class="JSCookTreeFolderOpen">' + nodeProperties.folderConnect[lvl][connectSelect][1] + '</span>';
}
else
{
connectSelect = ctHasNextItem (i, subMenu) ? 0 : 1;
lvl = ctGetPropertyLevel (level, nodeProperties.itemConnect);
str += nodeProperties.itemConnect[lvl][connectSelect];
}
if (item[0] != null && item[0] != _ctNoAction)
{
str += item[0];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -