APILoader.js 3.1 KB

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