Subversion Repositories Applications.papyrus

Rev

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