javascript:SwichData.js

var auxValue = "";
var bufferedObj = null;
var objFrom = null;
var objTo = null;
var undoBuffer;
var undoIndex = 0;

String.prototype.trim = function() { return this.replace(/^/s+|/s+$/, ''); };
undoBuffer = MultiDimensionalArray(200,2);

//main function
function execCommCell(commandValue){
 if(commandValue=="3"){
  undoSwitchData();
  highLightSwitchObject(objFrom,false);
  highLightSwitchObject(objTo,false);
  clearSwitch();
 }
 else if(commandValue=="0"){
  if (objFrom!=null)
   highLightSwitchObject(objFrom,false);
  
  objFrom = hiddenObj;
  bufferedObj = hiddenObj;
  highLightSwitchObject(hiddenObj,false);
  highLightSwitchObject(objFrom,true);
  if(objFrom.tagName == "TD"){
   auxValue = objFrom.fixedCellIndex;
  }
 }
 else if(commandValue=="1"){
  
  if (objFrom==null) return;
  objTo = hiddenObj;
  if (objFrom==objTo) return;
  
  if (objFrom.tagName != objTo.tagName){
   alert("Impossible to change values between selected objects./nThe selected objects are from different types.")
   clearSwitch();
   //hideSwitchCellMenu();
   return;
  }
  
  try{
  
//   if(objFrom.type == "cell"){
//    switchData(objFrom, objTo, true);
//   }
   if (objFrom.tagName == "TD"){
    //updateDatabase(objFrom, objTo);
    
    var tab = searchParentByType(objFrom,"TABLE");
    var intStartRow = 1;
    intStartRow = findStartRow(objFrom);
    if(undoIndex+tab.rows.length-intStartRow >= undoBuffer.length)
           doubleUndoBuffer();
    for(i=intStartRow;i<tab.rows.length;i++){
     try{
         switchData(  tab.rows[i].cells[objFrom.fixedCellIndex],
             tab.rows[i].cells[objTo.fixedCellIndex],   true);
     }
     catch(ex1){
      throw("Error trying to move value from line " + i + "/n");
     }
    }
   }
  }
  catch(ex){
   alert(ex);
  }
  highLightSwitchObject(objTo,false);
  highLightSwitchObject(objFrom,false);
  
  clearSwitch();
  
 }
 //hideSwitchCellMenu();
}

function switchData(oFrom, oTo, logBuffer){
 var strValue;
 var strName;

 try{
  //updateDatabase(oFrom, oTo);
  var inputFrom = searchChildByType(oFrom, "INPUT");
  var inputTo = searchChildByType(oTo, "INPUT");
  var tbl = searchParentByType(oFrom, "TABLE");
  
     if(logBuffer){
    //  if(undoIndex >= undoBuffer.length )
    //   doubleUndoBuffer();
      undoBuffer[undoIndex][0] = oFrom;
      undoBuffer[undoIndex][1] = oTo;
      undoIndex ++;
     }
  
  if (inputFrom.type=="text" && inputTo.type=="text")
  {
//            for (var i=findStartRow(oFrom); i<tbl.rows.length;i++)
//            {
                strValue = inputFrom.value;
                inputFrom.value = inputTo.value;
                inputTo.value = strValue;
//            }
  }
//  strValue = oTo.innerHTML;
//  oTo.innerHTML = oFrom.innerHTML;
//  oFrom.innerHTML = strValue;
 }
 catch(ex){
  throw(ex);
 }
}

function undoSwitchData(){
 if(undoIndex<=0)
  return;
 
 var intStartRow = 1;
 var intI = undoIndex - 1;
 var tab = searchParentByType(undoBuffer[intI][1],"TABLE");
 intStartRow = findStartRow(undoBuffer[intI][1]);
 for(i=intStartRow;i<tab.rows.length;i++){
     try{
         undoIndex --;
         switchData(undoBuffer[undoIndex][1],undoBuffer[undoIndex][0], false);
     }
     catch(ex1){
      throw("Error trying to undo move value from line " + i + "/n");
     }
 }
}

function clearSwitch(){
 highLightSwitchObject(bufferedObj,false);
 highLightSwitchObject(objFrom,false);
 highLightSwitchObject(objTo,false);
 bufferedObj = null;
 objFrom = null;
 objTo = null;
 
 auxValue = "";
}

//high light select
function highLightSwitchObject(obj,selected){
 var color;
 if (obj==null)
  return;
 
// if (obj.type=="cell"){
//  if(!selected){
//   obj.bgColor = obj.previousColor;
//  }
//  else{
//   obj.previousColor = obj.bgColor;
//   obj.bgColor = "#7fb9b1";
//  }
// }
 if (obj.tagName=="TD"){

  var tab = searchParentByType(obj,"TABLE");
  //for(i=0;i<tab.rows.length;i++)
  try{
      for(i=0;i<1;i++)
      {
       if(!selected){
           obj.bgColor = obj.previousColor;
        //tab.rows[i].cells[obj.fixedCellIndex].bgColor = tab.rows[i].cells[obj.fixedCellIndex].previousColor;
       }
       else{
           obj.previousColor = obj.bgColor;
           obj.bgColor = "#7fb9b1";
        //tab.rows[i].cells[obj.fixedCellIndex].previousColor = tab.rows[i].cells[obj.fixedCellIndex].bgColor;
        //tab.rows[i].cells[obj.fixedCellIndex].bgColor = "#7fb9b1";
       }
      }
      }
  catch(ex1){
   throw("Error trying to move value from line " + i + "/n" + ex1);
  }
  
 }
}

function searchParentByType(obj,type){
 if(obj.tagName==type)
  return(obj);
 
 do{
  obj = obj.parentElement;
 }while((obj.tagName!=type)&&(obj!=null))
 
 return(obj);
}

function searchChildByType(obj,type){
 var i;
 
 if(obj.tagName==type)
  return(obj);
 i=0; 
 
 do{
  obj = searchChildByType(obj.all[i],type);
  i++;
 }while((obj.tagName!=type)&&(i<obj.all.length))
 
 return(obj);
}

//initial new undo array
function MultiDimensionalArray(iRows,iCols)
{
    var i;
    var j;
    var a = new Array(iRows);
    for (i=0; i < iRows; i++)
    {
       a[i] = new Array(iCols);
       for (j=0; j < iCols; j++)
       {
           a[i][j] = "";
       }
    }
    return(a);
}

//extend buffer
function doubleUndoBuffer(){
   arr = MultiDimensionalArray(undoBuffer.length * 2,2);
   for (i=0; i < undoBuffer.length ; i++)
   {
    arr[i][0] = undoBuffer[i][0];
    arr[i][1] = undoBuffer[i][1];
   }
   undoBuffer = arr;
   arr = null;
}

function findStartRow(obj)
{
    var tab = searchParentByType(obj,"TABLE");
    var strinstid = tab.rows[0].getAttribute("instanceid");
 if(strinstid==null) DQA,DSP
 {
        if (tab.rows[1] && tab.rows[1].getAttribute("instanceid")=="headerinstance")
        {  
            if (tab.rows[2] && tab.rows[2].getAttribute("instanceid")=="headerinstance")
                return 3;
            else
                return 2;
            }
    }
    else if(strinstid=="headerinstance")//DET
    {
        if (tab.rows[1] && tab.rows[1].getAttribute("instanceid")=="headerinstance")
            return 2;
        else
            return 1;
    }
    return 1;
}


//=================================================================not used=====================================================//
//
function showSwitchCellMenu(){
 var div = document.getElementById("contextmenu"); 
 //if(event.ctrlKey)
 //{
  //bufferedObj = event.srcElement;
        if (event.target) bufferedObj = event.target
        else if (event.srcElement) bufferedObj = event.srcElement 
  
  if(bufferedObj.tagName != "INPUT"){
   bufferedObj = searchParentByType(bufferedObj,"TD");
  }

  highLightSwitchObject(bufferedObj,true);
  
  if (div==null){
   var div = createSwitchCellMenu();
   document.body.appendChild(div);
   if(frameElement!=null)
   {
    if((div.offsetTop + div.offsetHeight) > (frameElement.offsetHeight))
    {
     div.style.top = (frameElement.offsetHeight - div.offsetHeight);
    }
   }
  }
  return(false);
 //}
}

function hideSwitchCellMenu()
{
 var div = document.getElementById("contextmenu"); 
 if(div!=null){
  div.outerHTML = "";
 }
 
 if (bufferedObj!=null)
  highLightSwitchObject(bufferedObj,false);
}

function createSwitchCellMenu(){
 var options,div, tab, tr, td;
 options = new Array(4);
 options[0] = "Switch data from...";
 options[1] = "Switch data to";
 options[2] = "-";
 options[3] = "Undo";
 
 tab = document.createElement("table");
 tab.cellspacing = 0;
 tab.cellppading = 0;
 tab.bgColor = "#DCDCDC";
 tab.style.borderWidth="3px";
 tab.style.borderBottomStyle="outset";
 tab.style.borderLeftStyle="outset";
 //tab.style.borderTopStyle="inset";
 //tab.style.borderRightStyle="inset";
 
 for(i=0;i<options.length;i++){
  tr = tab.insertRow();
  td = tr.insertCell();
  if(options[i] == "-")
   td.innerHTML = "<hr>";
  else{
   td.commandValue = i;
   td.onmousedown = execCommCell;
   td.style.fontFamily="verdana";
   td.style.fontSize="10px";
   td.style.cursor="hand";
   td.innerHTML = options[i];
   if (i==1)
    td.disabled = ((i == 1)&&(objFrom == null));
   else if (i==3)
    td.disabled = (undoIndex<=0);
  
  }
 }
 div = document.createElement("DIV");
 div.id = "contextmenu";
 div.style.position="absolute";
 div.style.top = event.clientY + document.body.scrollTop;
 div.style.left = event.clientX + document.body.scrollLeft;
 div.appendChild(tab);
 return(div);
}

function initCellSwitcher(){
   
 undoBuffer = MultiDimensionalArray(200,2);
 /*
 for(i=0;i<document.all.length;i++){
  if (document.all[i].tagName=="TD"){
   if ((document.all[i].type=="columnHeader")||(document.all[i].type=="cell")){
    document.all[i].oncontextmenu = showSwitchCellMenu;
    document.all[i].style.cursor="hand";
   }
  }
 }
 */
    showSwitchCellMenu();
 document.onmouseup = hideSwitchCellMenu;
}


function updateDatabase(oFrom, oTo){
 
 var url;
 url = new Array(3);
 
 if (oFrom.type != oTo.type)
  throw("Aborting database operation./nThe selected objects are from different types.")
  
 if (oFrom.table != oTo.table)
  throw("Operation not allowed between different tables.")
 
 var fromPerson, toPerson;
 
 fromPerson = new String(oFrom.person);
 toPerson = new String(oTo.person);
 
 fromValue = new String(oFrom.innerText);
 toValue = new String(oTo.innerText);
  
 url[0] = "ajaxStoredProcedure.asp?stmtID=1";
 url[0] += "&p1="  + oFrom.dataSource;
 url[0] += "&p2="  + fromValue.trim();
 url[0] += "&p3="  + toValue.trim();
 url[0] += "&p4="  + oFrom.sequential;
 url[0] += "&p5="  + oTo.sequential;
 url[0] += "&p6="  + fromPerson;
 url[0] += "&p7="  + toPerson;
 url[0] += "&f1="  + oFrom.table;
 url[0] += "&f2="  + oFrom.columnName;
 url[0] += "&f3="  + oTo.columnName;
 url[0] += "&f4="  + oTo.pkField;
 
 url[1] = "ajaxStoredProcedure.asp?stmtID=2";
 url[1] += "&p1="  + oFrom.dataSource;
 url[1] += "&f1="  + oFrom.columnName;
 url[1] += "&f2="  + oTo.columnName;
 
 var spIndex = -1;
 
 if (oFrom.type=="cell")
  spIndex = 0;
 else if (oFrom.type=="columnHeader")
  spIndex = 1;
  
 if(spIndex<0)
  throw("Aborting database operation./nXML URL was not especified.");
  
 xml_doc = new ActiveXObject("Microsoft.XMLDOM");
 xml_doc.async = false;
 //window.open (url[spIndex]);
 //return;
 xml_doc.load(url[spIndex]);
 
 var errNum = xml_doc.documentElement.selectSingleNode("ERROR_NUMBER").text;
 var errDesc = xml_doc.documentElement.selectSingleNode("ERROR_MESSAGE").text;
 var sql = xml_doc.documentElement.selectSingleNode("SQL_STATEMENT").text;
 
 if (errNum!="0"){
  throw("Error trying to switch data on database./nOperation aborted./n/n"+errDesc);
  return(false);
 }
 
 if (oFrom.type=="columnHeader")
 {
  var str = searchChildByType(oFrom,"FONT").color;
  searchChildByType(oFrom,"FONT").color = searchChildByType(oTo,"FONT").color;
  searchChildByType(oTo,"FONT").color = str;
 }
 
 return(true);
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值