PDA

View Full Version : Ext.data.node improvement



DhakouaniM
10 May 2008, 11:25 AM
Sometimes, you cannot define nodes by their only id, the aim of this mod is to allow a user to specify some more arguments when using the 'contains' and 'isAncestor' methods:

Ext.override(Ext.data.Node, {
contains: function(node, attr, direct) {
/* Inputs:
* - node: the child node to look for in this node
* - attr: the attribute name to compare to determine whether two nodes
* are identical, or false to compare the objects
* - direct: true to look for immediate children only, false to look for
* all children (children of children..)
* Example: node1.contains(node2) ==> returns true if one of node1 children
* is the node2 object
* Example: node1.contains(node2, false, true) ==> returns true if one of
* node1 immediate children is the node2 object
* Example: node1.contains(node2, 'someOtherId') ==> returns true if one of
* node1 children has its "'child'.attributes.someOtherId" equal to its
* "node2.attributes.someOtherId"
*/
var direct = direct || false;
var attr = attr || false;
if (attr == '')
attr = false;
if (!direct)
return node.isAncestor(this, attr);
// All of the following applies to direct children only
if (!attr) {
// If no attribute name is specified, compare the objects
for (var i = 0 ; i < this.childNodes.length ; i++) {
if (this.childNodes[i] == node)
return true;
}
return false;
} else {
// If an attribute name is specified, compare the attribute's values
for (var i = 0 ; i < this.childNodes.length ; i++) {
if (this.childNodes[i].attributes[attr] = node.attributes[attr])
return true;
}
}
},
isAncestor: function(node, attr) {
var attr = attr || false;
if (attr == '')
attr = false;
var p = this.parentNode;
while (p) {
if (attr) {
// If an attribute name is specified, compare the attribute's values
if (node.attributes[attr] == p.attributes[attr])
return true;
} else {
// If no attribute name is specified, compare the objects
if (p == node)
return true;
}
p = p.parentNode;
}
return false;
}