Skip to content
Snippets Groups Projects
viamapi-client.js 1.21 KiB
Newer Older
  • Learn to ignore specific revisions
  • const Penpal = require('penpal').default;
    
    
    /**
     * Sets up interaction with Vereign Restful API
     * @param divId - target container to append iframe via Penpal
     * @param methods - list of methods to be used in iframe
     * @param iframeUrl - iframe URL to connect
     * @param apiUrl -  API URL used to access API endpoints
    
    Markin Igor's avatar
    Markin Igor committed
     * @param wopiPrefix -  WOPI URI prefix
    
     * @returns {*}
     */
    
    Markin Igor's avatar
    Markin Igor committed
    function setupViamAPI(divId, methods, iframeUrl, apiUrl, wopiPrefix = 'wopi') {
    
      if (!apiUrl) {
    
        apiUrl = `${window.location.origin}/api/`;
        console.warn(`API host URL not specified. Fall back to ${apiUrl}`); // eslint-disable-line no-console
      }
    
      if (!iframeUrl) {
        iframeUrl = `${window.location.origin}/vcl/js/iframe`;
        console.warn(`Iframe URL not specified. Fall back to ${iframeUrl}`); // eslint-disable-line no-console
    
      const connection = Penpal.connectToChild({
        // URL of page to load into iframe.
        url: iframeUrl,
        // Container to which the iframe should be appended.
        appendTo: document.getElementById(divId),
        // Methods parent is exposing to child
        methods
      });
    
      return connection.promise
    
    Markin Igor's avatar
    Markin Igor committed
        .then((child) => child.initialize(apiUrl, wopiPrefix).then(() => child));
    
    
    window.setupViamAPI = setupViamAPI;