APILoader.js 3.2 KB

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