preload.js 4.82 KB

// Global variable for desktop
const mxIsElectron = window && window.process && window.process.type
/**
   * Adds meta tag to the page.
   */
function mxmeta(name, content, httpEquiv) {
  try {
    const s = document.createElement('meta')

    if (name != null)

      s.setAttribute('name', name)

    s.setAttribute('content', content)

    if (httpEquiv != null)

      s.setAttribute('http-equiv', httpEquiv)

    const t = document.getElementsByTagName('meta')[0]
    t.parentNode.insertBefore(s, t)
  }
  catch (e) {
    // ignore
  }
}

/**
   * Synchronously adds scripts to the page.
   */
function mxscript(src, onLoad, id, dataAppKey, noWrite, onError) {
  const defer = onLoad == null && !noWrite

  if ((urlParams.dev != '1' && typeof document.createElement('canvas').getContext === 'function')
    || onLoad != null || noWrite) {
    const s = document.createElement('script')
    s.setAttribute('type', 'text/javascript')
    s.setAttribute('defer', 'true')
    s.setAttribute('src', src)

    if (id != null)
      s.setAttribute('id', id)

    if (dataAppKey != null)
      s.setAttribute('data-app-key', dataAppKey)

    if (onLoad != null) {
      let r = false

      s.onload = s.onreadystatechange = function () {
        if (!r && (!this.readyState || this.readyState == 'complete')) {
          r = true
          onLoad()
        }
      }
    }

    if (onError != null) {
      s.onerror = function (e) {
        onError(`Failed to load ${src}`, e)
      }
    }

    const t = document.getElementsByTagName('script')[0]

    if (t != null)
      t.parentNode.insertBefore(s, t)
  }
  else {
    document.write(`<script src="${src}"${(id != null) ? ` id="${id}" ` : ''
      }${(dataAppKey != null) ? ` data-app-key="${dataAppKey}" ` : ''}></scr` + 'ipt>')
  }
}

/**
   * Asynchronously adds scripts to the page.
   */
function mxinclude(src) {
  const g = document.createElement('script')
  g.type = 'text/javascript'
  g.async = true
  g.src = src

  const s = document.getElementsByTagName('script')[0]
  s.parentNode.insertBefore(g, s)
}


// Checks for local storage
let isLocalStorage = false

try {
  isLocalStorage = urlParams.local != '1' && typeof (localStorage) != 'undefined'
}
catch (e) {
  // ignored
}

/**
 * 
 * @param {string} ossFileName oss文件名
 * @param {string} localFileAddress 本地加载文件路径
 * @returns 
 */
function getLoadOssFileAddress(ossFileName, localFileAddress) {
  const { enableOss, ossAddress } = window.PROJECT_ENV
  return enableOss && ossAddress ? `${ossAddress}${ossAddress.endsWith('/') ? '' : '/'}${ossFileName}` : localFileAddress
}

let mxScriptsLoaded = false; let mxWinLoaded = false

// Changes paths for local development environment
if (window.urlParams.dev == '1') {
  // Used to request grapheditor/mxgraph sources in dev mode
  window.drawDevUrl = `//${location.host}${getProxyPrefix()}/webapp/`
  window.geBasePath = `//${location.host}${getProxyPrefix()}/webapp/js/grapheditor`
  window.mxBasePath = `//${location.host}${getProxyPrefix()}/webapp/mxgraph`
  window.mxForceIncludes = true

  // mxscript(`${window.drawDevUrl}js/PreConfig.js`)
  mxscript(`${window.drawDevUrl}js/diagramly/Init.js`)
  mxscript(`${window.geBasePath}/Init.js`)
  mxscript(`${window.mxBasePath}/mxClient.js`)

  // Adds all JS code that depends on mxClient. This indirection via Devel.js is
  // required in some browsers to make sure mxClient.js (and the files that it
  // loads asynchronously) are available when the code loaded in Devel.js runs.
  mxscript(`${window.drawDevUrl}js/diagramly/Devel.js`)

  mxscript(`${window.drawDevUrl}js/PostConfig.js`)

} else {
  (function () {
    var hostName = window.location.hostname;

    // Supported domains are *.draw.io and the packaged version in Quip
    var supportedDomain = (hostName.substring(hostName.length - 8, hostName.length) === '.draw.io') ||
      (hostName.substring(hostName.length - 13, hostName.length) === '.diagrams.net');

    function loadAppJS() {
      mxscript(getLoadOssFileAddress('app.min.js', `${getProxyPrefix()}/webapp/js/app.min.js`), function () {
        mxScriptsLoaded = true;
        checkAllLoaded();

        mxscript(`${getProxyPrefix()}/webapp/js/PostConfig.js`);
      });
    };

    if (!supportedDomain || mxIsElectron) {
      mxscript(`${getProxyPrefix()}/webapp/js/PreConfig.js`, loadAppJS);
    } else {
      loadAppJS();
    }
  })();
}

function checkAllLoaded() {
  if (mxScriptsLoaded && mxWinLoaded) {
    bootstrap()
  }
};


// Adds basic error handling
window.onerror = function () {
  const status = document.getElementById('geStatus')

  if (status != null)
    status.innerHTML = 'Page could not be loaded. Please try refreshing.'
}

/**
 * Main
 */
if (urlParams['dev'] != '1' && typeof document.createElement('canvas').getContext === "function") {
  window.addEventListener('load', function () {
    mxWinLoaded = true;
    checkAllLoaded();
  });
}