﻿// JScript File

Ext.namespace('FinnKodeWeb');



FinnKodeWeb.HistoryPanel = function(config) {
    config = config || {};
    this.bookSection = config.bookSection;

    var historyRecord = Ext.data.Record.create([
        { name: 'bookId' },
        { name: 'bookSectionId' },
        { name: 'objectId', type: 'int' },
        { name: 'source' },
        { name: 'code' },
        { name: 'term' }
    ]);
    var gridStore = new Ext.data.SimpleStore({
        fields: ["bookId", "bookSectionId", "objectId", "source", "code", "term"]
    });

    var columnTipRenderer = function(cellValue, cellMetadata, record, rowIndex, colIndex, store) {
        cellMetadata.attr = "ext:qtip=\"" + cellValue + "\" ext:qtitle=\"" + record.get("code") + "\"";
        return cellValue;
    };

    config.store = gridStore;
    config.columns = [
        { header: "BookId", width: 0, hidden: true, hideable: false, dataIndex: 'bookId', sortable: false },
        { header: "BookSectionId", width: 0, hidden: true, hideable: false, dataIndex: 'bookSectionId', sortable: false },
        { header: "ObjectId", width: 0, hidden: true, hideable: false, dataIndex: 'objectId', sortable: false },
        { header: "Kilde", width: 130, sortable: true, hidden: true, dataIndex: 'source', sortable: false },
        { header: "Kode", width: 80, sortable: true, dataIndex: 'code', fixed: true, sortable: false },
        { header: "Term", width: 440, sortable: true, dataIndex: 'term', sortable: false, renderer: columnTipRenderer }
    ];

    // call superclass constructor
    FinnKodeWeb.HistoryPanel.superclass.constructor.call(this, config);

    var rowClicked = function(grid, rowIndex, e) {
        var record = grid.getStore().getAt(rowIndex);
        var rowBookId = record.get('bookId');
        var rowBookSectionId = record.get('bookSectionId');
        var rowObjectId = record.get('objectId');
        MsgSvc.addMsg({
            TrackEvent: true,
            Category: 'HistoryNavigator',
            Action: 'Navigate',
            Label: rowBookId + ' - ' + rowBookSectionId + ' - ' + rowObjectId
        });
        NavigateTo(rowBookId, rowBookSectionId, rowObjectId);
    };

    this.addListener('rowclick', rowClicked, this);

    var succeededCallback = function(result, userContext, methodName) {
        if (result == null) {
            return;
        }
        MsgSvc.addMsg({
            Category: 'HistoryNavigator',
            Action: 'AfterGetShallowContentObject',
            Label: 'Lastet ' + result.BookSectionId + ' - ' + result.ObjectId
        });
        this.currentObjectId = result.ObjectId;
        this.loadingObjectId = result.ObjectId;
        this.nextObjectId = result.ObjectId;
        var record = new historyRecord({
            bookId: result.BookId,
            bookSectionId: result.BookSectionId,
            objectId: result.ObjectId,
            code: result.Number,
            term: result.Term
        });
        gridStore.insert(0, record);
    };
    var failedCallback = function(result, userContext, methodName) {
        MsgSvc.addMsg({
            Category: 'HistoryNavigator',
            Action: 'AfterGetShallowContentObject',
            Label: 'Feilet ' + userContext.bookSection.BookSectionId + ' - ' + userContext.nextObjectId
        });
    };
    this.addToHistory = function(bookId, bookSectionId, objectId) {
        this.loadingObjectId = objectId;
        var bookId = this.bookSection.BookId;
        var bookSectionId = this.bookSection.BookSectionId;
        var userContext = this;
        var ws = new finnkode.kith.no.icontentservice();
        ws.GetShallowContentObject(bookId, bookSectionId, objectId, succeededCallback, failedCallback, userContext);
    };
    this.navigate = function(hashManager) {
        if (!hashManager) return;
        if (hashManager.bookSectionId != this.bookSection.BookSectionId) return;
        if (hashManager.objectId == null) return;
        if (this.nextObjectId != hashManager.objectId) {
            this.nextObjectId = hashManager.objectId;
            this.addToHistory(hashManager.mainTabId, hashManager.bookSectionId, hashManager.objectId);
        }
    };

    FinnKodeWeb.HistoryPanel.superclass.constructor.call(this, config);
};

Ext.extend(FinnKodeWeb.HistoryPanel, Ext.grid.GridPanel, {
    loadMask: { msg: 'Laster data' },
    viewConfig: {
        autoFill: true,
        emptyText: 'Ingen besøkte koder så langt',
        forceFit: true,
        columnsText: 'Kolonner',
        sortAscText: 'Stigende sortering',
        sortDescText: 'Synkende sortering'
    },
    autoWidth: true
});


// register xtype to allow for lazy initialization
Ext.reg('historypanel', FinnKodeWeb.HistoryPanel);
