feiyang10086 发表于 2011-12-08 19:44

javascript undo redo

javascript undo redo











在浏览器中用调试工具看调用结果

Javascript代码<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
<html xmlns="http://www.w3.org/1999/xhtml">   
<head>   
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>   
    <title>javascript</title>   
</head>   

<body>   
<input type="button" value=" - " id="a" /> <input type="button" value=" + " id="b" />   

<input type="button" value=" - " id="c" /> <input type="button" value=" + " id="d" />   
<script type="text/javascript">   
    /*function a() {
      Do([].slice.call(arguments));
    }
    function Do() {
      var temp = (arguments),indicator = arguments.callee;
      indicator.stack = [];
      indicator.stack = temp;
    }
    a({c:1,b:2}, 123, function(arg) {
      return arg+1;
    });
    function dir() {
      console.log(arguments);
    }
    dir(Do.stack(3));*/

    function MemoryDo(step){   

      var indicator,that,fn,slice,args;   
      (   
            indicator=arguments.callee,   
            slice=[].slice   
      )   
      if(!(this instanceof indicator)){return new indicator(step);}   

      indicator.keys={};   
      that=this;   
      this.stack=[];   
      this.index=0;   

         
      fn = indicator.prototype;   
      fn.constructor = indicator;   

      fn.is=function(o){   
            return ({}).toString.call(o).slice(8, -1);   
      }   
      fn.slice=function(o,index){   
            return slice.call(o,index || 0);   
      }   
      fn.setIndex=function(number){   
            if(number<0 || this.is(this.index)!=='Number'){return};   
            this.index=number;   
      }   
      fn.getIndex=function(){   
            return this.is(this.index)==='Number' ? this.index : null;   
      }   
      var _temp = 200;   
      fn.sign=function(){   
            return ('abcdefghijk'+this.randomNumber(10000,10000000));   
      }   
      fn.randomNumber=function(a,b){   
            return Math.round(Math.random()*b+a);   
      }   
      fn.dataBuffer=function(){   
            var temp,socureCall=arguments.callee.caller,flag=arguments.length-1],data;   
            if (socureCall.arguments.length !== arguments.length || indicator.keys==='used') return;   

            data=this.stack;   
            if(data.length===this.step){data.shift()};   

            temp=data=[];   
            temp=(this.slice(arguments,0)).concat(this.sn);   
            temp=arguments.callee.caller;   

            indicator.keys='used';   

            this.index=this.stack.length;   
      }   
      fn.redo=function(){   
            //var data=indicator.stack;   
            //console.log(data);   
            if(this.index===this.stack.length){   
                this.re=-1;return;   
            }else{   
                this.re=1;   
                this.virtualDo(this.index+=1);   
            }   

      }   
      fn.undo=function(){   
            //var data=indicator.stack;   
            //console.log(data);   
               
            if(this.index===0){   
                return;   
            }else{   
                this.un=1;   
                this.virtualDo(this.index-=1);   
            }   

      }   
      fn.virtualDo=function(){   
            var data=this.stack,i=0,len=this.index,temp;   
            for(;i<len;){   
                temp=data;   
                temp.apply(undefined,temp);   
            }   
      }   


      this.step=this.is(step)==='Number'? step : 10;   
      this.sn=this.sign();   
         
    }   


var a=MemoryDo(4);   
var b=MemoryDo(2);   
function doIt(o,fn){   
    a.dataBuffer(arguments);   

    fn(arguments);   
    b.dataBuffer(arguments);   
}   
doIt({a:1,b:2,c:3},function(a){   
    console.dir(a);   
});   
doIt({b:2,c:3},function(a){   
    console.dir(a);   
});   
doIt({c:3},function(a){   
    console.dir(a);   
});   
doIt({d:4},function(a){   
    console.dir(a);   
});   
document.getElementById('a').onclick=function(){   
    a.undo();   
}   
document.getElementById('b').onclick=function(){   
    a.redo();   
}   
document.getElementById('c').onclick=function(){   
    b.undo();   
}   
document.getElementById('d').onclick=function(){   
    b.redo();   
}   
</script>   
</body>   
</html>

冰释一片天 发表于 2011-12-24 20:43

谢谢分享希望于楼主多多交流
页: [1]
查看完整版本: javascript undo redo