JavaScript Extension Development API for Oracle Visual Builder Cloud Service - Classic Applications

Source: pages.dt/js/api/PageUtils.js

define([
    'core.dt/js/api/application/ApplicationSupport',
    'pages.dt/js/api/Page',
    'pages.dt/js/api/Pages',
    'pages.dt/js/api/PageType',
    'pages.dt/js/api/ViewGeneratorModes',
    'viewmodel/js/api/ArchetypeType'
], function (
        ApplicationSupport,
        Page,
        pages,
        PageType,
        ViewGeneratorModes,
        ArchetypeType
        ) {

    'use strict';

    /**
     * This module provides various utility methods which helps to work
     * with {@link pages.dt/js/api/Page Page} objects.
     *
     * @AbcsExtension stable
     * @version 16.3.5
     *
     * @private
     * @singleton
     * @exports pages.dt/js/api/PageUtils
     * @constructor
     *
     * @see {@link pages.dt/js/api/Page Page} object which is being essential parameter for utility methods
     * @see {@link pages.dt/js/api/Pages Pages} module which helps to obtain particular {@link pages.dt/js/api/Page page} by ID
     * @see {@link pages.dt/js/api/Navigation Navigation} module which help to obtain active {@link pages.dt/js/api/Page page}
     */
    var PageUtils = function () {
        AbcsLib.throwStaticClassError();
    };

    /**
     *
     * @param {PageType} pageType page type
     * @param {string} [entityId] optional entity id
     * @returns {string[]} all applyable page ids
     */
    PageUtils.findPageIdsOfType = function (pageType, entityId) {
        var foundPages = PageUtils.findPagesOfType(pageType, entityId);
        return foundPages.map(function (page) {
            return page.getId();
        });
    };

    /**
     * Finds all {@link pages.dt/js/api/Page pages} of the given {@link pages.dt/js/api/PageType PageType}
     * and created for the given {@link entity/js/api/Entity entity}.
     *
     * <p>The examined {@link entity/js/api/Entity entity} doesn't need to be provided.
     * In that case the method returns all {@link pages.dt/js/api/Page page}s of the
     * particular {@link pages.dt/js/api/PageType PageType}.</p>
     *
     * @AbcsExtension stable
     * @version 16.3.5
     *
     * @param {pages.dt/js/api/PageType} pageType page type
     * @param {String} [entityId] entity ID
     * @returns {pages.dt/js/api/Page[]} list of all pages of the given type and related to the entity ID
     *
     * @see {@link pages.dt/js/api/PageType PageType} list of possible page types
     * @see {@link module:api/js/Entities.getAll Entities.getAll} list of all available entities within the application
     *
     * @example <caption>Seeks all CRUD pages for the entity 'myEntity'.</caption>
     * var result = [];
     * // lists all requested page types
     * var pageTypes = [PageType.EDIT, PageType.CRAETE, PageType.DETAIL];
     * // iterates over all page types and gathers them into the result
     * pageTypes.forEach(function(pageType) {
     *     result = result.concat(PageUtils.findPagesOfType(pageType, 'myEntity'));
     * });
     */
    PageUtils.findPagesOfType = function (pageType, entityId) {
        AbcsLib.checkParameterCount(arguments, 1, 1);
        AbcsLib.checkDefined(pageType, 'pageType');

        if (PageType.values().indexOf(pageType) === -1) {
            throw new Error('Unknown page type: ' + pageType);
        }
        var foundPages = [];
        pages.getPages().forEach(function (page) {
            var add = false;
            if (page.getType() === pageType) {
                if (entityId && pageType !== PageType.LANDING) {
                    page.findViews(ArchetypeType.ENTITY_DETAIL).forEach(function (view) {
                        var arch = view.getArchetype();
                        if (arch.getEntityId() === entityId) {
                            add = true;
                        }
                    });
                } else {
                    add = true;
                }
            }
            if (add) {
                foundPages.push(page);
            }
        });
        return foundPages;
    };

    /**
     * Gathers and gets all {@link viewmodel/js/api/Archetype archetypes} available
     * on the given {@link pages.dt/js/api/Page page}.
     *
     * @AbcsExtension stable
     * @version 16.3.5
     *
     * @param {pages.dt/js/api/Page} page page where to seek archetypes
     * @returns {viewmodel/js/api/Archetype[]} list of all archetype objects used on the given page
     *
     * @see {@link pages.dt/js/api/Pages Pages} module which helps to obtain particular {@link pages.dt/js/api/Page page} by ID
     * @see {@link pages.dt/js/api/Navigation Navigation} module which help to obtain active {@link pages.dt/js/api/Page page}
     *
     * @example <caption>Gets all entities related to the active page.</caption>
     * // gets the active page
     * var activePage = Navigation.getActivePage();
     * // gets all available archetypes
     * var archetypes = PageUtils.getPageArchetypes(activePage);
     * // gathers entities from the archetypes
     * var entityIDs = [];
     * archetypes.forEach(function(archetype) {
     *     entityIDs.push(archetype.getEntity())
     * });
     */
    PageUtils.getPageArchetypes = function(page) {
        AbcsLib.checkParameterCount(arguments, 1);
        AbcsLib.checkDefined(page, 'page');
        AbcsLib.checkDataType(page, Page);
        return page.getViewModelDefinition().getArchetypes().slice(0);
    };

    /**
     * Finds all {@link pages.dt/js/api/View View}s on the page with the given
     * view type.
     *
     * <p>Iterates through all {@link pages.dt/js/api/View view}s on the page starting at the top level
     * page's view and collects all that have view type given by the parameter.</p>
     *
     * @AbcsExtension stable
     * @version 16.3.5
     *
     * @param {pages.dt/js/api/Page} page to seek for views
     * @param {String} viewType type of the searched view
     * @returns {pages.dt/js/api/View[]} list of found views of the given type
     *
     * @see {@link pages.dt/js/api/View View} for description how the view objects are structured
     *
     * @example <caption>Seeking active page for all custom component's view types</caption>
     * // will yield active page
     * var page = Navigation.getActivePage();
     * // recursively finds all custom types on the page
     * var views = PageUtils.findViewsByType(page, 'org.company.abcs.components.coolButton');
     */
    PageUtils.findViewsByType = function(page, viewType) {
        AbcsLib.checkParameterCount(arguments, 1, 1);
        AbcsLib.checkDefined(page, 'page');
        return PageUtils._findViewsByType(page.getView(), viewType);
    };

    PageUtils.getPageUrl = function (pageId, generatorMode) {
        var path = '#';
        var appId = ApplicationSupport.getActiveApplication().getId();
        var mode;
        switch (generatorMode) {
            case ViewGeneratorModes.MODE_DEPLOYMENT:
                return path + pageId;
            case ViewGeneratorModes.MODE_RUNTIME:
                mode = 'preview';
                break;
            default:
                mode = 'design';
                break;
        }
        path += 'application';
        path += '/';
        path += appId;
        path += '/';
        path += mode;
        path += '/';
        path += pageId;

        return path;
    };

    PageUtils._findViewsByType = function (view, viewType) {
        var views = [];
        if (view.hasChildren()) {
            view.getChildren().forEach(function (childView) {
                var childViews = PageUtils._findViewsByType(childView, viewType);
                childViews.forEach(function (child) {
                    views.push(child);
                });
            });
        }
        var currentViewType = view.getType();
        if (currentViewType === viewType) {
            views.push(view);
        }
        return views;
    };

    return PageUtils;

});