treemenu.js

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· JavaScript 代码 · 共 698 行 · 第 1/2 页

JS
698
字号
// +-----------------------------------------------------------------------+// | Copyright (c) 2002-2005, Richard Heyes, Harald Radi                   |// | All rights reserved.                                                  |// |                                                                       |// | Redistribution and use in source and binary forms, with or without    |// | modification, are permitted provided that the following conditions    |// | are met:                                                              |// |                                                                       |// | o Redistributions of source code must retain the above copyright      |// |   notice, this list of conditions and the following disclaimer.       |// | o Redistributions in binary form must reproduce the above copyright   |// |   notice, this list of conditions and the following disclaimer in the |// |   documentation and/or other materials provided with the distribution.|// | o The names of the authors may not be used to endorse or promote      |// |   products derived from this software without specific prior written  |// |   permission.                                                         |// |                                                                       |// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   |// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     |// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  |// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      |// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   |// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  |// |                                                                       |// +-----------------------------------------------------------------------+// | Author: Richard Heyes <richard@phpguru.org>                           |// |         Harald Radi <harald.radi@nme.at>                              |// +-----------------------------------------------------------------------+//// $Id: TreeMenu.js,v 1.21 2005/03/02 02:39:26 richard Exp $/*** Function to create copies of objects which are* normally passed around by references (Arrays for example)*/function arrayCopy(input){	var output = new Array(input.length);	for (i in input) {		if (typeof(input[i]) == 'array') {			output[i] = arrayCopy(input[i]);		} else {			output[i] = input[i];		}	}	return output;}/*** TreeMenu class*/	function TreeMenu(iconpath, myname, linkTarget, defaultClass, usePersistence, noTopLevelImages)	{		// Properties		this.iconpath         = iconpath;		this.myname           = myname;		this.linkTarget       = linkTarget;		this.defaultClass     = defaultClass;		this.usePersistence   = usePersistence;		this.noTopLevelImages = noTopLevelImages;		this.n                = new Array();		this.output           = '';		this.nodeRefs       = new Array();		this.branches       = new Array();		this.branchStatus   = new Array();		this.layerRelations = new Array();		this.childParents   = new Array();		this.cookieStatuses = new Array();		this.preloadImages();	}/*** Adds a node to the tree*/	TreeMenu.prototype.addItem = function (newNode)	{		newIndex = this.n.length;		this.n[newIndex] = newNode;				return this.n[newIndex];	}/*** Preload images hack for Mozilla*/	TreeMenu.prototype.preloadImages = function ()	{		var plustop    = new Image; plustop.src    = this.iconpath + '/plustop.gif';		var plusbottom = new Image; plusbottom.src = this.iconpath + '/plusbottom.gif';		var plus       = new Image; plus.src       = this.iconpath + '/plus.gif';			var minustop    = new Image; minustop.src    = this.iconpath + '/minustop.gif';		var minusbottom = new Image; minusbottom.src = this.iconpath + '/minusbottom.gif';		var minus       = new Image; minus.src       = this.iconpath + '/minus.gif';			var branchtop    = new Image; branchtop.src    = this.iconpath + '/branchtop.gif';		var branchbottom = new Image; branchbottom.src = this.iconpath + '/branchbottom.gif';		var branch       = new Image; branch.src       = this.iconpath + '/branch.gif';			var linebottom = new Image; linebottom.src = this.iconpath + '/linebottom.gif';		var line       = new Image; line.src       = this.iconpath + '/line.gif';	}/*** Main function that draws the menu and assigns it* to the layer (or document.write()s it)*/	TreeMenu.prototype.drawMenu = function ()// OPTIONAL ARGS: nodes = [], level = [], prepend = '', expanded = false, visbility = 'inline', parentLayerID = null	{		/**	    * Necessary variables	    */		var output        = '';		var modifier      = '';		var layerID       = '';		var parentLayerID = '';			/**	    * Parse any optional arguments	    */		var nodes         = arguments[0] ? arguments[0] : this.n		var level         = arguments[1] ? arguments[1] : [];		var prepend       = arguments[2] ? arguments[2] : '';		var expanded      = arguments[3] ? arguments[3] : false;		var visibility    = arguments[4] ? arguments[4] : 'inline';		var parentLayerID = arguments[5] ? arguments[5] : null;		var currentlevel  = level.length;		for (var i=0; i<nodes.length; i++) {					level[currentlevel] = i+1;			layerID = this.myname + '_' + 'node_' + this.implode('_', level);			/**            * Store this object in the nodeRefs array            */			this.nodeRefs[layerID] = nodes[i];			/**	        * Store the child/parent relationship	        */			this.childParents[layerID] = parentLayerID;				/**	        * Gif modifier	        */			if (i == 0 && parentLayerID == null) {				modifier = nodes.length > 1 ? "top" : 'single';			} else if(i == (nodes.length-1)) {				modifier = "bottom";			} else {				modifier = "";			}				/**	        * Single root branch is always expanded	        */			if (!this.doesMenu() || (parentLayerID == null && (nodes.length == 1 || this.noTopLevelImages))) {				expanded = true;				} else if (nodes[i].expanded) {				expanded = true;				} else {				expanded = false;			}				/**	        * Make sure visibility is correct based on parent status	        */			visibility =  this.checkParentVisibility(layerID) ? visibility : 'none';			/**	        * Setup branch status and build an indexed array			* of branch layer ids	        */			if (nodes[i].n.length > 0) {				this.branchStatus[layerID] = expanded;				this.branches[this.branches.length] = layerID;			}				/**	        * Setup toggle relationship	        */			if (!this.layerRelations[parentLayerID]) {				this.layerRelations[parentLayerID] = new Array();			}			this.layerRelations[parentLayerID][this.layerRelations[parentLayerID].length] = layerID;				/**	        * Branch images	        */			var gifname  = nodes[i].n.length && this.doesMenu() && nodes[i].isDynamic ? (expanded ? 'minus' : 'plus') : 'branch';			var iconName = expanded && nodes[i].expandedIcon ? nodes[i].expandedIcon : nodes[i].icon;			var iconimg  = nodes[i].icon ? this.stringFormat('<img src="{0}/{1}" width="20" height="20" align="top" id="icon_{2}">', this.iconpath, iconName, layerID) : '';						/**			* Add event handlers			*/			var eventHandlers = "";			for (j in nodes[i].events) {				eventHandlers += this.stringFormat('{0}="{1}" ', j, nodes[i].events[j]);			}			/**	        * Build the html to write to the document			* IMPORTANT:			* document.write()ing the string: '<div style="display:...' will screw up nn4.x	        */			var layerTag  = this.doesMenu() ? this.stringFormat('<div id="{0}" style="display: {1}" class="{2}">', layerID, visibility, (nodes[i].cssClass ? nodes[i].cssClass : this.defaultClass)) : this.stringFormat('<div class="{0}">', nodes[i].cssClass ? nodes[i].cssClass : this.defaultClass);			var onMDown   = this.doesMenu() && nodes[i].n.length  && nodes[i].isDynamic ? this.stringFormat('onmousedown="{0}.toggleBranch(\'{1}\', true)" style="cursor: pointer; cursor: hand"', this.myname, layerID) : '';			var imgTag    = this.stringFormat('<img src="{0}/{1}{2}.gif" width="20" height="20" align="top" border="0" name="img_{3}" {4}>', this.iconpath, gifname, modifier, layerID, onMDown);			var linkTarget= nodes[i].linkTarget ? nodes[i].linkTarget : this.linkTarget;			var linkStart = nodes[i].link ? this.stringFormat('<a href="{0}" target="{1}">', nodes[i].link, linkTarget) : '';			var linkEnd   = nodes[i].link ? '</a>' : '';			this.output += this.stringFormat('{0}<nobr>{1}{2}{3}{4}<span {5}>{6}</span>{7}</nobr><br></div>',			                  layerTag,							  prepend,			                  parentLayerID == null && (nodes.length == 1 || this.noTopLevelImages) ? '' : imgTag,							  iconimg,							  linkStart,							  eventHandlers,							  nodes[i].title,							  linkEnd);			/**	        * Traverse sub nodes ?	        */			if (nodes[i].n.length) {				/**	            * Determine what to prepend. If there is only one root				* node then the prepend to pass to children is nothing.				* Otherwise it depends on where we are in the tree.	            */				if (parentLayerID == null && (nodes.length == 1 || this.noTopLevelImages)) {					var newPrepend = '';					} else if (i < (nodes.length - 1)) {					var newPrepend = prepend + this.stringFormat('<img src="{0}/line.gif" width="20" height="20" align="top">', this.iconpath);					} else {					var newPrepend = prepend + this.stringFormat('<img src="{0}/linebottom.gif" width="20" height="20" align="top">', this.iconpath);				}				this.drawMenu(nodes[i].n,				              arrayCopy(level),				              newPrepend,				              nodes[i].expanded,				              expanded ? 'inline' : 'none',				              layerID);			}		}	}/*** Writes the output generated by drawMenu() to the page*/	TreeMenu.prototype.writeOutput = function ()	{		document.write(this.output);	}/*** Toggles a branches visible status. Called from resetBranches()* and also when a +/- graphic is clicked.*/	TreeMenu.prototype.toggleBranch = function (layerID, updateStatus) // OPTIONAL ARGS: fireEvents = true	{		var currentDisplay = this.getLayer(layerID).style.display;		var newDisplay     = (this.branchStatus[layerID] && currentDisplay == 'inline') ? 'none' : 'inline';		var fireEvents     = arguments[2] != null ? arguments[2] : true;		for (var i=0; i<this.layerRelations[layerID].length; i++) {			if (this.branchStatus[this.layerRelations[layerID][i]]) {				this.toggleBranch(this.layerRelations[layerID][i], false);			}				this.getLayer(this.layerRelations[layerID][i]).style.display = newDisplay;		}			if (updateStatus) {			this.branchStatus[layerID] = !this.branchStatus[layerID];				/**	        * Persistence	        */			if (this.doesPersistence() && !arguments[2] && this.usePersistence) {				this.setExpandedStatusForCookie(layerID, this.branchStatus[layerID]);			}			/**			* Fire custom events			*/			if (fireEvents) {				nodeObject = this.nodeRefs[layerID];					if (nodeObject.ontoggle != null) {					eval(nodeObject.ontoggle);				}								if (newDisplay == 'none' && nodeObject.oncollapse != null) {					eval(nodeObject.oncollapse);				} else if (newDisplay == 'inline' && nodeObject.onexpand != null){					eval(nodeObject.onexpand);				}			}			// Swap image			this.swapImage(layerID);		}		// Swap icon		this.swapIcon(layerID);	}/*** Swaps the plus/minus branch images*/	TreeMenu.prototype.swapImage = function (layerID)	{		var imgSrc = document.images['img_' + layerID].src;			var re = /^(.*)(plus|minus)(bottom|top|single)?.gif$/		if (matches = imgSrc.match(re)) {				document.images['img_' + layerID].src = this.stringFormat('{0}{1}{2}{3}',			                                                matches[1],															matches[2] == 'plus' ? 'minus' : 'plus',															matches[3] ? matches[3] : '',															'.gif');		}	}/*** Swaps the icon for the expanded icon if one* has been supplied.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?