﻿/* $Id:: tree_constructor.js 10178 2011-08-04 14:46:36Z giarmoleol          $ */
// Les commentaires et la doc sont dans un fichier annexe: tree_constructor.txt
function treeNode(id, type, name, resource, language, show, indexed, metadata,
        isPubRoot, isWithinPub) {
    // properties
    this.id = id;
    this.type = type;
    this.name = name;
    this.resource = isWithinPub ? resource : null;
    this.language = language;
    this.show = show;
    this.indexed = indexed;
    this.metadata = metadata;
    this.children = new Array;
    this.isPubRoot = isPubRoot;
    this.isWithinPub = isWithinPub;
    //methods
    this.addChild = addChild;
    this.getAncestor = getAncestor;
    this.getFirstDoc = getFirstDoc;
    this.getFirstResource = getFirstResource;
    this.getIndex = getIndex;
    this.getLanguage = getlanguage;
    this.getLevel = getLevel;
    this.getMetadata = getMetadata;
    this.getNext = getNext;
    this.getPath = getPath;
    this.getPrev = getPrev;
    this.getRootPath = getRootPath;
    this.hasShownChildren = hasShownChildren;
    this.isAncestorOf = isAncestorOf;
    this.isDescendantOf = isDescendantOf;

    function addChild(node) {
        this.children[this.children.length] = node;
        node.parent = this;
    }
    function getAncestor(level,atom) {
        if (atom == undefined) atom = 5;
        if (this.parent==undefined) return this;
        else if (this.getLevel() > level || this.resource == undefined || this.type > atom)
            return this.parent.getAncestor(level, atom);
        else return this;
    }

    function getFirstDoc(){
      if (this.type == 2) {
            return this;
        }
        else {
            for (var i=0; i<this.children.length; i++) {
                if (this.children[i].type < 3 && this.children[i].show) {
                   var node = this.children[i].getFirstDoc();
                    if (node && node.type == 2) {
                        return node;
                    }
                }
            }
        }
    }


    function getFirstResource(atom, flags) {
        if (atom == undefined) atom = 4;
        if (flags == undefined) flags = 0;
        var childrenOnlyFlag = !!(flags & 0x01);
        var attachmentFlag = !!(flags & 0x02);
        var invisibleFlag = !!(flags & 0x04);
        if (this.isWithinPub) {
            if (this.resource && this.type >= atom &&
                    (attachmentFlag || this.type!=6))
                return this;
            else {
                for (var i=0; i<this.children.length; i++) {
                    if ((this.children[i].show || invisibleFlag) &&
                            (attachmentFlag || this.children[i].type!=6)) {
                        if (this.children[i].resource &&
                                this.children[i].type >= atom)
                            return this.children[i];
                        else if (!childrenOnlyFlag) {
                            var firstDescendant = this.children[i].getFirstResource(atom, flags);
                            if (firstDescendant) return firstDescendant;
                        }
                    }
                }
            }
        }
        return null;
    }
    function getIndex(flags) {
        if (flags == undefined) flags = 0;
        var sameTypeFlag = !!(flags & 0x01);
        var invisibleFlag = !!(flags & 0x04);
        if (this.parent && (this.show || invisibleFlag)) {
            var index = 0;
            for (var i=0; i<this.parent.children.length; i++) {
                if (this == this.parent.children[i]) return index;
                if ((this.parent.children[i].show || invisibleFlag) &&
                        (!sameTypeFlag || this.parent.children[i].type == this.type))
                    index++;
            }
        }
        else return null;
    }
    function getlanguage() {
        if (this.language) return this.language;
        else if (this.parent) return this.parent.getLanguage();
        else return null;
    }
    function getLevel(atom,level) {
        if (level == undefined) level = 0;
        if (atom == undefined) atom = 5;
        if (this.parent) {
            if (this.type <= atom) return this.parent.getLevel(atom,++level);
            else return this.parent.getLevel(atom,level)
        }
        else return level;
    }
    function getMetadata(name, lookup) {
        if (lookup == undefined) lookup = false;
        for (var i=0; i<this.metadata.length; i++) {
            if (this.metadata[i].name == name)
                return this.metadata[i].value;
        }
        if (lookup && this.parent)
            return this.parent.getMetadata(name, lookup);
        else return null;
    }
    function getNext(atom) {
        if (this.isWithinPub && !this.isPubRoot && this.show) {
            var indexNext;
            for (var i=0; i<this.parent.children.length; i++) {
                if (this == this.parent.children[i]) {
                    for (var j=i+1; j<this.parent.children.length; j++) {
                        if (this.parent.children[j].show &&
                                (atom == null || this.parent.children[j].type == atom)) {
                            indexNext=j;
                            break;
                        }
                    }
                    if (indexNext) return this.parent.children[indexNext];
                }
            }
        }
        return null;
    }
    function getPath(node) {
        if (this.isWithinPub) {
            var cheminFrom, cheminTo = '';

            if (!this.isPubRoot) {
                cheminTo = this.parent.getPath();
                if (this.resource) {
                    if (!this.parent.isPubRoot) cheminTo += '/';
                    cheminTo += this.resource;
                }
            }
            if (node) return node.getRootPath() + cheminTo;
            else return cheminTo;
        }
        else return null;
    }
    function getPrev(atom) {
        if (this.isWithinPub && !this.isPubRoot && this.show) {
            var indexPrev = -1;
            for (var i=0; i<this.parent.children.length; i++) {
                if (this == this.parent.children[i] && i>0) {
                    for (var j = i-1 ; j >= 0 ; j--) {
                        if (this.parent.children[j].show &&
                                (atom == undefined || this.parent.children[j].type == atom)) {
                            indexPrev=j;
                            break;
                        }
                    }
                    if (indexPrev>=0) return this.parent.children[indexPrev];
                }
            }
        }
        return null;
    }
    function getRootPath() {
        if (this.isWithinPub) {
            if (!this.isPubRoot) {
                var toParent = '';
                if (this.resource && !this.parent.isPubRoot) toParent = '../';
                return toParent + this.parent.getRootPath();
            }
            else return '';
        }
        else return null;
    }
    function hasShownChildren() {
        for (var i=0; i<this.children.length; i++) {
            if (this.children[i].show) return true
        }
        return false;
    }
    function isAncestorOf(node) {
        if (node != undefined) {
            if (this == node) return true
            else if (!node.isPubRoot) {
                if (this == node.parent) return true
                else return this.isAncestorOf(node.parent)
            }
        }
        return false;
    }
    function isDescendantOf(node) {
        if (node != undefined) {
            if (this.isWithinPub && node.isWithinPub) {
                if (this == node) return true
                else if (!this.isPubRoot) {
                    if (this.parent == node) return true
                    else return this.parent.isDescendantOf(node)
                }
            }
        }
        return false;
    }
}


