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
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.widget.TreeLoadingControllerV3");
14
dojo.require("dojo.widget.TreeBasicControllerV3");
15
dojo.require("dojo.event.*");
16
dojo.require("dojo.json");
17
dojo.require("dojo.io.*");
18
dojo.require("dojo.Deferred");
19
dojo.require("dojo.DeferredList");
20
dojo.declare("dojo.Error", Error, function (message, extra) {
21
	this.message = message;
22
	this.extra = extra;
23
	this.stack = (new Error()).stack;
24
});
25
dojo.declare("dojo.CommunicationError", dojo.Error, function () {
26
	this.name = "CommunicationError";
27
});
28
dojo.declare("dojo.LockedError", dojo.Error, function () {
29
	this.name = "LockedError";
30
});
31
dojo.declare("dojo.FormatError", dojo.Error, function () {
32
	this.name = "FormatError";
33
});
34
dojo.declare("dojo.RpcError", dojo.Error, function () {
35
	this.name = "RpcError";
36
});
37
dojo.widget.defineWidget("dojo.widget.TreeLoadingControllerV3", dojo.widget.TreeBasicControllerV3, {RpcUrl:"", RpcActionParam:"action", preventCache:true, checkValidRpcResponse:function (type, obj) {
38
	if (type != "load") {
39
		var extra = {};
40
		for (var i = 1; i < arguments.length; i++) {
41
			dojo.lang.mixin(extra, arguments[i]);
42
		}
43
		return new dojo.CommunicationError(obj, extra);
44
	}
45
	if (typeof obj != "object") {
46
		return new dojo.FormatError("Wrong server answer format " + (obj && obj.toSource ? obj.toSource() : obj) + " type " + (typeof obj), obj);
47
	}
48
	if (!dojo.lang.isUndefined(obj.error)) {
49
		return new dojo.RpcError(obj.error, obj);
50
	}
51
	return false;
52
}, getDeferredBindHandler:function (deferred) {
53
	return dojo.lang.hitch(this, function (type, obj) {
54
		var error = this.checkValidRpcResponse.apply(this, arguments);
55
		if (error) {
56
			deferred.errback(error);
57
			return;
58
		}
59
		deferred.callback(obj);
60
	});
61
}, getRpcUrl:function (action) {
62
	if (this.RpcUrl == "local") {
63
		var dir = document.location.href.substr(0, document.location.href.lastIndexOf("/"));
64
		var localUrl = dir + "/local/" + action;
65
		return localUrl;
66
	}
67
	if (!this.RpcUrl) {
68
		dojo.raise("Empty RpcUrl: can't load");
69
	}
70
	var url = this.RpcUrl;
71
	if (url.indexOf("/") != 0) {
72
		var protocol = document.location.href.replace(/:\/\/.*/, "");
73
		var prefix = document.location.href.substring(protocol.length + 3);
74
		if (prefix.lastIndexOf("/") != prefix.length - 1) {
75
			prefix = prefix.replace(/\/[^\/]+$/, "/");
76
		}
77
		if (prefix.lastIndexOf("/") != prefix.length - 1) {
78
			prefix = prefix + "/";
79
		}
80
		url = protocol + "://" + prefix + url;
81
	}
82
	return url + (url.indexOf("?") > -1 ? "&" : "?") + this.RpcActionParam + "=" + action;
83
}, loadProcessResponse:function (node, result) {
84
	if (!dojo.lang.isArray(result)) {
85
		throw new dojo.FormatError("loadProcessResponse: Not array loaded: " + result);
86
	}
87
	node.setChildren(result);
88
}, runRpc:function (kw) {
89
	var _this = this;
90
	var deferred = new dojo.Deferred();
91
	dojo.io.bind({url:kw.url, handle:this.getDeferredBindHandler(deferred), mimetype:"text/javascript", preventCache:this.preventCache, sync:kw.sync, content:{data:dojo.json.serialize(kw.params)}});
92
	return deferred;
93
}, loadRemote:function (node, sync) {
94
	var _this = this;
95
	var params = {node:this.getInfo(node), tree:this.getInfo(node.tree)};
96
	var deferred = this.runRpc({url:this.getRpcUrl("getChildren"), sync:sync, params:params});
97
	deferred.addCallback(function (res) {
98
		return _this.loadProcessResponse(node, res);
99
	});
100
	return deferred;
101
}, batchExpandTimeout:0, recurseToLevel:function (widget, level, callFunc, callObj, skipFirst, sync) {
102
	if (level == 0) {
103
		return;
104
	}
105
	if (!skipFirst) {
106
		var deferred = callFunc.call(callObj, widget, sync);
107
	} else {
108
		var deferred = dojo.Deferred.prototype.makeCalled();
109
	}
110
	var _this = this;
111
	var recurseOnExpand = function () {
112
		var children = widget.children;
113
		var deferreds = [];
114
		for (var i = 0; i < children.length; i++) {
115
			deferreds.push(_this.recurseToLevel(children[i], level - 1, callFunc, callObj, sync));
116
		}
117
		return new dojo.DeferredList(deferreds);
118
	};
119
	deferred.addCallback(recurseOnExpand);
120
	return deferred;
121
}, expandToLevel:function (nodeOrTree, level, sync) {
122
	return this.recurseToLevel(nodeOrTree, nodeOrTree.isTree ? level + 1 : level, this.expand, this, nodeOrTree.isTree, sync);
123
}, loadToLevel:function (nodeOrTree, level, sync) {
124
	return this.recurseToLevel(nodeOrTree, nodeOrTree.isTree ? level + 1 : level, this.loadIfNeeded, this, nodeOrTree.isTree, sync);
125
}, loadAll:function (nodeOrTree, sync) {
126
	return this.loadToLevel(nodeOrTree, Number.POSITIVE_INFINITY, sync);
127
}, expand:function (node, sync) {
128
	var _this = this;
129
	var deferred = this.startProcessing(node);
130
	deferred.addCallback(function () {
131
		return _this.loadIfNeeded(node, sync);
132
	});
133
	deferred.addCallback(function (res) {
134
		dojo.widget.TreeBasicControllerV3.prototype.expand(node);
135
		return res;
136
	});
137
	deferred.addBoth(function (res) {
138
		_this.finishProcessing(node);
139
		return res;
140
	});
141
	return deferred;
142
}, loadIfNeeded:function (node, sync) {
143
	var deferred;
144
	if (node.state == node.loadStates.UNCHECKED && node.isFolder && !node.children.length) {
145
		deferred = this.loadRemote(node, sync);
146
	} else {
147
		deferred = new dojo.Deferred();
148
		deferred.callback();
149
	}
150
	return deferred;
151
}, runStages:function (check, prepare, make, finalize, expose, args) {
152
	var _this = this;
153
	if (check && !check.apply(this, args)) {
154
		return false;
155
	}
156
	var deferred = dojo.Deferred.prototype.makeCalled();
157
	if (prepare) {
158
		deferred.addCallback(function () {
159
			return prepare.apply(_this, args);
160
		});
161
	}
162
	if (make) {
163
		deferred.addCallback(function () {
164
			var res = make.apply(_this, args);
165
			return res;
166
		});
167
	}
168
	if (finalize) {
169
		deferred.addBoth(function (res) {
170
			finalize.apply(_this, args);
171
			return res;
172
		});
173
	}
174
	if (expose) {
175
		deferred.addCallback(function (res) {
176
			expose.apply(_this, args);
177
			return res;
178
		});
179
	}
180
	return deferred;
181
}, startProcessing:function (nodesArray) {
182
	var deferred = new dojo.Deferred();
183
	var nodes = dojo.lang.isArray(nodesArray) ? nodesArray : arguments;
184
	for (var i = 0; i < nodes.length; i++) {
185
		if (nodes[i].isLocked()) {
186
			deferred.errback(new dojo.LockedError("item locked " + nodes[i], nodes[i]));
187
			return deferred;
188
		}
189
		if (nodes[i].isTreeNode) {
190
			nodes[i].markProcessing();
191
		}
192
		nodes[i].lock();
193
	}
194
	deferred.callback();
195
	return deferred;
196
}, finishProcessing:function (nodesArray) {
197
	var nodes = dojo.lang.isArray(nodesArray) ? nodesArray : arguments;
198
	for (var i = 0; i < nodes.length; i++) {
199
		if (!nodes[i].hasLock()) {
200
			continue;
201
		}
202
		nodes[i].unlock();
203
		if (nodes[i].isTreeNode) {
204
			nodes[i].unmarkProcessing();
205
		}
206
	}
207
}, refreshChildren:function (nodeOrTree, sync) {
208
	return this.runStages(null, this.prepareRefreshChildren, this.doRefreshChildren, this.finalizeRefreshChildren, this.exposeRefreshChildren, arguments);
209
}, prepareRefreshChildren:function (nodeOrTree, sync) {
210
	var deferred = this.startProcessing(nodeOrTree);
211
	nodeOrTree.destroyChildren();
212
	nodeOrTree.state = nodeOrTree.loadStates.UNCHECKED;
213
	return deferred;
214
}, doRefreshChildren:function (nodeOrTree, sync) {
215
	return this.loadRemote(nodeOrTree, sync);
216
}, finalizeRefreshChildren:function (nodeOrTree, sync) {
217
	this.finishProcessing(nodeOrTree);
218
}, exposeRefreshChildren:function (nodeOrTree, sync) {
219
	nodeOrTree.expand();
220
}, move:function (child, newParent, index) {
221
	return this.runStages(this.canMove, this.prepareMove, this.doMove, this.finalizeMove, this.exposeMove, arguments);
222
}, doMove:function (child, newParent, index) {
223
	child.tree.move(child, newParent, index);
224
	return true;
225
}, prepareMove:function (child, newParent, index, sync) {
226
	var deferred = this.startProcessing(newParent);
227
	deferred.addCallback(dojo.lang.hitch(this, function () {
228
		return this.loadIfNeeded(newParent, sync);
229
	}));
230
	return deferred;
231
}, finalizeMove:function (child, newParent) {
232
	this.finishProcessing(newParent);
233
}, prepareCreateChild:function (parent, index, data, sync) {
234
	var deferred = this.startProcessing(parent);
235
	deferred.addCallback(dojo.lang.hitch(this, function () {
236
		return this.loadIfNeeded(parent, sync);
237
	}));
238
	return deferred;
239
}, finalizeCreateChild:function (parent) {
240
	this.finishProcessing(parent);
241
}, prepareClone:function (child, newParent, index, deep, sync) {
242
	var deferred = this.startProcessing(child, newParent);
243
	deferred.addCallback(dojo.lang.hitch(this, function () {
244
		return this.loadIfNeeded(newParent, sync);
245
	}));
246
	return deferred;
247
}, finalizeClone:function (child, newParent) {
248
	this.finishProcessing(child, newParent);
249
}});
250