Subversion Repositories Applications.papyrus

Rev

Rev 1371 | 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
 
11
dojo.provide("dojo.lfx.html");
12
dojo.require("dojo.gfx.color");
13
dojo.require("dojo.lfx.Animation");
14
dojo.require("dojo.lang.array");
15
dojo.require("dojo.html.display");
16
dojo.require("dojo.html.color");
17
dojo.require("dojo.html.layout");
18
dojo.lfx.html._byId = function (nodes) {
19
	if (!nodes) {
20
		return [];
21
	}
22
	if (dojo.lang.isArrayLike(nodes)) {
23
		if (!nodes.alreadyChecked) {
24
			var n = [];
25
			dojo.lang.forEach(nodes, function (node) {
26
				n.push(dojo.byId(node));
27
			});
28
			n.alreadyChecked = true;
29
			return n;
30
		} else {
31
			return nodes;
32
		}
33
	} else {
34
		var n = [];
35
		n.push(dojo.byId(nodes));
36
		n.alreadyChecked = true;
37
		return n;
38
	}
39
};
40
dojo.lfx.html.propertyAnimation = function (nodes, propertyMap, duration, easing, handlers) {
41
	nodes = dojo.lfx.html._byId(nodes);
42
	var targs = {"propertyMap":propertyMap, "nodes":nodes, "duration":duration, "easing":easing || dojo.lfx.easeDefault};
43
	var setEmUp = function (args) {
44
		if (args.nodes.length == 1) {
45
			var pm = args.propertyMap;
46
			if (!dojo.lang.isArray(args.propertyMap)) {
47
				var parr = [];
48
				for (var pname in pm) {
49
					pm[pname].property = pname;
50
					parr.push(pm[pname]);
51
				}
52
				pm = args.propertyMap = parr;
53
			}
54
			dojo.lang.forEach(pm, function (prop) {
55
				if (dj_undef("start", prop)) {
56
					if (prop.property != "opacity") {
57
						prop.start = parseInt(dojo.html.getComputedStyle(args.nodes[0], prop.property));
58
					} else {
59
						prop.start = dojo.html.getOpacity(args.nodes[0]);
60
					}
61
				}
62
			});
63
		}
64
	};
65
	var coordsAsInts = function (coords) {
66
		var cints = [];
67
		dojo.lang.forEach(coords, function (c) {
68
			cints.push(Math.round(c));
69
		});
70
		return cints;
71
	};
72
	var setStyle = function (n, style) {
73
		n = dojo.byId(n);
74
		if (!n || !n.style) {
75
			return;
76
		}
77
		for (var s in style) {
78
			try {
79
				if (s == "opacity") {
80
					dojo.html.setOpacity(n, style[s]);
81
				} else {
82
					n.style[s] = style[s];
83
				}
84
			}
85
			catch (e) {
86
				dojo.debug(e);
87
			}
88
		}
89
	};
90
	var propLine = function (properties) {
91
		this._properties = properties;
92
		this.diffs = new Array(properties.length);
93
		dojo.lang.forEach(properties, function (prop, i) {
94
			if (dojo.lang.isFunction(prop.start)) {
95
				prop.start = prop.start(prop, i);
96
			}
97
			if (dojo.lang.isFunction(prop.end)) {
98
				prop.end = prop.end(prop, i);
99
			}
100
			if (dojo.lang.isArray(prop.start)) {
101
				this.diffs[i] = null;
102
			} else {
103
				if (prop.start instanceof dojo.gfx.color.Color) {
104
					prop.startRgb = prop.start.toRgb();
105
					prop.endRgb = prop.end.toRgb();
106
				} else {
107
					this.diffs[i] = prop.end - prop.start;
108
				}
109
			}
110
		}, this);
111
		this.getValue = function (n) {
112
			var ret = {};
113
			dojo.lang.forEach(this._properties, function (prop, i) {
114
				var value = null;
115
				if (dojo.lang.isArray(prop.start)) {
116
				} else {
117
					if (prop.start instanceof dojo.gfx.color.Color) {
118
						value = (prop.units || "rgb") + "(";
119
						for (var j = 0; j < prop.startRgb.length; j++) {
120
							value += Math.round(((prop.endRgb[j] - prop.startRgb[j]) * n) + prop.startRgb[j]) + (j < prop.startRgb.length - 1 ? "," : "");
121
						}
122
						value += ")";
123
					} else {
124
						value = ((this.diffs[i]) * n) + prop.start + (prop.property != "opacity" ? prop.units || "px" : "");
125
					}
126
				}
127
				ret[dojo.html.toCamelCase(prop.property)] = value;
128
			}, this);
129
			return ret;
130
		};
131
	};
132
	var anim = new dojo.lfx.Animation({beforeBegin:function () {
133
		setEmUp(targs);
134
		anim.curve = new propLine(targs.propertyMap);
135
	}, onAnimate:function (propValues) {
136
		dojo.lang.forEach(targs.nodes, function (node) {
137
			setStyle(node, propValues);
138
		});
139
	}}, targs.duration, null, targs.easing);
140
	if (handlers) {
141
		for (var x in handlers) {
142
			if (dojo.lang.isFunction(handlers[x])) {
143
				anim.connect(x, anim, handlers[x]);
144
			}
145
		}
146
	}
147
	return anim;
148
};
149
dojo.lfx.html._makeFadeable = function (nodes) {
150
	var makeFade = function (node) {
151
		if (dojo.render.html.ie) {
152
			if ((node.style.zoom.length == 0) && (dojo.html.getStyle(node, "zoom") == "normal")) {
153
				node.style.zoom = "1";
154
			}
155
			if ((node.style.width.length == 0) && (dojo.html.getStyle(node, "width") == "auto")) {
156
				node.style.width = "auto";
157
			}
158
		}
159
	};
160
	if (dojo.lang.isArrayLike(nodes)) {
161
		dojo.lang.forEach(nodes, makeFade);
162
	} else {
163
		makeFade(nodes);
164
	}
165
};
166
dojo.lfx.html.fade = function (nodes, values, duration, easing, callback) {
167
	nodes = dojo.lfx.html._byId(nodes);
168
	var props = {property:"opacity"};
169
	if (!dj_undef("start", values)) {
170
		props.start = values.start;
171
	} else {
172
		props.start = function () {
173
			return dojo.html.getOpacity(nodes[0]);
174
		};
175
	}
176
	if (!dj_undef("end", values)) {
177
		props.end = values.end;
178
	} else {
179
		dojo.raise("dojo.lfx.html.fade needs an end value");
180
	}
181
	var anim = dojo.lfx.propertyAnimation(nodes, [props], duration, easing);
182
	anim.connect("beforeBegin", function () {
183
		dojo.lfx.html._makeFadeable(nodes);
184
	});
185
	if (callback) {
186
		anim.connect("onEnd", function () {
187
			callback(nodes, anim);
188
		});
189
	}
190
	return anim;
191
};
192
dojo.lfx.html.fadeIn = function (nodes, duration, easing, callback) {
193
	return dojo.lfx.html.fade(nodes, {end:1}, duration, easing, callback);
194
};
195
dojo.lfx.html.fadeOut = function (nodes, duration, easing, callback) {
196
	return dojo.lfx.html.fade(nodes, {end:0}, duration, easing, callback);
197
};
198
dojo.lfx.html.fadeShow = function (nodes, duration, easing, callback) {
199
	nodes = dojo.lfx.html._byId(nodes);
200
	dojo.lang.forEach(nodes, function (node) {
201
		dojo.html.setOpacity(node, 0);
202
	});
203
	var anim = dojo.lfx.html.fadeIn(nodes, duration, easing, callback);
204
	anim.connect("beforeBegin", function () {
205
		if (dojo.lang.isArrayLike(nodes)) {
206
			dojo.lang.forEach(nodes, dojo.html.show);
207
		} else {
208
			dojo.html.show(nodes);
209
		}
210
	});
211
	return anim;
212
};
213
dojo.lfx.html.fadeHide = function (nodes, duration, easing, callback) {
214
	var anim = dojo.lfx.html.fadeOut(nodes, duration, easing, function () {
215
		if (dojo.lang.isArrayLike(nodes)) {
216
			dojo.lang.forEach(nodes, dojo.html.hide);
217
		} else {
218
			dojo.html.hide(nodes);
219
		}
220
		if (callback) {
221
			callback(nodes, anim);
222
		}
223
	});
224
	return anim;
225
};
226
dojo.lfx.html.wipeIn = function (nodes, duration, easing, callback) {
227
	nodes = dojo.lfx.html._byId(nodes);
228
	var anims = [];
229
	dojo.lang.forEach(nodes, function (node) {
230
		var oprop = {};
231
		var origTop, origLeft, origPosition;
232
		with (node.style) {
233
			origTop = top;
234
			origLeft = left;
235
			origPosition = position;
236
			top = "-9999px";
237
			left = "-9999px";
238
			position = "absolute";
239
			display = "";
240
		}
241
		var nodeHeight = dojo.html.getBorderBox(node).height;
242
		with (node.style) {
243
			top = origTop;
244
			left = origLeft;
245
			position = origPosition;
246
			display = "none";
247
		}
248
		var anim = dojo.lfx.propertyAnimation(node, {"height":{start:1, end:function () {
249
			return nodeHeight;
250
		}}}, duration, easing);
251
		anim.connect("beforeBegin", function () {
252
			oprop.overflow = node.style.overflow;
253
			oprop.height = node.style.height;
254
			with (node.style) {
255
				overflow = "hidden";
256
				height = "1px";
257
			}
258
			dojo.html.show(node);
259
		});
260
		anim.connect("onEnd", function () {
261
			with (node.style) {
262
				overflow = oprop.overflow;
263
				height = oprop.height;
264
			}
265
			if (callback) {
266
				callback(node, anim);
267
			}
268
		});
269
		anims.push(anim);
270
	});
271
	return dojo.lfx.combine(anims);
272
};
273
dojo.lfx.html.wipeOut = function (nodes, duration, easing, callback) {
274
	nodes = dojo.lfx.html._byId(nodes);
275
	var anims = [];
276
	dojo.lang.forEach(nodes, function (node) {
277
		var oprop = {};
278
		var anim = dojo.lfx.propertyAnimation(node, {"height":{start:function () {
279
			return dojo.html.getContentBox(node).height;
280
		}, end:1}}, duration, easing, {"beforeBegin":function () {
281
			oprop.overflow = node.style.overflow;
282
			oprop.height = node.style.height;
283
			with (node.style) {
284
				overflow = "hidden";
285
			}
286
			dojo.html.show(node);
287
		}, "onEnd":function () {
288
			dojo.html.hide(node);
289
			with (node.style) {
290
				overflow = oprop.overflow;
291
				height = oprop.height;
292
			}
293
			if (callback) {
294
				callback(node, anim);
295
			}
296
		}});
297
		anims.push(anim);
298
	});
299
	return dojo.lfx.combine(anims);
300
};
301
dojo.lfx.html.slideTo = function (nodes, coords, duration, easing, callback) {
302
	nodes = dojo.lfx.html._byId(nodes);
303
	var anims = [];
304
	var compute = dojo.html.getComputedStyle;
305
	if (dojo.lang.isArray(coords)) {
306
		dojo.deprecated("dojo.lfx.html.slideTo(node, array)", "use dojo.lfx.html.slideTo(node, {top: value, left: value});", "0.5");
307
		coords = {top:coords[0], left:coords[1]};
308
	}
309
	dojo.lang.forEach(nodes, function (node) {
310
		var top = null;
311
		var left = null;
312
		var init = (function () {
313
			var innerNode = node;
314
			return function () {
315
				var pos = compute(innerNode, "position");
316
				top = (pos == "absolute" ? node.offsetTop : parseInt(compute(node, "top")) || 0);
317
				left = (pos == "absolute" ? node.offsetLeft : parseInt(compute(node, "left")) || 0);
318
				if (!dojo.lang.inArray(["absolute", "relative"], pos)) {
319
					var ret = dojo.html.abs(innerNode, true);
320
					dojo.html.setStyleAttributes(innerNode, "position:absolute;top:" + ret.y + "px;left:" + ret.x + "px;");
321
					top = ret.y;
322
					left = ret.x;
323
				}
324
			};
325
		})();
326
		init();
327
		var anim = dojo.lfx.propertyAnimation(node, {"top":{start:top, end:(coords.top || 0)}, "left":{start:left, end:(coords.left || 0)}}, duration, easing, {"beforeBegin":init});
328
		if (callback) {
329
			anim.connect("onEnd", function () {
330
				callback(nodes, anim);
331
			});
332
		}
333
		anims.push(anim);
334
	});
335
	return dojo.lfx.combine(anims);
336
};
337
dojo.lfx.html.slideBy = function (nodes, coords, duration, easing, callback) {
338
	nodes = dojo.lfx.html._byId(nodes);
339
	var anims = [];
340
	var compute = dojo.html.getComputedStyle;
341
	if (dojo.lang.isArray(coords)) {
342
		dojo.deprecated("dojo.lfx.html.slideBy(node, array)", "use dojo.lfx.html.slideBy(node, {top: value, left: value});", "0.5");
343
		coords = {top:coords[0], left:coords[1]};
344
	}
345
	dojo.lang.forEach(nodes, function (node) {
346
		var top = null;
347
		var left = null;
348
		var init = (function () {
349
			var innerNode = node;
350
			return function () {
351
				var pos = compute(innerNode, "position");
352
				top = (pos == "absolute" ? node.offsetTop : parseInt(compute(node, "top")) || 0);
353
				left = (pos == "absolute" ? node.offsetLeft : parseInt(compute(node, "left")) || 0);
354
				if (!dojo.lang.inArray(["absolute", "relative"], pos)) {
355
					var ret = dojo.html.abs(innerNode, true);
356
					dojo.html.setStyleAttributes(innerNode, "position:absolute;top:" + ret.y + "px;left:" + ret.x + "px;");
357
					top = ret.y;
358
					left = ret.x;
359
				}
360
			};
361
		})();
362
		init();
363
		var anim = dojo.lfx.propertyAnimation(node, {"top":{start:top, end:top + (coords.top || 0)}, "left":{start:left, end:left + (coords.left || 0)}}, duration, easing).connect("beforeBegin", init);
364
		if (callback) {
365
			anim.connect("onEnd", function () {
366
				callback(nodes, anim);
367
			});
368
		}
369
		anims.push(anim);
370
	});
371
	return dojo.lfx.combine(anims);
372
};
373
dojo.lfx.html.explode = function (start, endNode, duration, easing, callback) {
374
	var h = dojo.html;
375
	start = dojo.byId(start);
376
	endNode = dojo.byId(endNode);
377
	var startCoords = h.toCoordinateObject(start, true);
378
	var outline = document.createElement("div");
379
	h.copyStyle(outline, endNode);
380
	if (endNode.explodeClassName) {
381
		outline.className = endNode.explodeClassName;
382
	}
383
	with (outline.style) {
384
		position = "absolute";
385
		display = "none";
386
		var backgroundStyle = h.getStyle(start, "background-color");
387
		backgroundColor = backgroundStyle ? backgroundStyle.toLowerCase() : "transparent";
388
		backgroundColor = (backgroundColor == "transparent") ? "rgb(221, 221, 221)" : backgroundColor;
389
	}
390
	dojo.body().appendChild(outline);
391
	with (endNode.style) {
392
		visibility = "hidden";
393
		display = "block";
394
	}
395
	var endCoords = h.toCoordinateObject(endNode, true);
396
	with (endNode.style) {
397
		display = "none";
398
		visibility = "visible";
399
	}
400
	var props = {opacity:{start:0.5, end:1}};
401
	dojo.lang.forEach(["height", "width", "top", "left"], function (type) {
402
		props[type] = {start:startCoords[type], end:endCoords[type]};
403
	});
404
	var anim = new dojo.lfx.propertyAnimation(outline, props, duration, easing, {"beforeBegin":function () {
405
		h.setDisplay(outline, "block");
406
	}, "onEnd":function () {
407
		h.setDisplay(endNode, "block");
408
		outline.parentNode.removeChild(outline);
409
	}});
410
	if (callback) {
411
		anim.connect("onEnd", function () {
412
			callback(endNode, anim);
413
		});
414
	}
415
	return anim;
416
};
417
dojo.lfx.html.implode = function (startNode, end, duration, easing, callback) {
418
	var h = dojo.html;
419
	startNode = dojo.byId(startNode);
420
	end = dojo.byId(end);
421
	var startCoords = dojo.html.toCoordinateObject(startNode, true);
422
	var endCoords = dojo.html.toCoordinateObject(end, true);
423
	var outline = document.createElement("div");
424
	dojo.html.copyStyle(outline, startNode);
425
	if (startNode.explodeClassName) {
426
		outline.className = startNode.explodeClassName;
427
	}
428
	dojo.html.setOpacity(outline, 0.3);
429
	with (outline.style) {
430
		position = "absolute";
431
		display = "none";
432
		backgroundColor = h.getStyle(startNode, "background-color").toLowerCase();
433
	}
434
	dojo.body().appendChild(outline);
435
	var props = {opacity:{start:1, end:0.5}};
436
	dojo.lang.forEach(["height", "width", "top", "left"], function (type) {
437
		props[type] = {start:startCoords[type], end:endCoords[type]};
438
	});
439
	var anim = new dojo.lfx.propertyAnimation(outline, props, duration, easing, {"beforeBegin":function () {
440
		dojo.html.hide(startNode);
441
		dojo.html.show(outline);
442
	}, "onEnd":function () {
443
		outline.parentNode.removeChild(outline);
444
	}});
445
	if (callback) {
446
		anim.connect("onEnd", function () {
447
			callback(startNode, anim);
448
		});
449
	}
450
	return anim;
451
};
452
dojo.lfx.html.highlight = function (nodes, startColor, duration, easing, callback) {
453
	nodes = dojo.lfx.html._byId(nodes);
454
	var anims = [];
455
	dojo.lang.forEach(nodes, function (node) {
456
		var color = dojo.html.getBackgroundColor(node);
457
		var bg = dojo.html.getStyle(node, "background-color").toLowerCase();
458
		var bgImage = dojo.html.getStyle(node, "background-image");
459
		var wasTransparent = (bg == "transparent" || bg == "rgba(0, 0, 0, 0)");
460
		while (color.length > 3) {
461
			color.pop();
462
		}
463
		var rgb = new dojo.gfx.color.Color(startColor);
464
		var endRgb = new dojo.gfx.color.Color(color);
465
		var anim = dojo.lfx.propertyAnimation(node, {"background-color":{start:rgb, end:endRgb}}, duration, easing, {"beforeBegin":function () {
466
			if (bgImage) {
467
				node.style.backgroundImage = "none";
468
			}
469
			node.style.backgroundColor = "rgb(" + rgb.toRgb().join(",") + ")";
470
		}, "onEnd":function () {
471
			if (bgImage) {
472
				node.style.backgroundImage = bgImage;
473
			}
474
			if (wasTransparent) {
475
				node.style.backgroundColor = "transparent";
476
			}
477
			if (callback) {
478
				callback(node, anim);
479
			}
480
		}});
481
		anims.push(anim);
482
	});
483
	return dojo.lfx.combine(anims);
484
};
485
dojo.lfx.html.unhighlight = function (nodes, endColor, duration, easing, callback) {
486
	nodes = dojo.lfx.html._byId(nodes);
487
	var anims = [];
488
	dojo.lang.forEach(nodes, function (node) {
489
		var color = new dojo.gfx.color.Color(dojo.html.getBackgroundColor(node));
490
		var rgb = new dojo.gfx.color.Color(endColor);
491
		var bgImage = dojo.html.getStyle(node, "background-image");
492
		var anim = dojo.lfx.propertyAnimation(node, {"background-color":{start:color, end:rgb}}, duration, easing, {"beforeBegin":function () {
493
			if (bgImage) {
494
				node.style.backgroundImage = "none";
495
			}
496
			node.style.backgroundColor = "rgb(" + color.toRgb().join(",") + ")";
497
		}, "onEnd":function () {
498
			if (callback) {
499
				callback(node, anim);
500
			}
501
		}});
502
		anims.push(anim);
503
	});
504
	return dojo.lfx.combine(anims);
505
};
506
dojo.lang.mixin(dojo.lfx, dojo.lfx.html);
507