refactor(core): layer register

This commit is contained in:
thinkinggis 2019-02-22 12:03:01 +08:00
parent 94368efb20
commit d36cece869
3 changed files with 30 additions and 13 deletions

View File

@ -1,5 +1,5 @@
import Engine from './engine'; import Engine from './engine';
import * as layers from '../layer'; import { LAYER_MAP } from '../layer';
import Base from './base'; import Base from './base';
import LoadImage from './image'; import LoadImage from './image';
import WorkerPool from './worker'; import WorkerPool from './worker';
@ -48,14 +48,14 @@ export default class Scene extends Base {
} }
initLayer() { initLayer() {
for (const methodName in layers) { for (const key in LAYER_MAP) {
this[methodName] = cfg => { Scene.prototype[key] = cfg => {
cfg ? cfg.mapType = this.mapType : cfg = { mapType: this.mapType }; const layer = new LAYER_MAP[key](this, cfg);
const layer = new layers[methodName](this, cfg);
this._layers.push(layer); this._layers.push(layer);
return layer; return layer;
}; };
} }
} }
on(type, hander) { on(type, hander) {
if (this.map) { this.map.on(type, hander); } if (this.map) { this.map.on(type, hander); }
@ -83,14 +83,6 @@ export default class Scene extends Base {
getLayers() { getLayers() {
return this._layers; return this._layers;
} }
_addLight() {
// const scene = this._engine._scene;
// //const ambientLight = new THREE.AmbientLight(0xaaaaaa);
// scene.add(ambientLight);
// const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
// scene.add(directionalLight);
}
_addLayer() { _addLayer() {
} }

11
src/layer/factory.js Normal file
View File

@ -0,0 +1,11 @@
export const LAYER_MAP = {};
export const getLayer = type => {
return LAYER_MAP[type.toLowerCase()];
};
export const registerLayer = (type, layer) => {
if (getLayer(type)) {
throw new Error(`Layer type '${type}' existed.`);
}
// 存储到 map 中
LAYER_MAP[type] = layer;
};

View File

@ -1,3 +1,17 @@
import { registerLayer } from './factory';
import PolygonLayer from './polygonLayer';
import PointLayer from './pointLayer';
import LineLayer from './lineLayer';
import ImageLayer from './imageLayer';
import RasterLayer from './rasterLayer';
registerLayer('PolygonLayer', PolygonLayer);
registerLayer('PointLayer', PointLayer);
registerLayer('LineLayer', LineLayer);
registerLayer('ImageLayer', ImageLayer);
registerLayer('RasterLayer', RasterLayer);
export { LAYER_MAP } from './factory';
export { default as PolygonLayer } from './polygonLayer'; export { default as PolygonLayer } from './polygonLayer';
export { default as PointLayer } from './pointLayer'; export { default as PointLayer } from './pointLayer';
export { default as LineLayer } from './lineLayer'; export { default as LineLayer } from './lineLayer';