Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.undo.Manager");
14
dojo.require("dojo.lang.common");
15
dojo.undo.Manager = function (parent) {
16
	this.clear();
17
	this._parent = parent;
18
};
19
dojo.extend(dojo.undo.Manager, {_parent:null, _undoStack:null, _redoStack:null, _currentManager:null, canUndo:false, canRedo:false, isUndoing:false, isRedoing:false, onUndo:function (manager, item) {
20
}, onRedo:function (manager, item) {
21
}, onUndoAny:function (manager, item) {
22
}, onRedoAny:function (manager, item) {
23
}, _updateStatus:function () {
24
	this.canUndo = this._undoStack.length > 0;
25
	this.canRedo = this._redoStack.length > 0;
26
}, clear:function () {
27
	this._undoStack = [];
28
	this._redoStack = [];
29
	this._currentManager = this;
30
	this.isUndoing = false;
31
	this.isRedoing = false;
32
	this._updateStatus();
33
}, undo:function () {
34
	if (!this.canUndo) {
35
		return false;
36
	}
37
	this.endAllTransactions();
38
	this.isUndoing = true;
39
	var top = this._undoStack.pop();
40
	if (top instanceof dojo.undo.Manager) {
41
		top.undoAll();
42
	} else {
43
		top.undo();
44
	}
45
	if (top.redo) {
46
		this._redoStack.push(top);
47
	}
48
	this.isUndoing = false;
49
	this._updateStatus();
50
	this.onUndo(this, top);
51
	if (!(top instanceof dojo.undo.Manager)) {
52
		this.getTop().onUndoAny(this, top);
53
	}
54
	return true;
55
}, redo:function () {
56
	if (!this.canRedo) {
57
		return false;
58
	}
59
	this.isRedoing = true;
60
	var top = this._redoStack.pop();
61
	if (top instanceof dojo.undo.Manager) {
62
		top.redoAll();
63
	} else {
64
		top.redo();
65
	}
66
	this._undoStack.push(top);
67
	this.isRedoing = false;
68
	this._updateStatus();
69
	this.onRedo(this, top);
70
	if (!(top instanceof dojo.undo.Manager)) {
71
		this.getTop().onRedoAny(this, top);
72
	}
73
	return true;
74
}, undoAll:function () {
75
	while (this._undoStack.length > 0) {
76
		this.undo();
77
	}
78
}, redoAll:function () {
79
	while (this._redoStack.length > 0) {
80
		this.redo();
81
	}
82
}, push:function (undo, redo, description) {
83
	if (!undo) {
84
		return;
85
	}
86
	if (this._currentManager == this) {
87
		this._undoStack.push({undo:undo, redo:redo, description:description});
88
	} else {
89
		this._currentManager.push.apply(this._currentManager, arguments);
90
	}
91
	this._redoStack = [];
92
	this._updateStatus();
93
}, concat:function (manager) {
94
	if (!manager) {
95
		return;
96
	}
97
	if (this._currentManager == this) {
98
		for (var x = 0; x < manager._undoStack.length; x++) {
99
			this._undoStack.push(manager._undoStack[x]);
100
		}
101
		if (manager._undoStack.length > 0) {
102
			this._redoStack = [];
103
		}
104
		this._updateStatus();
105
	} else {
106
		this._currentManager.concat.apply(this._currentManager, arguments);
107
	}
108
}, beginTransaction:function (description) {
109
	if (this._currentManager == this) {
110
		var mgr = new dojo.undo.Manager(this);
111
		mgr.description = description ? description : "";
112
		this._undoStack.push(mgr);
113
		this._currentManager = mgr;
114
		return mgr;
115
	} else {
116
		this._currentManager = this._currentManager.beginTransaction.apply(this._currentManager, arguments);
117
	}
118
}, endTransaction:function (flatten) {
119
	if (this._currentManager == this) {
120
		if (this._parent) {
121
			this._parent._currentManager = this._parent;
122
			if (this._undoStack.length == 0 || flatten) {
123
				var idx = dojo.lang.find(this._parent._undoStack, this);
124
				if (idx >= 0) {
125
					this._parent._undoStack.splice(idx, 1);
126
					if (flatten) {
127
						for (var x = 0; x < this._undoStack.length; x++) {
128
							this._parent._undoStack.splice(idx++, 0, this._undoStack[x]);
129
						}
130
						this._updateStatus();
131
					}
132
				}
133
			}
134
			return this._parent;
135
		}
136
	} else {
137
		this._currentManager = this._currentManager.endTransaction.apply(this._currentManager, arguments);
138
	}
139
}, endAllTransactions:function () {
140
	while (this._currentManager != this) {
141
		this.endTransaction();
142
	}
143
}, getTop:function () {
144
	if (this._parent) {
145
		return this._parent.getTop();
146
	} else {
147
		return this;
148
	}
149
}});
150