2010-11-15 07:39:52 +00:00
|
|
|
/*
|
2011-11-08 16:40:44 +00:00
|
|
|
Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
|
2010-11-15 07:39:52 +00:00
|
|
|
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
|
|
|
see: http://dojotoolkit.org/license for details
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-03-04 16:02:28 +00:00
|
|
|
if(!dojo._hasResource["dojo.behavior"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
|
dojo._hasResource["dojo.behavior"] = true;
|
2010-11-15 07:39:52 +00:00
|
|
|
dojo.provide("dojo.behavior");
|
2011-03-04 16:02:28 +00:00
|
|
|
|
2011-11-08 16:40:44 +00:00
|
|
|
|
2011-03-04 16:02:28 +00:00
|
|
|
dojo.behavior = new function(){
|
2011-11-08 16:40:44 +00:00
|
|
|
// summary:
|
2011-03-04 16:02:28 +00:00
|
|
|
// Utility for unobtrusive/progressive event binding, DOM traversal,
|
|
|
|
// and manipulation.
|
|
|
|
//
|
|
|
|
// description:
|
2011-11-08 16:40:44 +00:00
|
|
|
//
|
|
|
|
// A very simple, lightweight mechanism for applying code to
|
|
|
|
// existing documents, based around `dojo.query` (CSS3 selectors) for node selection,
|
2011-03-04 16:02:28 +00:00
|
|
|
// and a simple two-command API: `dojo.behavior.add()` and `dojo.behavior.apply()`;
|
2011-11-08 16:40:44 +00:00
|
|
|
//
|
|
|
|
// Behaviors apply to a given page, and are registered following the syntax
|
2011-03-04 16:02:28 +00:00
|
|
|
// options described by `dojo.behavior.add` to match nodes to actions, or "behaviors".
|
2011-11-08 16:40:44 +00:00
|
|
|
//
|
2011-03-04 16:02:28 +00:00
|
|
|
// Added behaviors are applied to the current DOM when .apply() is called,
|
2011-11-08 16:40:44 +00:00
|
|
|
// matching only new nodes found since .apply() was last called.
|
|
|
|
//
|
2011-03-04 16:02:28 +00:00
|
|
|
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
|
2011-11-08 16:40:44 +00:00
|
|
|
// matches.
|
2011-03-04 16:02:28 +00:00
|
|
|
//
|
|
|
|
// 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.
|
2011-11-08 16:40:44 +00:00
|
|
|
//
|
2011-03-04 16:02:28 +00:00
|
|
|
// 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.
|
2011-11-08 16:40:44 +00:00
|
|
|
//
|
|
|
|
// The "on*" handlers are attached with `dojo.connect()`, using the
|
2011-03-04 16:02:28 +00:00
|
|
|
// matching node
|
2011-11-08 16:40:44 +00:00
|
|
|
//
|
2011-03-04 16:02:28 +00:00
|
|
|
// 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".
|
|
|
|
//
|
2011-11-08 16:40:44 +00:00
|
|
|
// dojo.behavior.add() can be called any number of times before
|
2011-03-04 16:02:28 +00:00
|
|
|
// 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
|
2011-11-08 16:40:44 +00:00
|
|
|
//
|
2011-03-04 16:02:28 +00:00
|
|
|
// example:
|
2011-11-08 16:40:44 +00:00
|
|
|
// Simple list of properties. "found" is special. "Found" is assumed if
|
2011-03-04 16:02:28 +00:00
|
|
|
// no property object for a given selector, and property is a function.
|
|
|
|
//
|
|
|
|
// | dojo.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
|
|
|
|
// | }
|
|
|
|
// | });
|
|
|
|
//
|
2011-11-08 16:40:44 +00:00
|
|
|
// example:
|
2011-03-04 16:02:28 +00:00
|
|
|
// If property is a string, a dojo.publish will be issued on the channel:
|
|
|
|
//
|
|
|
|
// | dojo.behavior.add({
|
|
|
|
// | // dojo.publish() whenever class="noclick" found on anchors
|
|
|
|
// | "a.noclick": "/got/newAnchor",
|
|
|
|
// | "div.wrapper": {
|
|
|
|
// | "onclick": "/node/wasClicked"
|
|
|
|
// | }
|
|
|
|
// | });
|
|
|
|
// | dojo.subscribe("/got/newAnchor", function(node){
|
2011-11-08 16:40:44 +00:00
|
|
|
// | // handle node finding when dojo.behavior.apply() is called,
|
2011-03-04 16:02:28 +00:00
|
|
|
// | // provided a newly matched node is found.
|
|
|
|
// | });
|
|
|
|
//
|
|
|
|
// example:
|
2011-11-08 16:40:44 +00:00
|
|
|
// Scoping can be accomplished by passing an object as a property to
|
2011-03-04 16:02:28 +00:00
|
|
|
// a connection handle (on*):
|
2011-11-08 16:40:44 +00:00
|
|
|
//
|
|
|
|
// | dojo.behavior.add({
|
2011-03-04 16:02:28 +00:00
|
|
|
// | "#id": {
|
|
|
|
// | // like calling dojo.hitch(foo,"bar"). execute foo.bar() in scope of foo
|
|
|
|
// | "onmouseenter": { targetObj: foo, targetFunc: "bar" },
|
|
|
|
// | "onmouseleave": { targetObj: foo, targetFunc: "baz" }
|
|
|
|
// | }
|
|
|
|
// | });
|
|
|
|
//
|
2011-11-08 16:40:44 +00:00
|
|
|
// example:
|
2011-03-04 16:02:28 +00:00
|
|
|
// Bahaviors match on CSS3 Selectors, powered by dojo.query. Example selectors:
|
|
|
|
//
|
|
|
|
// | dojo.behavior.add({
|
|
|
|
// | // match all direct descendants
|
|
|
|
// | "#id4 > *": function(element){
|
|
|
|
// | // ...
|
|
|
|
// | },
|
2011-11-08 16:40:44 +00:00
|
|
|
// |
|
2011-03-04 16:02:28 +00:00
|
|
|
// | // match the first child node that's an element
|
|
|
|
// | "#id4 > :first-child": { ... },
|
2011-11-08 16:40:44 +00:00
|
|
|
// |
|
2011-03-04 16:02:28 +00:00
|
|
|
// | // match the last child node that's an element
|
|
|
|
// | "#id4 > :last-child": { ... },
|
2011-11-08 16:40:44 +00:00
|
|
|
// |
|
2011-03-04 16:02:28 +00:00
|
|
|
// | // all elements of type tagname
|
|
|
|
// | "tagname": {
|
|
|
|
// | // ...
|
|
|
|
// | },
|
2011-11-08 16:40:44 +00:00
|
|
|
// |
|
2011-03-04 16:02:28 +00:00
|
|
|
// | "tagname1 tagname2 tagname3": {
|
|
|
|
// | // ...
|
|
|
|
// | },
|
2011-11-08 16:40:44 +00:00
|
|
|
// |
|
2011-03-04 16:02:28 +00:00
|
|
|
// | ".classname": {
|
|
|
|
// | // ...
|
|
|
|
// | },
|
2011-11-08 16:40:44 +00:00
|
|
|
// |
|
2011-03-04 16:02:28 +00:00
|
|
|
// | "tagname.classname": {
|
|
|
|
// | // ...
|
|
|
|
// | }
|
|
|
|
// | });
|
2011-11-08 16:40:44 +00:00
|
|
|
//
|
2011-03-04 16:02:28 +00:00
|
|
|
|
|
|
|
var tmpObj = {};
|
|
|
|
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((dojo.isString(behavior))||(dojo.isFunction(behavior))){
|
|
|
|
behavior = { found: behavior };
|
|
|
|
}
|
|
|
|
forIn(behavior, function(rule, ruleName){
|
|
|
|
arrIn(cversion, ruleName).push(rule);
|
|
|
|
});
|
|
|
|
});
|
2011-11-08 16:40:44 +00:00
|
|
|
};
|
2011-03-04 16:02:28 +00:00
|
|
|
|
|
|
|
var _applyToNode = function(node, action, ruleSetName){
|
|
|
|
if(dojo.isString(action)){
|
|
|
|
if(ruleSetName == "found"){
|
|
|
|
dojo.publish(action, [ node ]);
|
|
|
|
}else{
|
|
|
|
dojo.connect(node, ruleSetName, function(){
|
|
|
|
dojo.publish(action, arguments);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}else if(dojo.isFunction(action)){
|
|
|
|
if(ruleSetName == "found"){
|
|
|
|
action(node);
|
|
|
|
}else{
|
|
|
|
dojo.connect(node, ruleSetName, action);
|
|
|
|
}
|
|
|
|
}
|
2011-11-08 16:40:44 +00:00
|
|
|
};
|
2011-03-04 16:02:28 +00:00
|
|
|
|
|
|
|
this.apply = function(){
|
|
|
|
// summary:
|
|
|
|
// Applies all currently registered behaviors to the document.
|
2011-11-08 16:40:44 +00:00
|
|
|
//
|
2011-03-04 16:02:28 +00:00
|
|
|
// description:
|
|
|
|
// Applies all currently registered behaviors to the document,
|
|
|
|
// taking care to ensure that only incremental updates are made
|
2011-11-08 16:40:44 +00:00
|
|
|
// since the last time add() or apply() were called.
|
|
|
|
//
|
2011-03-04 16:02:28 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2011-11-08 16:40:44 +00:00
|
|
|
// apply() is called once automatically by `dojo.addOnLoad`, so
|
2011-03-04 16:02:28 +00:00
|
|
|
// registering behaviors with `dojo.behavior.add` before the DOM is
|
|
|
|
// ready is acceptable, provided the dojo.behavior module is ready.
|
2011-11-08 16:40:44 +00:00
|
|
|
//
|
|
|
|
// Calling appy() manually after manipulating the DOM is required
|
2011-03-04 16:02:28 +00:00
|
|
|
// to rescan the DOM and apply newly .add()ed behaviors, or to match
|
2011-11-08 16:40:44 +00:00
|
|
|
// nodes that match existing behaviors when those nodes are added to
|
2011-03-04 16:02:28 +00:00
|
|
|
// the DOM.
|
2011-11-08 16:40:44 +00:00
|
|
|
//
|
2011-03-04 16:02:28 +00:00
|
|
|
forIn(this._behaviors, function(tBehavior, id){
|
2011-11-08 16:40:44 +00:00
|
|
|
dojo.query(id).forEach(
|
2011-03-04 16:02:28 +00:00
|
|
|
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(dojo.isArray(ruleSet)){
|
|
|
|
dojo.forEach(ruleSet, function(action){
|
|
|
|
_applyToNode(elem, action, ruleSetName);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure that re-application only adds new rules to the node
|
|
|
|
elem[bid] = tBehavior.length;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2011-11-08 16:40:44 +00:00
|
|
|
};
|
|
|
|
};
|
2011-03-04 16:02:28 +00:00
|
|
|
|
|
|
|
dojo.addOnLoad(dojo.behavior, "apply");
|
|
|
|
|
2010-11-15 07:39:52 +00:00
|
|
|
}
|