From d36cece8690b6d572bc5f8a78dc9b4ef0c75aa96 Mon Sep 17 00:00:00 2001 From: thinkinggis Date: Fri, 22 Feb 2019 12:03:01 +0800 Subject: [PATCH] refactor(core): layer register --- src/core/scene.js | 18 +++++------------- src/layer/factory.js | 11 +++++++++++ src/layer/index.js | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 src/layer/factory.js diff --git a/src/core/scene.js b/src/core/scene.js index 36aa09b287..d9229a2512 100644 --- a/src/core/scene.js +++ b/src/core/scene.js @@ -1,5 +1,5 @@ import Engine from './engine'; -import * as layers from '../layer'; +import { LAYER_MAP } from '../layer'; import Base from './base'; import LoadImage from './image'; import WorkerPool from './worker'; @@ -48,14 +48,14 @@ export default class Scene extends Base { } initLayer() { - for (const methodName in layers) { - this[methodName] = cfg => { - cfg ? cfg.mapType = this.mapType : cfg = { mapType: this.mapType }; - const layer = new layers[methodName](this, cfg); + for (const key in LAYER_MAP) { + Scene.prototype[key] = cfg => { + const layer = new LAYER_MAP[key](this, cfg); this._layers.push(layer); return layer; }; } + } on(type, hander) { if (this.map) { this.map.on(type, hander); } @@ -83,14 +83,6 @@ export default class Scene extends Base { getLayers() { 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() { } diff --git a/src/layer/factory.js b/src/layer/factory.js new file mode 100644 index 0000000000..081f27fe90 --- /dev/null +++ b/src/layer/factory.js @@ -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; +}; diff --git a/src/layer/index.js b/src/layer/index.js index 08c2e0d201..5e8fef27c6 100644 --- a/src/layer/index.js +++ b/src/layer/index.js @@ -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 PointLayer } from './pointLayer'; export { default as LineLayer } from './lineLayer';