@ecogis/maps-style-switcher 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,33 @@
1
+ import { IControl, Map as MapboxMap } from "mapbox-gl";
2
+ export declare type VnptMapStyleDefinition = {
3
+ title: string;
4
+ uri: string;
5
+ };
6
+ export declare type VnptMapStyleSwitcherOptions = {
7
+ defaultStyle?: string;
8
+ eventListeners?: VnptMapStyleSwitcherEvents;
9
+ };
10
+ declare type VnptMapStyleSwitcherEvents = {
11
+ onOpen?: (event: MouseEvent) => boolean;
12
+ onSelect?: (event: MouseEvent) => boolean;
13
+ onChange?: (event: MouseEvent, style: string) => boolean;
14
+ };
15
+ export declare class VnptMapStyleSwitcherControl implements IControl {
16
+ private static readonly DEFAULT_STYLE;
17
+ private static readonly DEFAULT_STYLES;
18
+ private controlContainer;
19
+ private events?;
20
+ private map?;
21
+ private mapStyleContainer;
22
+ private styleButton;
23
+ private styles;
24
+ private defaultStyle;
25
+ constructor(styles?: VnptMapStyleDefinition[], options?: VnptMapStyleSwitcherOptions | string);
26
+ getDefaultPosition(): string;
27
+ onAdd(map: MapboxMap): HTMLElement;
28
+ onRemove(): void;
29
+ private closeModal;
30
+ private openModal;
31
+ private onDocumentClick;
32
+ }
33
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class VnptMapStyleSwitcherControl {
4
+ constructor(styles, options) {
5
+ this.styles = styles || VnptMapStyleSwitcherControl.DEFAULT_STYLES;
6
+ const defaultStyle = typeof (options) === "string" ? options : options ? options.defaultStyle : undefined;
7
+ this.defaultStyle = defaultStyle || VnptMapStyleSwitcherControl.DEFAULT_STYLE;
8
+ this.onDocumentClick = this.onDocumentClick.bind(this);
9
+ this.events = typeof (options) !== "string" && options ? options.eventListeners : undefined;
10
+ }
11
+ getDefaultPosition() {
12
+ const defaultPosition = "top-right";
13
+ return defaultPosition;
14
+ }
15
+ onAdd(map) {
16
+ this.map = map;
17
+ this.controlContainer = document.createElement("div");
18
+ this.controlContainer.classList.add("ecogis-ctrl");
19
+ this.controlContainer.classList.add("ecogis-ctrl-group");
20
+ this.mapStyleContainer = document.createElement("div");
21
+ this.styleButton = document.createElement("button");
22
+ this.styleButton.type = "button";
23
+ this.mapStyleContainer.classList.add("ecogis-style-list");
24
+ for (const style of this.styles) {
25
+ const styleElement = document.createElement("button");
26
+ styleElement.type = "button";
27
+ styleElement.innerText = style.title;
28
+ styleElement.classList.add(style.title.replace(/[^a-z0-9-]/gi, '_'));
29
+ styleElement.dataset.uri = JSON.stringify(style.uri);
30
+ styleElement.addEventListener("click", event => {
31
+ const srcElement = event.srcElement;
32
+ this.closeModal();
33
+ if (srcElement.classList.contains("active")) {
34
+ return;
35
+ }
36
+ if (this.events && this.events.onOpen && this.events.onOpen(event)) {
37
+ return;
38
+ }
39
+ const style = JSON.parse(srcElement.dataset.uri);
40
+ this.map.setStyle(style);
41
+ const elms = this.mapStyleContainer.getElementsByClassName("active");
42
+ while (elms[0]) {
43
+ elms[0].classList.remove("active");
44
+ }
45
+ srcElement.classList.add("active");
46
+ if (this.events && this.events.onChange && this.events.onChange(event, style)) {
47
+ return;
48
+ }
49
+ });
50
+ if (style.title === this.defaultStyle) {
51
+ styleElement.classList.add("active");
52
+ }
53
+ this.mapStyleContainer.appendChild(styleElement);
54
+ }
55
+ this.styleButton.classList.add("ecogis-ctrl-icon");
56
+ this.styleButton.classList.add("ecogis-style-switcher");
57
+ this.styleButton.addEventListener("click", event => {
58
+ if (this.events && this.events.onSelect && this.events.onSelect(event)) {
59
+ return;
60
+ }
61
+ this.openModal();
62
+ });
63
+ document.addEventListener("click", this.onDocumentClick);
64
+ this.controlContainer.appendChild(this.styleButton);
65
+ this.controlContainer.appendChild(this.mapStyleContainer);
66
+ return this.controlContainer;
67
+ }
68
+ onRemove() {
69
+ if (!this.controlContainer || !this.controlContainer.parentNode || !this.map || !this.styleButton) {
70
+ return;
71
+ }
72
+ this.styleButton.removeEventListener("click", this.onDocumentClick);
73
+ this.controlContainer.parentNode.removeChild(this.controlContainer);
74
+ document.removeEventListener("click", this.onDocumentClick);
75
+ this.map = undefined;
76
+ }
77
+ closeModal() {
78
+ if (this.mapStyleContainer && this.styleButton) {
79
+ this.mapStyleContainer.style.display = "none";
80
+ this.styleButton.style.display = "block";
81
+ }
82
+ }
83
+ openModal() {
84
+ if (this.mapStyleContainer && this.styleButton) {
85
+ this.mapStyleContainer.style.display = "block";
86
+ this.styleButton.style.display = "none";
87
+ }
88
+ }
89
+ onDocumentClick(event) {
90
+ if (this.controlContainer && !this.controlContainer.contains(event.target)) {
91
+ this.closeModal();
92
+ }
93
+ }
94
+ }
95
+ VnptMapStyleSwitcherControl.DEFAULT_STYLE = "Streets";
96
+ VnptMapStyleSwitcherControl.DEFAULT_STYLES = [
97
+ { title: "Dark", uri: "mapbox://styles/mapbox/dark-v10" },
98
+ { title: "Light", uri: "mapbox://styles/mapbox/light-v10" },
99
+ { title: "Outdoors", uri: "mapbox://styles/mapbox/outdoors-v11" },
100
+ { title: "Satellite", uri: "mapbox://styles/mapbox/satellite-streets-v11" },
101
+ { title: "Streets", uri: "mapbox://styles/mapbox/streets-v11" }
102
+ ];
103
+ exports.VnptMapStyleSwitcherControl = VnptMapStyleSwitcherControl;
104
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;AAqBA,MAAa,2BAA2B;IAmBpC,YAAY,MAAiC,EAAE,OAA8C;QAEzF,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,2BAA2B,CAAC,cAAc,CAAC;QACnE,MAAM,YAAY,GAAG,OAAM,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,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-switcher",
3
+ "version": "0.0.1",
4
+ "description": "Ecogis Style Switcher",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "test": "echo \"Error: no test specified\" && exit 1"
10
+ },
11
+ "keywords": [
12
+ "ecogis",
13
+ "maps",
14
+ "style-switcher"
15
+ ],
16
+ "license": "GPL-3.0",
17
+ "dependencies": {
18
+ "mapbox-gl": "^1.11.1"
19
+ },
20
+ "devDependencies": {
21
+ "@types/mapbox-gl": "^1.9.0",
22
+ "typescript": "^3.1.3",
23
+ "minimist": ">=0.2.1"
24
+ }
25
+ }
package/styles.css ADDED
@@ -0,0 +1,40 @@
1
+ .ecogis-style-list
2
+ {
3
+ display: none;
4
+ }
5
+
6
+ .ecogis-ctrl-group .ecogis-style-list button
7
+ {
8
+ background: none;
9
+ border: none;
10
+ cursor: pointer;
11
+ display: block;
12
+ font-size: 14px;
13
+ padding: 8px 8px 6px;
14
+ text-align: right;
15
+ width: 100%;
16
+ height: auto;
17
+ }
18
+
19
+ .ecogis-style-list button.active
20
+ {
21
+ font-weight: bold;
22
+ }
23
+
24
+ .ecogis-style-list button:hover
25
+ {
26
+ background-color: rgba(0, 0, 0, 0.05);
27
+ }
28
+
29
+ .ecogis-style-list button + button
30
+ {
31
+ border-top: 1px solid #ddd;
32
+ }
33
+
34
+ .ecogis-style-switcher
35
+ {
36
+ background-image: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/PjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+PHN2ZyB2ZXJzaW9uPSIxLjEiIGlkPSJDYXBhXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpb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1LjQ2NWwxMi41MzcsNS4wMTVjMC40NzcsMC4xOTEsMS4yNTEsMC4xOTEsMS43MjksMGwxMi41NDEtNS4wMTZsMTMuNjU4LTUuNDYzYzAuNDc3LTAuMTkxLDAuNDgtMC41MTEsMC4wMS0wLjcxNkwyOC4yNzcsNC4wNDhjLTAuNDcxLTAuMjA0LTEuMjM2LTAuMjA0LTEuNzA4LDBMMC4zNTEsMTUuNDFDLTAuMTIxLDE1LjYxNC0wLjExNiwxNS45MzUsMC4zNjEsMTYuMTI1eiIvPjwvZz48L2c+PC9nPjxnPjwvZz48Zz48L2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PGc+PC9nPjwvc3ZnPg==);
37
+ background-position: center;
38
+ background-repeat: no-repeat;
39
+ background-size: 70%;
40
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "compilerOptions": {
3
+ "alwaysStrict": true,
4
+ "baseUrl": ".",
5
+ "declaration": true,
6
+ "experimentalDecorators": true,
7
+ "lib": [
8
+ "dom",
9
+ "es2017"
10
+ ],
11
+ "module": "commonjs",
12
+ "moduleResolution": "node",
13
+ "noImplicitThis": true,
14
+ "noUnusedLocals": true,
15
+ "outDir": "./dist/",
16
+ "preserveConstEnums": true,
17
+ "removeComments": true,
18
+ "strictFunctionTypes": true,
19
+ "strictNullChecks": true,
20
+ "sourceMap": true,
21
+ "target": "es6"
22
+ }
23
+ }