tools.js

来自「《JavaScript王者归来》examples.rar」· JavaScript 代码 · 共 649 行 · 第 1/2 页

JS
649
字号
/*----------------------------------------------------------------------------\|                                JSVM 2.0                                     ||-----------------------------------------------------------------------------||                         Created by Wan Changhua                             ||                      (Email,MSN: wch3116@hotmail.com)                       ||                   For Homolo Co., Ltd. (http://jsvm.org/)                   ||-----------------------------------------------------------------------------|| An object based javascript framework, targeted at rich web applications,    || JSVM (JavaScript Virtual Machine) is implemented in JavaScript. Currently   |
| only internet explorer 5.5 and later and firefox and opera are supported.   ||-----------------------------------------------------------------------------||                 Copyright (c) 2004 - 2005 Homolo Co., Ltd.                  |
|-----------------------------------------------------------------------------|
|                                                                             |
| BSD - FreeBSD Copyright Information License                                 |
|                                                                             |
| 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.                      |
|                                                                             |
| This software is provided "as is", without warranty of any kind, express or |
| implied, including  but not limited  to the warranties of  merchantability, |
| fitness 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.                                                   |
|                                                                             |
|-----------------------------------------------------------------------------|
| Dependencies: ../../jsre.js, ../runtime.js                                  |
|-----------------------------------------------------------------------------|
| 2006-12-07 | Created by Wan Changhua.                                       |
|-----------------------------------------------------------------------------|
| Created 2006-12-07 | All changes are in the log above. | Updated 2006-12-07 |
\----------------------------------------------------------------------------*/

/**
 * JSVM, extend tools
 * @file:	tools.js
 * @function:	load tools
 * @author:	Wan Changhua
 * @date:	2006.12.07
 *
 */

_JSVM_Namespace.runtimeEnvironment.loadModule("tools", function()
{
	var jsre = _JSVM_Namespace.runtimeEnvironment, JSVM = jsre.JSVM;

	/**
	 *	plug-in SimpleTree
	 */
	JSVM.getEngine().definePackage("jsvm.tools");
	jsvm.tools.TreeNode = function (text, value) {

		this.id = createUID();
		globalNodes[this.id] = this;

		this.text  = text;
		this.value = value;
		this.attributes = {};
		this.childNodes = [];

		this.checked = false;
		this.open = false;

		this.appendChild = function (cn) {
			this.childNodes.push(cn);
		}

		this.setAttribute = function (name, value) {
			this.attributes[name] = value;
		}

		this.getAttribute = function (name) {
			return this.attributes[name];
		}

		this.hasChild = function () {
			return this.childNodes.length > 0;
		}

		this.getElement = function () {
			return document.getElementById("div_" + this.id);
		}

		this.getChildElement = function () {
			return document.getElementById("cn_" + this.id);
		}

		this.getCheckBox = function () {
			return document.getElementById("cb_" + this.id);
		}

		this.getICON = function () {
			return document.getElementById("icon_" + this.id);
		}

		this.changeCheck = function () {
			var el = this.getCheckBox();
			if (el.checked) {
				this.check();
			} else {
				this.uncheck();
			}
		}

		this.check = function () {
			this.checked = true;
			var el = this.getCheckBox();
			el.checked = true;
			for (var i = 0; i < this.childNodes.length; i++) {
				this.childNodes[i].check();
			}
		}

		this.uncheck = function () {
			this.checked = false;
			var el = this.getCheckBox();
			el.checked = false;
			for (var i = 0; i < this.childNodes.length; i++) {
				this.childNodes[i].uncheck();
			}
		}

		this.toggle = function () {
			if (this.open) {
				this.collapse();
			} else {
				this.expand();
			}
		}
		
		this.expandAll = function () {
			this.expand();
			for (var i = 0; i < this.childNodes.length; i++) {
				this.childNodes[i].expandAll();
			}
		}
		
		this.expand = function () {
			this.open = true;
			if (this.hasChild()) {
				this.getICON().innerHTML = "6";
			}
			var el = this.getChildElement();
			el.style.display = "";
		}
			
		this.collapse = function () {
			this.open = false;
			if (this.hasChild()) {
				this.getICON().innerHTML = "4";
			}
			var el = this.getChildElement();
			el.style.display = "none";	
		}
			
		collapse = function () {
			this.expand();
			for (var i = 0; i < this.childNodes.length; i++) {
				this.childNodes[i].collapseAll();
			}
		}

		this.getCheckedChildNodes = function () {
			var ccns = [];
			for (var i = 0; i < this.childNodes.length; i++) {
				var cn = this.childNodes[i];
				if (cn.checked) {
					ccns.push(cn);
				}
				if (cn.hasChild()) {
					ccns = ccns.concat(cn.getCheckedChildNodes());
				}
			}
			return ccns;
		}

		this.toString = function (n) {

			if (n == null) {
				n = 0;
			}
			var sb = [];
			sb.push('<div id="div_' + this.id + '">');
			sb.push(getSpaceCharHTML(n));
			if (this.hasChild()) {
				sb.push(getControlableIconHTML(this.id));
			} else {
				sb.push(getLeafIconHTML());
			}
			sb.push(' <input type=checkbox id="cb_');
			sb.push(this.id);
			sb.push('" style="width:11pt;height:11pt" onclick="jsvm.tools.TreeNode.checkNode(\'');
			sb.push(this.id);
			sb.push('\');"> <label for="cb_');
			sb.push(this.id);
			sb.push('" title="');
			sb.push((this.value || '').replace(/&/g, '&amp').replace(/"/g, '&quot;'));
			sb.push('">');
			sb.push(this.text);
			sb.push('</label>');
			sb.push('<div id="cn_');
			sb.push(this.id);
			sb.push('" style="display:none">');
			for (var i = 0; i < this.childNodes.length; i++) {
				sb.push(this.childNodes[i].toString(n + 1));
			}
			sb.push('</div>');
			sb.push('</div>');
			return sb.join('');

		}

		this.build = function (el) {
			// prevent dom leak
			if (el.nodeName == null && typeof el == "string") {
					el = document.getElementById(el);
			}
			if (el != null) {
				el.innerHTML = this.toString();
			}
		}

	}

	var seqNumber = 0;
	var globalNodes = {};
	var createUID = function () {
		return "__n" + (seqNumber++);
	}

	var getSpaceCharHTML = function (n) {
		if (n == null || n == 0) {
				return "";
			}
		var sb = ['<font face="Webdings" color="white">'];
		for (var i = 0; i < n; i++) {
			sb.push('g');
			}
		sb.push('</font>');
		return sb.join('');
	}
	var getControlableIconHTML = function (id) {
		return '<font face="Webdings" style="cursor:pointer" id="icon_' + id + '" onclick="jsvm.tools.TreeNode.toggleNode(\'' + id + '\')">4</font>';
	}
	var getLeafIconHTML = function () {
		return '<font face="Webdings" color="white">g</font>';
	}

	jsvm.tools.TreeNode.get = function (id) {
		return globalNodes[id];
	}
	jsvm.tools.TreeNode.checkNode = function (id) {
		jsvm.tools.TreeNode.get(id).changeCheck();
	}
	jsvm.tools.TreeNode.toggleNode = function (id) {
		jsvm.tools.TreeNode.get(id).toggle();
	}

	jsvm.tools.JSArchivePackager = {};

	// defines constants
	var str_regexps = [/("(\\"|[^"\n]|(\\\n))*")/g, /('(\\'|[^'\n]|(\\\n))*')/g];
	var str_prefix = '${14a2af98-ba5a-4391-a7ab-06eeddf874c4', str_suffix = '}';
	var regExps_regexps = /(\/(\\\/|[^\/\n])*\/)/g;

	var getConstantString = function (t, i, rnd) {
		return str_prefix + rnd + "|" + t + i + str_suffix;
	}

	// compress source code.
	var compress = function (src) {

		var ds = src.replace(/\r\n/g, '\n');

		// create a random string
		var rnd = new Date().getTime() + "." + Math.round(Math.random() * 1000);

		// replace string expression.
		var constStrs = (ds.match(str_regexps[0]) || []).concat(ds.match(str_regexps[1]) || []);
		for (var i = 0; i < constStrs.length; i++) {
			ds = ds.replace(constStrs[i], getConstantString("S", i, rnd));
		}

		// remove comments.
		ds = ds.replace(/([^\\]|^)\/\/([^\n])*/g, '$1').replace(/(\/\*)\/?(([^\*]\/)|[^\/])*(\*\/)/g, '');

		// replace regexp expression.
		var constRegExps = ds.match(regExps_regexps) || [];
		for (var i = 0; i < constRegExps.length; i++) {
			ds = ds.replace(constRegExps[i], getConstantString("R", i, rnd));
		}

		// remove space charset.
		var ss = null;
		while(ds != ss) {
			ss = ds;
			ds = ss.replace(/(\ |\t)(=|>|<|\{|\}|\(|\)|\+|\-|\*|\?|\:|\;|\,)/g, '$2')
				.replace(/(=|>|<|\{|\}|\(|\)|\+|\-|\*|\?|\:|\;|\,)(\ |\t)/g, '$1')
				.replace(/(\ |\t)==/g, '==')
				.replace(/==(\ |\t)/g, '==')
				.replace(/(\ |\t)!=/g, '!=')
				.replace(/!=(\ |\t)/g, '!=')
				.replace(/(\ |\t)<=/g, '<=')
				.replace(/<=(\ |\t)/g, '<=')
				.replace(/(\ |\t)>=/g, '>=')
				.replace(/>=(\ |\t)/g, '>=')
				.replace(/(\ |\t)&&/g, '&&')
				.replace(/&&(\ |\t)/g, '&&')
				.replace(/(\ |\t)\|\|/g, '||')
				.replace(/\|\|(\ |\t)/g, '||');
		}

		// remove blank line.
		var ss = null;
		while(ds != ss) {

⌨️ 快捷键说明

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