﻿// JScript File

Ext.namespace('FinnKodeWeb');

FinnKodeWeb.TreeStaticXml = function(config) {
    Ext.apply(this, {
        listeners: {
            'click': {
                fn: function(node, e) {
                    NavigateTo(this.bookSection.BookId, this.bookSection.BookSectionId, node.anchor || node.id, null, node.source);
                },
                scope: this
            },
            'afterrender': {
                fn: function() {
                    finnkode.kith.no.icontentservice.GetContentTree(this.bookSection.BookId, this.bookSection.BookSectionId, this.callbackLoadChildren, null, this);
                },
                single: true
            }
        },
        navigate: function(hashManager) {
        }
    });

    // call superclass constructor
    FinnKodeWeb.TreeStaticXml.superclass.constructor.call(this, config);

    // set custom config option
    this.bookSection = config.bookSection;

    var myloader = new Ext.tree.TreeLoader({
        dataUrl: 'FinnKodeWS/ContentService.svc/GetContentTree',
        preloadChildren: true,
        baseParams: { bookId: this.bookSection.BookId, bookSectionId: this.bookSection.BookSectionId }
    });

    this.scrollToNode = function(tree, node) {
        var top = (Ext.fly(node.getUI().getAnchor()).getOffsetsTo(tree.body)[1]) + tree.body.dom.scrollTop;
        tree.body.scrollTo('top', top - 25, true);
    };

    this.addChildNodes = function(parentNode, childNodes) {
        if (childNodes.length > 0) {
            var i = 0;
            for (i = 0; i < childNodes.length; i++) {
                var childNode = new Ext.tree.TreeNode({
                    id: childNodes[i].id,
                    text: childNodes[i].text,
                    leaf: childNodes[i].leaf,
                    qtipCfg: {
                        text: childNodes[i].text
                    }
                });
                // extend node with properties for anchor and source
                Ext.apply(childNode, {
                    anchor: childNodes[i].anchor ? childNodes[i].anchor : '',
                    source: childNodes[i].source ? childNodes[i].source : ''
                });
                parentNode.appendChild(childNode);
                if (childNodes[i].children && childNodes[i].children.length > 0) {
                    this.addChildNodes(childNode, childNodes[i].children);
                }
            }
        }
        //parentNode.expand();
    };
    var rootNode = new Ext.tree.TreeNode({
        id: 0,
        text: 'Laster data',
        leaf: false
    });
    Ext.apply(rootNode, {
        anchor: '',
        source: ''
    });
    this.setRootNode(rootNode);
    this.callbackLoadChildren = function(result, userContext, methodName) {
        userContext.root.setText(result.text);
        userContext.root.anchor = result.anchor;
        userContext.root.source = result.source;
        userContext.root.setId(result.text);

        userContext.addChildNodes.call(userContext, userContext.root, result.children);

        var node = userContext.getRootNode();
        if (node) {
            rootNode.select();
            rootNode.expand(true);
            NavigateTo(userContext.bookSection.BookId, userContext.bookSection.BookSectionId, node.anchor || node.id, null, node.source);
        }
    };
}


Ext.extend(FinnKodeWeb.TreeStaticXml, Ext.tree.TreePanel, {
    autoScroll: true,
    border: false
});

