client/src/luigi-client.js
function luigiClientInit() {
  /**
   * Save context data every time navigation to a different node happens
   * @private
   */
  function setContext(rawData) {
    for (var index = 0; index < defaultContextKeys.length; index++) {
      var key = defaultContextKeys[index];
      try {
        if (typeof rawData[key] === 'string') {
          rawData[key] = JSON.parse(rawData[key]);
        }
      } catch (e) {
        console.info(
          'unable to parse luigi context data for',
          key,
          rawData[key],
          e
        );
      }
    }
    currentContext = rawData;
  }

  function setAuthData(eventPayload) {
    if (eventPayload) {
      authData = eventPayload;
    }
  }

  window.addEventListener('message', function messageListener(e) {
    if ('luigi.init' === e.data.msg) {
      setContext(e.data);
      setAuthData(e.data.authData);
      luigiInitialized = true;
      _callAllFns(_onInitFns, currentContext.context);
    } else if ('luigi.navigate' === e.data.msg) {
      setContext(e.data);
      if (!currentContext.internal.isNavigateBack) {
        window.location.replace(e.data.viewUrl);
      }

      // execute the context change listener if set by the microfrontend
      _callAllFns(_onContextUpdatedFns, currentContext.context);

      window.parent.postMessage(
        {
          msg: 'luigi.navigate.ok'
        },
        '*'
      );
    } else if ('luigi.auth.tokenIssued' === e.data.msg) {
      setAuthData(e.data.authData);
    }

    if ('luigi.navigation.pathExists.answer' === e.data.msg) {
      var data = e.data.data;
      pathExistsPromises[data.correlationId].resolveFn(data.pathExists);
      delete pathExistsPromises[data.correlationId];
    }

    if ('luigi.ux.confirmationModal.hide' === e.data.msg) {
      const data = e.data.data;
      const promise = promises.confirmationModal;
      if (promise) {
        data.confirmed ? promise.resolveFn() : promise.rejectFn();
        delete promises.confirmationModal;
      }
    }

    if ('luigi.ux.alert.hide' === e.data.msg) {
      const { id } = e.data;
      if (id && promises.alerts[id]) {
        promises.alerts[id].resolveFn(id);
        delete promises.alerts[id];
      }
    }
  });

  window.parent.postMessage(
    {
      msg: 'luigi.get-context'
    },
    '*'
  );
}
function luigiClientInit() {
  /**
   * Save context data every time navigation to a different node happens
   * @private
   */
  function setContext(rawData) {
    for (var index = 0; index < defaultContextKeys.length; index++) {
      var key = defaultContextKeys[index];
      try {
        if (typeof rawData[key] === 'string') {
          rawData[key] = JSON.parse(rawData[key]);
        }
      } catch (e) {
        console.info(
          'unable to parse luigi context data for',
          key,
          rawData[key],
          e
        );
      }
    }
    currentContext = rawData;
  }

  function setAuthData(eventPayload) {
    if (eventPayload) {
      authData = eventPayload;
    }
  }

  window.addEventListener('message', function messageListener(e) {
    if ('luigi.init' === e.data.msg) {
      setContext(e.data);
      setAuthData(e.data.authData);
      luigiInitialized = true;
      _callAllFns(_onInitFns, currentContext.context);
    } else if ('luigi.navigate' === e.data.msg) {
      setContext(e.data);
      if (!currentContext.internal.isNavigateBack) {
        window.location.replace(e.data.viewUrl);
      }

      // execute the context change listener if set by the microfrontend
      _callAllFns(_onContextUpdatedFns, currentContext.context);

      window.parent.postMessage(
        {
          msg: 'luigi.navigate.ok'
        },
        '*'
      );
    } else if ('luigi.auth.tokenIssued' === e.data.msg) {
      setAuthData(e.data.authData);
    }

    if ('luigi.navigation.pathExists.answer' === e.data.msg) {
      var data = e.data.data;
      pathExistsPromises[data.correlationId].resolveFn(data.pathExists);
      delete pathExistsPromises[data.correlationId];
    }

    if ('luigi.ux.confirmationModal.hide' === e.data.msg) {
      const data = e.data.data;
      const promise = promises.confirmationModal;
      if (promise) {
        data.confirmed ? promise.resolveFn() : promise.rejectFn();
        delete promises.confirmationModal;
      }
    }

    if ('luigi.ux.alert.hide' === e.data.msg) {
      const { id } = e.data;
      if (id && promises.alerts[id]) {
        promises.alerts[id].resolveFn(id);
        delete promises.alerts[id];
      }
    }
  });

  window.parent.postMessage(
    {
      msg: 'luigi.get-context'
    },
    '*'
  );
}
function luigiClientInit ( ) { /**
   * Save context data every time navigation to a different node happens
   * @private
   */ function setContext ( rawData ) { for ( var index = 0 ; index < defaultContextKeys . length ; index ++ ) { var key = defaultContextKeys [ index ] ; try { if ( typeof rawData [ key ] === 'string' ) { rawData [ key ] = JSON . parse ( rawData [ key ] ) ; } } catch ( e ) { console . info ( 'unable to parse luigi context data for' , key , rawData [ key ] , e ) ; } } currentContext = rawData ; } function setAuthData ( eventPayload ) { if ( eventPayload ) { authData = eventPayload ; } } window . addEventListener ( 'message' , function messageListener ( e ) { if ( 'luigi.init' === e . data . msg ) { setContext ( e . data ) ; setAuthData ( e . data . authData ) ; luigiInitialized = true ; _callAllFns ( _onInitFns , currentContext . context ) ; } else if ( 'luigi.navigate' === e . data . msg ) { setContext ( e . data ) ; if ( ! currentContext . internal . isNavigateBack ) { window . location . replace ( e . data . viewUrl ) ; } // execute the context change listener if set by the microfrontend _callAllFns ( _onContextUpdatedFns , currentContext . context ) ; window . parent . postMessage ( { msg : 'luigi.navigate.ok' } , '*' ) ; } else if ( 'luigi.auth.tokenIssued' === e . data . msg ) { setAuthData ( e . data . authData ) ; } if ( 'luigi.navigation.pathExists.answer' === e . data . msg ) { var data = e . data . data ; pathExistsPromises [ data . correlationId ] . resolveFn ( data . pathExists ) ; delete pathExistsPromises [ data . correlationId ] ; } if ( 'luigi.ux.confirmationModal.hide' === e . data . msg ) { const data = e . data . data ; const promise = promises . confirmationModal ; if ( promise ) { data . confirmed ? promise . resolveFn ( ) : promise . rejectFn ( ) ; delete promises . confirmationModal ; } } if ( 'luigi.ux.alert.hide' === e . data . msg ) { const { id } = e . data ; if ( id && promises . alerts [ id ] ) { promises . alerts [ id ] . resolveFn ( id ) ; delete promises . alerts [ id ] ; } } } ) ; window . parent . postMessage ( { msg : 'luigi.get-context' } , '*' ) ; }
Adds event listener for communication with Luigi Core and starts communication
@private
Adds event listener for communication with Luigi Core and starts communication