﻿// JScript File

Ext.namespace('FinnKodeWeb');

FinnKodeWeb.PanelTableView = function(config) {
    var _tb = new Ext.Toolbar({
        items: [
            { xtype: 'tbtext', text: 'Stinavigator' }
        ]
    });

    var _objectPathRecord = Ext.data.Record.create([
        { name: 'caption', mapping: '@caption' },
        { name: 'locateId', mapping: '@locateId' }
    ]);
    var _xmlObjectPathReader = new Ext.data.XmlReader({
        record: 'pathItem',
        idPath: '@locateId'
    }, _objectPathRecord);
    var _httpProxyLoadObjectPath = new Ext.data.HttpProxy({
        url: 'FinnKodeWS/ContentService.svc/GetObjectPath',
        method: 'GET',
        listeners: {
            'exception': function(dataProxy, type, action, options, response, args) {
                var dp = dataProxy;
                var t = type;
                var a = action;
                var o = options;
                var r = response;
                var ar = args;
            }
        }
    });

    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
        ]
    });

    config = config || {};
    this.bookSection = config.bookSection;
    this.currentObjectId = 0;
    this.nextObjectId = -1;
    this.loadingObjectId = 0;

    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();

    var callbackLoadObjectPath = function(result, options, success) {
        MsgSvc.addMsg({
            Category: 'TableView',
            Action: 'AfterLoadObjectPath',
            Lable: options
        });
        if (!success) {
            MsgSvc.addMsg({
                ShowAlertInfo: true,
                Category: 'TableView',
                Action: 'AfterLoadObjectPath',
                Lable: 'Kunne ikke laste' + options
            });
        }
        else {
            var toolbar = _tb;
            // remove previous items
            toolbar.items.each(function(item) {
                toolbar.items.remove(item);
                item.destroy();
            }, toolbar.items);
            if (!result) {
                toolbar.add(new Ext.Toolbar.TextItem({
                    text: 'Ingen stinavigator'
                }));
            }
            else {
                toolbar.add(new Ext.Toolbar.TextItem({
                    text: 'Stinavigator'
                }));
                var records = result.records;
                var recordIndex = 0;
                for (recordIndex = 0; recordIndex < records.length; recordIndex++) {
                    var splitButton = new FinnKodeWeb.PathButton({
                        text: records[recordIndex].get('caption'),
                        bookId: this.bookSection.BookId,
                        bookSectionId: this.bookSection.BookSectionId,
                        objectId: result.records[recordIndex].get('locateId'),
                        handler: handlerToolClicked,
                        scope: this
                    });
                    toolbar.add(splitButton);
                }
            }
            toolbar.doLayout();
        }
    };
    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
                            });
                            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
                    });
                };
                this.loadingObjectId = objectId;
                MsgSvc.addMsg({
                    ShowMaskInfo: true,
                    MaskElement: this.body,
                    Category: 'TableView',
                    Action: 'BeforeLoadObject',
                    Label: 'Laster ' + objectId
                });
                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);
                this.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;
    };

    var handlerToolClicked = function(item, eventObject) {
        if (item.bookSectionId != this.bookSectionId || item.objectId != this.currentObjectId) {
            NavigateTo(this.bookSection.BookId, item.bookSectionId, item.objectId);
        }
    };

    this.loadObjectPath = function(bookId, bookSectionId, objectId) {
        MsgSvc.addMsg({
            Category: 'TableView',
            Action: 'BeforeLoadObjectPath',
            Label: objectId
        });
        if (_tb) {
            _tb.items.each(function(item) {
                _tb.items.remove(item);
                item.destroy();
            }, _tb.items);
            _tb.add(new Ext.Toolbar.TextItem({
                text: 'Laster stinavigator'
            }));
            _tb.doLayout();
        }
        _httpProxyLoadObjectPath.doRequest(
        'read',
        null,
        {
            bookId: bookId,
            bookSectionId: bookSectionId,
            objectId: objectId
        },
        _xmlObjectPathReader,
        callbackLoadObjectPath,
        this,
        objectId);
    };
};

Ext.extend(FinnKodeWeb.PanelTableView, Ext.Panel);

Ext.reg('paneltableview', FinnKodeWeb.PanelTableView);
