utils.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /******************************************************************
  2. ** 文件名: utils.js
  3. ** Copyright (c) 2005 金蝶国际软件集团有限公司 研发中心
  4. ** http://www.kingdee.com
  5. **
  6. ** 创建人: 叶汉良
  7. ** 日 期: 2005.08.01
  8. **
  9. ** 描述:工具类
  10. ** 版 本:Web V1.0
  11. ******************************************************************/
  12. /**
  13. * HashMap构造函数
  14. *
  15. */
  16. function HashMap()
  17. {
  18. this.length = 0;
  19. this.prefix = "hashmap_prefix_kingdee_";
  20. }
  21. /**
  22. * 向HashMap中添加键值对
  23. *
  24. */
  25. HashMap.prototype.put = function (key, value)
  26. {
  27. this[this.prefix + key] = value;
  28. this.length ++;
  29. }
  30. /**
  31. * 从HashMap中获取key对应的value值
  32. *
  33. */
  34. HashMap.prototype.get = function(key)
  35. {
  36. return typeof this[this.prefix + key] == "undefined" ? null : this[this.prefix + key];
  37. }
  38. /**
  39. * 从HashMap中获取所有key的集合,以数组形式返回
  40. *
  41. */
  42. HashMap.prototype.keySet = function()
  43. {
  44. var arrKeySet = new Array();
  45. var index = 0;
  46. for(var strKey in this)
  47. {
  48. if(strKey.substring(0,this.prefix.length) == this.prefix)
  49. arrKeySet[index ++] = strKey.substring(this.prefix.length);
  50. }
  51. return arrKeySet.length == 0 ? null : arrKeySet;
  52. }
  53. /**
  54. * 从HashMap中获取value的集合,以数组形式返回
  55. *
  56. */
  57. HashMap.prototype.values = function()
  58. {
  59. var arrValues = new Array();
  60. var index = 0;
  61. for(var strKey in this)
  62. {
  63. if(strKey.substring(0,this.prefix.length) == this.prefix)
  64. {
  65. arrValues[index ++] = this[strKey];
  66. }
  67. }
  68. return arrValues.length == 0 ? null : arrValues;
  69. }
  70. /**
  71. * 获取HashMap的value值数量
  72. *
  73. */
  74. HashMap.prototype.size = function()
  75. {
  76. return this.length;
  77. }
  78. /**
  79. * 删除key对应的值
  80. *
  81. */
  82. HashMap.prototype.remove = function(key)
  83. {
  84. delete this[this.prefix + key];
  85. this.length --;
  86. }
  87. /**
  88. * 清空HashMap
  89. *
  90. */
  91. HashMap.prototype.clear = function()
  92. {
  93. for(var strKey in this)
  94. {
  95. if(strKey.substring(0,this.prefix.length) == this.prefix)
  96. {
  97. delete this[strKey];
  98. }
  99. }
  100. this.length = 0;
  101. }
  102. /**
  103. * 判断HashMap是否为空
  104. *
  105. */
  106. HashMap.prototype.isEmpty = function()
  107. {
  108. return this.length == 0;
  109. }
  110. /**
  111. * 判断HashMap是否存在某个key
  112. *
  113. */
  114. HashMap.prototype.containsKey = function(key)
  115. {
  116. for(var strKey in this)
  117. {
  118. if(strKey == this.prefix + key)
  119. {
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125. /**
  126. * 判断HashMap是否存在某个value
  127. *
  128. */
  129. HashMap.prototype.containsValue = function(value)
  130. {
  131. for(var strKey in this)
  132. {
  133. if(this[strKey] == value)
  134. {
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. /**
  141. * 把一个HashMap的值加入到另一个HashMap中,参数必须是HashMap
  142. *
  143. */
  144. HashMap.prototype.putAll = function(map)
  145. {
  146. if(map == null)
  147. {
  148. return;
  149. }
  150. if(map.constructor != HashMap)
  151. {
  152. return;
  153. }
  154. var arrKey = map.keySet();
  155. var arrValue = map.values();
  156. for(var i in arrKey)
  157. {
  158. this.put(arrKey[i],arrValue[i]);
  159. }
  160. }
  161. /**
  162. *HashMap的toString函数
  163. *
  164. */
  165. HashMap.prototype.toString = function()
  166. {
  167. var str = "";
  168. for(var strKey in this)
  169. {
  170. if(strKey.substring(0,this.prefix.length) == this.prefix)
  171. {
  172. str += strKey.substring(this.prefix.length)
  173. + " : " + this[strKey] + "\r\n";
  174. }
  175. }
  176. return str;
  177. }
  178. HashMap.prototype.setUrlParameter = function(key,value)
  179. {
  180. if (this.containsKey(key))
  181. {
  182. this.remove(key);
  183. }
  184. this.put(key,value);
  185. }
  186. /**
  187. *把keyset对应的值转换为key=value&key=value,主要对内容进行编码,encodeURIComponent
  188. *
  189. */
  190. HashMap.prototype.toUrlString = function()
  191. {
  192. var str = "";
  193. for(var strKey in this)
  194. {
  195. if(strKey.substring(0,this.prefix.length) == this.prefix)
  196. {
  197. str += strKey.substring(this.prefix.length)
  198. + "=" + encodeURIComponent(this[strKey]) + "&";
  199. }
  200. }
  201. if (str != "")
  202. {
  203. str = str.substring(0,str.lastIndexOf("&"));
  204. }
  205. return str;
  206. }
  207. /**
  208. *预装载图片的类
  209. *
  210. */
  211. function ImageLoader(arr)
  212. {
  213. //图片数组
  214. var tempImageArr = new Array();
  215. //当前下载位置
  216. var iLoadNum = 0;
  217. //总长度
  218. var iLoadeCount = 0;
  219. //默认超时设定
  220. var iTmieOut = 10 * 1000;
  221. //定时器
  222. var iCheckInterval = 50;
  223. var _randomFun;
  224. //当前临时对象
  225. var _this;
  226. //载入动作时候的操作,可重新定义
  227. this.onProgressChange=new Function();
  228. //载入完成后的操作,可重新定义
  229. this.onLoadFinish=new Function();
  230. //超时操作,可重新定义
  231. this.onTimeOut=new Function();
  232. //耗费的时间
  233. var iTimeUsed = 0;
  234. /**
  235. *参数1为图片名称,参数2为路径,参数3为可选参数,可以是字符串的下标
  236. *
  237. */
  238. this.add = function()
  239. {
  240. var arr = arguments;
  241. var tempLength;
  242. if(arguments[0] == null || arguments[0] == "")
  243. {
  244. return;
  245. }
  246. if (arr[2] !="" &&arr[2] != "undefined" && arr[2] != null)
  247. {
  248. tempLength = arr[2];
  249. }
  250. else
  251. {
  252. tempLength = tempImageArr.length;
  253. }
  254. tempImageArr[tempLength] = new Image();
  255. tempImageArr[tempLength].src = arr[1] + arr[0];
  256. tempImageArr[tempLength].isLoad = false;
  257. iLoadeCount++;
  258. //设定每张图片的平均超时时间为10秒
  259. iTmieOut = iLoadeCount*(10*1000);
  260. }
  261. /**
  262. *开始载入图片
  263. *
  264. */
  265. this.startLoad = function()
  266. {
  267. this.checkLoad();
  268. }
  269. /**
  270. *检测图片载入状态
  271. *
  272. */
  273. this.checkLoad = function()
  274. {
  275. if(iLoadNum == iLoadeCount)
  276. {
  277. eval(arr + "=tempImageArr;")
  278. tempImageArr = null;
  279. this.onLoadFinish();
  280. return;
  281. }
  282. if (iTimeUsed >= iTmieOut)
  283. {
  284. //如果返回操作为false则停止当前下载
  285. if(this.onTimeOut() == false)
  286. {
  287. return;
  288. }
  289. }
  290. /**
  291. *处理过程
  292. *
  293. */
  294. this.onProgressChange();
  295. for(var i in tempImageArr)
  296. {
  297. if(tempImageArr[i].isLoad == false && tempImageArr[i].complete)
  298. {
  299. iLoadNum += tempImageArr[i].complete?1:0;
  300. tempImageArr[i].isLoad = true;
  301. }
  302. }
  303. iTimeUsed += iCheckInterval;
  304. setTimeout(_this + ".checkLoad()",iCheckInterval);
  305. }
  306. /**
  307. *当前已经下载的数量
  308. *
  309. */
  310. this.getLoadedCount=function()
  311. {
  312. return iLoadNum;
  313. }
  314. /**
  315. *需要下载的总数量
  316. *
  317. */
  318. this.getResourceCount=function()
  319. {
  320. return iLoadeCount;
  321. }
  322. /**
  323. *把父函数建立到一个全局变量里去,便于在函数内部调用实例化后的对象
  324. *
  325. */
  326. _this=function()
  327. {
  328. randomFun = "Fun"+String(Date.UTC(new Date())/10000+(Math.random()*10000));
  329. if(typeof(window.oFun) != "object")
  330. {
  331. window.oFun ={};
  332. }
  333. return ("window.oFun." + randomFun)
  334. }();
  335. eval("window.oFun." + randomFun + "=this");
  336. }
  337. /**
  338. *得到css的样式
  339. *
  340. */
  341. function getCssStyle(propValue,defaultValue,cssValue)
  342. {
  343. if (propValue != "" && propValue != defaultValue)
  344. {
  345. return propValue;
  346. }
  347. if (cssValue != null && cssValue != "auto")
  348. {
  349. return cssValue;
  350. }
  351. return defaultValue;
  352. }
  353. /**
  354. *得到样式
  355. *
  356. */
  357. function getCssStyle1(csPath,imgPath,propValue,defaultValue,cssValue)
  358. {
  359. if (propValue != "")
  360. {
  361. return propValue;
  362. }
  363. if (cssValue != null && cssValue != "auto")
  364. {
  365. return csPath.concat(cssValue);
  366. }
  367. return imgPath.concat(defaultValue);
  368. }
  369. /**
  370. *高效率的字符串操作类
  371. *
  372. */
  373. function StringBuilder(str)
  374. {
  375. this.length = 0;
  376. //append方法
  377. this.append = function(str)
  378. {
  379. this.length += (this._parts[this._current ++] = String(str)).length;
  380. this._str = null;
  381. return this;
  382. }
  383. //toString方法
  384. this.toString = function()
  385. {
  386. if(this._str != null)
  387. {
  388. return this._str;
  389. }
  390. var s = this._parts.join("");
  391. this._parts = [s];
  392. this._current = 1;
  393. return this._str = s;
  394. }
  395. this._current = 0;
  396. this._parts = [];
  397. this._str = null;
  398. if(str != null)
  399. {
  400. this.append(str);
  401. }
  402. }
  403. /**
  404. *对象克隆
  405. *
  406. */
  407. Object.prototype.clone = function()
  408. {
  409. var objClone;
  410. if ( this.constructor == Object )
  411. {
  412. objClone = new this.constructor();
  413. }
  414. else
  415. {
  416. objClone = new this.constructor(this.valueOf());
  417. }
  418. for ( var key in this )
  419. {
  420. if ( objClone[key] != this[key] )
  421. {
  422. if ( typeof(this[key]) == 'object' )
  423. {
  424. objClone[key] = this[key].clone();
  425. }
  426. else
  427. {
  428. objClone[key] = this[key];
  429. }
  430. }
  431. }
  432. objClone.toString = this.toString;
  433. objClone.valueOf = this.valueOf;
  434. return objClone;
  435. }
  436. /**
  437. *移动光标到指定位置
  438. *
  439. */
  440. function moveCursor(oInput,iPos)
  441. {
  442. if (iPos > oInput.value.length)
  443. {
  444. return;
  445. }
  446. var oRange = oInput.createTextRange();
  447. oRange.moveStart('character',iPos);
  448. oRange.collapse(true);
  449. oRange.select();
  450. }
  451. /**
  452. *估算字符显示的宽度(javascript没有找到可以直接计算的函数,只能用估算的宽度了)
  453. *@str:需要计算的字符串
  454. *@fontSize:字符串使用的字体大小
  455. *@minWidth:有没有限制最小宽度 -1则不现最小宽度
  456. *@maxWidth:有没有限制最大宽度 -1则不限制最大宽度
  457. */
  458. function stringWidth(str,fontSize,minWidth,maxWidth)
  459. {
  460. //返回结果数组
  461. var aResult = new Array();
  462. if (str == "")
  463. {
  464. if (minWidth != -1)
  465. {
  466. aResult[0] = 0;
  467. aResult[1] = minWidth;
  468. aResult[2] = 0;
  469. }
  470. else
  471. {
  472. aResult[0] = 0;
  473. aResult[1] = 0;
  474. aResult[2] = 0;
  475. }
  476. }
  477. //字体取出pt后得出的大小
  478. var iFontSize = 9;
  479. //单字节字符显示宽度
  480. var iSingleCharWidth = 0;
  481. //tabCaption的长度
  482. var iLength = 0;
  483. //计算出来的宽度
  484. var iWidth = 0;
  485. //最后让显示字符位置
  486. var iLastCharPos = -1;
  487. //数字串间隔稍微需要+1
  488. var sNumber = "0123456789";
  489. //临时字符
  490. var sTempChar;
  491. //字符宽度
  492. var iCharWidth = 0;
  493. if (fontSize == null)
  494. {
  495. fontSize = "9pt";
  496. }
  497. var iFontSize = parseInt(fontSize.substring(0,fontSize.indexOf("pt")));
  498. switch(iFontSize)
  499. {
  500. case 8:
  501. iSingleCharWidth = 5;
  502. break;
  503. case 9:
  504. iSingleCharWidth = 6;
  505. break;
  506. case 10:
  507. iSingleCharWidth = 7;
  508. break;
  509. case 11:
  510. case 12:
  511. iSingleCharWidth = 8;
  512. break;
  513. case 13:
  514. iSingleCharWidth = 9;
  515. break;
  516. case 14:
  517. case 15:
  518. iSingleCharWidth = 10;
  519. break;
  520. case 16:
  521. iSingleCharWidth = 11;
  522. break;
  523. default:
  524. iSingleCharWidth = 6;
  525. break;
  526. }
  527. iLen = str.length;
  528. for (var i = 0; i < iLen; i ++)
  529. {
  530. sTempChar = str.charAt(i);
  531. //是双字节字符
  532. if (/[^\x00-\xff]/g.test(sTempChar))
  533. {
  534. iWidth += iSingleCharWidth * 2;
  535. }
  536. //数字
  537. else if (sNumber.indexOf(sTempChar) != -1)
  538. {
  539. iWidth += iSingleCharWidth + 3;
  540. }
  541. else
  542. {
  543. iWidth += iSingleCharWidth;
  544. }
  545. //要计算是否超过最大宽度
  546. if (maxWidth != -1 && iWidth > maxWidth)
  547. {
  548. iWidth = maxWidth;
  549. iLastCharPos = i - 1;
  550. break;
  551. }
  552. iLastCharPos = i;
  553. }
  554. iCharWidth = iWidth + 8;
  555. if (minWidth != -1 && iWidth < minWidth)
  556. {
  557. iWidth = minWidth;
  558. iLastCharPos = iLen;
  559. }
  560. //存放字符最后显示字符位置
  561. aResult[0] = iLastCharPos;
  562. //字符显示宽度
  563. aResult[1] = iWidth;
  564. //字符长度
  565. aResult[2] = iLen;
  566. //字符宽度
  567. aResult[3] = iCharWidth;
  568. return aResult;
  569. }
  570. /**
  571. *生成唯一值
  572. *
  573. */
  574. function generateUUID()
  575. {
  576. var date = new Date();
  577. return (new String(date.valueOf()) + Math.round(Math.random() * 1000));
  578. }
  579. /**
  580. *
  581. *判断是不是win98或winme操作系统
  582. *
  583. */
  584. function isWin98()
  585. {
  586. var version = window.navigator.appVersion;
  587. if (version != "" && (version.indexOf("Windows 98") != -1 || version.indexOf("Windows Me") != -1))
  588. {
  589. return true;
  590. }
  591. return false;
  592. }