<commit_before>/*
 * Copyright 2013-2015 Hewlett-Packard Development Company, L.P.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
 */

/**
 * @module js-whatever/js/abstract-pages
 */
define([
    'backbone',
    'underscore'
], function(Backbone, _) {

    /**
     * @name module:js-whatever/js/abstract-pages.AbstractPages
     * @constructor
     * @desc Abstract base class for representing groups of pages
     * @extends Backbone.View
     */
    return Backbone.View.extend(/** @lends module:js-whatever/js/abstract-pages.AbstractPages.prototype */ {
        /**
         * @desc Backbone.Router instance which is observed for navigation events
         * @abstract
         */
        router: null,

        /**
         * @desc Instance of Vent used for navigation
         * @type {module:js-whatever/js/vent-constructor.Vent}
         * @abstract
         */
        vent: null,

        initialize: function() {
            _.bindAll(this, 'changePage', 'findPage');

            if (!this.router || !this.vent) {
                throw 'abstract-pages.js error: router and vent must be provided!';
            }

            this.initializePages();

            _.each(this.pages, function(page) {
                page.view = new page.constructor();
                page.view.pageName = page.pageName;
                page.route = this.buildRoute(page)
            }, this);

            this.router.on(this.eventName, this.changePage);
        },

        /**
         * @desc Sets the value of this.pages to the list of page objects.
         * These must have a pageName and a constructor function.
         * The constructors should extend {@link module:js-whatever/js/base-page.BasePage|BasePage}
         * @abstract
         * @method
         * @example
         * this.pages = [{
         *     constructor: Foo,
         *     pageName: 'foo'
         * }, {
         *     constructor: Bar,
         *     pageName: 'bar'
         * }]
         */
        initializePages: $.noop,

        /**
         * @param {String} page The page object
         * @returns {String} The current route for a given page
         */
        buildRoute: function(page) {
            return this.routePrefix + (page.view.getSelectedRoute() || page.pageName )
        },

        /**
         * @param {String} pageName The name of the page
         * @returns {Object} The page object with the given name
         */
        findPage: function(pageName) {
            return _.findWhere(this.pages, { pageName: pageName });
        },

        /**
         * @desc Change to the named page.  This will call it's render method if this hasn't already been called,
         * and hide the current page
         * @param {String} pageName The name of the page
         */
        changePage: function(pageName) {
            var newPage = this.findPage(pageName);

            if (!newPage.hasRendered) {
                newPage.view.render();
                this.$el.append(newPage.view.el);
                newPage.hasRendered = true;
            }

            if (this.currentPage) {
                this.currentPage.view.hide();
            }

            newPage.view.show();

            this.currentPage = newPage;
        },

        /**
         * Navigate to the named page, showing it and using the vent to propagate the navigation
         * @param {String} pageName The name of the page<commit_msg>[CCUK-3624] - find page will now return default page if set in pages [rev:alex.scown]<commit_after>/*
 * Copyright 2013-2015 Hewlett-Packard Development Company, L.P.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
 */

/**
 * @module js-whatever/js/abstract-pages
 */
define([
    'backbone',
    'underscore'
], function(Backbone, _) {

    /**
     * @name module:js-whatever/js/abstract-pages.AbstractPages
     * @constructor
     * @desc Abstract base class for representing groups of pages
     * @extends Backbone.View
     */
    return Backbone.View.extend(/** @lends module:js-whatever/js/abstract-pages.AbstractPages.prototype */ {
        /**
         * @desc Backbone.Router instance which is observed for navigation events
         * @abstract
         */
        router: null,

        /**
         * @desc Instance of Vent used for navigation
         * @type {module:js-whatever/js/vent-constructor.Vent}
         * @abstract
         */
        vent: null,

        initialize: function() {
            _.bindAll(this, 'changePage', 'findPage');

            if (!this.router || !this.vent) {
                throw 'abstract-pages.js error: router and vent must be provided!';
            }

            this.initializePages();

            _.each(this.pages, function(page) {
                page.view = new page.constructor();
                page.view.pageName = page.pageName;
                page.route = this.buildRoute(page)
            }, this);

            this.router.on(this.eventName, this.changePage);
        },

        /**
         * @desc Sets the value of this.pages to the list of page objects.
         * These must have a pageName and a constructor function.
         * The constructors should extend {@link module:js-whatever/js/base-page.BasePage|BasePage}
         * A page marked with defaultPage will be used if the given page is not found.
         * @abstract
         * @method
         * @example
         * this.pages = [{
         *     constructor: Foo,
         *     pageName: 'foo'
         * }, {
         *     constructor: Bar,
         *     defaultPage: true,
         *     pageName: 'bar'
         * }]
         */
        initializePages: $.noop,

        /**
         * @param {String} page The page object
         * @returns {String} The current route for a given page
         */
        buildRoute: function(page) {
            return this.routePrefix + (page.view.getSelectedRoute() || page.pageName )
        },

        /**
         * @param {String} pageName The name of the page
         * @returns {Object} The page object with the given name
         */
        findPage: function(pageName) {
            var page = _.findWhere(this.pages, { pageName: pageName });

            if (!page) {
                page = _.findWhere(this.pages, { defaultPage: true });
            }

            return page;
        },

        /**
         * @desc Change to the named page.  This will call it's render method if this hasn't already been called,
         * and hide the current page
         * @param {String} pageName The name of the page
         */
        changePage: function(pageName) {
            var newPage = this.findPage(pageName);

            if (!newPage.hasRendered) {
                newPage.view.render();
                this.$el.append(newPage.view.el);
                newPage.hasRendered = true;
            }

            if (this.currentPage) {
                this.currentPage.view.hide();
            }

            newPage.view.show();

            this.currentPage = newPage;
        },

        /**
         * Navigate to the named page, showing it and using the vent to propagate the navigation
         * @param {String} pageName The name of the page
{'fraction_non_alphanumeric': 0.10788608981380066, 'fraction_numerical': 0.002738225629791895, 'mean_word_length': 1.691599115696389, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2453955', 'n_tokens_mistral': 1990, 'n_tokens_neox': 1898, 'n_words': 557}