Zebra API Doc

zebra.util.findInTree()

findInTree

(
  • root
  • path
  • eq
  • cb
)

Find by xpath-like path an element in a tree-like structure. The method is flexible way to look up elements in tree structures. The only requirements the passed tree-like structure has to follow is declaring a "kids" array field if the element has a children element. To understand if the given tree element matches the current path fragment a special equality function has to be passed.

   var treeLikeRoot = { 
       value : "Root", 
       kids : [
           { value: "Item 1" },
           { value: "Item 2" }
       ]
   };

   zebra.util.findInTree(treeLikeRoot, 
                         "/Root/item1", 
                         function(item, fragment) {
                             return item.value == fragment;
                         },
                         function(foundElement) {
                            ...
                            // true means stop lookaup
                            return true;   
                         });

Parameters:

  • root Object

    a tree root element. If the element has a children element it has to declare "kids" field. This field is an array of all children elements

  • path String

    a xpath-like path. The path has to satisfy number of requirements and rules:

    • "/"" means lookup among all direct children elements
    • "//"" means lookup among all children elements recursively
    • "*" means any path value -[@attr=100] means number attribute -[@attr=true] means boolean attribute -[@attr='value'] means string attribute
    • Path has always starts from "/" or "//"
    • Path element always has to be defined: "*" or an symbolic name

    Path examples:

    • "//*" traverse all tree elements
    • "//*[@a=10]" traverse all tree elements that has an attribute "a" that equals 10
    • "/Root/Item" find an element by exact path
  • eq Function

    an equality function. The function gets current evaluated tree element
    and a path fragment against which the tree element has to be evaluated. It is expected the method returns boolean value to say if the given passed tree element matches the path fragment.

  • cb Function

    callback function that is called every time a new tree element matches the given path fragment. The function has to return true if the tree look up has to be stopped