Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c37563b

Browse files
committed
refacto: move ImageryLayers in its own module
1 parent e1a8911 commit c37563b

File tree

6 files changed

+65
-60
lines changed

6 files changed

+65
-60
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
// move this to new index
3+
// After the modification :
4+
// * the minimum sequence will always be 0
5+
// * the maximum sequence will always be layers.lenght - 1
6+
// the ordering of all layers (Except that specified) doesn't change
7+
export function moveLayerToIndex(layer, newIndex, imageryLayers) {
8+
newIndex = Math.min(newIndex, imageryLayers.length - 1);
9+
newIndex = Math.max(newIndex, 0);
10+
const oldIndex = layer.sequence;
11+
12+
for (const imagery of imageryLayers) {
13+
if (imagery.id === layer.id) {
14+
// change index of specified layer
15+
imagery.sequence = newIndex;
16+
} else if (imagery.sequence > oldIndex && imagery.sequence <= newIndex) {
17+
// down all layers between the old index and new index (to compensate the deletion of the old index)
18+
imagery.sequence--;
19+
} else if (imagery.sequence >= newIndex && imagery.sequence < oldIndex) {
20+
// up all layers between the new index and old index (to compensate the insertion of the new index)
21+
imagery.sequence++;
22+
}
23+
}
24+
}
25+
26+
export function moveLayerDown(layer, imageryLayers) {
27+
if (layer.sequence > 0) {
28+
moveLayerToIndex(layer, layer.sequence - 1, imageryLayers);
29+
}
30+
}
31+
32+
export function moveLayerUp(layer, imageryLayers) {
33+
const m = imageryLayers.length - 1;
34+
if (layer.sequence < m) {
35+
moveLayerToIndex(layer, layer.sequence + 1, imageryLayers);
36+
}
37+
}
38+
39+
export function getColorLayersIdOrderedBySequence(imageryLayers) {
40+
const copy = Array.from(imageryLayers);
41+
copy.sort((a, b) => a.sequence - b.sequence);
42+
return copy.map(l => l.id);
43+
}

packages/Main/src/Layer/Layer.js

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -263,48 +263,3 @@ class Layer extends THREE.EventDispatcher {
263263
}
264264

265265
export default Layer;
266-
267-
export const ImageryLayers = {
268-
// move this to new index
269-
// After the modification :
270-
// * the minimum sequence will always be 0
271-
// * the maximum sequence will always be layers.lenght - 1
272-
// the ordering of all layers (Except that specified) doesn't change
273-
moveLayerToIndex: function moveLayerToIndex(layer, newIndex, imageryLayers) {
274-
newIndex = Math.min(newIndex, imageryLayers.length - 1);
275-
newIndex = Math.max(newIndex, 0);
276-
const oldIndex = layer.sequence;
277-
278-
for (const imagery of imageryLayers) {
279-
if (imagery.id === layer.id) {
280-
// change index of specified layer
281-
imagery.sequence = newIndex;
282-
} else if (imagery.sequence > oldIndex && imagery.sequence <= newIndex) {
283-
// down all layers between the old index and new index (to compensate the deletion of the old index)
284-
imagery.sequence--;
285-
} else if (imagery.sequence >= newIndex && imagery.sequence < oldIndex) {
286-
// up all layers between the new index and old index (to compensate the insertion of the new index)
287-
imagery.sequence++;
288-
}
289-
}
290-
},
291-
292-
moveLayerDown: function moveLayerDown(layer, imageryLayers) {
293-
if (layer.sequence > 0) {
294-
this.moveLayerToIndex(layer, layer.sequence - 1, imageryLayers);
295-
}
296-
},
297-
298-
moveLayerUp: function moveLayerUp(layer, imageryLayers) {
299-
const m = imageryLayers.length - 1;
300-
if (layer.sequence < m) {
301-
this.moveLayerToIndex(layer, layer.sequence + 1, imageryLayers);
302-
}
303-
},
304-
305-
getColorLayersIdOrderedBySequence: function getColorLayersIdOrderedBySequence(imageryLayers) {
306-
const copy = Array.from(imageryLayers);
307-
copy.sort((a, b) => a.sequence - b.sequence);
308-
return copy.map(l => l.id);
309-
},
310-
};

packages/Main/src/Layer/TiledGeometryLayer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { InfoTiledGeometryLayer } from 'Layer/InfoLayer';
44
import Picking from 'Core/Picking';
55
import convertToTile from 'Converter/convertToTile';
66
import ObjectRemovalHelper from 'Process/ObjectRemovalHelper';
7-
import { ImageryLayers } from 'Layer/Layer';
7+
import { getColorLayersIdOrderedBySequence } from 'Layer/ImageryLayers';
88
import { CACHE_POLICIES } from 'Core/Scheduler/Cache';
99

1010
const subdivisionVector = new THREE.Vector3();
@@ -249,7 +249,7 @@ class TiledGeometryLayer extends GeometryLayer {
249249
// Prepare ColorLayer sequence order
250250
// In this moment, there is only one color layers sequence, because they are attached to tileLayer.
251251
// In future, the sequence must be returned by parent geometry layer.
252-
this.colorLayersOrder = ImageryLayers.getColorLayersIdOrderedBySequence(context.colorLayers);
252+
this.colorLayersOrder = getColorLayersIdOrderedBySequence(context.colorLayers);
253253

254254
let commonAncestor;
255255
for (const source of sources.values()) {

packages/Main/src/Main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export { default as Label } from 'Core/Label';
4646
// Layers provided by default in iTowns
4747
// A custom layer should at least implements Layer
4848
// See http://www.itowns-project.org/itowns/docs/#api/Layer/Layer
49-
export { default as Layer, ImageryLayers } from 'Layer/Layer';
49+
export { default as Layer } from 'Layer/Layer';
50+
export * as ImageryLayers from 'Layer/ImageryLayers';
5051
export { default as ColorLayer } from 'Layer/ColorLayer';
5152
export { default as ElevationLayer } from 'Layer/ElevationLayer';
5253
export { default as GeometryLayer } from 'Layer/GeometryLayer';

packages/Main/src/Renderer/ColorLayersOrdering.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import { ImageryLayers } from 'Layer/Layer';
1+
import {
2+
getColorLayersIdOrderedBySequence,
3+
moveLayerUp,
4+
moveLayerDown,
5+
moveLayerToIndex,
6+
} from 'Layer/ImageryLayers';
27

38
function updateLayersOrdering(geometryLayer, imageryLayers) {
4-
const sequence = ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers);
9+
const sequence = getColorLayersIdOrderedBySequence(imageryLayers);
510
const cO = function cO(object) {
611
if (object.material?.setColorTileIds) {
712
object.material.setColorTileIds(sequence);
@@ -35,13 +40,13 @@ export default {
3540
const imageryLayers = view.getLayers(l => l.isColorLayer);
3641
const layer = view.getLayerById(layerId);
3742
if (layer) {
38-
const previousSequence = ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers);
39-
ImageryLayers.moveLayerUp(layer, imageryLayers);
43+
const previousSequence = getColorLayersIdOrderedBySequence(imageryLayers);
44+
moveLayerUp(layer, imageryLayers);
4045
updateLayersOrdering(view.tileLayer, imageryLayers);
4146
view.dispatchEvent({
4247
type: COLOR_LAYERS_ORDER_CHANGED,
4348
previous: { sequence: previousSequence },
44-
new: { sequence: ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers) },
49+
new: { sequence: getColorLayersIdOrderedBySequence(imageryLayers) },
4550
});
4651
view.notifyChange(view.tileLayer);
4752
} else {
@@ -62,13 +67,13 @@ export default {
6267
const imageryLayers = view.getLayers(l => l.isColorLayer);
6368
const layer = view.getLayerById(layerId);
6469
if (layer) {
65-
const previousSequence = ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers);
66-
ImageryLayers.moveLayerDown(layer, imageryLayers);
70+
const previousSequence = getColorLayersIdOrderedBySequence(imageryLayers);
71+
moveLayerDown(layer, imageryLayers);
6772
updateLayersOrdering(view.tileLayer, imageryLayers);
6873
view.dispatchEvent({
6974
type: COLOR_LAYERS_ORDER_CHANGED,
7075
previous: { sequence: previousSequence },
71-
new: { sequence: ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers) },
76+
new: { sequence: getColorLayersIdOrderedBySequence(imageryLayers) },
7277
});
7378
view.notifyChange(view.tileLayer);
7479
} else {
@@ -90,13 +95,13 @@ export default {
9095
const imageryLayers = view.getLayers(l => l.isColorLayer);
9196
const layer = view.getLayerById(layerId);
9297
if (layer) {
93-
const previousSequence = ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers);
94-
ImageryLayers.moveLayerToIndex(layer, index, imageryLayers);
98+
const previousSequence = getColorLayersIdOrderedBySequence(imageryLayers);
99+
moveLayerToIndex(layer, index, imageryLayers);
95100
updateLayersOrdering(view.tileLayer, imageryLayers);
96101
view.dispatchEvent({
97102
type: COLOR_LAYERS_ORDER_CHANGED,
98103
previous: { sequence: previousSequence },
99-
new: { sequence: ImageryLayers.getColorLayersIdOrderedBySequence(imageryLayers) },
104+
new: { sequence: getColorLayersIdOrderedBySequence(imageryLayers) },
100105
});
101106
view.notifyChange(view.tileLayer);
102107
} else {

packages/Main/test/unit/layer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import assert from 'assert';
2-
import Layer, { ImageryLayers } from 'Layer/Layer';
2+
import Layer from 'Layer/Layer';
3+
import * as ImageryLayers from 'Layer/ImageryLayers';
34
import ColorLayer from 'Layer/ColorLayer';
45

56
describe('Layer', function () {

0 commit comments

Comments
 (0)