/* See license.txt for terms of usage */

define([
    "lib/domplate",
    "lib/lib",
    "lib/trace",
    "tabs/baseTab",
    "lib/options",
    "app/objectTree",
    "app/objectGraphGenerator",
    "app/objectTableView"
],

function(Domplate, Lib, FBTrace, BaseTab, Options, ObjectTree, ObjectGraphGenerator,
    ObjectTableView) {
with (Domplate) {

// ********************************************************************************************* //
// Home Tab

function GraphTab()
{
}

GraphTab.prototype = Lib.extend(BaseTab.prototype,
{
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
    // Tab

    id: "Graph",
    label: "Graph",

    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
    // Content

    noSelection:
        SPAN("No object selected"),

    noGraph:
        SPAN("No graph found"),

    onUpdateBody: function(tabView, body)
    {
        BaseTab.prototype.onUpdateBody.apply(this, arguments);

        var content = body.querySelector(".tabContent");
        this.selection = tabView.selection;
        if (!this.selection)
        {
            this.noSelection.replace({}, content);
            return;
        }

        var searchId = this.tabView.analyzer.getSearchId();
        var generator = new ObjectGraphGenerator(searchId);
        this.graph = generator.findGraph(this.selection);

        if (Lib.hasProperties(this.graph))
        {
            var tree = new ObjectTree(this.graph);
            tree.append(content, true);
        }
        else
        {
            this.noGraph.replace({}, content);
        }
    },

    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
    // Search

    onSearch: function(text, keyCode)
    {
        if (!this.graph)
            return;

        var results = [];

        if (text)
        {
            var caseSensitive = Options.getPref("search.caseSensitive");
            if (!caseSensitive)
                text = text.toLowerCase();

            var iterator = new GraphIterator(this.graph);
            var searchId = this.tabView.analyzer.getSearchId();
            iterator.run(searchId, function(obj)
            {
                var name = obj ? obj.name : "";
                if (!caseSensitive)
                    name = name.toLowerCase();

                if (name.indexOf(text) >= 0)
                    results.push(obj);
            });
        }

        var content = this._body.querySelector(".tabContent");
        if (!results.length)
            return;

        var table = new ObjectTableView();
        table.render(content, results);

        return true;
    },

    getSearchOptions: function()
    {
        var items = BaseTab.prototype.getSearchOptions.apply(this, arguments);

        return items;
    },
});

// ********************************************************************************************* //

function GraphIterator(graph)
{
    this.graph = graph;
}

GraphIterator.prototype =
{
    run: function(searchId, callback)
    {
        this.searchId = searchId;
        this.callback = callback;

        this._iterate(this.graph);
    },

    _iterate: function(obj)
    {
        if (obj.searchMark == this.searchId)
            return;

        obj.searchMark = this.searchId;

        this.callback(obj._o);

        for each (var child in obj)
        {
            if (child instanceof ObjectGraphGenerator.Object)
                this._iterate(child);
        }
    }
}

// ********************************************************************************************* //

return GraphTab;

// ********************************************************************************************* //
}});

@@ -1412,24 +1412,147 @@
 election);%0A%0A
+        this.renderGraph(content);%0A    %7D,%0A%0A    renderGraph: function(parentNode)%0A    %7B%0A        Lib.eraseNode(parentNode);%0A%0A
         if (
@@ -1667,23 +1667,26 @@
 .append(
-content
+parentNode
 , true);
@@ -1748,39 +1748,42 @@
 aph.replace(%7B%7D, 
-content
+parentNode
 );%0A        %7D%0A   
@@ -2678,32 +2678,110 @@
 (%22.tabContent%22);
+%0A%0A        // Show search results if any; otherwise display the original graph.
 %0A        if (!re
@@ -2777,17 +2777,16 @@
     if (
-!
 results.
@@ -2797,37 +2797,30 @@
 th)%0A        
+%7B%0A
     
-return;%0A%0A
         var 
@@ -2858,16 +2858,20 @@
         
+    
 table.re
@@ -2889,24 +2889,106 @@
 t, results);
+%0A        %7D%0A        else%0A        %7B%0A            this.renderGraph(content);%0A        %7D
 %0A%0A        re

Undo search results in Graph tab