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

Source: api/js/Pages.js

define(['pages/js/api/NavigationProvider'], function (NavigationProvider) {

    'use strict';

    /**
     * Application pages support and navigation.
     *
     * @AbcsAPI stable
     * @exports api/js/Pages
     */
    var Pages = function() {
        AbcsLib.throwStaticClassError();
    };

    /**
     * Navigates application to the given page.
     *
     * @AbcsAPI stable
     * @version 15.4.5
     * @param {String} pageId ID of the page where to navigate to
     * @param {viewmodel/js/api/context/ContextualData} [contextualData=emptyContext] context to be passed to the page.
     * Defaults to empty context ({@link viewmodel/js/api/context/ContextualData.createBlankPageContext ContextualData.createBlankPageContext}).
     * @returns {Promise} Can be used when you want to chain another operation after the page switch.
     *
     * @example
     * <caption>
     * Navigates user to the page specified by the page ID ('home').
     * </caption>
     * Abcs.Pages().navigateToPage('home');
     *
     * @example
     * <caption>
     * Navigates user to the page specified by the page ID ('home'). During the call
     * it provides context of the caller page which can help to return back to the original page.
     * After the page switch a chained action (alert dialog) is invoked.
     * </caption>
     * var blankPageContext = ContextualData.createBlankPageContext({caller:'currentPageId'});
     * Abcs.Pages().navigateToPage('home', blankPageContext)
     *         .then(function() {
     *             // after the page switch open the alert dialog
     *             alert('you are on "home" now');
     *         });
     */
    Pages.navigateToPage = function (pageId, contextualData) {
        AbcsLib.checkParameterCount(arguments, 1, 1);
        AbcsLib.checkDefined(pageId, 'pageId');
        return NavigationProvider.getNavigation().navigateToPage(pageId, contextualData);
    };

    /**
     * Navigates application back to particular page. Difference in compare with
     * {@link module:api/js/Pages.navigateToPage Pages.navigateToPage} is
     * that upon returning to a page it is assumed that page was already initialized
     * and should be reopened in the state it was left and possibly just updated
     * of some data changed in the meantime.
     *
     * @AbcsAPI unstable
     *
     * @param {String} pageId page ID where it should go (return)
     * @param {viewmodel/js/api/context/ContextualData} [contextualData=emptyContext] context to be passed to the page.
     * Defaults to empty context ({@link viewmodel/js/api/context/ContextualData.createBlankPageContext ContextualData.createBlankPageContext}).
     * @returns {Promise} Can be used when you want to chain another operation after the page switch.
     *
     * @example
     * <caption>
     * Navigates user back to the page specified by the page ID ('home'). During the call it provides context of the changed record.
     * </caption>
     * // loads the Record RJS module
     * var Record = require(['viewmodel/js/api/Record'], function (Record) {
     *     // creates simple record
     *     var changedRecord = Record.createSimpleRecord({
     *         id: 1,
     *         firstname: 'Martin'
     *     });
     *
     *     // creates context for the navigation call
     *     var recordChangedContext = ContextualData.createRecordWasChangedContext({
     *         entityId: 'myCustomEntity',
     *         data: changedRecord
     *     });
     *
     *     // navigation call itself
     *     Abcs.Pages().returnToPage('home', recordChangedContext);
     * });
     */
    Pages.returnToPage = function (pageId, contextualData) {
        AbcsLib.checkParameterCount(arguments, 1, 1);
        AbcsLib.checkDefined(pageId, 'pageId');
        return NavigationProvider.getNavigation().returnToPage(pageId, contextualData);
    };

    /**
     * Navigates application to the homepage.
     *
     * @AbcsAPI stable
     * @version 15.4.5
     * @returns {Promise} Can be used when you want to chain another operation after the page switch.
     *
     * @example
     * <caption>
     * Navigates user back to the homepage.
     * </caption>
     * Abcs.Pages().navigateHome();
     *
     * @example
     * <caption>
     * Navigates user back to the homepage with the chained action (opened alert message).
     * </caption>
     * Abcs.Pages().navigateHome()
     *         .then(function() {
     *             // after the page switch open the alert dialog
     *             alert('you are on HOMEPAGE now');
     *         });
     */
    Pages.navigateHome = function () {
        AbcsLib.checkParameterCount(arguments, 0);
        return NavigationProvider.getNavigation().navigateHome();
    };

    /**
     * Gets the page ID of the currently active page, can be undefined.
     *
     * @AbcsAPI stable
     * @version 16.3.5
     * @returns {String|undefined} ID of the active page or undefined in case not beeing in the Page Designer
     *
     * @example
     * <caption>
     * Shows welcome dialog related to current page ID.
     * </caption>
     * // gets the active page ID
     * var activePageId = Abcs.Pages().getActivePageId();
     * // shows notification
     * Abcs.UI().showNotification(Abcs.UI().Notification.create({
     *      message: 'Welcome on the page \'' + activePageId + '\'.'
     * });
     */
    Pages.getActivePageId = function() {
        AbcsLib.checkParameterCount(arguments, 0);
        return NavigationProvider.getNavigation().getActivePageId();
    };

    /**
     * Gets the page ID of the homepage.
     *
     * @AbcsAPI stable
     * @version 16.3.5
     * @returns {String} ID of the home page
     *
     * @example
     * <caption>
     * Alternative way how to navigate ABCS to home page.
     * </caption>
     * // gets the homepage ID
     * var homepageId = Abcs.Pages().getHomepageId();
     * // navigates to the homepage (using the Page's ID)
     * Abcs.Pages().navigateToPage(homepageId);
     */
    Pages.getHomepageId = function() {
        AbcsLib.checkParameterCount(arguments, 0);
        return NavigationProvider.getNavigation().getHomePageId();
    };

    return Pages;

});