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.widget.SplitContainer");
12
dojo.require("dojo.widget.*");
13
dojo.require("dojo.widget.ContentPane");
14
dojo.require("dojo.widget.HtmlWidget");
15
dojo.require("dojo.html.style");
16
dojo.require("dojo.html.layout");
17
dojo.require("dojo.html.selection");
18
dojo.require("dojo.io.cookie");
19
dojo.widget.defineWidget("dojo.widget.SplitContainer", dojo.widget.HtmlWidget, function () {
20
	this.sizers = [];
21
}, {isContainer:true, templateCssString:".dojoSplitContainer{\n\tposition: relative;\n\toverflow: hidden;\n\tdisplay: block;\n}\n\n.dojoSplitPane{\n\tposition: absolute;\n}\n\n.dojoSplitContainerSizerH,\n.dojoSplitContainerSizerV {\n\tfont-size: 1px;\n\tcursor: move;\n\tcursor: w-resize;\n\tbackground-color: ThreeDFace;\n\tborder: 1px solid;\n\tborder-color: ThreeDHighlight ThreeDShadow ThreeDShadow ThreeDHighlight;\n\tmargin: 0;\n}\n\n.dojoSplitContainerSizerV {\n\tcursor: n-resize;\n}\n\n.dojoSplitContainerVirtualSizerH,\n.dojoSplitContainerVirtualSizerV {\n\tfont-size: 1px;\n\tcursor: move;\n\tcursor: w-resize;\n\tbackground-color: ThreeDShadow;\n\t-moz-opacity: 0.5;\n\topacity: 0.5;\n\tfilter: Alpha(Opacity=50);\n\tmargin: 0;\n}\n\n.dojoSplitContainerVirtualSizerV {\n\tcursor: n-resize;\n}\n", templateCssPath:dojo.uri.moduleUri("dojo.widget", "templates/SplitContainer.css"), activeSizing:false, sizerWidth:15, orientation:"horizontal", persist:true, postMixInProperties:function () {
22
	dojo.widget.SplitContainer.superclass.postMixInProperties.apply(this, arguments);
23
	this.isHorizontal = (this.orientation == "horizontal");
24
}, fillInTemplate:function () {
25
	dojo.widget.SplitContainer.superclass.fillInTemplate.apply(this, arguments);
26
	dojo.html.addClass(this.domNode, "dojoSplitContainer");
27
	if (dojo.render.html.moz) {
28
		this.domNode.style.overflow = "-moz-scrollbars-none";
29
	}
30
	var content = dojo.html.getContentBox(this.domNode);
31
	this.paneWidth = content.width;
32
	this.paneHeight = content.height;
33
}, onResized:function (e) {
34
	var content = dojo.html.getContentBox(this.domNode);
35
	this.paneWidth = content.width;
36
	this.paneHeight = content.height;
37
	this._layoutPanels();
38
}, postCreate:function (args, fragment, parentComp) {
39
	dojo.widget.SplitContainer.superclass.postCreate.apply(this, arguments);
40
	for (var i = 0; i < this.children.length; i++) {
41
		with (this.children[i].domNode.style) {
42
			position = "absolute";
43
		}
44
		dojo.html.addClass(this.children[i].domNode, "dojoSplitPane");
45
		if (i == this.children.length - 1) {
46
			break;
47
		}
48
		this._addSizer();
49
	}
50
	if (typeof this.sizerWidth == "object") {
51
		try {
52
			this.sizerWidth = parseInt(this.sizerWidth.toString());
53
		}
54
		catch (e) {
55
			this.sizerWidth = 15;
56
		}
57
	}
58
	this.virtualSizer = document.createElement("div");
59
	this.virtualSizer.style.position = "absolute";
60
	this.virtualSizer.style.display = "none";
61
	this.virtualSizer.style.zIndex = 10;
62
	this.virtualSizer.className = this.isHorizontal ? "dojoSplitContainerVirtualSizerH" : "dojoSplitContainerVirtualSizerV";
63
	this.domNode.appendChild(this.virtualSizer);
64
	dojo.html.disableSelection(this.virtualSizer);
65
	if (this.persist) {
66
		this._restoreState();
67
	}
68
	this.resizeSoon();
69
}, _injectChild:function (child) {
70
	with (child.domNode.style) {
71
		position = "absolute";
72
	}
73
	dojo.html.addClass(child.domNode, "dojoSplitPane");
74
}, _addSizer:function () {
75
	var i = this.sizers.length;
76
	this.sizers[i] = document.createElement("div");
77
	this.sizers[i].style.position = "absolute";
78
	this.sizers[i].className = this.isHorizontal ? "dojoSplitContainerSizerH" : "dojoSplitContainerSizerV";
79
	var self = this;
80
	var handler = (function () {
81
		var sizer_i = i;
82
		return function (e) {
83
			self.beginSizing(e, sizer_i);
84
		};
85
	})();
86
	dojo.event.connect(this.sizers[i], "onmousedown", handler);
87
	this.domNode.appendChild(this.sizers[i]);
88
	dojo.html.disableSelection(this.sizers[i]);
89
}, removeChild:function (widget) {
90
	if (this.sizers.length > 0) {
91
		for (var x = 0; x < this.children.length; x++) {
92
			if (this.children[x] === widget) {
93
				var i = this.sizers.length - 1;
94
				this.domNode.removeChild(this.sizers[i]);
95
				this.sizers.length = i;
96
				break;
97
			}
98
		}
99
	}
100
	dojo.widget.SplitContainer.superclass.removeChild.call(this, widget, arguments);
101
	this.onResized();
102
}, addChild:function (widget) {
103
	dojo.widget.SplitContainer.superclass.addChild.apply(this, arguments);
104
	this._injectChild(widget);
105
	if (this.children.length > 1) {
106
		this._addSizer();
107
	}
108
	this._layoutPanels();
109
}, _layoutPanels:function () {
110
	if (this.children.length == 0) {
111
		return;
112
	}
113
	var space = this.isHorizontal ? this.paneWidth : this.paneHeight;
114
	if (this.children.length > 1) {
115
		space -= this.sizerWidth * (this.children.length - 1);
116
	}
117
	var out_of = 0;
118
	for (var i = 0; i < this.children.length; i++) {
119
		out_of += this.children[i].sizeShare;
120
	}
121
	var pix_per_unit = space / out_of;
122
	var total_size = 0;
123
	for (var i = 0; i < this.children.length - 1; i++) {
124
		var size = Math.round(pix_per_unit * this.children[i].sizeShare);
125
		this.children[i].sizeActual = size;
126
		total_size += size;
127
	}
128
	this.children[this.children.length - 1].sizeActual = space - total_size;
129
	this._checkSizes();
130
	var pos = 0;
131
	var size = this.children[0].sizeActual;
132
	this._movePanel(this.children[0], pos, size);
133
	this.children[0].position = pos;
134
	pos += size;
135
	for (var i = 1; i < this.children.length; i++) {
136
		this._moveSlider(this.sizers[i - 1], pos, this.sizerWidth);
137
		this.sizers[i - 1].position = pos;
138
		pos += this.sizerWidth;
139
		size = this.children[i].sizeActual;
140
		this._movePanel(this.children[i], pos, size);
141
		this.children[i].position = pos;
142
		pos += size;
143
	}
144
}, _movePanel:function (panel, pos, size) {
145
	if (this.isHorizontal) {
146
		panel.domNode.style.left = pos + "px";
147
		panel.domNode.style.top = 0;
148
		panel.resizeTo(size, this.paneHeight);
149
	} else {
150
		panel.domNode.style.left = 0;
151
		panel.domNode.style.top = pos + "px";
152
		panel.resizeTo(this.paneWidth, size);
153
	}
154
}, _moveSlider:function (slider, pos, size) {
155
	if (this.isHorizontal) {
156
		slider.style.left = pos + "px";
157
		slider.style.top = 0;
158
		dojo.html.setMarginBox(slider, {width:size, height:this.paneHeight});
159
	} else {
160
		slider.style.left = 0;
161
		slider.style.top = pos + "px";
162
		dojo.html.setMarginBox(slider, {width:this.paneWidth, height:size});
163
	}
164
}, _growPane:function (growth, pane) {
165
	if (growth > 0) {
166
		if (pane.sizeActual > pane.sizeMin) {
167
			if ((pane.sizeActual - pane.sizeMin) > growth) {
168
				pane.sizeActual = pane.sizeActual - growth;
169
				growth = 0;
170
			} else {
171
				growth -= pane.sizeActual - pane.sizeMin;
172
				pane.sizeActual = pane.sizeMin;
173
			}
174
		}
175
	}
176
	return growth;
177
}, _checkSizes:function () {
178
	var total_min_size = 0;
179
	var total_size = 0;
180
	for (var i = 0; i < this.children.length; i++) {
181
		total_size += this.children[i].sizeActual;
182
		total_min_size += this.children[i].sizeMin;
183
	}
184
	if (total_min_size <= total_size) {
185
		var growth = 0;
186
		for (var i = 0; i < this.children.length; i++) {
187
			if (this.children[i].sizeActual < this.children[i].sizeMin) {
188
				growth += this.children[i].sizeMin - this.children[i].sizeActual;
189
				this.children[i].sizeActual = this.children[i].sizeMin;
190
			}
191
		}
192
		if (growth > 0) {
193
			if (this.isDraggingLeft) {
194
				for (var i = this.children.length - 1; i >= 0; i--) {
195
					growth = this._growPane(growth, this.children[i]);
196
				}
197
			} else {
198
				for (var i = 0; i < this.children.length; i++) {
199
					growth = this._growPane(growth, this.children[i]);
200
				}
201
			}
202
		}
203
	} else {
204
		for (var i = 0; i < this.children.length; i++) {
205
			this.children[i].sizeActual = Math.round(total_size * (this.children[i].sizeMin / total_min_size));
206
		}
207
	}
208
}, beginSizing:function (e, i) {
209
	this.paneBefore = this.children[i];
210
	this.paneAfter = this.children[i + 1];
211
	this.isSizing = true;
212
	this.sizingSplitter = this.sizers[i];
213
	this.originPos = dojo.html.getAbsolutePosition(this.children[0].domNode, true, dojo.html.boxSizing.MARGIN_BOX);
214
	if (this.isHorizontal) {
215
		var client = (e.layerX ? e.layerX : e.offsetX);
216
		var screen = e.pageX;
217
		this.originPos = this.originPos.x;
218
	} else {
219
		var client = (e.layerY ? e.layerY : e.offsetY);
220
		var screen = e.pageY;
221
		this.originPos = this.originPos.y;
222
	}
223
	this.startPoint = this.lastPoint = screen;
224
	this.screenToClientOffset = screen - client;
225
	this.dragOffset = this.lastPoint - this.paneBefore.sizeActual - this.originPos - this.paneBefore.position;
226
	if (!this.activeSizing) {
227
		this._showSizingLine();
228
	}
229
	dojo.event.connect(document.documentElement, "onmousemove", this, "changeSizing");
230
	dojo.event.connect(document.documentElement, "onmouseup", this, "endSizing");
231
	dojo.event.browser.stopEvent(e);
232
}, changeSizing:function (e) {
233
	this.lastPoint = this.isHorizontal ? e.pageX : e.pageY;
234
	if (this.activeSizing) {
235
		this.movePoint();
236
		this._updateSize();
237
	} else {
238
		this.movePoint();
239
		this._moveSizingLine();
240
	}
241
	dojo.event.browser.stopEvent(e);
242
}, endSizing:function (e) {
243
	if (!this.activeSizing) {
244
		this._hideSizingLine();
245
	}
246
	this._updateSize();
247
	this.isSizing = false;
248
	dojo.event.disconnect(document.documentElement, "onmousemove", this, "changeSizing");
249
	dojo.event.disconnect(document.documentElement, "onmouseup", this, "endSizing");
250
	if (this.persist) {
251
		this._saveState(this);
252
	}
253
}, movePoint:function () {
254
	var p = this.lastPoint - this.screenToClientOffset;
255
	var a = p - this.dragOffset;
256
	a = this.legaliseSplitPoint(a);
257
	p = a + this.dragOffset;
258
	this.lastPoint = p + this.screenToClientOffset;
259
}, legaliseSplitPoint:function (a) {
260
	a += this.sizingSplitter.position;
261
	this.isDraggingLeft = (a > 0) ? true : false;
262
	if (!this.activeSizing) {
263
		if (a < this.paneBefore.position + this.paneBefore.sizeMin) {
264
			a = this.paneBefore.position + this.paneBefore.sizeMin;
265
		}
266
		if (a > this.paneAfter.position + (this.paneAfter.sizeActual - (this.sizerWidth + this.paneAfter.sizeMin))) {
267
			a = this.paneAfter.position + (this.paneAfter.sizeActual - (this.sizerWidth + this.paneAfter.sizeMin));
268
		}
269
	}
270
	a -= this.sizingSplitter.position;
271
	this._checkSizes();
272
	return a;
273
}, _updateSize:function () {
274
	var pos = this.lastPoint - this.dragOffset - this.originPos;
275
	var start_region = this.paneBefore.position;
276
	var end_region = this.paneAfter.position + this.paneAfter.sizeActual;
277
	this.paneBefore.sizeActual = pos - start_region;
278
	this.paneAfter.position = pos + this.sizerWidth;
279
	this.paneAfter.sizeActual = end_region - this.paneAfter.position;
280
	for (var i = 0; i < this.children.length; i++) {
281
		this.children[i].sizeShare = this.children[i].sizeActual;
282
	}
283
	this._layoutPanels();
284
}, _showSizingLine:function () {
285
	this._moveSizingLine();
286
	if (this.isHorizontal) {
287
		dojo.html.setMarginBox(this.virtualSizer, {width:this.sizerWidth, height:this.paneHeight});
288
	} else {
289
		dojo.html.setMarginBox(this.virtualSizer, {width:this.paneWidth, height:this.sizerWidth});
290
	}
291
	this.virtualSizer.style.display = "block";
292
}, _hideSizingLine:function () {
293
	this.virtualSizer.style.display = "none";
294
}, _moveSizingLine:function () {
295
	var pos = this.lastPoint - this.startPoint + this.sizingSplitter.position;
296
	if (this.isHorizontal) {
297
		this.virtualSizer.style.left = pos + "px";
298
	} else {
299
		var pos = (this.lastPoint - this.startPoint) + this.sizingSplitter.position;
300
		this.virtualSizer.style.top = pos + "px";
301
	}
302
}, _getCookieName:function (i) {
303
	return this.widgetId + "_" + i;
304
}, _restoreState:function () {
305
	for (var i = 0; i < this.children.length; i++) {
306
		var cookieName = this._getCookieName(i);
307
		var cookieValue = dojo.io.cookie.getCookie(cookieName);
308
		if (cookieValue != null) {
309
			var pos = parseInt(cookieValue);
310
			if (typeof pos == "number") {
311
				this.children[i].sizeShare = pos;
312
			}
313
		}
314
	}
315
}, _saveState:function () {
316
	for (var i = 0; i < this.children.length; i++) {
317
		var cookieName = this._getCookieName(i);
318
		dojo.io.cookie.setCookie(cookieName, this.children[i].sizeShare, null, null, null, null);
319
	}
320
}});
321
dojo.lang.extend(dojo.widget.Widget, {sizeMin:10, sizeShare:10});
322
dojo.widget.defineWidget("dojo.widget.SplitContainerPanel", dojo.widget.ContentPane, {});
323