Skip to content
Open
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
2 changes: 2 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export declare type MapboxStyleDefinition = {
export declare type MapboxStyleSwitcherOptions = {
defaultStyle?: string;
eventListeners?: MapboxStyleSwitcherEvents;
bindClickToMapContainer?: boolean;
};
declare type MapboxStyleSwitcherEvents = {
onOpen?: (event: MouseEvent) => boolean;
Expand All @@ -22,6 +23,7 @@ export declare class MapboxStyleSwitcherControl implements IControl {
private styleButton;
private styles;
private defaultStyle;
private bindClickToMapContainer;
constructor(styles?: MapboxStyleDefinition[], options?: MapboxStyleSwitcherOptions | string);
getDefaultPosition(): string;
onAdd(map: MapboxMap): HTMLElement;
Expand Down
16 changes: 14 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type MapboxStyleSwitcherOptions =
{
defaultStyle?: string;
eventListeners?: MapboxStyleSwitcherEvents;
bindClickToMapContainer?: boolean;
}

type MapboxStyleSwitcherEvents =
Expand Down Expand Up @@ -38,13 +39,18 @@ export class MapboxStyleSwitcherControl implements IControl
private styles: MapboxStyleDefinition[];
private defaultStyle: string;

//#41: could be an optional parameter a cleaner way to cope with shadowDOM issue?
private bindClickToMapContainer: boolean = false;

constructor(styles?: MapboxStyleDefinition[], options?: MapboxStyleSwitcherOptions | string)
{
this.styles = styles || MapboxStyleSwitcherControl.DEFAULT_STYLES;
const defaultStyle = typeof(options) === "string" ? options : options ? options.defaultStyle : undefined;
this.defaultStyle = defaultStyle || MapboxStyleSwitcherControl.DEFAULT_STYLE;
this.onDocumentClick = this.onDocumentClick.bind(this);
this.events = typeof(options) !== "string" && options ? options.eventListeners : undefined;

this.bindClickToMapContainer = (typeof (options) !== "string" && options) ? (typeof (options.bindClickToMapContainer) == "boolean" ? options.bindClickToMapContainer : false) : false;
}

public getDefaultPosition(): string
Expand Down Expand Up @@ -112,7 +118,11 @@ export class MapboxStyleSwitcherControl implements IControl
this.openModal();
});

document.addEventListener("click", this.onDocumentClick);
if(this.bindClickToMapContainer){
this.map.getContainer().addEventListener("click", this.onDocumentClick);
} else {
document.addEventListener("click", this.onDocumentClick);
}

this.controlContainer.appendChild(this.styleButton);
this.controlContainer.appendChild(this.mapStyleContainer);
Expand All @@ -127,7 +137,12 @@ export class MapboxStyleSwitcherControl implements IControl
}
this.styleButton.removeEventListener("click", this.onDocumentClick);
this.controlContainer.parentNode.removeChild(this.controlContainer);
document.removeEventListener("click", this.onDocumentClick);

if (this.bindClickToMapContainer) {
this.map.getContainer().removeEventListener("click", this.onDocumentClick);
} else {
document.removeEventListener("click", this.onDocumentClick);
}
this.map = undefined;
}

Expand Down
Loading