view service doc
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137/// FOURJS_START_COPYRIGHT(D,2014)
/// Property of Four Js*
/// (c) Copyright Four Js 2014, 2022. All Rights Reserved.
/// * Trademark of Four Js Development Tools Europe Ltd
///   in the United States and elsewhere
///
/// This file can be modified by licensees according to the
/// product manual.
/// FOURJS_END_COPYRIGHT

"use strict";

modulum('NavigationService', ['InitService'],
  function(context, cls) {

    /**
     * Service that manages navigation
     * @namespace gbc.NavigationService
     * @gbcService
     * @publicdoc
     */
    context.NavigationService = context.oo.StaticClass( /** @lends gbc.NavigationService */ {
      __name: "NavigationService",

      /**
       * @type {classes.EventListener}
       */
      _eventListener: null,
      _handlerRegistered: false,
      _newRootApplicationEvent: "gbc.NavigationService.newRootApplication",

      /**
       * Initialize the navigation Service
       */
      init: function() {
        this._eventListener = new cls.EventListener();

        window.requestAnimationFrame(() => {
          gbc.HostService.onCurrentWindowChange(this._registerHandler.bind(this));
        });
      },

      /**
       * Register the new Application internal handler
       * @private
       */
      _registerHandler: function() {
        if (this._handlerRegistered) {
          return;
        }

        this._handlerRegistered = true;
        var navigationManager = gbc.SessionService.getCurrent().getNavigationManager();

        navigationManager.when(context.constants.VMSessionNavigationManagerEvents
          .addSessionSidebarApplicationStackItem,
          (event) => {
            let application = event.data[0];
            this._eventListener.emit(this._newRootApplicationEvent, application);
          });
      },

      /**
       * Run the new application
       * @param {string} applicationId appId defined in the XCF file
       */
      newRootApplication: function(applicationId) {
        let application = gbc.SessionService.getCurrent().getCurrentApplication();
        application.protocolInterface.runApplication(application, applicationId);
      },

      /**
       * Event raised when a new application is created. Callback syntax: fn(application)
       * @param {function} callback function syntaxe: fn(VMApplication)
       * @return {HandleRegistration}
       */
      onNewRootApplication: function(callback) {
        return this._eventListener.when(this._newRootApplicationEvent, function(event) {
          callback(event.data[0]);
        });
      },

      /**
       * Get all the root applications
       * @return {classes.VMApplication[]}
       */
      getRootApplicationList: function() {
        var res = new Set();
        var navigationManager = gbc.SessionService.getCurrent().getNavigationManager();
        var appList = navigationManager.getApplications();

        appList.forEach((app) => {
          res.add(navigationManager.getRootWaitingApplication(app));
        });

        return Array.from(res);
      },

      /**
       * Get the current root application
       * @return {classes.VMApplication}
       */
      getCurrentRootApplication: function() {
        var session = gbc.SessionService.getCurrent();
        var navigationManager = session.getNavigationManager();

        return navigationManager.getRootWaitingApplication(session.getCurrentApplication());
      },

      /**
       * Show the active window of the application
       * @param {classes.VMApplication} application
       */
      setForegroundApplication: function(application) {
        application.getSession().getNavigationManager().goBackToLastActiveWindow(application);
      },

      /**
       * Close the current application
       */
      closeApplication: function() {
        gbc.SessionService.getCurrent().getCurrentApplication().close();
      },

      /**
       * Set the sidebar application list display mode
       * @param {String} visible could be 'yes', 'no' or 'auto'
       */
      setApplicationListVisible: function(visible) {
        gbc.HostLeftSidebarService.setApplicationListVisible(visible);
      }

    });

    context.InitService.register(context.NavigationService);
  });