<commit_before>"use strict";

var _ = require("underscore"),
    Class = require("nodeclass").Class,
    is = require("nodeclass").is,
    EventEmitter = require("../shared/EventEmitter.class.js"),
    Page = require("./Page.class.js"),
    pageJS = require("page"),
    historyJS = require("history.js");

var Router = new Class({

    Extends: EventEmitter,

    /**
     * @type {Page}
     * @private
     */
    __lastPage: null,

    /**
     * @type {Page}
     * @private
     */
    __currentPage: null,

    /**
     * @type {Page}
     * @private
     */
    __mainPage: null,

    /**
     * @param {Page} mainPage
     * @constructor
     */
    init: function (mainPage) {

        if (is(mainPage).notInstanceOf(Page)) {
            throw new Error("(alamid)");
        }

        this.__mainPage = mainPage;
        this.__currentPage = mainPage;

    },

    /**
     * NOTE: Not every Page must have an Service, but length of both returned arrays from
     * resolvePageLoaders() and resolvePageServices() must have the same length.
     * Params will be parsed and applied on each service-call
     *
     * @param {string} route
     * @param {function(): array} resolvePageLoaders
     * @param {function(): array} resolvePageServices
     * @return {Router}
     */
    register: function (route, resolvePageLoaders, resolvePageServices) {

        pageJS(route, this._getRouteHandler(resolvePageLoaders, resolvePageServices));

        return this.Instance;
    },

    /**
     * @param {string} route
     * @param {function(): array} resolvePageLoaders
     *
     * @return (function)
     */
    _getRouteHandler: function (route, resolvePageLoaders, resolvePageServices) {

        return function routeHandler() {

            var pageLoaders,
                loadedPages = [],
                pageServices,
                pageCount,
                pageServicesParams,
                addedEvents = [],
                self = this;

            function markPageAsLoaded(page) {
                loadedPages.push(page);

                if (loadedPages.length === pageCount) {
                    self.emit("route" + " resolved");
                }

            }

            if (resolvePageLoaders === undefined) {
                pageLoaders = this.__resolvePageLoaders(route);
            } else {
                pageLoaders = resolvePageLoaders();
            }

            pageCount = pageLoaders.lenght;

            if (resolvePageServices === undefined) {
                pageServices = this.__resolveServices(route);
            }

            pageServicesParams = this.__resolveServiceParams(route);

            _(pageLoaders).each(function loadPage(pageLoader, pageLoaderIndex) {
                pageLoader(function pageLoaded(page) {

                    var parentPageIndex,
                        parentPage,
                        uniquePageEvent;

                    //Create an unique event-name that will be emitted to tell Sub-Pages that their Parent-Page was loaded
                    uniquePageEvent = route + pageLoaderIndex;

                    parentPageIndex = pageLoaderIndex - 1;
                    //Parent-Page will be undefined if it is not loaded yet
                    parentPage = loadedPages[parentPageIndex];

                    //Mark Page as loaded
                    markPageAsLoaded(page);

                    //Emit "pageloaded"-Event for the Page-Life-Cycle/-Controller-Logic
                    page.emit("pageloaded");

                    if (pageLoaderIndex > 0) {
                        //If Parent-Page is already loaded then set loaded Page as Sub-Page of it
                        if (parentPage) {<commit_msg>Small Bug-Fixes<commit_after>"use strict";

var _ = require("underscore"),
    Class = require("nodeclass").Class,
    is = require("nodeclass").is,
    EventEmitter = require("../shared/EventEmitter.class.js"),
    Page = require("./Page.class.js"),
    pageJS = require("page"),
    historyJS = require("history.js");

var Router = new Class({

    Extends: EventEmitter,

    /**
     * @type {Page}
     * @private
     */
    __lastPage: null,

    /**
     * @type {Page}
     * @private
     */
    __currentPage: null,

    /**
     * @type {Page}
     * @private
     */
    __mainPage: null,

    /**
     * @param {Page} mainPage
     * @constructor
     */
    init: function (mainPage) {

        if (is(mainPage).notInstanceOf(Page)) {
            throw new Error("(alamid)");
        }

        this.__mainPage = mainPage;
        this.__currentPage = mainPage;

    },

    /**
     * NOTE: Not every Page must have an Service, but length of both returned arrays from
     * resolvePageLoaders() and resolvePageServices() must have the same length.
     * Params will be parsed and applied on each service-call
     *
     * @param {string} route
     * @param {function(): array} resolvePageLoaders
     * @param {function(): array} resolvePageServices
     * @return {Router}
     */
    register: function (route, resolvePageLoaders, resolvePageServices) {

        pageJS(route, this._getRouteHandler(resolvePageLoaders, resolvePageServices));

        return this.Instance;
    },

    /**
     * @param {string} route
     * @param {function(): array} resolvePageLoaders
     *
     * @return (function)
     */
    _getRouteHandler: function (route, resolvePageLoaders, resolvePageServices) {

        var self = this;

        return function routeHandler() {

            var pageLoaders,
                loadedPages = [],
                pageServices,
                pageCount,
                pageServicesParams,
                addedEvents = [];

            function markPageAsLoaded(pageIndex, page) {
                loadedPages[pageIndex] = page;

                if (loadedPages.length === pageCount) {
                    self.emit("route" + " resolved");
                }

            }

            if (resolvePageLoaders === undefined) {
                pageLoaders = self.__resolvePageLoaders(route);
            } else {
                pageLoaders = resolvePageLoaders();
            }

            pageCount = pageLoaders.lenght;

            if (resolvePageServices === undefined) {
                pageServices = self.__resolveServices(route);
            }

            pageServicesParams = self.__resolveServiceParams(route);

            _(pageLoaders).each(function loadPage(pageLoader, pageLoaderIndex) {
                pageLoader(function pageLoaded(page) {

                    var parentPageIndex,
                        parentPage,
                        uniquePageEvent;

                    parentPageIndex = pageLoaderIndex - 1;
                    //Parent-Page will be undefined if it is not loaded yet
                    parentPage = loadedPages[parentPageIndex];

                    //Create an unique event-name that will be emitted to tell Sub-Pages that their Parent-Page was loaded
                    uniquePageEvent = route + parentPageIndex;

                    //Mark Page as loaded
                    markPageAsLoaded(pageLoaderIndex, page);

                    //Emit "pageloaded"-Event for the Page-Life-Cycle/-Controller-Logic
                    page.emit("pageloaded");

                    if (pageLoaderIndex > 0) {
                        //If Parent-Page is already loaded then set loaded Page as Sub-Page of it
                        if (parentPage) {
{'fraction_non_alphanumeric': 0.10059092130002686, 'fraction_numerical': 0.0005372011818426001, 'mean_word_length': 1.721856725146199, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4196739', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1840, 'n_words': 467}