Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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