Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | 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
 
11
dojo.provide("dojo.undo.Manager");
12
dojo.require("dojo.lang.common");
13
dojo.undo.Manager = function (parent) {
14
	this.clear();
15
	this._parent = parent;
16
};
17
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) {
18
}, onRedo:function (manager, item) {
19
}, onUndoAny:function (manager, item) {
20
}, onRedoAny:function (manager, item) {
21
}, _updateStatus:function () {
22
	this.canUndo = this._undoStack.length > 0;
23
	this.canRedo = this._redoStack.length > 0;
24
}, clear:function () {
25
	this._undoStack = [];
26
	this._redoStack = [];
27
	this._currentManager = this;
28
	this.isUndoing = false;
29
	this.isRedoing = false;
30
	this._updateStatus();
31
}, undo:function () {
32
	if (!this.canUndo) {
33
		return false;
34
	}
35
	this.endAllTransactions();
36
	this.isUndoing = true;
37
	var top = this._undoStack.pop();
38
	if (top instanceof dojo.undo.Manager) {
39
		top.undoAll();
40
	} else {
41
		top.undo();
42
	}
43
	if (top.redo) {
44
		this._redoStack.push(top);
45
	}
46
	this.isUndoing = false;
47
	this._updateStatus();
48
	this.onUndo(this, top);
49
	if (!(top instanceof dojo.undo.Manager)) {
50
		this.getTop().onUndoAny(this, top);
51
	}
52
	return true;
53
}, redo:function () {
54
	if (!this.canRedo) {
55
		return false;
56
	}
57
	this.isRedoing = true;
58
	var top = this._redoStack.pop();
59
	if (top instanceof dojo.undo.Manager) {
60
		top.redoAll();
61
	} else {
62
		top.redo();
63
	}
64
	this._undoStack.push(top);
65
	this.isRedoing = false;
66
	this._updateStatus();
67
	this.onRedo(this, top);
68
	if (!(top instanceof dojo.undo.Manager)) {
69
		this.getTop().onRedoAny(this, top);
70
	}
71
	return true;
72
}, undoAll:function () {
73
	while (this._undoStack.length > 0) {
74
		this.undo();
75
	}
76
}, redoAll:function () {
77
	while (this._redoStack.length > 0) {
78
		this.redo();
79
	}
80
}, push:function (undo, redo, description) {
81
	if (!undo) {
82
		return;
83
	}
84
	if (this._currentManager == this) {
85
		this._undoStack.push({undo:undo, redo:redo, description:description});
86
	} else {
87
		this._currentManager.push.apply(this._currentManager, arguments);
88
	}
89
	this._redoStack = [];
90
	this._updateStatus();
91
}, concat:function (manager) {
92
	if (!manager) {
93
		return;
94
	}
95
	if (this._currentManager == this) {
96
		for (var x = 0; x < manager._undoStack.length; x++) {
97
			this._undoStack.push(manager._undoStack[x]);
98
		}
99
		if (manager._undoStack.length > 0) {
100
			this._redoStack = [];
101
		}
102
		this._updateStatus();
103
	} else {
104
		this._currentManager.concat.apply(this._currentManager, arguments);
105
	}
106
}, beginTransaction:function (description) {
107
	if (this._currentManager == this) {
108
		var mgr = new dojo.undo.Manager(this);
109
		mgr.description = description ? description : "";
110
		this._undoStack.push(mgr);
111
		this._currentManager = mgr;
112
		return mgr;
113
	} else {
114
		this._currentManager = this._currentManager.beginTransaction.apply(this._currentManager, arguments);
115
	}
116
}, endTransaction:function (flatten) {
117
	if (this._currentManager == this) {
118
		if (this._parent) {
119
			this._parent._currentManager = this._parent;
120
			if (this._undoStack.length == 0 || flatten) {
121
				var idx = dojo.lang.find(this._parent._undoStack, this);
122
				if (idx >= 0) {
123
					this._parent._undoStack.splice(idx, 1);
124
					if (flatten) {
125
						for (var x = 0; x < this._undoStack.length; x++) {
126
							this._parent._undoStack.splice(idx++, 0, this._undoStack[x]);
127
						}
128
						this._updateStatus();
129
					}
130
				}
131
			}
132
			return this._parent;
133
		}
134
	} else {
135
		this._currentManager = this._currentManager.endTransaction.apply(this._currentManager, arguments);
136
	}
137
}, endAllTransactions:function () {
138
	while (this._currentManager != this) {
139
		this.endTransaction();
140
	}
141
}, getTop:function () {
142
	if (this._parent) {
143
		return this._parent.getTop();
144
	} else {
145
		return this;
146
	}
147
}});
148