export function download()

in src/editor/downloadExample.js [5:96]


export function download(sourceHeader) {
  const SCRIPT_URLS = getScriptURLs(store.locale);

  const hasRootPath = store.sourceCode.indexOf('ROOT_PATH') > -1;
  const rootPathCode = hasRootPath ? `var ROOT_PATH = '${store.cdnRoot}';` : '';
  const lang = store.locale === 'zh' > -1 ? 'zh-CN' : 'en';

  const hasJQueryJS = /\$[\.\(]+/g.test(store.sourceCode);
  const jqueryScriptCode = hasJQueryJS
    ? `<script type="text/javascript" src="${SCRIPT_URLS.jQueryJS}"></script>`
    : '';
  const echartsDir = SCRIPT_URLS[
    store.echartsVersion.indexOf('dev') > -1
      ? 'echartsNightlyDir'
      : 'echartsDir'
  ].replace('{{version}}', store.echartsVersion);
  const echarts4Dir = SCRIPT_URLS.echartsDir.replace('{{version}}', '4.9.0');

  const themeScriptCode =
    !store.darkMode && store.theme
      ? `<!-- theme -->\n  <script type="text/javascript" src="${echartsDir}/theme/${store.theme}.js"></script>`
      : '';

  const code = `<!--
${sourceHeader}
-->
<!DOCTYPE html>
<html lang="${lang}" style="height: 100%">
<head>
  <meta charset="utf-8">
</head>
<body style="height: 100%; margin: 0">
  <div id="container" style="height: 100%"></div>

  ${jqueryScriptCode}
  <script type="text/javascript" src="${echartsDir}${
    SCRIPT_URLS.echartsJS
  }"></script>
  ${themeScriptCode}
  <!-- Uncomment this line if you want to dataTool extension
  <script type="text/javascript" src="${echartsDir}/dist/extension/dataTool.min.js"></script>
  -->
  <!-- Uncomment this line if you want to use gl extension
  <script type="text/javascript" src="${SCRIPT_URLS.echartsGLJS}"></script>
  -->
  <!-- Uncomment this line if you want to echarts-stat extension
  <script type="text/javascript" src="${SCRIPT_URLS.echartsStatJS}"></script>
  -->
  <!-- Uncomment this line if you want to echarts-graph-modularity extension
  <script type="text/javascript" src="${
    SCRIPT_URLS.echartsGraphModularityJS
  }"></script>
  -->
  <!-- Uncomment this line if you want to use map
  <script type="text/javascript" src="${echarts4Dir}/map/js/world.js"></script>
  -->
  <!-- Uncomment these two lines if you want to use bmap extension
  <script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=YOUR_API_KEY"></script>
  <script type="text/javascript" src="${echartsDir}${
    SCRIPT_URLS.echartsBMapJS
  }"></script>
  -->

  <script type="text/javascript">
    var dom = document.getElementById('container');
    var myChart = echarts.init(dom, ${
      store.darkMode ? "'dark'" : store.theme ? `'${store.theme}'` : 'null'
    }, {
      renderer: '${store.renderer}',
      useDirtyRect: ${store.useDirtyRect}
    });
    var app = {};
    ${rootPathCode}
    var option;

    ${store.sourceCode}

    if (option && typeof option === 'object') {
      myChart.setOption(option);
    }

    window.addEventListener('resize', myChart.resize);
  </script>
</body>
</html>`;
  const file = new Blob([code], {
    type: 'text/html;charset=UTF-8',
    encoding: 'UTF-8'
  });
  // download the blob
  downloadBlob(file, (URL_PARAMS.c || Date.now()) + '.html');
}