123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- (function () {
- let scriptArr = Array.from(document.getElementsByTagName('script'))
- let host, targetScript;
- scriptArr.map(item => {
- let src = item.getAttribute('src')
- if (item.src.match('earthLoader.js')) {
- host = src.split('earthLoader.js')[0];
- targetScript = item;
- }
- })
- let cesiumDir = "./thirdParty/Cesium/";
- let cesiumJS = `${cesiumDir}Cesium.js`;
- let cesiumCss = `${cesiumDir}Widgets/widgets.css`;
- let earthLib = "./Uninpho.js";
- let thirdJsArr = {
- "turfjs": './thirdParty/turf.min.js',
- "heatmap": './thirdParty/heatmap.js',
- "echarts": './thirdParty/echarts.js',
- "kriging": './thirdParty/Kriging/kriging.js',
- "dat.gui": './thirdParty/dat.gui.min.js',
- }
- function loadResource(arr, type) {
- function loadScript(url) {
- var script = '<script type="text/javascript" src="' + url + '"><' + '/script>';
- document.writeln(script);
- }
- function loadCSS(url) {
- var css = '<link rel="stylesheet" href="' + url + '">';
- document.writeln(css);
- }
- if (type === "js") {
- for (let i = 0; i < arr.length; i++) {
- loadScript(`${host}${arr[i]}`);
- }
- } else if (type === "css") {
- for (let i = 0; i < arr.length; i++) {
- loadCSS(`${host}${arr[i]}`)
- }
- }
- }
- function loadJS(url, callback) {
- var script = document.createElement('script'),
- fn = callback || function () {};
- script.type = 'text/javascript';
- //IE
- if (script.readyState) {
- script.onreadystatechange = function () {
- if (script.readyState == 'loaded' || script.readyState == 'complete') {
- script.onreadystatechange = null;
- fn();
- }
- };
- } else {
- //其他浏览器
- script.onload = function () {
- fn();
- };
- }
- script.src = host + url;
- document.getElementsByTagName('head')[0].appendChild(script);
- }
- //按需加载第三方资源库
- function loadIncludes(callback) {
- let include = targetScript.getAttribute('include');
- if (include && include.split(',').length > 0) {
- let includes = include.split(',');
- let total = includes.length;
- let tag = 0;
- includes.map(item => {
- if (thirdJsArr[item]) {
- loadJS(thirdJsArr[item], () => {
- tag++;
- if (tag == total) {
- callback();
- }
- });
- }
- });
- }
- }
- function main() {
- //加载球
- function loadEarth() {
- loadJS(earthLib, () => {
- if (window.onload) {
- onload();
- }
- if (window.earthReady!==undefined) {
- earthReady();
- }
- })
- }
- //加载Cesium以及第三方库
- loadResource([cesiumCss], "css");
- loadJS(cesiumJS, () => {
- let include = targetScript.getAttribute('include');
- if (include && include.split(',').length > 0) {
- loadIncludes(() => {
- loadEarth();
- });
- } else {
- loadEarth();
- }
- });
- }
- main();
- })()
|