Subversion Repositories Applications.papyrus

Rev

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