论坛首页 Web前端技术论坛

javascript undo redo

浏览 1396 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-12-09  
在浏览器中用调试工具看调用结果

<!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[0]),indicator = arguments.callee;
		indicator.stack = [];
		indicator.stack[0] = temp;
	}
	a({c:1,b:2}, 123, function(arg) {
		return arg+1;
	});
	function dir() {
		console.log(arguments[0]);
	}
	dir(Do.stack[0][2](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;
		}
		fn.sign=function(){
			return ('abcdefghijk'[Math.random().toFixed(1) * 10]+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[0][arguments[0].length-1],data;
			if (socureCall.arguments.length !== arguments[0].length || indicator.keys[flag]==='used') return;

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

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

			indicator.keys[this.sn]='used';

			this.index=this.stack.length;
		}
		fn.redo=function(){
			//var data=indicator.stack[indicator.stack.length-1];
			//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[indicator.stack.length-1];
			//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[i++];
				temp[1].apply(undefined,temp[0]);
			}
		}


		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>


论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics