Skip to content

Commit 13cc743

Browse files
author
daiwanxing
committed
feat: rewrite by typescript
1 parent 98d611e commit 13cc743

18 files changed

Lines changed: 1834 additions & 10973 deletions

.babelrc

Lines changed: 0 additions & 8 deletions
This file was deleted.

.browserslistrc

Lines changed: 0 additions & 1 deletion
This file was deleted.

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
registry=https://registry.npmmirror.com

babel.config.cjs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const presets = [
2+
[
3+
"@babel/preset-env",
4+
{
5+
targets: {
6+
chrome: "50",
7+
},
8+
modules: false
9+
},
10+
],
11+
];
12+
13+
module.exports = {
14+
presets
15+
}

dist/bmap-loader.es.js

Lines changed: 0 additions & 4701 deletions
This file was deleted.

dist/bmap-loader.umd.js

Lines changed: 0 additions & 4701 deletions
This file was deleted.

dist/index.js

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2+
try {
3+
var info = gen[key](arg);
4+
var value = info.value;
5+
} catch (error) {
6+
reject(error);
7+
return;
8+
}
9+
if (info.done) {
10+
resolve(value);
11+
} else {
12+
Promise.resolve(value).then(_next, _throw);
13+
}
14+
}
15+
function _asyncToGenerator(fn) {
16+
return function () {
17+
var self = this,
18+
args = arguments;
19+
return new Promise(function (resolve, reject) {
20+
var gen = fn.apply(self, args);
21+
function _next(value) {
22+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
23+
}
24+
function _throw(err) {
25+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
26+
}
27+
_next(undefined);
28+
});
29+
};
30+
}const LOAD_STATE = {
31+
NOT_LOAD: 1 << 1,
32+
LOADING: 1 << 2,
33+
LOADED: 1 << 3,
34+
FAILED: 1 << 4
35+
};
36+
const BMapGLLib = new Map([["DrawingManager", {
37+
install: LOAD_STATE.NOT_LOAD,
38+
hasCSS: true
39+
}], ["AreaRestriction", {
40+
install: LOAD_STATE.NOT_LOAD,
41+
hasCSS: false
42+
}], ["CustomOverlay", {
43+
install: LOAD_STATE.NOT_LOAD,
44+
hasCSS: false
45+
}], ["DistanceTool", {
46+
install: LOAD_STATE.NOT_LOAD,
47+
hasCSS: false
48+
}], ["Lushu", {
49+
install: LOAD_STATE.NOT_LOAD,
50+
hasCSS: false
51+
}], ["TrackAnimation", {
52+
install: LOAD_STATE.NOT_LOAD,
53+
hasCSS: false
54+
}], ["RichMarker", {
55+
install: LOAD_STATE.NOT_LOAD,
56+
hasCSS: false
57+
}]]);
58+
// https://lbsyun.baidu.com/index.php?title=jspopular3.0/openlibrary
59+
const BMapLib = new Map([["Heatmap", {
60+
install: LOAD_STATE.NOT_LOAD,
61+
hasCSS: false
62+
}], ["LuShu", {
63+
install: LOAD_STATE.NOT_LOAD,
64+
hasCSS: false
65+
}], ["AreaRestriction", {
66+
install: LOAD_STATE.NOT_LOAD,
67+
hasCSS: false
68+
}], ["RectangleZoom", {
69+
install: LOAD_STATE.NOT_LOAD,
70+
hasCSS: false
71+
}], ["RichMarker", {
72+
install: LOAD_STATE.NOT_LOAD,
73+
hasCSS: false
74+
}], ["InfoBox", {
75+
install: LOAD_STATE.NOT_LOAD,
76+
hasCSS: false
77+
}], ["CurveLine", {
78+
install: LOAD_STATE.NOT_LOAD,
79+
hasCSS: false
80+
}], ["DrawingManager", {
81+
install: LOAD_STATE.NOT_LOAD,
82+
hasCSS: true
83+
}], ["SearchInfoWindow", {
84+
install: LOAD_STATE.NOT_LOAD,
85+
hasCSS: true
86+
}], ["MarkerClusterer", {
87+
install: LOAD_STATE.NOT_LOAD,
88+
hasCSS: false
89+
}], ["TextIconOverlay", {
90+
install: LOAD_STATE.NOT_LOAD,
91+
hasCSS: false
92+
}]]);const BMAP_API_HOST = "https://api.map.baidu.com";
93+
let BMAP_STATE = LOAD_STATE.NOT_LOAD;
94+
const BMAP_GL = "webgl";
95+
function loader(_x) {
96+
return _loader.apply(this, arguments);
97+
}
98+
function _loader() {
99+
_loader = _asyncToGenerator(function* (params) {
100+
const ak = params.ak,
101+
v = params.v,
102+
type = params.type,
103+
library = params.library;
104+
if (!ak) return Promise.reject("please provide map ak");
105+
if (typeof ak !== "string") return Promise.reject(`illegal ak value: ${ak}`);
106+
if (!v) return Promise.reject("please set bmap version");
107+
if (type && type !== BMAP_GL) return Promise.reject(`expected type value webgl but got value ${type}`);
108+
return new Promise((resolve, rejected) => {
109+
if (BMAP_STATE & LOAD_STATE.LOADED) return resolve(null);
110+
if (BMAP_STATE & LOAD_STATE.FAILED) return rejected("install map script failed");
111+
if (BMAP_STATE & LOAD_STATE.LOADING) return appendUnSetteldTask(resolve, "bmap");
112+
const mapUrl = new URL(`${BMAP_API_HOST}/api`);
113+
mapUrl.searchParams.set("ak", ak);
114+
mapUrl.searchParams.set("v", v);
115+
mapUrl.searchParams.set("callback", "__BMapLoadedCallBack");
116+
if (type) mapUrl.searchParams.set("type", BMAP_GL);
117+
const script = document.createElement("script");
118+
script.src = mapUrl.toString();
119+
script.type = "text/javascript";
120+
BMAP_STATE = LOAD_STATE.LOADING;
121+
script.addEventListener("error", function (e) {
122+
BMAP_STATE = LOAD_STATE.FAILED;
123+
rejected(e.message);
124+
});
125+
window.__BMapLoadedCallBack = function (err) {
126+
if (err) {
127+
BMAP_STATE = LOAD_STATE.FAILED;
128+
return rejected(err);
129+
}
130+
BMAP_STATE = LOAD_STATE.LOADED;
131+
if (!Array.isArray(library) || !library.length) {
132+
resolve(null);
133+
run(unResolvedMapTask);
134+
} else {
135+
(type ? addBMapGLLibrary(library) : addBMapLibrary(library)).then(values => {
136+
resolve(null);
137+
run(unResolvedMapTask);
138+
const failedRecords = values.filter(record => record.status === "rejected");
139+
failedRecords.forEach(d => console.error(d.reason));
140+
}).catch(rejected);
141+
}
142+
};
143+
document.head.appendChild(script);
144+
});
145+
});
146+
return _loader.apply(this, arguments);
147+
}
148+
function addBMapLibrary(libs) {
149+
const awaitJobs = libs.map(data => {
150+
const lib = data.lib,
151+
version = data.version;
152+
const libData = BMapLib.get(lib);
153+
if (!libData) {
154+
console.warn("It's seems like you provide uncorrect lib name, please checkout your lib name spell");
155+
return Promise.resolve();
156+
} else {
157+
const install = libData.install,
158+
hasCSS = libData.hasCSS;
159+
if (install & LOAD_STATE.LOADED) return Promise.resolve();
160+
if (install & LOAD_STATE.FAILED) return Promise.reject(`try install library ${lib} failed`);
161+
if (install & LOAD_STATE.LOADING) return new Promise(resolve => appendUnSetteldTask(resolve, "normal"));
162+
return new Promise((resolve, rejected) => {
163+
const st = document.createElement("script");
164+
st.src = `${BMAP_API_HOST}/library/${lib}/${version}/src/${lib}.min.js`;
165+
document.head.appendChild(st);
166+
libData.install = LOAD_STATE.LOADING;
167+
st.addEventListener("error", function (e) {
168+
libData.install = LOAD_STATE.FAILED;
169+
rejected(e.message);
170+
});
171+
st.addEventListener("load", function () {
172+
libData.install = LOAD_STATE.LOADED;
173+
run(unResolvedLibTask);
174+
resolve(void 0);
175+
});
176+
if (hasCSS) addLibStyle(`${BMAP_API_HOST}/library/${lib}/${version}/src/${lib}.min.css`);
177+
});
178+
}
179+
});
180+
return Promise.allSettled(awaitJobs);
181+
}
182+
// GL版的开源工具库放到了百度云的BOS存储上, 参见右侧链接 https://github.com/huiyan-fe/BMapGLLib
183+
const BMapGLLibURL = "https://mapopen.bj.bcebos.com/github/BMapGLLib";
184+
function addBMapGLLibrary(libs) {
185+
// 对于已经安装了的lib,可以直接resolve
186+
const awaitJobs = libs.map(data => {
187+
const lib = data.lib;
188+
const libData = BMapGLLib.get(lib);
189+
if (!libData) {
190+
console.warn("It's seems like you provide uncorrect GLlib name, please checkout your GLlib name spell");
191+
return Promise.resolve();
192+
} else {
193+
const install = libData.install,
194+
hasCSS = libData.hasCSS;
195+
if (install & LOAD_STATE.LOADED) return Promise.resolve();
196+
if (install & LOAD_STATE.FAILED) return Promise.reject(`try install GL-library ${lib} failed`);
197+
if (install & LOAD_STATE.LOADING) return new Promise(resolve => appendUnSetteldTask(resolve, "gl"));
198+
return new Promise((resolve, rejected) => {
199+
const st = document.createElement("script");
200+
st.src = `${BMapGLLibURL}/${lib}/src/${lib}.min.js`;
201+
document.head.appendChild(st);
202+
libData.install = LOAD_STATE.LOADING;
203+
st.addEventListener("error", function (e) {
204+
libData.install = LOAD_STATE.FAILED;
205+
rejected(e.message);
206+
});
207+
st.addEventListener("load", function () {
208+
libData.install = LOAD_STATE.LOADED;
209+
run(unResolvedGLLibTask);
210+
resolve(void 0);
211+
});
212+
if (hasCSS) addLibStyle(`${BMapGLLibURL}/${lib}/src/${lib}.min.css`);
213+
});
214+
}
215+
});
216+
return Promise.allSettled(awaitJobs);
217+
}
218+
function addLibStyle(libUrl) {
219+
const style = document.createElement("link");
220+
style.rel = "stylesheet";
221+
style.href = libUrl;
222+
document.head.appendChild(style);
223+
}
224+
const unResolvedMapTask = [];
225+
const unResolvedGLLibTask = [];
226+
const unResolvedLibTask = [];
227+
function appendUnSetteldTask(fn, libType) {
228+
switch (libType) {
229+
case "bmap":
230+
unResolvedMapTask.push(fn);
231+
break;
232+
case "gl":
233+
unResolvedGLLibTask.push(fn);
234+
break;
235+
case "normal":
236+
unResolvedLibTask.push(fn);
237+
break;
238+
}
239+
}
240+
function run(taskSets) {
241+
taskSets.forEach(d => d());
242+
taskSets.splice(0);
243+
}export{loader as default};

0 commit comments

Comments
 (0)