xooki.init = function()

in xooki.js [918:1152]


xooki.init = function() {
    ////////////////////////////////////////////////////////////////////////////
    ////////////////// config init
    ////////////////////////////////////////////////////////////////////////////
    initConfigProperty = function(prop, value, defaultValue) {
        if (typeof this[prop] == "undefined") {
            if (typeof value == "undefined") {
                this[prop] = defaultValue;
            } else if (typeof value == "function") {
                this[prop] = value();
            } else {
                this[prop] = value;
            }
        }
    };
    if (typeof xookiConfig != "undefined") {xooki.util.mix(xookiConfig, xooki.config);}
    xooki.c.initProperty = initConfigProperty;
    xooki.c.computeRoot = function() {
    	root = xooki.pageURL;
    	// remove trailing parts of the URL to go the root depending on level
    	for (var i=0; i < xooki.c.level + 1; i++) {
    		root = root.substring(0, root.lastIndexOf('/'));
    	}
    	return root + '/';
    };
    xooki.c.computeRelativeRoot = function() {
    	return xooki.string.mul('../', xooki.c.level);
    };
    xooki.c.setImportLevel = function(level) {
        // compute roots with old level value, for paths relative to the local (non imported) root
        this.localRoot = this.computeRoot();
        this.localRelativeRoot = this.computeRelativeRoot();
        // change level and update roots
        this.level+=level;
        this.root = this.computeRoot();
        this.relativeRoot = this.computeRelativeRoot();
    };
    xooki.c.initProperty("level", 0);
    xooki.c.initProperty("root", xooki.c.computeRoot);
    xooki.c.initProperty("relativeRoot", xooki.c.computeRelativeRoot);
    xooki.c.initProperty("localRoot", xooki.c.root);
    xooki.c.initProperty("localRelativeRoot", xooki.c.relativeRoot);
    globalConfig = xooki.url.loadURL(u("config.json"), false);
    if (globalConfig != null && globalConfig.length != 0) {
        globalConfig = eval('('+globalConfig+')');
        xooki.util.mix(globalConfig, xooki.c, false);
    }
    xooki.url.evalURL(u("config.js"), false);
    xooki.url.evalURL(u("config.extra.js"), false);


    xooki.c.initProperty("defaultInputFormat", "xooki");
    xooki.c.initProperty("xookiInputFormat", ["xooki"]);
    xooki.c.initProperty("allowEdit", !batchMode && xooki.pageURL.substr(0,5) == "file:");
    
    xooki.input.format.define("xooki", ["code", "shortcuts", "url", "xookiLinks", "jira", "lineBreak" , "includes", "printFormatImgFix"]);
    
    xooki.c.path = (typeof xooki.c.path != "undefined")?xooki.c.path:{};
    xooki.c.path.initProperty = initConfigProperty;
    xooki.c.path.initProperty("install", "xooki");
    xooki.c.path.initProperty("messages", xooki.p("messages.json"));
    xooki.c.path.initProperty("template", "template.html");
    xooki.c.path.initProperty("printTemplate", "printTemplate.html");
    xooki.c.path.initProperty("toc", "toc.json");
    xooki.c.path.initProperty("blankPageTpl", xooki.p("blankPageTpl.html"));
    
    
    xooki.c.css = (typeof xooki.c.css != "undefined")?xooki.c.css:{};    
        
    xooki.c.messages = xooki.json.loadURL(cu("messages")); 
	if (!batchMode) {
	    xooki.c.browser = {
	        NS: (window.Event) ? 1 : 0
	    };
    
	    // action
	    if (! xooki.c.action) xooki.c.action = 'render';
	    // TODO: better handle action extraction
		xooki.c.action = window.location.search == '?action=print'?'print':xooki.c.action;
	}
	
	var match = new RegExp("^.*\\/((?:.*\\/){"+xooki.c.level+"}[^\\/]*)(?:\\.\\w+)(?:\\?.+)?$", "g").exec(xooki.pageURL);
	if (match == null || match[1] == '') {
		xooki.c.curPageId = "index";
	} else {
		xooki.c.curPageId = match[1];
	}
    
    ////////////////////////////////////////////////////////////////////////////
    ////////////////// TOC init
    ////////////////////////////////////////////////////////////////////////////
    xooki.toc = xooki.json.loadURL(cu("toc"));
    xooki.toc.url = cu("toc");
    xooki.toc.pages = {}; // to store a by id map of pages objects
    xooki.toc.importRoot = '';
    xooki.toc.actualRoot = xooki.toc; // this is the real root of the TOC, in case of a TOC imported, it will point to the root of the TOC on which import has been performed

	// populate meta data
	(function(page, parent, index, level, prefix) {
        if (prefix.length > 0) {
            page.meta = xooki.util.mix({id: page.id}, page.meta);
            page.id = prefix + page.id;
        }
        xooki.toc.pages[page.id] = page;
        
        page.meta = xooki.util.mix({
            index: index,
            level: level,
            getSerializeValue: function(o, k) {
                if (k == 'id' && typeof this.id != 'undefined') {
                    return this.id;
                } else {
                    return o[k];
                }
            }
        }, page.meta);
        page.meta.parent = parent;
        if (typeof page.importNode != 'undefined' && !page.isImported) {
            // this node requires to import another xooki TOC
            importedTocUrl = u(page.importRoot + '/toc.json');
            importedToc = xooki.json.loadURL(importedTocUrl);
            // look for the imported node in the importedTOC and import it in main TOC
            (function(page, parent, index, level, prefix, importedToc, node, id, populateFunction) {
                if (node.id == id) {
                    xooki.util.mix(node, page, false);
                    page.id = id;
                    page.isImported = true;
                    page.meta = xooki.util.mix({
                        isTransient: function(k) {
                            // only title, importRoot and importNode should be serialized
                            return k != 'title' && k != 'importRoot' && k != 'importNode';
                        }
                    }, page.meta);
                    if (xooki.c.curPageId.indexOf(prefix) == 0) {
                        // the current page is in this imported TOC
                        xooki.toc.actualRoot = importedToc;
                        xooki.toc.url = u(page.importRoot + '/toc.json');
                        xooki.toc.importRoot = prefix;
                    }
                    populateFunction(page, parent, index, level, prefix);
                    return true;
                } else if (typeof node.children != 'undefined') {
                    for (var i=0; i<node.children.length; i++) {
                        if (arguments.callee(page, parent, index, level, prefix, importedToc, node.children[i], id, populateFunction)) {
                            return true;
                        }
                    }
                }
                return false;
            })(page, parent, index, level, page.importRoot+'/', importedToc, importedToc, page.importNode, arguments.callee);
        }
        if (typeof page.children == 'undefined') {
            page.children = [];
        } else {
            for (var i=0; i<page.children.length; i++) {
                arguments.callee(page.children[i], page, i, level+1, prefix); // recurse
            }
        }
    })(xooki.toc, null, 0, -1, '');
    
    xooki.toc.getNextPage = function(page, root) {
        if (page.children.length > 0) {
        	return page.children[0];
        } else if (page.meta.parent != null) {
        	var cur = page;
        	var next = xooki.toc.getNextSibling(cur);
        	while (next == null) {
        		cur = cur.meta.parent;
        		if (cur == null || cur == root) {
        			return null;
        		}
        		next = xooki.toc.getNextSibling(cur);
        	}
       		return next;
        } else {
        	return null;
        }
    };
    xooki.toc.getNextSibling = function(page) {
    	if (page.meta.parent == null) {
    		return null;
    	}
       	if (page.meta.parent.children.length > page.meta.index) {
       		return page.meta.parent.children[page.meta.index+1];
       	} else {
       		return null;
       	}
    };
	xooki.page = xooki.toc.pages[xooki.c.curPageId];

	if (xooki.page == null) {
		xooki.warn(t('page id not found in TOC: ${0}',xooki.c.curPageId));
		xooki.page = xooki.toc.children[0];
	} 
	if (typeof xooki.config.title == 'undefined') {
		xooki.config.title = xooki.page.title;
	}		
	xooki.config.page = xooki.page;
	
    ////////////////////////////////////////////////////////////////////////////
    ////////////////// main template loading + head output
    ////////////////////////////////////////////////////////////////////////////
	xooki.template = {};
    xooki.template.source = xooki.url.loadURL(xooki.c.action == "print"?cu("printTemplate"):cu("template"));
	if(xooki.template.source != null) {
		xooki.template.head = xooki.template.source.match(/<head>([^�]*)<\/head>/im)[1];
		var root = batchMode?xooki.c.relativeRoot:xooki.c.root;
		
        var head = xooki.string.processTemplate(xooki.template.head, xooki.config);
		head = head.replace(/href="([^\\$:"]+)"/g, 'href="'+root+'$1"');
		head = head.replace(/src="([^\\$:"]+)"/g, 'src="'+root+'$1"');
		xooki.html.addHeader(head);

		var body = xooki.template.source.match(/<body>([^�]*)<\/body>/im)[1];
		body = body.replace(/href="([^\\$:"]+)"/g, 'href="'+root+'$1"');
		xooki.template.body = body.replace(/src="([^\\$:"]+)"/g, 'src="'+root+'$1"');		
	}
	

    ////////////////////////////////////////////////////////////////////////////
    ////////////////// includes
    ////////////////////////////////////////////////////////////////////////////
	if (batchMode) {
		xooki.html.addHeader('<script language="javascript" type="text/javascript">xooki = {u: function(url) {return "'+xooki.c.relativeRoot+'xooki/"+url;}};</script>');
	}
    if (xooki.c.allowEdit) {
        xooki.url.include("xookiEdit.js");
    }

    for (var k in xooki.c) {
        if (typeof xooki.c[k] == "string" || typeof xooki.c[k] == "number" || typeof xooki.c[k] == "boolean") {
            xooki.debug(k+": "+xooki.c[k]);
        }
    }
};