Hashtable

<html>
<head>
<title> New Document </title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<style>
 .dwStyle         { width:400px;font-family:宋体; font-size:12px; border:solid 1 #D3D3D3; border-width:1 0 1 1;}
 .dwStyle div     { width:100%; height:400px; overflow:auto; border-right: 1px solid #D3D3D3; border-bottom: 1px solid #E8F4FF;  }
 .dwTHStyle       { table-layout:fixed; border:0; }
 .dwTHStyle    tr { height:20px; background-color:#F5F5F5; }
 .dwTHStyle    th { width:80px;  border:solid 1 #D3D3D3; border-width:0 1 1 0; }
 .dwTHStyle  span { width:100%; height:100%; font-size:14px; font-weight:500; color:Darkblue; text-align:center; padding-top:3; cursor:hand; }
 .dwTBStyle      { table-layout:fixed; border:0; }
 .dwTBStyle   tr { height:20px; background-color:white;}
 .dwTBStyle   th { background-color:#F5F5F5; width:40px}
 .dwTBStyle   td { width:80px; border:solid 1 red; border-color:white #ADD8E6 #ADD8E6 white;  }
 .dwTBStyle    div{ width:100%; height:100%; overflow:hidden;text-overflow:clip; border-width:0; }
</style>
<script language="JavaScript">
/********************************************************
 *  功能:Hashtable          *

 *  使用方法: var yourVariable = new Hashtable();  *
 *  实现了如下方法:          *
 *  ◆put(key,value)          *
 *  ◆get(key,[defaultValue]),其中defaultValue参数可选 *
 *   无defaultValue,get没有匹配时返回null    *
 * ◆remove(key),成功返回ture,失败返回false(要求IE5.5) *
 * ◆clear()           *
 * ◆containsKey(key),包含返回ture,否则返回false   *
 * ◆containsValue(value),包含返回ture,否则返回false  *
 * ◆size(),返回hashtable中数据总数                 *
 ********************************************************/ 
function Hashtable()
{
 this.hash = new ActiveXObject("Scripting.Dictionary");
 this.put = put;
 this.get = get;
 this.keys = keys;
 this.values = values;
 this.clear = clear;
 this.remove = remove;
 this.containsKey = containsKey;
 this.containsValue = containsValue;
 this.size = size;
 function put(key,value) { if (this.hash.Exists(key)) this.remove(key); this.hash.Add(key,value); }
 function get(key) { if(typeof( this.hash.Item(key)) != 'undefined') return this.hash.Item(key); return (arguments.length==2) ? arguments[1] : null; }
 function remove(key) { if (this.hash.Exists(key)) this.hash.Remove(key); }
 function keys() { return (new VBArray(this.hash.Keys())).toArray(); }
 function values() { return (new VBArray(this.hash.Items())).toArray(); }
 function clear() { this.hash.RemoveAll(); }
 function containsKey(key) { return (this.hash.Exists(key)) ? true : false; }
 function containsValue(value) { var values = (new VBArray(this.hash.Items())).toArray(); for (var i=0; i<values.length; i++) if(values[i] == value) return true; return false; }
 function size() { return this.hash.Count; }
}
</script>
</head>
<body>
<!----------------------------------------------------------------------------------------------------->
<table cellPadding=0 cellSpacing=0 class=dwStyle   onselectstart='return false'>
<tr><td>
<!----------------------------------------------------------------------------------------------------->
<table cellPadding=0 cellSpacing=0 class=dwTHStyle>
<thead><tr>
  <th style="width:40"><span></span></th>
  <script language=vbscript>
   function getLetter(i)
    getLetter=Chr(i)
   end function
  </script>
  <script language="JavaScript">
  <!--
   var width = '';
//   for(var i=65; i<91; i++) {
   for(var i=65; i<70; i++){
    //if(i==90) width = ' style="width:100%"';
    document.write("<th" + width + "><span>" + getLetter(i) + "</span></th>");
   }
  //-->
  </script>
 </tr>
 </thead>
</table>
<!----------------------------------------------------------------------------------------------------->
</td></tr><tr><td><div>
<!----------------------------------------------------------------------------------------------------->
<table id=t1 cellPadding=0 cellSpacing=0 class=dwTHStyle>
 <tbody  class= dwTBStyle ondrag="return false"  onmousedown='down()' onmouseover='move()' onmouseup='up()' onselectstart='return false' >
 <script language="JavaScript">
 <!--
//  for(var i=1; i<60; i++){
  for(var i=1; i<10; i++){
   document.write("<tr><th><span>" + i + "</span></th>");
//   for(var j=0; j<26; j++){
   for(var j=0; j<5; j++){
    document.write("<td><div></div></td>");
   }
   document.write("</tr>");
  }
 //-->
 </script>
 </tbody>
</table>
<!----------------------------------------------------------------------------------------------------->
</div></td></tr></table>
<!----------------------------------------------------------------------------------------------------->
<button onclick='selected_area.drawNet()'>网</button>
<button onclick='selected_area.drawRect()'>框</button>
<button onclick='selected_area.drawTopLine()'>上</button>
<button onclick='selected_area.drawBottomLine()'>下</button>
<button onclick='selected_area.drawLeftLine()'>左</button>
<button onclick='selected_area.drawRightLine()'>右</button>
<button onclick='selected_area.drawNone()'>无</button>
<button onclick='selected_area.unitTD()'>合并</button>
<div style="position:absolute; left:100; top:300" id= ohoh></div>
<div style="position:absolute; left:300; top:300" id= ohoh1></div>
<div style="position:absolute; left:500; top:400" id= ohoh2></div>
<div style="position:absolute; left:500; top:430" id= ohoh3></div>
<div style="position:absolute; left:50; top:450" id= ohoh4></div>
<div style="position:absolute; left:500; top:450" id= ohoh5></div>
<input type=button onclick=window.location="view-source:"+document.location.href>
<div id = hehe style="position:absolute;background-color:yellow; FILTER: Alpha( style=0,opacity=25);display:none;font-size:9pt" onmousemove='move()' onmousedown='down()' onmouseup='up()' onselectstart='return false'></div>
<script language="JavaScript">
<!--
 var tbs = new Hashtable();
 var unit_cells = new Hashtable();
 var unit_objs = new Hashtable();
 var n = 0;
 var tbObj = t1;
 Array.prototype.digitalsort=function(){this.sort(new Function("a","b","return digitalcomp(a,b,'1')"));}
function coordinates(x,y,sx,sy,ex,ey)
{
 this.x = x;
 this.y = y;
 this.sx = sx;
 this.sy = sy;
 this.ex = ex;
 this.ey = ey;
}
//----------------------------------------------------------------------------
function cal_coordinates(){
 
 var first_cell = tbObj.rows[0].cells[1];
 var sx = fnGetPosition(first_cell,'Left');
 var sy = fnGetPosition(first_cell,'Top');
 var width = first_cell.offsetWidth;
 var height= first_cell.offsetHeight;
 var height0= 0;
 
 tbs.put('width', width);
 tbs.put('height', height);
 for(var i=0; i<tbObj.rows.length; i++){
  var width0 = 0;
  for(var j=1; j<tbObj.rows[0].cells.length; j++){
   var key = i + '-' + j;
   var col = new coordinates(0,0,0,0,0,0);
   col.x = i;
   col.y = j;
   col.sx = sx + width0;
   col.sy = sy + height0;
   col.ex = col.sx + width;
   col.ey = col.sy + height;
//  ohoh.innerHTML = ohoh.innerHTML + col.x + ',' + col.y + ',' + col.sx + ',' + col.sy + ',' + col.ex + ',' + col.ey + '<br>';
   width0 += width;
   
   tbs.put(key, col);
  }
  height0 += height;
 }
}
function cal_time()
{
   var d, s = "Time: ";
   var c = ":";
   d = new Date();
   s += d.getHours() + c;
   s += d.getMinutes() + c;
   s += d.getSeconds() + c;
   s += d.getMilliseconds();
   return(s);
}
ohoh.innerText = cal_time();
cal_coordinates();
ohoh1.innerText = cal_time();
//alert(tbs.size());
//----------------------------------------------------------------------------
function selectedArea(sObj, eObj, lineColor)
{
 this.sObj = sObj;
 this.eObj = eObj;
 this.lineColor = lineColor;
 
 var scol = 0;
 var srow = 0;
 var ecol = 0;
 var erow = 0;
 var startObj = null;
 this.show = show;
 this.drawNet = drawNet;
 this.drawRect = drawRect;
 this.drawTopLine = drawTopLine;
 this.drawBottomLine = drawBottomLine;
 this.drawLeftLine = drawLeftLine;
 this.drawRightLine = drawRightLine;
 this.drawNone = drawNone;
 this.unitTD = unitTD;
 function xy(row,col){
  this.row = row;
  this.col = col;
 }
 function unitTD()
 {
  var delObjs = new Array();
  var selObjs = new Array();
  var n=0;
  var m=0;
  var unit_obj_name = srow+'.'+scol+'-'+erow+'.'+ecol;
//alert('srow='+ srow + ',scol=' + scol + 'erow='+ erow + ',ecol=' + ecol);
  for(var i = srow; i<=erow; i++)
  for(var j = scol; j<=ecol; j++) {
   var key = i + '-' + j;
   if(unit_cells.containsKey(key)){
    var value = unit_cells.get(key);
    var x_y = unit_objs.get(value);
    if(x_y!=null) {
     var rows = new Array();
     var cols = new Array();
     for(var k=0; k<x_y.length; k++){
      rows[k] = x_y[k].row;
      cols[k] = x_y[k].col;
     }
     rows.digitalsort(); 
     cols.digitalsort();
     delObjs[n++] = tbObj.rows[rows[0]].cells[cols[0]];
     unit_objs.remove(value);
    }
   }else{
    delObjs[n++] = tbObj.rows[i].cells[j];
   }
  
   var x_y = new xy(i,j);
   //alert(x_y.row + '|' +  x_y.col);
   selObjs[m++] = x_y; 
//ohoh5.innerHTML = ohoh5.innerHTML + i+'-'+j + '<br>';
   unit_cells.put(i+'-'+j , unit_obj_name);
  }
//alert(2);
  unit_objs.put(unit_obj_name,selObjs);
//alert(3);
  for(var i=1; i<delObjs.length; i++){
   delObjs[i].removeNode(1);
  }
  ohoh5.innerText = 'srow=' + srow + ',scol=' + scol + '|erow=' + erow + ',ecol=' + ecol;
  delObjs[0].colSpan = ecol-scol + 1;
  delObjs[0].rowSpan = erow-srow + 1;
 }
 
 function show()
 {
//  if(this.sObj == null || this.eObj == null) return;
  var n = 0;
  var m = 0;
  var rows = new Array();
  var cols = new Array();
  function cal_row_col(){ 
   rows.digitalsort(); 
   cols.digitalsort(); 
   srow = rows[0]; 
   erow = rows[rows.length-1]; 
   scol = cols[0]; 
   ecol = cols[cols.length-1]; 
  }
  function cal_contain_cell(key){
   if(!unit_cells.containsKey(key)) return;
   var value = unit_cells.get(key);
   var x_y = unit_objs.get(value);
   for(var k=0; k<x_y.length; k++){
    rows[n++] = x_y[k].row;
    cols[m++] = x_y[k].col; 
   }
  }
  function cal_contain(){
   if(unit_cells.size()!=0){
    for(var i=scol; i<=ecol; i++) {
     var key1 = srow + '-' + i;
     var key2 = erow + '-' + i;
     cal_contain_cell(key1);
     cal_contain_cell(key2);
    }
    for(var i=srow; i<=erow; i++) {
     var key1 = i + '-' + scol;
     var key2 = i + '-' + ecol;
     cal_contain_cell(key1);
     cal_contain_cell(key2);
    }
    cal_row_col();
   }
  }
  rows[n++] = this.sObj.x;
  rows[n++] = this.eObj.x;
  cols[m++] = this.sObj.y;
  cols[m++] = this.eObj.y;
  cal_row_col();
//ohoh.innerText= 'ohoh:' + srow + ',' + scol + ',' + erow + ',' + ecol;
  cal_contain();
  cal_contain();
//ohoh1.innerText= 'ohoh1:' + srow + ',' + scol + ',' + erow + ',' + ecol;
  startObj = tbs.get(srow + '-' + scol);
  var width = 0;
  var height = 0;
  for(var i=scol; i<=ecol; i++) width += tbs.get('width');
  for(var i=srow; i<=erow; i++) height += tbs.get('height');
  hehe.style.left = startObj.sx;
  hehe.style.top = startObj.sy;
  hehe.style.width = width;
  hehe.style.height = height;
  hehe.style.display = 'block';
 }
 function drawNet()//网
 {
  for(var i = srow; i<=erow; i++)
  for(var j = scol; j<=ecol; j++){
   tbObj.rows[i].cells[j].style.borderColor = 'white ' + this.lineColor + ' ' + this.lineColor + ' white';
  }
  this.drawTopLine();
  this.drawLeftLine();
 }
 function drawNone()//无
 {
  for(var i = srow; i<=erow; i++)
  for(var j = scol; j<=ecol; j++){
   tbObj.rows[i].cells[j].style.borderColor = 'white #ADD8E6 #ADD8E6 white'; 
  }
  if( srow-1 > 0){
   for(var j = scol; j<=ecol; j++) tbObj.rows[srow-1].cells[j].style.borderBottomColor = '#ADD8E6';
  }
  if( scol-1 > 0){
   for(var j = srow; j<=erow; j++) tbObj.rows[j].cells[scol-1].style.borderRightColor = '#ADD8E6';
  }
 }
 function drawRect() //框
 {
  this.drawTopLine();
  this.drawBottomLine();
  this.drawLeftLine();
  this.drawRightLine();
 }
 function drawTopLine() //上边
 {
  if( srow-1 > 0){
   for(var j = scol; j<=ecol; j++) tbObj.rows[srow-1].cells[j].style.borderBottomColor = this.lineColor;
  }
 }
 function drawBottomLine() //下边
 {
  for(var i = scol; i<=ecol; i++){
   tbObj.rows[erow].cells[i].style.borderBottomColor = this.lineColor;
  }
 }
 function drawLeftLine() //左边
 {
  if( scol-1 > 0){
   for(var j = srow; j<=erow; j++) tbObj.rows[j].cells[scol-1].style.borderRightColor = this.lineColor;
  }
 }
 function drawRightLine() //右边
 {
  for(var i = srow; i<=erow; i++){
   tbObj.rows[i].cells[ecol].style.borderRightColor = this.lineColor;
  }
 }
}
var selected_area = new selectedArea(null,null,'blue');
function aa()
{
 a1.colSpan = 2;
 a2.removeNode(1);
 a3.rowSpan = 2;
 a7.removeNode(1);
}
function getParentNodeByTagName(node,tagName)
{
 while(node.tagName != tagName) node = node.parentElement;
 return node;
}
function down()
{
 n=1;
 getFocusObj(true);
 selected_area.show();
// tbObj.setCapture();
//alert(tbs.size());
}
function up()
{
 n=0;
// tbObj.releaseCapture();
}
function move()
{
 if(n!=1) return;
 getFocusObj(false);
 selected_area.show();
}
function getFocusObj(kind){
 var x = event.clientX + document.body.scrollLeft;
 var y = event.clientY + document.body.scrollTop;
 var key = null;
 var col = null;
 for(var i=0; i<tbObj.rows.length; i++)
 for(var j=1; j<tbObj.rows[0].cells.length; j++){
  key = i + '-' + j;
  col = tbs.get(key);
  if(x > col.sx & y > col.sy && x < col.ex & y < col.ey){
   if(kind) selected_area.sObj = col;
   selected_area.eObj = col;
   break;
  }//end if
 }
}
function digitalcomp(x,y,flag)
{
 if(flag!=1) return Number(y)-Number(x);
 else return Number(x)-Number(y);
}
function fnGetPosition(obj,direction)
{
    var objPosition=0;
    while (obj !=null){
      objPosition+=obj["offset"+direction];
      obj=obj.offsetParent;
    }
    return objPosition;
}
//-->
</script>
</body>
</html>
posted @ 2010-04-11 21:33  夜色狼  阅读(240)  评论(0编辑  收藏  举报