earthLoader.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. (function () {
  2. let scriptArr = Array.from(document.getElementsByTagName('script'))
  3. let host, targetScript;
  4. scriptArr.map(item => {
  5. let src = item.getAttribute('src')
  6. if (item.src.match('earthLoader.js')) {
  7. host = src.split('earthLoader.js')[0];
  8. targetScript = item;
  9. }
  10. })
  11. let cesiumDir = "./thirdParty/Cesium/";
  12. let cesiumJS = `${cesiumDir}Cesium.js`;
  13. let cesiumCss = `${cesiumDir}Widgets/widgets.css`;
  14. let earthLib = "./Uninpho.js";
  15. let thirdJsArr = {
  16. "turfjs": './thirdParty/turf.min.js',
  17. "heatmap": './thirdParty/heatmap.js',
  18. "echarts": './thirdParty/echarts.js',
  19. "kriging": './thirdParty/Kriging/kriging.js',
  20. "dat.gui": './thirdParty/dat.gui.min.js',
  21. }
  22. function loadResource(arr, type) {
  23. function loadScript(url) {
  24. var script = '<script type="text/javascript" src="' + url + '"><' + '/script>';
  25. document.writeln(script);
  26. }
  27. function loadCSS(url) {
  28. var css = '<link rel="stylesheet" href="' + url + '">';
  29. document.writeln(css);
  30. }
  31. if (type === "js") {
  32. for (let i = 0; i < arr.length; i++) {
  33. loadScript(`${host}${arr[i]}`);
  34. }
  35. } else if (type === "css") {
  36. for (let i = 0; i < arr.length; i++) {
  37. loadCSS(`${host}${arr[i]}`)
  38. }
  39. }
  40. }
  41. function loadJS(url, callback) {
  42. var script = document.createElement('script'),
  43. fn = callback || function () {};
  44. script.type = 'text/javascript';
  45. //IE
  46. if (script.readyState) {
  47. script.onreadystatechange = function () {
  48. if (script.readyState == 'loaded' || script.readyState == 'complete') {
  49. script.onreadystatechange = null;
  50. fn();
  51. }
  52. };
  53. } else {
  54. //其他浏览器
  55. script.onload = function () {
  56. fn();
  57. };
  58. }
  59. script.src = host + url;
  60. document.getElementsByTagName('head')[0].appendChild(script);
  61. }
  62. //按需加载第三方资源库
  63. function loadIncludes(callback) {
  64. let include = targetScript.getAttribute('include');
  65. if (include && include.split(',').length > 0) {
  66. let includes = include.split(',');
  67. let total = includes.length;
  68. let tag = 0;
  69. includes.map(item => {
  70. if (thirdJsArr[item]) {
  71. loadJS(thirdJsArr[item], () => {
  72. tag++;
  73. if (tag == total) {
  74. callback();
  75. }
  76. });
  77. }
  78. });
  79. }
  80. }
  81. function main() {
  82. //加载球
  83. function loadEarth() {
  84. loadJS(earthLib, () => {
  85. if (window.onload) {
  86. onload();
  87. }
  88. if (window.earthReady!==undefined) {
  89. earthReady();
  90. }
  91. })
  92. }
  93. //加载Cesium以及第三方库
  94. loadResource([cesiumCss], "css");
  95. loadJS(cesiumJS, () => {
  96. let include = targetScript.getAttribute('include');
  97. if (include && include.split(',').length > 0) {
  98. loadIncludes(() => {
  99. loadEarth();
  100. });
  101. } else {
  102. loadEarth();
  103. }
  104. });
  105. }
  106. main();
  107. })()