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.behavior");
12
dojo.require("dojo.event.*");
13
dojo.require("dojo.experimental");
14
dojo.experimental("dojo.behavior");
15
dojo.behavior = new function () {
16
	function arrIn(obj, name) {
17
		if (!obj[name]) {
18
			obj[name] = [];
19
		}
20
		return obj[name];
21
	}
22
	function forIn(obj, scope, func) {
23
		var tmpObj = {};
24
		for (var x in obj) {
25
			if (typeof tmpObj[x] == "undefined") {
26
				if (!func) {
27
					scope(obj[x], x);
28
				} else {
29
					func.call(scope, obj[x], x);
30
				}
31
			}
32
		}
33
	}
34
	this.behaviors = {};
35
	this.add = function (behaviorObj) {
36
		var tmpObj = {};
37
		forIn(behaviorObj, this, function (behavior, name) {
38
			var tBehavior = arrIn(this.behaviors, name);
39
			if ((dojo.lang.isString(behavior)) || (dojo.lang.isFunction(behavior))) {
40
				behavior = {found:behavior};
41
			}
42
			forIn(behavior, function (rule, ruleName) {
43
				arrIn(tBehavior, ruleName).push(rule);
44
			});
45
		});
46
	};
47
	this.apply = function () {
48
		dojo.profile.start("dojo.behavior.apply");
49
		var r = dojo.render.html;
50
		var safariGoodEnough = (!r.safari);
51
		if (r.safari) {
52
			var uas = r.UA.split("AppleWebKit/")[1];
53
			if (parseInt(uas.match(/[0-9.]{3,}/)) >= 420) {
54
				safariGoodEnough = true;
55
			}
56
		}
57
		if ((dj_undef("behaviorFastParse", djConfig) ? (safariGoodEnough) : djConfig["behaviorFastParse"])) {
58
			this.applyFast();
59
		} else {
60
			this.applySlow();
61
		}
62
		dojo.profile.end("dojo.behavior.apply");
63
	};
64
	this.matchCache = {};
65
	this.elementsById = function (id, handleRemoved) {
66
		var removed = [];
67
		var added = [];
68
		arrIn(this.matchCache, id);
69
		if (handleRemoved) {
70
			var nodes = this.matchCache[id];
71
			for (var x = 0; x < nodes.length; x++) {
72
				if (nodes[x].id != "") {
73
					removed.push(nodes[x]);
74
					nodes.splice(x, 1);
75
					x--;
76
				}
77
			}
78
		}
79
		var tElem = dojo.byId(id);
80
		while (tElem) {
81
			if (!tElem["idcached"]) {
82
				added.push(tElem);
83
			}
84
			tElem.id = "";
85
			tElem = dojo.byId(id);
86
		}
87
		this.matchCache[id] = this.matchCache[id].concat(added);
88
		dojo.lang.forEach(this.matchCache[id], function (node) {
89
			node.id = id;
90
			node.idcached = true;
91
		});
92
		return {"removed":removed, "added":added, "match":this.matchCache[id]};
93
	};
94
	this.applyToNode = function (node, action, ruleSetName) {
95
		if (typeof action == "string") {
96
			dojo.event.topic.registerPublisher(action, node, ruleSetName);
97
		} else {
98
			if (typeof action == "function") {
99
				if (ruleSetName == "found") {
100
					action(node);
101
				} else {
102
					dojo.event.connect(node, ruleSetName, action);
103
				}
104
			} else {
105
				action.srcObj = node;
106
				action.srcFunc = ruleSetName;
107
				dojo.event.kwConnect(action);
108
			}
109
		}
110
	};
111
	this.applyFast = function () {
112
		dojo.profile.start("dojo.behavior.applyFast");
113
		forIn(this.behaviors, function (tBehavior, id) {
114
			var elems = dojo.behavior.elementsById(id);
115
			dojo.lang.forEach(elems.added, function (elem) {
116
				forIn(tBehavior, function (ruleSet, ruleSetName) {
117
					if (dojo.lang.isArray(ruleSet)) {
118
						dojo.lang.forEach(ruleSet, function (action) {
119
							dojo.behavior.applyToNode(elem, action, ruleSetName);
120
						});
121
					}
122
				});
123
			});
124
		});
125
		dojo.profile.end("dojo.behavior.applyFast");
126
	};
127
	this.applySlow = function () {
128
		dojo.profile.start("dojo.behavior.applySlow");
129
		var all = document.getElementsByTagName("*");
130
		var allLen = all.length;
131
		for (var x = 0; x < allLen; x++) {
132
			var elem = all[x];
133
			if ((elem.id) && (!elem["behaviorAdded"]) && (this.behaviors[elem.id])) {
134
				elem["behaviorAdded"] = true;
135
				forIn(this.behaviors[elem.id], function (ruleSet, ruleSetName) {
136
					if (dojo.lang.isArray(ruleSet)) {
137
						dojo.lang.forEach(ruleSet, function (action) {
138
							dojo.behavior.applyToNode(elem, action, ruleSetName);
139
						});
140
					}
141
				});
142
			}
143
		}
144
		dojo.profile.end("dojo.behavior.applySlow");
145
	};
146
};
147
dojo.addOnLoad(dojo.behavior, "apply");
148