[%# 1.0@bugzilla.org %]
[%# The contents of this file are subject to the Mozilla Public
  # License Version 1.1 (the "License"); you may not use this file
  # except in compliance with the License. You may obtain a copy of
  # the License at http://www.mozilla.org/MPL/
  #
  # Software distributed under the License is distributed on an "AS
  # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  # implied. See the License for the specific language governing
  # rights and limitations under the License.
  #
  # The Original Code is the Bugzilla Testopia System.
  #
  # The Initial Developer of the Original Code is Greg Hendricks.
  # Portions created by Greg Hendricks are Copyright (C) 2006
  # Novell. All Rights Reserved.
  #
  # Contributor(s): Greg Hendricks <ghendricks@novell.com>
  #%]

[% PROCESS global/variables.none.tmpl %]
[% PROCESS testopia/style.none.tmpl %]
[% PROCESS testopia/blocks.html.tmpl %]
[% PROCESS testopia/search/variables.none.tmpl %]

[% javascript_urls.push("extensions/Testopia/extjs/examples/ux/Portal.js") %]
[% javascript_urls.push("extensions/Testopia/extjs/examples/ux/PortalColumn.js") %]
[% javascript_urls.push("extensions/Testopia/extjs/examples/ux/Portlet.js") %]

[% title = "Testopia Product Dashboard" %]
[% PROCESS global/header.html.tmpl %]


<!-- ENDLIBS -->

<style type="text/css">
.x-panel-body p {
    margin:5px;
}
.x-column-layout-ct .x-panel {
    margin-bottom:10px;
}
.x-column-layout-ct .x-panel-dd-spacer {
    margin-bottom:10px;
}
.x-grid3-row .x-grid-row-checker {
    width: 18px;
}
</style>
<script type="text/javascript">
Testopia.userid = [% user.id FILTER js %];

Ext.onReady(function(){
    var product_id;
    product_id = Ext.state.Manager.get('TESTOPIA_PRODUCT_ID');
  [% IF product %]
    product_id = [% product.id || 0 %];
  [% END %]
    var testopia_form = new Ext.form.BasicForm('testopia_helper_frm');
    var t = new Testopia.Util.CascadeProductSelection();    
    Ext.QuickTips.init();
    
    var pgrid = new Testopia.TestPlan.Grid({product_id: product_id},{id: 'product_plan_grid'});
    var cgrid = new Testopia.TestCase.Grid({product_id: product_id},{id: 'product_case_grid'});
    var rgrid = new Testopia.TestRun.Grid({product_id: product_id},{id: 'product_run_grid'});
    var egrid = new Testopia.Environment.Grid({product_id: product_id});
    var utag = new Testopia.Tags.ProductTags("User Tags", "user");
    var ptag = new Testopia.Tags.ProductTags("Product Tags", "product", product_id);
    var dtab = new Testopia.Search.SavedReportsList({
        title: 'Dashboards',
        id: 'dashboard_grid',
        type: 3
    });
    var rtab = new Testopia.Search.SavedReportsList({
        title: 'Reports',
        id: 'reports_grid',
        type: 1
    });
    var stab = new Testopia.Search.SavedReportsList({
        title: 'Searches',
        id: 'searches_grid',
        type: 0
    });
    
    var product_menu = new Ext.menu.Menu({
        id: 'product_menu',
        items:[{
            text: 'New Test Plan',
            handler: function(){
                Testopia.TestPlan.NewPlanPopup(product_id);
            }
        },{
            text: 'New Build',
            handler: function(){
                Ext.getCmp('object_panel').activate('build_grid');
                Ext.getCmp('build_grid').newRecord();
            }
        },{
            text: 'New Case Category',
            handler: function(){
                Ext.getCmp('object_panel').activate('category_grid');
                Ext.getCmp('category_grid').newRecord();
            }
        },{
            text: 'New Environment',
            handler: function(){
                window.location = 'tr_new_environment.cgi?product_id=' + Ext.getCmp('products_pane').getSelectionModel().getSelectedNode().id;
            }
        },{
            text: 'Import',
            menu: [{
                text: 'Test Plans from XML',
                handler: function(){
                    if(!product_id){
                        alert('Please select a product first!');
                        return;
                    }
                    Testopia.TestPlan.Import({product_id: Ext.getCmp('products_pane').getSelectionModel().getSelectedNode().id});             
                }
            }]
        },{
            text: 'Export',
            menu: [{
                text: 'Test Plans as XML',
                handler: function(){
                    if(!product_id){
                        alert('Please select a product first!');
                        return;
                    }
                    window.location = 'tr_list_plans.cgi?ctype=xml&viewall=1&product_id=' + Ext.getCmp('products_pane').getSelectionModel().getSelectedNode().id;             
                }
            }]
        }]
    });
    var search_menu = new Ext.menu.Menu({
        id: 'search_menu',
        items:[{
            text: 'Test Plan Search',
            handler: function(){
                Testopia.Search.Popup('plan');
            }
        },{
            text: 'Test Case Search',
            handler: function(){
                Testopia.Search.Popup('case');
            }
        },{
            text: 'Test Run Search',
            handler: function(){
                Testopia.Search.Popup('run');
            }
        },{
            text: 'Test Results Search',
            handler: function(){
                Testopia.Search.Popup('caserun');
            }
        }]
    });

    var report_menu = new Ext.menu.Menu({
        id: 'report_menu',
        items: [{
            text: 'Tabular Reports',
            menu: {
                items:[{
                    text: 'Test Plan Report',
                    handler: function(){
                        Testopia.Search.Popup('plan', {report: true});
                    }
                },{
                    text: 'Test Case Report',
                    handler: function(){
                        Testopia.Search.Popup('case', {report: true});
                    }
                },{
                    text: 'Test Run Report',
                    handler: function(){
                        Testopia.Search.Popup('run', {report: true});
                    }
                },{
                    text: 'Test Results Report',
                    handler: function(){
                        Testopia.Search.Popup('caserun', {report: true});
                    }
                }]
            }
        }]
    });
    
    var product_toolbar = new Ext.Toolbar({
        items:[{
            text: 'File',
            menu: product_menu
        },{
            text: 'Search',
            menu: search_menu
        },{
            text: 'Reports',
            menu: report_menu
        }]
    });
    
    var productView = new Ext.Panel({
        layout: 'border',
        height: 680,
        title:'Product: [% product.name || "(Please select a product from the tree)" FILTER js %]',
        monitorResize: true,
        split: true,
        id:'product_view',
        applyTo: 'productcontainer',
        tbar: product_toolbar,
        items:[{
            region: 'west',
            split: true,
            title: 'Products',
            width: 212,
            maxsize: 400,
            id: 'products_pane',
            xtype: 'treepanel',
            loader: new Ext.tree.TreeLoader({
              dataUrl:'tr_quicksearch.cgi',
              baseParams: {action: 'getclassificationstree'}
            }),
            root: new Ext.tree.AsyncTreeNode({
              text: 'Classifications',
              dragable: false,
              id: 'classes',
              expanded: true
            }),
            animate: true,
            enableDD: false,
            rootVisible: true,
            singleExpand:true,
            collapsible: true,
            autoScroll: true,
            listeners: {
                'click': function(node, e){
                    if(node.isLeaf()){
                        var tabs = Ext.getCmp('object_panel').items;
                        for (i in tabs.items){
                            if (tabs.items[i].store){
                                tabs.items[i].store.removeAll();
                                tabs.items[i].store.baseParams.product_id = node.id; 
                            }
                            if (tabs.items[i].params){
                                tabs.items[i].params.product_id = node.id;
                            }
                            tabs.items[i].product_id = node.id;
                            if (Ext.getCmp('edit_build_btn')){
                                Ext.getCmp('edit_build_btn').enable();
                                Ext.getCmp('add_build_btn').enable();
                            }
                            if (Ext.getCmp('edit_category_btn')){
                                Ext.getCmp('edit_category_btn').enable();
                                Ext.getCmp('add_category_btn').enable();
                            }
                        }
                        
                        product_id = node.id;
                        p = Ext.getCmp('object_panel').getActiveTab();
                        if (p && p.store){
                            p.store.load();
                        }
                        Ext.getCmp('product_view').setTitle('Product: ' + node.text);
                        Ext.getCmp('producttags').store.load({params: {product_id: product_id}});
                        document.cookie = "TESTOPIA_PRODUCT_ID=" + product_id + "; path=[% cookiepath %]; expires=Fri, 01 Jan 2038 00:00:04 GMT";
                    }
                },
                'contextmenu': function(node, e){
                    node.select();
                    e.stopEvent();
                    product_menu.showAt(e.getXY());
                }
            }
        },{
            region: 'east',
            width: '250',
            split: true,
            id:'prod_tag_panel',
            enableTabScroll: true,
            xtype: 'tabpanel',
            listeners: { 
              'tabchange': function(){
                    Ext.state.Manager.set('product_tabs_tab', Ext.getCmp('prod_tag_panel').getActiveTab().getId());
              }
            },
            items: [ 
                dtab, rtab, stab, utag, ptag
            ]
        },{
            region: 'center',
            id: 'object_panel',
            split: true,
            xtype: 'tabpanel',
            enableTabScroll: true,
            listeners: { 
              'tabchange': function(){
                    Ext.state.Manager.set('product_active_tab', Ext.getCmp('object_panel').getActiveTab().getId());
              }
            },
            items: [new Testopia.Search.DashboardPanel({}),
                pgrid, 
                cgrid, 
                rgrid, 
                new Testopia.Build.Grid(product_id),
                new Testopia.Category.Grid(product_id), 
                egrid
            ]
    
        }]
    });
    Ext.getCmp('dashboardpanel').on('render', function(){
        Ext.getCmp('dashboardpanel').getTopToolbar().add({
            xtype: 'button',
            id: 'save_dashboard_btn',
            icon: 'extensions/Testopia/img/save.png',
            iconCls: 'img_button_16x',
            tooltip: 'Save this dashboard',
            handler: function(b,e){
                Testopia.Search.save('dashboard', Testopia.Search.dashboard_urls);
            }
        });
    });
  [% IF product %]
    Ext.getCmp('products_pane').getLoader().on('load', function(l,n,r){
        if (n.isExpanded() && n.id != 'classes'){
            return;
        }
        var tree = Ext.getCmp('products_pane');
        var cnode = tree.getNodeById('c[% product.classification_id FILTER none %]');
        cnode.expand(1,true,function(n){
            tree.getNodeById('[% product.id FILTER none %]').select();
        });
        
    });
  [% END %]
  [% IF dashboard AND user.in_group('Testers')  %]
    var dstore = new Ext.data.JsonStore({
        url: 'tr_query.cgi',
        baseParams: {action: 'get_saved_searches', type: 3, userid: [% userid || user.id FILTER js %]},
        root: 'searches',
        fields: ["name","query","author","type"],
        listeners: {'exception': Testopia.Util.loadError,
            'load': function(obj, rs){
            Ext.getCmp('object_panel').setActiveTab('dashboardpanel');
            var current_col = 'dashboard_leftcol';
            var query;
            var r;
            for (var i in rs){
                if(typeof rs[i] == 'function'){
                    continue;
                }
                if (rs[i].get('name') == '[% dashboard FILTER js %]'){
                    r = rs[i];
                    break;
                }
            }
            var qs = r.get('query').split('::>');
            i = 0;
            for (i in qs){ 
                if(typeof qs[i] == 'function'){
                    continue;
                }
                var p = Testopia.Util.urlQueryToJSON(qs[i]);
                var t;
                typeof p.qname == 'object' ? t = p.qname[0] : t = p.qname;
                
                var newPortlet = new Ext.ux.Portlet({
                    title: t || '',
                    id: r.get('name') + i,
                    closable: true,
                    autoScroll: true,
                    tools: PortalTools,
                    url: qs[i]
                });
                
                Ext.getCmp(current_col).add(newPortlet);
                Ext.getCmp(current_col).doLayout();
                Testopia.Search.dashboard_urls.push(qs[i]);
                newPortlet.load({
                    url: qs[i]
                });
                current_col = current_col == 'dashboard_leftcol' ? 'dashboard_rightcol' : 'dashboard_leftcol';
            }        
        }}
    });
    dstore.load();

  [% END %]
    var s = Testopia.Util.urlQueryToJSON(window.location.search);
    if (s.tab){
        Ext.getCmp('object_panel').activate('product_' + s.tab + '_grid');
    }
    else{
        Ext.getCmp('object_panel').activate(Ext.state.Manager.get('product_active_tab', 0));
    }
    
    Ext.getCmp('prod_tag_panel').activate(Ext.state.Manager.get('product_tabs_tab', 0));

  [% IF search %]
    Testopia.Search.Popup('case');
  [% END %]
});

</script>
<style type="text/css">
.x-menu-item{
    text-decoration: none;
}
</style>

<div id="productcontainer"></div>

[% PROCESS global/footer.html.tmpl %]