﻿// JScript File

Ext.namespace('FinnKodeWeb');

FinnKodeWeb.PanelTableView = function(config) {

    config = config || {};
    this.bookSection = config.bookSection;
    this.currentObjectId = 0;
    this.nextObjectId = -1;
    this.loadingObjectId = 0;
    
    var _tb = new Ext.Toolbar({

    });

    var _pathNavigator = new FinnKodeWeb.PathNavigatorToolbar({
        toolbar: _tb,
        bookSection: this.bookSection,
        category: 'FlowViewPathNavigator',        
        listeners: {
            navigate: {
                fn: function(bookSectionId, objectId) {
                    if (bookSectionId != this.bookSectionId || objectId != this.currentObjectId) {
                        NavigateTo(this.bookSection.BookId, bookSectionId, objectId);
                    }
                },
                scope: this
            }
        }
    });

    var _panelStructuredItems = new FinnKodeWeb.PortletPanel({
        title: 'Strukturert visning',
        layout: 'fit',
        bodyStyle: 'padding:5px 5px 5px',
        collapsed: false,
        autoscroll: true
    });

    var _panelInheritedExclusionsPanel = new FinnKodeWeb.PortletInheritancePanel({
        title: 'Arvede eksklusjoner',
        layout: 'fit',
        inheritType: 'Exclusions',
        collapsed: false
    });

    var _panelInheritedCommentsPanel = new FinnKodeWeb.PortletInheritancePanel({
        title: 'Arvede merknader',
        layout: 'fit',
        inheritType: 'Comments',
        collapsed: false
    });

    var _panelInheritedInclusionsPanel = new FinnKodeWeb.PortletInheritancePanel({
        title: 'Arvede inklusjoner',
        layout: 'fit',
        inheritType: 'Inclusions',
        collapsed: false
    });

    var _panelSiblings = new FinnKodeWeb.PanelObjectList({
        title: 'Søsken',
        listType: 'SiblingsContentObjects'
    });
    var _panelChildren = new FinnKodeWeb.PanelObjectList({
        title: 'Barn',
        listType: 'ChildrenContentObjects',
        hidden: true
    });
    var _tabPanelChildrenSiblings = new Ext.TabPanel({
        deferredRender: false,
        activeTab: 0,
        items: [
            _panelSiblings,
            _panelChildren
        ]
    });

    Ext.apply(config, {
        layout: 'border',
        border: false,
        tbar: _tb,
        listeners: {
            'activate': function() {
                NavigateTo(this.bookSection.BookId, this.bookSection.BookSectionId, this.nextObjectId);
            }
        },
        items: [
            {
                region: 'center',
                layout: 'anchor',
                autoScroll: true,
                minHeight: 250,
                minSize: 250,
                minWidth: 300,
                minHeight: 50,
                bodyStyle: 'padding:10px 25px 10px 10px',
                items: [
                    _panelStructuredItems,
                    _panelInheritedExclusionsPanel,
                    _panelInheritedInclusionsPanel,
                    _panelInheritedCommentsPanel
                ]
            }, {
                region: 'east',
                layout: 'fit',
                border: false,
                split: true,
                width: 350,
                minWidth: 100,
                minHeight: 50,
                title: 'Søsken og barn',
                collapsible: true,
                items: _tabPanelChildrenSiblings
            }
        ]
    });

    // call superclass constructor
    FinnKodeWeb.PanelTableView.superclass.constructor.call(this, config);

    this.tpl = new Ext.XTemplate(
        '<table>',
            '<tr valign="top"><td nowrap>Kode:</td><td>{Number}</td></tr>',
            '<tr valign="top"><td nowrap>Term:</td><td>{Term}</td></tr>',
            '<tpl for="SubTerms">',
                '<tpl if="xindex==1">',
                    '<tr valign="top"><td nowrap>Undertermer:</td><td>{.}</td></tr>',
                '</tpl>',
                '<tpl if="xindex &gt; 1">',
                    '<tr valign="top"><td></td><td>{.}</td></tr>',
                '</tpl>',
            '</tpl>',
            '<tpl for="Exclusions">',
                '<tpl if="xindex==1">',
                    '<tr valign="top"><td nowrap>Eksklusjoner:</td><td>{.}</td></tr>',
                '</tpl>',
                '<tpl if="xindex &gt; 1">',
                    '<tr valign="top"><td></td><td>{.}</td></tr>',
                '</tpl>',
            '</tpl>',
            '<tpl for="Inclusions">',
                '<tpl if="xindex==1">',
                    '<tr valign="top"><td nowrap>Inklusjoner:</td><td>{.}</td></tr>',
                '</tpl>',
                '<tpl if="xindex &gt; 1">',
                    '<tr valign="top"><td></td><td>{.}</td></tr>',
                '</tpl>',
            '</tpl>',
            '<tpl for="Comments">',
                '<tpl if="xindex==1">',
                    '<tr valign="top"><td nowrap>Merknader:</td><td>{.}</td></tr>',
                '</tpl>',
                '<tpl if="xindex &gt; 1">',
                    '<tr valign="top"><td></td><td>{.}</td></tr>',
                '</tpl>',
            '</tpl>',
        '</table>'
    );
    this.tpl.compile();

    this.loadObject = function(objectId) {
        if (!objectId) return;
        objectId = typeof (objectId) == "number" ? objectId.toString() : objectId;
        this.nextObjectId = objectId;
        if (this.currentObjectId != objectId && this.loadingObjectId != objectId) {
            if (this.isVisible()) {
                var succeededCallback = function(result, userContext, methodName) {
                    if (result && userContext) {
                        var loadingObjectId = userContext.loadingObjectId;
                        var resultObjectId = result.ObjectId.toString();
                        if (loadingObjectId == resultObjectId) {
                            userContext.currentObjectId = resultObjectId;
                            MsgSvc.addMsg({
                                MaskElement: userContext.body,
                                Category: 'TableView',
                                Action: 'AfterLoadObject',
                                Label: 'Lastet ' + result.ObjectId,
                                TrackEvent: false
                            });
                            userContext.tpl.overwrite(_panelStructuredItems.body, result);

                            _panelInheritedExclusionsPanel.loadObjectList(result);
                            _panelInheritedInclusionsPanel.loadObjectList(result);
                            _panelInheritedCommentsPanel.loadObjectList(result);
                            var bookSection = userContext.bookSection;

                            _panelSiblings.loadObjectList(result, bookSection.BookId, bookSection.BookSectionId);
                            _panelChildren.loadObjectList(result, bookSection.BookId, bookSection.BookSectionId);

                            if (_panelSiblings.disabled && !_panelChildren.disabled) {
                                _tabPanelChildrenSiblings.activate(_panelChildren);
                            }
                            if (!_panelSiblings.disabled && _panelChildren.disabled) {
                                _tabPanelChildrenSiblings.activate(_panelSiblings);
                            }
                        }
                    }
                };
                var failedCallback = function(result, userContext, methodName) {
                    MsgSvc.addMsg({
                        ShowAlertInfo: true,
                        Category: 'TableView',
                        Action: 'AfterLoadObject',
                        Label: userContext.nextObjectId,
                        TrackEvent: false
                    });
                };
                this.loadingObjectId = objectId;
                MsgSvc.addMsg({
                    ShowMaskInfo: true,
                    MaskElement: this.body,
                    Category: 'TableView',
                    Action: 'BeforeLoadObject',
                    Label: 'Laster ' + objectId,
                    TrackEvent: false
                });
                var bookId = this.bookSection.BookId;
                var bookSectionId = this.bookSection.BookSectionId;
                var userContext = this;
                var ws = new finnkode.kith.no.icontentservice();
                ws.GetContentObject(bookId, bookSectionId, objectId, succeededCallback, failedCallback, userContext);
                _pathNavigator.loadObjectPath(bookId, bookSectionId, objectId);
            }
        }
    };

    this.navigate = function(hashManager) {
        if (!hashManager || !hashManager.objectId) {
            return;
        }
        this.loadObject(hashManager.objectId);
    };

    var _defaultNavigated = false;
    this.navigateDefault = function() {
        if (!_defaultNavigated) {
            _defaultNavigated = true;
            this.loadObject(this.nextObjectId);
        }
    };
    this.updateHash = function(hashManager) {
        hashManager.bookSectionViewId = 'table';
        hashManager.objectId = this.currentObjectId;
    };


};

Ext.extend(FinnKodeWeb.PanelTableView, Ext.Panel);

Ext.reg('paneltableview', FinnKodeWeb.PanelTableView);
