]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/behavior.js.uncompressed.js
1 define("dojo/behavior", ["./_base/kernel", "./_base/lang", "./_base/array", "./_base/connect", "./query", "./ready"], function(dojo
, lang
, darray
, connect
, query
, ready
) {
8 dojo
.behavior
= new function(){
10 // Utility for unobtrusive/progressive event binding, DOM traversal,
15 // A very simple, lightweight mechanism for applying code to
16 // existing documents, based around `dojo.query` (CSS3 selectors) for node selection,
17 // and a simple two-command API: `dojo.behavior.add()` and `dojo.behavior.apply()`;
19 // Behaviors apply to a given page, and are registered following the syntax
20 // options described by `dojo.behavior.add` to match nodes to actions, or "behaviors".
22 // Added behaviors are applied to the current DOM when .apply() is called,
23 // matching only new nodes found since .apply() was last called.
25 function arrIn(obj
, name
){
26 if(!obj
[name
]){ obj
[name
] = []; }
32 function forIn(obj
, scope
, func
){
35 if(typeof tmpObj
[x
] == "undefined"){
39 func
.call(scope
, obj
[x
], x
);
45 // FIXME: need a better test so we don't exclude nightly Safari's!
47 this.add = function(/* Object */behaviorObj
){
49 // Add the specified behavior to the list of behaviors, ignoring existing
51 // behaviorObj: Object
52 // The behavior object that will be added to behaviors list. The behaviors
53 // in the list will be applied the next time apply() is called.
55 // Add the specified behavior to the list of behaviors which will
56 // be applied the next time apply() is called. Calls to add() for
57 // an already existing behavior do not replace the previous rules,
58 // but are instead additive. New nodes which match the rule will
59 // have all add()-ed behaviors applied to them when matched.
61 // The "found" method is a generalized handler that's called as soon
62 // as the node matches the selector. Rules for values that follow also
63 // apply to the "found" key.
65 // The "on*" handlers are attached with `dojo.connect()`, using the
68 // If the value corresponding to the ID key is a function and not a
69 // list, it's treated as though it was the value of "found".
71 // dojo.behavior.add() can be called any number of times before
72 // the DOM is ready. `dojo.behavior.apply()` is called automatically
73 // by `dojo.addOnLoad`, though can be called to re-apply previously added
74 // behaviors anytime the DOM changes.
76 // There are a variety of formats permitted in the behaviorObject
79 // Simple list of properties. "found" is special. "Found" is assumed if
80 // no property object for a given selector, and property is a function.
82 // | dojo.behavior.add({
84 // | "found": function(element){
85 // | // node match found
87 // | "onclick": function(evt){
88 // | // register onclick handler for found node
91 // | "#otherid": function(element){
92 // | // assumes "found" with this syntax
97 // If property is a string, a dojo.publish will be issued on the channel:
99 // | dojo.behavior.add({
100 // | // dojo.publish() whenever class="noclick" found on anchors
101 // | "a.noclick": "/got/newAnchor",
102 // | "div.wrapper": {
103 // | "onclick": "/node/wasClicked"
106 // | dojo.subscribe("/got/newAnchor", function(node){
107 // | // handle node finding when dojo.behavior.apply() is called,
108 // | // provided a newly matched node is found.
112 // Scoping can be accomplished by passing an object as a property to
113 // a connection handle (on*):
115 // | dojo.behavior.add({
117 // | // like calling dojo.hitch(foo,"bar"). execute foo.bar() in scope of foo
118 // | "onmouseenter": { targetObj: foo, targetFunc: "bar" },
119 // | "onmouseleave": { targetObj: foo, targetFunc: "baz" }
124 // Bahaviors match on CSS3 Selectors, powered by dojo.query. Example selectors:
126 // | dojo.behavior.add({
127 // | // match all direct descendants
128 // | "#id4 > *": function(element){
132 // | // match the first child node that's an element
133 // | "#id4 > :first-child": { ... },
135 // | // match the last child node that's an element
136 // | "#id4 > :last-child": { ... },
138 // | // all elements of type tagname
143 // | "tagname1 tagname2 tagname3": {
151 // | "tagname.classname": {
157 forIn(behaviorObj
, this, function(behavior
, name
){
158 var tBehavior
= arrIn(this._behaviors
, name
);
159 if(typeof tBehavior
["id"] != "number"){
160 tBehavior
.id
= _inc
++;
163 tBehavior
.push(cversion
);
164 if((lang
.isString(behavior
))||(lang
.isFunction(behavior
))){
165 behavior
= { found
: behavior
};
167 forIn(behavior
, function(rule
, ruleName
){
168 arrIn(cversion
, ruleName
).push(rule
);
173 var _applyToNode = function(node
, action
, ruleSetName
){
174 if(lang
.isString(action
)){
175 if(ruleSetName
== "found"){
176 connect
.publish(action
, [ node
]);
178 connect
.connect(node
, ruleSetName
, function(){
179 connect
.publish(action
, arguments
);
182 }else if(lang
.isFunction(action
)){
183 if(ruleSetName
== "found"){
186 connect
.connect(node
, ruleSetName
, action
);
191 this.apply = function(){
193 // Applies all currently registered behaviors to the document.
196 // Applies all currently registered behaviors to the document,
197 // taking care to ensure that only incremental updates are made
198 // since the last time add() or apply() were called.
200 // If new matching nodes have been added, all rules in a behavior will be
201 // applied to that node. For previously matched nodes, only
202 // behaviors which have been added since the last call to apply()
203 // will be added to the nodes.
205 // apply() is called once automatically by `dojo.addOnLoad`, so
206 // registering behaviors with `dojo.behavior.add` before the DOM is
207 // ready is acceptable, provided the dojo.behavior module is ready.
209 // Calling appy() manually after manipulating the DOM is required
210 // to rescan the DOM and apply newly .add()ed behaviors, or to match
211 // nodes that match existing behaviors when those nodes are added to
214 forIn(this._behaviors
, function(tBehavior
, id
){
218 var bid
= "_dj_behavior_"+tBehavior
.id
;
219 if(typeof elem
[bid
] == "number"){
221 if(runFrom
== (tBehavior
.length
)){
225 // run through the versions, applying newer rules at each step
227 for(var x
=runFrom
, tver
; tver
= tBehavior
[x
]; x
++){
228 forIn(tver
, function(ruleSet
, ruleSetName
){
229 if(lang
.isArray(ruleSet
)){
230 darray
.forEach(ruleSet
, function(action
){
231 _applyToNode(elem
, action
, ruleSetName
);
237 // ensure that re-application only adds new rules to the node
238 elem
[bid
] = tBehavior
.length
;
245 ready(dojo
.behavior
, "apply"); // FIXME: should this use a priority? before/after parser priority?
247 return dojo
.behavior
;