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.html.layout");
12
dojo.require("dojo.html.common");
13
dojo.require("dojo.html.style");
14
dojo.require("dojo.html.display");
15
dojo.html.sumAncestorProperties = function (node, prop) {
16
	node = dojo.byId(node);
17
	if (!node) {
18
		return 0;
19
	}
20
	var retVal = 0;
21
	while (node) {
22
		if (dojo.html.getComputedStyle(node, "position") == "fixed") {
23
			return 0;
24
		}
25
		var val = node[prop];
26
		if (val) {
27
			retVal += val - 0;
28
			if (node == dojo.body()) {
29
				break;
30
			}
31
		}
32
		node = node.parentNode;
33
	}
34
	return retVal;
35
};
36
dojo.html.setStyleAttributes = function (node, attributes) {
37
	node = dojo.byId(node);
38
	var splittedAttribs = attributes.replace(/(;)?\s*$/, "").split(";");
39
	for (var i = 0; i < splittedAttribs.length; i++) {
40
		var nameValue = splittedAttribs[i].split(":");
41
		var name = nameValue[0].replace(/\s*$/, "").replace(/^\s*/, "").toLowerCase();
42
		var value = nameValue[1].replace(/\s*$/, "").replace(/^\s*/, "");
43
		switch (name) {
44
		  case "opacity":
45
			dojo.html.setOpacity(node, value);
46
			break;
47
		  case "content-height":
48
			dojo.html.setContentBox(node, {height:value});
49
			break;
50
		  case "content-width":
51
			dojo.html.setContentBox(node, {width:value});
52
			break;
53
		  case "outer-height":
54
			dojo.html.setMarginBox(node, {height:value});
55
			break;
56
		  case "outer-width":
57
			dojo.html.setMarginBox(node, {width:value});
58
			break;
59
		  default:
60
			node.style[dojo.html.toCamelCase(name)] = value;
61
		}
62
	}
63
};
64
dojo.html.boxSizing = {MARGIN_BOX:"margin-box", BORDER_BOX:"border-box", PADDING_BOX:"padding-box", CONTENT_BOX:"content-box"};
65
dojo.html.getAbsolutePosition = dojo.html.abs = function (node, includeScroll, boxType) {
66
	node = dojo.byId(node, node.ownerDocument);
67
	var ret = {x:0, y:0};
68
	var bs = dojo.html.boxSizing;
69
	if (!boxType) {
70
		boxType = bs.CONTENT_BOX;
71
	}
72
	var nativeBoxType = 2;
73
	var targetBoxType;
74
	switch (boxType) {
75
	  case bs.MARGIN_BOX:
76
		targetBoxType = 3;
77
		break;
78
	  case bs.BORDER_BOX:
79
		targetBoxType = 2;
80
		break;
81
	  case bs.PADDING_BOX:
82
	  default:
83
		targetBoxType = 1;
84
		break;
85
	  case bs.CONTENT_BOX:
86
		targetBoxType = 0;
87
		break;
88
	}
89
	var h = dojo.render.html;
90
	var db = document["body"] || document["documentElement"];
91
	if (h.ie) {
92
		with (node.getBoundingClientRect()) {
93
			ret.x = left - 2;
94
			ret.y = top - 2;
95
		}
96
	} else {
97
		if (document.getBoxObjectFor) {
98
			nativeBoxType = 1;
99
			try {
100
				var bo = document.getBoxObjectFor(node);
101
				ret.x = bo.x - dojo.html.sumAncestorProperties(node, "scrollLeft");
102
				ret.y = bo.y - dojo.html.sumAncestorProperties(node, "scrollTop");
103
			}
104
			catch (e) {
105
			}
106
		} else {
107
			if (node["offsetParent"]) {
108
				var endNode;
109
				if ((h.safari) && (node.style.getPropertyValue("position") == "absolute") && (node.parentNode == db)) {
110
					endNode = db;
111
				} else {
112
					endNode = db.parentNode;
113
				}
114
				if (node.parentNode != db) {
115
					var nd = node;
116
					if (dojo.render.html.opera) {
117
						nd = db;
118
					}
119
					ret.x -= dojo.html.sumAncestorProperties(nd, "scrollLeft");
120
					ret.y -= dojo.html.sumAncestorProperties(nd, "scrollTop");
121
				}
122
				var curnode = node;
123
				do {
124
					var n = curnode["offsetLeft"];
125
					if (!h.opera || n > 0) {
126
						ret.x += isNaN(n) ? 0 : n;
127
					}
128
					var m = curnode["offsetTop"];
129
					ret.y += isNaN(m) ? 0 : m;
130
					curnode = curnode.offsetParent;
131
				} while ((curnode != endNode) && (curnode != null));
132
			} else {
133
				if (node["x"] && node["y"]) {
134
					ret.x += isNaN(node.x) ? 0 : node.x;
135
					ret.y += isNaN(node.y) ? 0 : node.y;
136
				}
137
			}
138
		}
139
	}
140
	if (includeScroll) {
141
		var scroll = dojo.html.getScroll();
142
		ret.y += scroll.top;
143
		ret.x += scroll.left;
144
	}
145
	var extentFuncArray = [dojo.html.getPaddingExtent, dojo.html.getBorderExtent, dojo.html.getMarginExtent];
146
	if (nativeBoxType > targetBoxType) {
147
		for (var i = targetBoxType; i < nativeBoxType; ++i) {
148
			ret.y += extentFuncArray[i](node, "top");
149
			ret.x += extentFuncArray[i](node, "left");
150
		}
151
	} else {
152
		if (nativeBoxType < targetBoxType) {
153
			for (var i = targetBoxType; i > nativeBoxType; --i) {
154
				ret.y -= extentFuncArray[i - 1](node, "top");
155
				ret.x -= extentFuncArray[i - 1](node, "left");
156
			}
157
		}
158
	}
159
	ret.top = ret.y;
160
	ret.left = ret.x;
161
	return ret;
162
};
163
dojo.html.isPositionAbsolute = function (node) {
164
	return (dojo.html.getComputedStyle(node, "position") == "absolute");
165
};
166
dojo.html._sumPixelValues = function (node, selectors, autoIsZero) {
167
	var total = 0;
168
	for (var x = 0; x < selectors.length; x++) {
169
		total += dojo.html.getPixelValue(node, selectors[x], autoIsZero);
170
	}
171
	return total;
172
};
173
dojo.html.getMargin = function (node) {
174
	return {width:dojo.html._sumPixelValues(node, ["margin-left", "margin-right"], (dojo.html.getComputedStyle(node, "position") == "absolute")), height:dojo.html._sumPixelValues(node, ["margin-top", "margin-bottom"], (dojo.html.getComputedStyle(node, "position") == "absolute"))};
175
};
176
dojo.html.getBorder = function (node) {
177
	return {width:dojo.html.getBorderExtent(node, "left") + dojo.html.getBorderExtent(node, "right"), height:dojo.html.getBorderExtent(node, "top") + dojo.html.getBorderExtent(node, "bottom")};
178
};
179
dojo.html.getBorderExtent = function (node, side) {
180
	return (dojo.html.getStyle(node, "border-" + side + "-style") == "none" ? 0 : dojo.html.getPixelValue(node, "border-" + side + "-width"));
181
};
182
dojo.html.getMarginExtent = function (node, side) {
183
	return dojo.html._sumPixelValues(node, ["margin-" + side], dojo.html.isPositionAbsolute(node));
184
};
185
dojo.html.getPaddingExtent = function (node, side) {
186
	return dojo.html._sumPixelValues(node, ["padding-" + side], true);
187
};
188
dojo.html.getPadding = function (node) {
189
	return {width:dojo.html._sumPixelValues(node, ["padding-left", "padding-right"], true), height:dojo.html._sumPixelValues(node, ["padding-top", "padding-bottom"], true)};
190
};
191
dojo.html.getPadBorder = function (node) {
192
	var pad = dojo.html.getPadding(node);
193
	var border = dojo.html.getBorder(node);
194
	return {width:pad.width + border.width, height:pad.height + border.height};
195
};
196
dojo.html.getBoxSizing = function (node) {
197
	var h = dojo.render.html;
198
	var bs = dojo.html.boxSizing;
199
	if (((h.ie) || (h.opera)) && node.nodeName.toLowerCase() != "img") {
200
		var cm = document["compatMode"];
201
		if ((cm == "BackCompat") || (cm == "QuirksMode")) {
202
			return bs.BORDER_BOX;
203
		} else {
204
			return bs.CONTENT_BOX;
205
		}
206
	} else {
207
		if (arguments.length == 0) {
208
			node = document.documentElement;
209
		}
210
		var sizing;
211
		if (!h.ie) {
212
			sizing = dojo.html.getStyle(node, "-moz-box-sizing");
213
			if (!sizing) {
214
				sizing = dojo.html.getStyle(node, "box-sizing");
215
			}
216
		}
217
		return (sizing ? sizing : bs.CONTENT_BOX);
218
	}
219
};
220
dojo.html.isBorderBox = function (node) {
221
	return (dojo.html.getBoxSizing(node) == dojo.html.boxSizing.BORDER_BOX);
222
};
223
dojo.html.getBorderBox = function (node) {
224
	node = dojo.byId(node);
225
	return {width:node.offsetWidth, height:node.offsetHeight};
226
};
227
dojo.html.getPaddingBox = function (node) {
228
	var box = dojo.html.getBorderBox(node);
229
	var border = dojo.html.getBorder(node);
230
	return {width:box.width - border.width, height:box.height - border.height};
231
};
232
dojo.html.getContentBox = function (node) {
233
	node = dojo.byId(node);
234
	var padborder = dojo.html.getPadBorder(node);
235
	return {width:node.offsetWidth - padborder.width, height:node.offsetHeight - padborder.height};
236
};
237
dojo.html.setContentBox = function (node, args) {
238
	node = dojo.byId(node);
239
	var width = 0;
240
	var height = 0;
241
	var isbb = dojo.html.isBorderBox(node);
242
	var padborder = (isbb ? dojo.html.getPadBorder(node) : {width:0, height:0});
243
	var ret = {};
244
	if (typeof args.width != "undefined") {
245
		width = args.width + padborder.width;
246
		ret.width = dojo.html.setPositivePixelValue(node, "width", width);
247
	}
248
	if (typeof args.height != "undefined") {
249
		height = args.height + padborder.height;
250
		ret.height = dojo.html.setPositivePixelValue(node, "height", height);
251
	}
252
	return ret;
253
};
254
dojo.html.getMarginBox = function (node) {
255
	var borderbox = dojo.html.getBorderBox(node);
256
	var margin = dojo.html.getMargin(node);
257
	return {width:borderbox.width + margin.width, height:borderbox.height + margin.height};
258
};
259
dojo.html.setMarginBox = function (node, args) {
260
	node = dojo.byId(node);
261
	var width = 0;
262
	var height = 0;
263
	var isbb = dojo.html.isBorderBox(node);
264
	var padborder = (!isbb ? dojo.html.getPadBorder(node) : {width:0, height:0});
265
	var margin = dojo.html.getMargin(node);
266
	var ret = {};
267
	if (typeof args.width != "undefined") {
268
		width = args.width - padborder.width;
269
		width -= margin.width;
270
		ret.width = dojo.html.setPositivePixelValue(node, "width", width);
271
	}
272
	if (typeof args.height != "undefined") {
273
		height = args.height - padborder.height;
274
		height -= margin.height;
275
		ret.height = dojo.html.setPositivePixelValue(node, "height", height);
276
	}
277
	return ret;
278
};
279
dojo.html.getElementBox = function (node, type) {
280
	var bs = dojo.html.boxSizing;
281
	switch (type) {
282
	  case bs.MARGIN_BOX:
283
		return dojo.html.getMarginBox(node);
284
	  case bs.BORDER_BOX:
285
		return dojo.html.getBorderBox(node);
286
	  case bs.PADDING_BOX:
287
		return dojo.html.getPaddingBox(node);
288
	  case bs.CONTENT_BOX:
289
	  default:
290
		return dojo.html.getContentBox(node);
291
	}
292
};
293
dojo.html.toCoordinateObject = dojo.html.toCoordinateArray = function (coords, includeScroll, boxtype) {
294
	if (coords instanceof Array || typeof coords == "array") {
295
		dojo.deprecated("dojo.html.toCoordinateArray", "use dojo.html.toCoordinateObject({left: , top: , width: , height: }) instead", "0.5");
296
		while (coords.length < 4) {
297
			coords.push(0);
298
		}
299
		while (coords.length > 4) {
300
			coords.pop();
301
		}
302
		var ret = {left:coords[0], top:coords[1], width:coords[2], height:coords[3]};
303
	} else {
304
		if (!coords.nodeType && !(coords instanceof String || typeof coords == "string") && ("width" in coords || "height" in coords || "left" in coords || "x" in coords || "top" in coords || "y" in coords)) {
305
			var ret = {left:coords.left || coords.x || 0, top:coords.top || coords.y || 0, width:coords.width || 0, height:coords.height || 0};
306
		} else {
307
			var node = dojo.byId(coords);
308
			var pos = dojo.html.abs(node, includeScroll, boxtype);
309
			var marginbox = dojo.html.getMarginBox(node);
310
			var ret = {left:pos.left, top:pos.top, width:marginbox.width, height:marginbox.height};
311
		}
312
	}
313
	ret.x = ret.left;
314
	ret.y = ret.top;
315
	return ret;
316
};
317
dojo.html.setMarginBoxWidth = dojo.html.setOuterWidth = function (node, width) {
318
	return dojo.html._callDeprecated("setMarginBoxWidth", "setMarginBox", arguments, "width");
319
};
320
dojo.html.setMarginBoxHeight = dojo.html.setOuterHeight = function () {
321
	return dojo.html._callDeprecated("setMarginBoxHeight", "setMarginBox", arguments, "height");
322
};
323
dojo.html.getMarginBoxWidth = dojo.html.getOuterWidth = function () {
324
	return dojo.html._callDeprecated("getMarginBoxWidth", "getMarginBox", arguments, null, "width");
325
};
326
dojo.html.getMarginBoxHeight = dojo.html.getOuterHeight = function () {
327
	return dojo.html._callDeprecated("getMarginBoxHeight", "getMarginBox", arguments, null, "height");
328
};
329
dojo.html.getTotalOffset = function (node, type, includeScroll) {
330
	return dojo.html._callDeprecated("getTotalOffset", "getAbsolutePosition", arguments, null, type);
331
};
332
dojo.html.getAbsoluteX = function (node, includeScroll) {
333
	return dojo.html._callDeprecated("getAbsoluteX", "getAbsolutePosition", arguments, null, "x");
334
};
335
dojo.html.getAbsoluteY = function (node, includeScroll) {
336
	return dojo.html._callDeprecated("getAbsoluteY", "getAbsolutePosition", arguments, null, "y");
337
};
338
dojo.html.totalOffsetLeft = function (node, includeScroll) {
339
	return dojo.html._callDeprecated("totalOffsetLeft", "getAbsolutePosition", arguments, null, "left");
340
};
341
dojo.html.totalOffsetTop = function (node, includeScroll) {
342
	return dojo.html._callDeprecated("totalOffsetTop", "getAbsolutePosition", arguments, null, "top");
343
};
344
dojo.html.getMarginWidth = function (node) {
345
	return dojo.html._callDeprecated("getMarginWidth", "getMargin", arguments, null, "width");
346
};
347
dojo.html.getMarginHeight = function (node) {
348
	return dojo.html._callDeprecated("getMarginHeight", "getMargin", arguments, null, "height");
349
};
350
dojo.html.getBorderWidth = function (node) {
351
	return dojo.html._callDeprecated("getBorderWidth", "getBorder", arguments, null, "width");
352
};
353
dojo.html.getBorderHeight = function (node) {
354
	return dojo.html._callDeprecated("getBorderHeight", "getBorder", arguments, null, "height");
355
};
356
dojo.html.getPaddingWidth = function (node) {
357
	return dojo.html._callDeprecated("getPaddingWidth", "getPadding", arguments, null, "width");
358
};
359
dojo.html.getPaddingHeight = function (node) {
360
	return dojo.html._callDeprecated("getPaddingHeight", "getPadding", arguments, null, "height");
361
};
362
dojo.html.getPadBorderWidth = function (node) {
363
	return dojo.html._callDeprecated("getPadBorderWidth", "getPadBorder", arguments, null, "width");
364
};
365
dojo.html.getPadBorderHeight = function (node) {
366
	return dojo.html._callDeprecated("getPadBorderHeight", "getPadBorder", arguments, null, "height");
367
};
368
dojo.html.getBorderBoxWidth = dojo.html.getInnerWidth = function () {
369
	return dojo.html._callDeprecated("getBorderBoxWidth", "getBorderBox", arguments, null, "width");
370
};
371
dojo.html.getBorderBoxHeight = dojo.html.getInnerHeight = function () {
372
	return dojo.html._callDeprecated("getBorderBoxHeight", "getBorderBox", arguments, null, "height");
373
};
374
dojo.html.getContentBoxWidth = dojo.html.getContentWidth = function () {
375
	return dojo.html._callDeprecated("getContentBoxWidth", "getContentBox", arguments, null, "width");
376
};
377
dojo.html.getContentBoxHeight = dojo.html.getContentHeight = function () {
378
	return dojo.html._callDeprecated("getContentBoxHeight", "getContentBox", arguments, null, "height");
379
};
380
dojo.html.setContentBoxWidth = dojo.html.setContentWidth = function (node, width) {
381
	return dojo.html._callDeprecated("setContentBoxWidth", "setContentBox", arguments, "width");
382
};
383
dojo.html.setContentBoxHeight = dojo.html.setContentHeight = function (node, height) {
384
	return dojo.html._callDeprecated("setContentBoxHeight", "setContentBox", arguments, "height");
385
};
386