Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check if before layer exists #5679

Merged
merged 4 commits into from
Nov 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ class Style extends Evented {
/**
* Add a layer to the map style. The layer will be inserted before the layer with
* ID `before`, or appended if `before` is omitted.
* @param {string=} before ID of an existing layer to insert before
* @param {string} before ID of an existing layer to insert before
*/
addLayer(layerObject: LayerSpecification, before?: string, options?: {validate?: boolean}) {
this._checkLoaded();
Expand All @@ -568,7 +568,7 @@ class Style extends Evented {

const index = before ? this._order.indexOf(before) : this._order.length;
if (before && index === -1) {
this.fire('error', { message: new Error(`Layer with id "${before}" does not exist on this map.`)});
this.fire('error', { error: new Error(`Layer with id "${before}" does not exist on this map.`)});
return;
}

Expand Down Expand Up @@ -599,9 +599,10 @@ class Style extends Evented {
}

/**
* Add a layer to the map style. The layer will be inserted before the layer with
* Moves a layer to a different z-position. The layer will be inserted before the layer with
* ID `before`, or appended if `before` is omitted.
* @param {string=} before ID of an existing layer to insert before
* @param {string} id ID of the layer to move
* @param {string} before ID of an existing layer to insert before
*/
moveLayer(id: string, before?: string) {
this._checkLoaded();
Expand All @@ -622,6 +623,10 @@ class Style extends Evented {
this._order.splice(index, 1);

const newIndex = before ? this._order.indexOf(before) : this._order.length;
if (before && newIndex === -1) {
this.fire('error', { error: new Error(`Layer with id "${before}" does not exist on this map.`)});
return;
}
this._order.splice(newIndex, 0, id);

this._layerOrderChanged = true;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/style/style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ test('Style#addLayer', (t) => {

style.on('style.load', () => {
style.on('error', (error)=>{
t.match(error.message, /does not exist on this map/);
t.match(error.error, /does not exist on this map/);
t.end();
});
style.addLayer(layer, 'z');
Expand Down