forked from ascr-ecx/colormoves
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActionManager.js
83 lines (76 loc) · 2.19 KB
/
ActionManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var HIDE_UNDO_REDO_BUTTONS = false;
function ActionManager(_cmdUndo, _cmdUndo2, _cmdRedo, _cmdRedo2)
{
var registered = [];
var history = {prev: null, next: null, action: null};
var cmdUndo = _cmdUndo, cmdUndo2 = _cmdUndo2, cmdRedo = _cmdRedo, cmdRedo2 = _cmdRedo2;
this.register = function(name, dofunc, undofunc, redofunc) // redofunc ... optional
{
registered[name] = {name, dofunc, undofunc, redofunc};
}
this.perform = function(name, params)
{
var action = registered[name];
if(action == null)
return;
history = (history.next = {prev: history, next: null, action: action, doParams: params});
history.undoParams = action.dofunc(params);
this.UpdateUndoRedoButtons();
//console.log("DO: " + name + "(" + params + ")");
}
this.undo = function()
{
if(this.canUndo())
{
if(history.action.redofunc)
history.doParams = history.action.undofunc(history.undoParams);
else
history.action.undofunc(history.undoParams);
//console.log("UNDO: " + history.action.name + "(" + history.undoParams + ")");
history = history.prev;
this.UpdateUndoRedoButtons();
}
}
this.redo = function()
{
if(this.canRedo())
{
history = history.next;
history.undoParams = history.action.redofunc ? history.action.redofunc(history.doParams) : history.action.dofunc(history.doParams);
this.UpdateUndoRedoButtons();
//console.log("REDO: " + history.action.name + "(" + history.doParams + ")");
}
}
this.canUndo = function()
{
return history.action !== null;
}
this.canRedo = function()
{
return history.next !== null;
}
this.UpdateUndoRedoButtons = function()
{
if(this.canUndo())
{
cmdUndo2.style.display = 'none';
cmdUndo.style.display = HIDE_UNDO_REDO_BUTTONS === true ? 'none' : 'inline-block';
}
else
{
cmdUndo.style.display = 'none';
cmdUndo2.style.display = HIDE_UNDO_REDO_BUTTONS === true ? 'none' : 'inline-block';
}
if(this.canRedo())
{
cmdRedo2.style.display = 'none';
cmdRedo.style.display = HIDE_UNDO_REDO_BUTTONS === true ? 'none' : 'inline-block';
}
else
{
cmdRedo.style.display = 'none';
cmdRedo2.style.display = HIDE_UNDO_REDO_BUTTONS === true ? 'none' : 'inline-block';
}
}
this.UpdateUndoRedoButtons();
}