APILoader.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  2. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. var DEFAULT_CONFIG = {
  5. v: '1.4.0',
  6. hostAndPath: 'webapi.amap.com/maps',
  7. key: 'f97efc35164149d0c0f299e7a8adb3d2',
  8. callback: '__amap_init_callback',
  9. useAMapUI: false
  10. };
  11. var mainPromise = null;
  12. var amapuiPromise = null;
  13. var amapuiInited = false;
  14. var APILoader = function () {
  15. function APILoader(_ref) {
  16. var key = _ref.key,
  17. useAMapUI = _ref.useAMapUI,
  18. version = _ref.version,
  19. protocol = _ref.protocol;
  20. _classCallCheck(this, APILoader);
  21. this.config = _extends({}, DEFAULT_CONFIG, { useAMapUI: useAMapUI, protocol: protocol });
  22. if (typeof window !== 'undefined') {
  23. if (key) {
  24. this.config.key = key;
  25. } else if ('amapkey' in window) {
  26. this.config.key = window.amapkey;
  27. }
  28. }
  29. if (version) {
  30. this.config.v = version;
  31. }
  32. this.protocol = protocol || window.location.protocol;
  33. if (this.protocol.indexOf(':') === -1) {
  34. this.protocol += ':';
  35. }
  36. }
  37. _createClass(APILoader, [{
  38. key: 'getScriptSrc',
  39. value: function getScriptSrc(cfg) {
  40. return this.protocol + '//' + cfg.hostAndPath + '?v=' + cfg.v + '&key=' + cfg.key + '&callback=' + cfg.callback;
  41. }
  42. }, {
  43. key: 'buildScriptTag',
  44. value: function buildScriptTag(src) {
  45. var script = document.createElement('script');
  46. script.type = 'text/javascript';
  47. script.async = true;
  48. script.defer = true;
  49. script.src = src;
  50. return script;
  51. }
  52. }, {
  53. key: 'getAmapuiPromise',
  54. value: function getAmapuiPromise() {
  55. var script = this.buildScriptTag(this.protocol + '//webapi.amap.com/ui/1.0/main-async.js');
  56. var p = new Promise(function (resolve) {
  57. script.onload = function () {
  58. resolve();
  59. };
  60. });
  61. document.body.appendChild(script);
  62. return p;
  63. }
  64. }, {
  65. key: 'getMainPromise',
  66. value: function getMainPromise() {
  67. var _this = this;
  68. var script = this.buildScriptTag(this.getScriptSrc(this.config));
  69. var p = new Promise(function (resolve) {
  70. window[_this.config.callback] = function () {
  71. resolve();
  72. delete window[_this.config.callback];
  73. };
  74. });
  75. document.body.appendChild(script);
  76. return p;
  77. }
  78. }, {
  79. key: 'load',
  80. value: function load() {
  81. if (typeof window === 'undefined') {
  82. return null;
  83. }
  84. var useAMapUI = this.config.useAMapUI;
  85. mainPromise = mainPromise || this.getMainPromise();
  86. if (useAMapUI) {
  87. amapuiPromise = amapuiPromise || this.getAmapuiPromise();
  88. }
  89. return new Promise(function (resolve) {
  90. mainPromise.then(function () {
  91. if (useAMapUI && amapuiPromise) {
  92. amapuiPromise.then(function () {
  93. if (window.initAMapUI && !amapuiInited) {
  94. window.initAMapUI();
  95. if (typeof useAMapUI === 'function') {
  96. useAMapUI();
  97. }
  98. amapuiInited = true;
  99. }
  100. resolve();
  101. });
  102. } else {
  103. resolve();
  104. }
  105. });
  106. });
  107. }
  108. }]);
  109. return APILoader;
  110. }();
  111. export default APILoader;