/****************************************************************** ** 文件名: utils.js ** Copyright (c) 2005 金蝶国际软件集团有限公司 研发中心 ** http://www.kingdee.com ** ** 创建人: 叶汉良 ** 日 期: 2005.08.01 ** ** 描述:工具类 ** 版 本:Web V1.0 ******************************************************************/ /** * HashMap构造函数 * */ function HashMap() { this.length = 0; this.prefix = "hashmap_prefix_kingdee_"; } /** * 向HashMap中添加键值对 * */ HashMap.prototype.put = function (key, value) { this[this.prefix + key] = value; this.length ++; } /** * 从HashMap中获取key对应的value值 * */ HashMap.prototype.get = function(key) { return typeof this[this.prefix + key] == "undefined" ? null : this[this.prefix + key]; } /** * 从HashMap中获取所有key的集合,以数组形式返回 * */ HashMap.prototype.keySet = function() { var arrKeySet = new Array(); var index = 0; for(var strKey in this) { if(strKey.substring(0,this.prefix.length) == this.prefix) arrKeySet[index ++] = strKey.substring(this.prefix.length); } return arrKeySet.length == 0 ? null : arrKeySet; } /** * 从HashMap中获取value的集合,以数组形式返回 * */ HashMap.prototype.values = function() { var arrValues = new Array(); var index = 0; for(var strKey in this) { if(strKey.substring(0,this.prefix.length) == this.prefix) { arrValues[index ++] = this[strKey]; } } return arrValues.length == 0 ? null : arrValues; } /** * 获取HashMap的value值数量 * */ HashMap.prototype.size = function() { return this.length; } /** * 删除key对应的值 * */ HashMap.prototype.remove = function(key) { delete this[this.prefix + key]; this.length --; } /** * 清空HashMap * */ HashMap.prototype.clear = function() { for(var strKey in this) { if(strKey.substring(0,this.prefix.length) == this.prefix) { delete this[strKey]; } } this.length = 0; } /** * 判断HashMap是否为空 * */ HashMap.prototype.isEmpty = function() { return this.length == 0; } /** * 判断HashMap是否存在某个key * */ HashMap.prototype.containsKey = function(key) { for(var strKey in this) { if(strKey == this.prefix + key) { return true; } } return false; } /** * 判断HashMap是否存在某个value * */ HashMap.prototype.containsValue = function(value) { for(var strKey in this) { if(this[strKey] == value) { return true; } } return false; } /** * 把一个HashMap的值加入到另一个HashMap中,参数必须是HashMap * */ HashMap.prototype.putAll = function(map) { if(map == null) { return; } if(map.constructor != HashMap) { return; } var arrKey = map.keySet(); var arrValue = map.values(); for(var i in arrKey) { this.put(arrKey[i],arrValue[i]); } } /** *HashMap的toString函数 * */ HashMap.prototype.toString = function() { var str = ""; for(var strKey in this) { if(strKey.substring(0,this.prefix.length) == this.prefix) { str += strKey.substring(this.prefix.length) + " : " + this[strKey] + "\r\n"; } } return str; } HashMap.prototype.setUrlParameter = function(key,value) { if (this.containsKey(key)) { this.remove(key); } this.put(key,value); } /** *把keyset对应的值转换为key=value&key=value,主要对内容进行编码,encodeURIComponent * */ HashMap.prototype.toUrlString = function() { var str = ""; for(var strKey in this) { if(strKey.substring(0,this.prefix.length) == this.prefix) { str += strKey.substring(this.prefix.length) + "=" + encodeURIComponent(this[strKey]) + "&"; } } if (str != "") { str = str.substring(0,str.lastIndexOf("&")); } return str; } /** *预装载图片的类 * */ function ImageLoader(arr) { //图片数组 var tempImageArr = new Array(); //当前下载位置 var iLoadNum = 0; //总长度 var iLoadeCount = 0; //默认超时设定 var iTmieOut = 10 * 1000; //定时器 var iCheckInterval = 50; var _randomFun; //当前临时对象 var _this; //载入动作时候的操作,可重新定义 this.onProgressChange=new Function(); //载入完成后的操作,可重新定义 this.onLoadFinish=new Function(); //超时操作,可重新定义 this.onTimeOut=new Function(); //耗费的时间 var iTimeUsed = 0; /** *参数1为图片名称,参数2为路径,参数3为可选参数,可以是字符串的下标 * */ this.add = function() { var arr = arguments; var tempLength; if(arguments[0] == null || arguments[0] == "") { return; } if (arr[2] !="" &&arr[2] != "undefined" && arr[2] != null) { tempLength = arr[2]; } else { tempLength = tempImageArr.length; } tempImageArr[tempLength] = new Image(); tempImageArr[tempLength].src = arr[1] + arr[0]; tempImageArr[tempLength].isLoad = false; iLoadeCount++; //设定每张图片的平均超时时间为10秒 iTmieOut = iLoadeCount*(10*1000); } /** *开始载入图片 * */ this.startLoad = function() { this.checkLoad(); } /** *检测图片载入状态 * */ this.checkLoad = function() { if(iLoadNum == iLoadeCount) { eval(arr + "=tempImageArr;") tempImageArr = null; this.onLoadFinish(); return; } if (iTimeUsed >= iTmieOut) { //如果返回操作为false则停止当前下载 if(this.onTimeOut() == false) { return; } } /** *处理过程 * */ this.onProgressChange(); for(var i in tempImageArr) { if(tempImageArr[i].isLoad == false && tempImageArr[i].complete) { iLoadNum += tempImageArr[i].complete?1:0; tempImageArr[i].isLoad = true; } } iTimeUsed += iCheckInterval; setTimeout(_this + ".checkLoad()",iCheckInterval); } /** *当前已经下载的数量 * */ this.getLoadedCount=function() { return iLoadNum; } /** *需要下载的总数量 * */ this.getResourceCount=function() { return iLoadeCount; } /** *把父函数建立到一个全局变量里去,便于在函数内部调用实例化后的对象 * */ _this=function() { randomFun = "Fun"+String(Date.UTC(new Date())/10000+(Math.random()*10000)); if(typeof(window.oFun) != "object") { window.oFun ={}; } return ("window.oFun." + randomFun) }(); eval("window.oFun." + randomFun + "=this"); } /** *得到css的样式 * */ function getCssStyle(propValue,defaultValue,cssValue) { if (propValue != "" && propValue != defaultValue) { return propValue; } if (cssValue != null && cssValue != "auto") { return cssValue; } return defaultValue; } /** *得到样式 * */ function getCssStyle1(csPath,imgPath,propValue,defaultValue,cssValue) { if (propValue != "") { return propValue; } if (cssValue != null && cssValue != "auto") { return csPath.concat(cssValue); } return imgPath.concat(defaultValue); } /** *高效率的字符串操作类 * */ function StringBuilder(str) { this.length = 0; //append方法 this.append = function(str) { this.length += (this._parts[this._current ++] = String(str)).length; this._str = null; return this; } //toString方法 this.toString = function() { if(this._str != null) { return this._str; } var s = this._parts.join(""); this._parts = [s]; this._current = 1; return this._str = s; } this._current = 0; this._parts = []; this._str = null; if(str != null) { this.append(str); } } /** *对象克隆 * */ Object.prototype.clone = function() { var objClone; if ( this.constructor == Object ) { objClone = new this.constructor(); } else { objClone = new this.constructor(this.valueOf()); } for ( var key in this ) { if ( objClone[key] != this[key] ) { if ( typeof(this[key]) == 'object' ) { objClone[key] = this[key].clone(); } else { objClone[key] = this[key]; } } } objClone.toString = this.toString; objClone.valueOf = this.valueOf; return objClone; } /** *移动光标到指定位置 * */ function moveCursor(oInput,iPos) { if (iPos > oInput.value.length) { return; } var oRange = oInput.createTextRange(); oRange.moveStart('character',iPos); oRange.collapse(true); oRange.select(); } /** *估算字符显示的宽度(javascript没有找到可以直接计算的函数,只能用估算的宽度了) *@str:需要计算的字符串 *@fontSize:字符串使用的字体大小 *@minWidth:有没有限制最小宽度 -1则不现最小宽度 *@maxWidth:有没有限制最大宽度 -1则不限制最大宽度 */ function stringWidth(str,fontSize,minWidth,maxWidth) { //返回结果数组 var aResult = new Array(); if (str == "") { if (minWidth != -1) { aResult[0] = 0; aResult[1] = minWidth; aResult[2] = 0; } else { aResult[0] = 0; aResult[1] = 0; aResult[2] = 0; } } //字体取出pt后得出的大小 var iFontSize = 9; //单字节字符显示宽度 var iSingleCharWidth = 0; //tabCaption的长度 var iLength = 0; //计算出来的宽度 var iWidth = 0; //最后让显示字符位置 var iLastCharPos = -1; //数字串间隔稍微需要+1 var sNumber = "0123456789"; //临时字符 var sTempChar; //字符宽度 var iCharWidth = 0; if (fontSize == null) { fontSize = "9pt"; } var iFontSize = parseInt(fontSize.substring(0,fontSize.indexOf("pt"))); switch(iFontSize) { case 8: iSingleCharWidth = 5; break; case 9: iSingleCharWidth = 6; break; case 10: iSingleCharWidth = 7; break; case 11: case 12: iSingleCharWidth = 8; break; case 13: iSingleCharWidth = 9; break; case 14: case 15: iSingleCharWidth = 10; break; case 16: iSingleCharWidth = 11; break; default: iSingleCharWidth = 6; break; } iLen = str.length; for (var i = 0; i < iLen; i ++) { sTempChar = str.charAt(i); //是双字节字符 if (/[^\x00-\xff]/g.test(sTempChar)) { iWidth += iSingleCharWidth * 2; } //数字 else if (sNumber.indexOf(sTempChar) != -1) { iWidth += iSingleCharWidth + 3; } else { iWidth += iSingleCharWidth; } //要计算是否超过最大宽度 if (maxWidth != -1 && iWidth > maxWidth) { iWidth = maxWidth; iLastCharPos = i - 1; break; } iLastCharPos = i; } iCharWidth = iWidth + 8; if (minWidth != -1 && iWidth < minWidth) { iWidth = minWidth; iLastCharPos = iLen; } //存放字符最后显示字符位置 aResult[0] = iLastCharPos; //字符显示宽度 aResult[1] = iWidth; //字符长度 aResult[2] = iLen; //字符宽度 aResult[3] = iCharWidth; return aResult; } /** *生成唯一值 * */ function generateUUID() { var date = new Date(); return (new String(date.valueOf()) + Math.round(Math.random() * 1000)); } /** * *判断是不是win98或winme操作系统 * */ function isWin98() { var version = window.navigator.appVersion; if (version != "" && (version.indexOf("Windows 98") != -1 || version.indexOf("Windows Me") != -1)) { return true; } return false; }