<SCRIPT LANGUAGE="JavaScript">
<!--
var actionStack = [];//操作栈
var actionIndex = 0;//操作栈中当前操作的指针
//----------------------- command对象基类 ------------------------------
function BaseAction(){
}
BaseAction.prototype.exec=function(){
actionStack[actionIndex++] = this;
}
BaseAction.prototype.undo=function(){
alert("此操作未定义相应的undo操作");
}
BaseAction.prototype.redo=function(){
alert("此操作未定义相应的redo操作");
}
//----------------------- change操作的command对象 ------------------------------
function ChangeAction(elm){
this.sourceElement = elm;
this.oldValue = elm.defaultValue;
this.newValue = elm.value;
elm.defaultValue = elm.value;
this.status = "done";
}
ChangeAction.prototype = new BaseAction();
ChangeAction.prototype.undo = function(){
if (this.status != "done") return;
this.sourceElement.value = this.sourceElement.defaultValue = this.oldValue;
this.status = "undone";
}
ChangeAction.prototype.redo = function(){
if (this.status != "undone") return;
this.sourceElement.value = this.newValue;
this.status = "done";
}
ChangeAction.prototype.exec=function(){
actionStack[actionIndex++] = this;
}
//--------------------- 全局函数 ----------------------------
function undo(){
if (actionIndex>0){
actionStack[--actionIndex].undo();
}
}
function redo(){
if (actionIndex<actionStack.length){
actionStack[actionIndex++].redo();
}
}
function changeValue(){
new ChangeAction(event.srcElement).exec();
}
//-->
<!--
Function.prototype.bind = function(object) {
var __method = this;
return function() {
__method.apply(object, arguments);
}
}
String.prototype.hasSubString=function(s,f){
if(!f) f="";
return (f+this+f).indexOf(f+s+f)==-1?false:true;
}
/*
浏览器类型判断;
*/
var Browse = (new function(){
this.isIE = function(){return navigator.userAgent.hasSubString("MSIE");}
this.isFF = function(){return navigator.userAgent.hasSubString("Firefox");}
this.isOpera = function(){return navigator.userAgent.hasSubString("Opera")}
this.isSafari = function(){return navigator.userAgent.hasSubString("Safari");}
this.isGecko = function(){return navigator.userAgent.hasSubString("Gecko");}
this.IEVer = function(){return $.Browse.isIE() ? parseInt(navigator.userAgent.split(";")[1].trim().split(" ")[1]) : 0;}
})
/*
热键管理器;
*/
var HotKeyManager = (new function(){
this._temp_key_list = {
8:"Backspace",
9:"Tab",
13:"Enter",
16:"Shift",
17:"Ctrl",
18:"Alt",
19:"Pause",
20:"CapsLock",
27:"Esc",
32:"Space",
33:"PageUp",
34:"PageDown",
35:"End",
36:"Home",
37:"Left",
38:"Up",
39:"Right",
40:"Down",
45:"Insert",
46:"Delete",
110:"Del",
112:"F1",
113:"F2",
114:"F3",
115:"F4",
116:"F5",
117:"F6",
118:"F7",
119:"F8",
120:"F9",
121:"F10",
122:"F11",
123:"F12"
};
/*
保存被允许触发的热键列;
*/
this.KeyListeners = [];
/*
存放热键的临时栈;
*/
this.KeyStock = [];
/*
@public
@parameters
keyCode number 按键的数值;
@return
returnValue string ascii字符;
func: change event.keyCode to ascii string!
*/
this.getCharCode=function(keyCode){
var fstr = this._temp_key_list[keyCode];
if(fstr==undefined){
return String.fromCharCode(keyCode);
}else{
return fstr;
}
}
this.Execute=function(evt){
var keystr = this.KeyStock.join("+");
if(!keystr.hasSubString(this.getCharCode(evt.keyCode))){
this.KeyStock.push(this.getCharCode(evt.keyCode));
}
this.ExecuteHotKeyFunction();
}
/*
@public 设置热键监听环境并开始监听;
*/
this.beginListener=function(){
if(document.οnkeydοwn==null){
document.οnkeydοwn=function(evt){
evt = evt || window.event;
this.Execute(evt);
}.bind(this)
document.οnkeyup=function(evt){
evt = evt || window.event;
this.KeyStock.pop();
}.bind(this)
}else{
if(Browse.isIE()){
document.attachEvent("onkeydown",function(){
this.Execute(window.event);
}.bind(this))
}else if(Browse.isFF()){
document.addEventListener("keydown",function(evt){
this.Execute(evt);
}.bind(this),false)
}
}
}
/*
@public
@parameters
arrHK array 热键组合
funExecute function 要执行的内容
*/
this.addHotKey=function(arrHK,funExecute){
if(typeof arrHK == "string"){
this.KeyListeners[arrHK] = funExecute;
}else if((typeof(arrHK)=="object")&&(arrHK.constructor==Array)){
this.KeyListeners[arrHK.join("+")] = funExecute;
}
}
this.delHotKey=function(arrHK){
if(typeof arrHK == "string"){
this.KeyListeners[arrHK] = null;
delete this.KeyListeners[arrHK];
}else if((typeof(arrHK)=="object")&&(arrHK.constructor==Array)){
this.KeyListeners[arrHK.join("+")] = null;
delete this.KeyListeners[arrHK.join("+")];
}
}
/*
@private
*/
this.ExecuteHotKeyFunction=function(){
var keystr = this.KeyStock.join("+");
try{
this.KeyListeners[keystr]();
this.KeyStock = [];
}catch(e){}
}
//*===========================================================================;
})
window.οnlοad=function(){
//--当存在其他keydown事件测试--;
/*document.οnkeydοwn=function(evt){
evt = evt || window.event;
window.status = evt.keyCode;
}*/
HotKeyManager.addHotKey(["Ctrl","Z"],function(){undo()})
HotKeyManager.addHotKey(["Ctrl","Y"],function(){redo()})
HotKeyManager.beginListener();
}
</SCRIPT>
</HEAD>
<BODY >
<input value="drag me" οnchange="changeValue()" style="position:absolute;left:150;color:blue">
<input value="drag me" οnchange="changeValue()" style="position:absolute;left:350;color:green">
<input value="drag me" οnchange="changeValue()" style="position:absolute;left:550;color:violet">
</BODY>
</HTML>