-
Notifications
You must be signed in to change notification settings - Fork 88
/
behavior.js
249 lines (234 loc) · 7.58 KB
/
behavior.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
define(["./_base/kernel", "./_base/lang", "./_base/array", "./_base/connect", "./query", "./domReady"],
function(dojo, lang, darray, connect, query, domReady){
// module:
// dojo/behavior
dojo.deprecated("dojo.behavior", "Use dojo/on with event delegation (on.selector())");
var Behavior = function(){
// summary:
// Deprecated. dojo/behavior's functionality can be achieved using event delegation using dojo/on
// and on.selector().
// description:
// A very simple, lightweight mechanism for applying code to
// existing documents, based around `dojo/query` (CSS3 selectors) for node selection,
// and a simple two-command API: `add()` and `apply()`;
//
// Behaviors apply to a given page, and are registered following the syntax
// options described by `add()` to match nodes to actions, or "behaviors".
//
// Added behaviors are applied to the current DOM when .apply() is called,
// matching only new nodes found since .apply() was last called.
function arrIn(obj, name){
if(!obj[name]){ obj[name] = []; }
return obj[name];
}
var _inc = 0;
function forIn(obj, scope, func){
var tmpObj = {};
for(var x in obj){
if(typeof tmpObj[x] == "undefined"){
if(!func){
scope(obj[x], x);
}else{
func.call(scope, obj[x], x);
}
}
}
}
// FIXME: need a better test so we don't exclude nightly Safari's!
this._behaviors = {};
this.add = function(/* Object */behaviorObj){
// summary:
// Add the specified behavior to the list of behaviors, ignoring existing
// matches.
// behaviorObj: Object
// The behavior object that will be added to behaviors list. The behaviors
// in the list will be applied the next time apply() is called.
// description:
// Add the specified behavior to the list of behaviors which will
// be applied the next time apply() is called. Calls to add() for
// an already existing behavior do not replace the previous rules,
// but are instead additive. New nodes which match the rule will
// have all add()-ed behaviors applied to them when matched.
//
// The "found" method is a generalized handler that's called as soon
// as the node matches the selector. Rules for values that follow also
// apply to the "found" key.
//
// The "on*" handlers are attached with `dojo.connect()`, using the
// matching node
//
// If the value corresponding to the ID key is a function and not a
// list, it's treated as though it was the value of "found".
//
// dojo/behavior.add() can be called any number of times before
// the DOM is ready. `dojo/behavior.apply()` is called automatically
// by `dojo.addOnLoad`, though can be called to re-apply previously added
// behaviors anytime the DOM changes.
//
// There are a variety of formats permitted in the behaviorObject
//
// example:
// Simple list of properties. "found" is special. "Found" is assumed if
// no property object for a given selector, and property is a function.
//
// | behavior.add({
// | "#id": {
// | "found": function(element){
// | // node match found
// | },
// | "onclick": function(evt){
// | // register onclick handler for found node
// | }
// | },
// | "#otherid": function(element){
// | // assumes "found" with this syntax
// | }
// | });
//
// example:
// If property is a string, a dojo.publish will be issued on the channel:
//
// | behavior.add({
// | // topic.publish() whenever class="noclick" found on anchors
// | "a.noclick": "/got/newAnchor",
// | "div.wrapper": {
// | "onclick": "/node/wasClicked"
// | }
// | });
// | topic.subscribe("/got/newAnchor", function(node){
// | // handle node finding when dojo/behavior.apply() is called,
// | // provided a newly matched node is found.
// | });
//
// example:
// Scoping can be accomplished by passing an object as a property to
// a connection handle (on*):
//
// | behavior.add({
// | "#id": {
// | // like calling dojo.hitch(foo,"bar"). execute foo.bar() in scope of foo
// | "onmouseenter": { targetObj: foo, targetFunc: "bar" },
// | "onmouseleave": { targetObj: foo, targetFunc: "baz" }
// | }
// | });
//
// example:
// Behaviors match on CSS3 Selectors, powered by dojo/query. Example selectors:
//
// | behavior.add({
// | // match all direct descendants
// | "#id4 > *": function(element){
// | // ...
// | },
// |
// | // match the first child node that's an element
// | "#id4 > :first-child": { ... },
// |
// | // match the last child node that's an element
// | "#id4 > :last-child": { ... },
// |
// | // all elements of type tagname
// | "tagname": {
// | // ...
// | },
// |
// | "tagname1 tagname2 tagname3": {
// | // ...
// | },
// |
// | ".classname": {
// | // ...
// | },
// |
// | "tagname.classname": {
// | // ...
// | }
// | });
//
forIn(behaviorObj, this, function(behavior, name){
var tBehavior = arrIn(this._behaviors, name);
if(typeof tBehavior["id"] != "number"){
tBehavior.id = _inc++;
}
var cversion = [];
tBehavior.push(cversion);
if((lang.isString(behavior))||(lang.isFunction(behavior))){
behavior = { found: behavior };
}
forIn(behavior, function(rule, ruleName){
arrIn(cversion, ruleName).push(rule);
});
});
};
var _applyToNode = function(node, action, ruleSetName){
if(lang.isString(action)){
if(ruleSetName == "found"){
connect.publish(action, [ node ]);
}else{
connect.connect(node, ruleSetName, function(){
connect.publish(action, arguments);
});
}
}else if(lang.isFunction(action)){
if(ruleSetName == "found"){
action(node);
}else{
connect.connect(node, ruleSetName, action);
}
}
};
this.apply = function(){
// summary:
// Applies all currently registered behaviors to the document.
//
// description:
// Applies all currently registered behaviors to the document,
// taking care to ensure that only incremental updates are made
// since the last time add() or apply() were called.
//
// If new matching nodes have been added, all rules in a behavior will be
// applied to that node. For previously matched nodes, only
// behaviors which have been added since the last call to apply()
// will be added to the nodes.
//
// apply() is called once automatically by `dojo.addOnLoad`, so
// registering behaviors with `dojo/behavior.add()` before the DOM is
// ready is acceptable, provided the dojo.behavior module is ready.
//
// Calling appy() manually after manipulating the DOM is required
// to rescan the DOM and apply newly .add()ed behaviors, or to match
// nodes that match existing behaviors when those nodes are added to
// the DOM.
//
forIn(this._behaviors, function(tBehavior, id){
query(id).forEach(
function(elem){
var runFrom = 0;
var bid = "_dj_behavior_"+tBehavior.id;
if(typeof elem[bid] == "number"){
runFrom = elem[bid];
if(runFrom == (tBehavior.length)){
return;
}
}
// run through the versions, applying newer rules at each step
for(var x=runFrom, tver; tver = tBehavior[x]; x++){
forIn(tver, function(ruleSet, ruleSetName){
if(lang.isArray(ruleSet)){
darray.forEach(ruleSet, function(action){
_applyToNode(elem, action, ruleSetName);
});
}
});
}
// ensure that re-application only adds new rules to the node
elem[bid] = tBehavior.length;
}
);
});
};
};
dojo.behavior = new Behavior();
domReady( function(){ dojo.behavior.apply(); } );
return dojo.behavior;
});