diff --git a/cypress/e2e/accessibility.cy.ts b/cypress/e2e/accessibility.cy.ts
index 5a47f1ad..5c5ebfee 100644
--- a/cypress/e2e/accessibility.cy.ts
+++ b/cypress/e2e/accessibility.cy.ts
@@ -11,7 +11,7 @@ describe("accessibility", () => {
it("skip link to layer list", () => {
const selector = "root:skip:layer-list";
- should.isExists(selector);
+ should.exist(selector);
when.tab();
should.beFocused(selector);
when.click(selector);
@@ -20,7 +20,7 @@ describe("accessibility", () => {
it("skip link to layer editor", () => {
const selector = "root:skip:layer-editor";
- should.isExists(selector);
+ should.exist(selector);
when.tab().tab();
should.beFocused(selector);
when.click(selector);
@@ -29,7 +29,7 @@ describe("accessibility", () => {
it("skip link to map view", () => {
const selector = "root:skip:map-view";
- should.isExists(selector);
+ should.exist(selector);
when.tab().tab().tab();
should.beFocused(selector);
when.click(selector);
diff --git a/cypress/e2e/driver.ts b/cypress/e2e/driver.ts
index 16cd60e4..71762db2 100644
--- a/cypress/e2e/driver.ts
+++ b/cypress/e2e/driver.ts
@@ -73,7 +73,7 @@ export default class MaputnikDriver {
}
cy.get(".maputnik-toolbar-link").should("be.visible");
},
- fillLayersModal: (opts: any) => {
+ fillLayersModal: (opts: {type: string, layer?: string, id?: string}) => {
var type = opts.type;
var layer = opts.layer;
var id;
@@ -84,12 +84,12 @@ export default class MaputnikDriver {
}
cy.get(
- this.get.getDataAttribute("add-layer.layer-type", "select")
+ this.get.dataAttribute("add-layer.layer-type", "select")
).select(type);
- cy.get(this.get.getDataAttribute("add-layer.layer-id", "input")).type(id);
+ cy.get(this.get.dataAttribute("add-layer.layer-id", "input")).type(id);
if (layer) {
cy.get(
- this.get.getDataAttribute("add-layer.layer-source-block", "input")
+ this.get.dataAttribute("add-layer.layer-source-block", "input")
).type(layer);
}
this.when.click("add-layer");
@@ -103,11 +103,20 @@ export default class MaputnikDriver {
click: (selector: string) => {
this.helper.when.click(selector);
- // cy.get(selector).click({ force: true });
+ },
+
+ clickZoomin: () => {
+ cy.get(".maplibregl-ctrl-zoom-in").click();
+ },
+
+ selectWithin: (selector: string, value: string) => {
+ this.when.within(selector, () => {
+ cy.get("select").select(value);
+ });
},
select: (selector: string, value: string) => {
- cy.get(selector).select(value);
+ this.helper.get.element(selector).select(value);
},
focus: (selector: string) => {
@@ -126,8 +135,8 @@ export default class MaputnikDriver {
openLayersModal: () => {
this.helper.when.click("layer-list:add-layer");
- cy.get(this.get.getDataAttribute("modal:add-layer")).should("exist");
- cy.get(this.get.getDataAttribute("modal:add-layer")).should("be.visible");
+ cy.get(this.get.dataAttribute("modal:add-layer")).should("exist");
+ cy.get(this.get.dataAttribute("modal:add-layer")).should("be.visible");
},
};
@@ -135,16 +144,16 @@ export default class MaputnikDriver {
isMac: () => {
return Cypress.platform === "darwin";
},
- getStyleFromWindow: (win: Window) => {
+ styleFromWindow: (win: Window) => {
const styleId = win.localStorage.getItem("maputnik:latest_style");
const styleItem = win.localStorage.getItem(`maputnik:style:${styleId}`);
const obj = JSON.parse(styleItem || "");
return obj;
},
- getExampleFileUrl: () => {
+ exampleFileUrl: () => {
return "http://localhost:8888/example-style.json";
},
- getDataAttribute: (key: string, selector?: string): string => {
+ dataAttribute: (key: string, selector?: string): string => {
return `*[data-wd-key='${key}'] ${selector || ""}`;
},
};
@@ -176,23 +185,26 @@ export default class MaputnikDriver {
equalStyleStore: (getter: (obj: any) => any, styleObj: any) => {
cy.window().then((win: any) => {
- const obj = this.get.getStyleFromWindow(win);
+ const obj = this.get.styleFromWindow(win);
assert.deepEqual(getter(obj), styleObj);
});
},
- isStyleStoreEqualToExampleFileData: () => {
+ styleStoreEqualToExampleFileData: () => {
cy.window().then((win: any) => {
- const obj = this.get.getStyleFromWindow(win);
+ const obj = this.get.styleFromWindow(win);
cy.fixture("example-style.json").should("deep.equal", obj);
});
},
- isExists: (selector: string) => {
+ exist: (selector: string) => {
this.helper.get.element(selector).should("exist");
},
- isSelected: (selector: string, value: string) => {
- cy.get(selector).find(`option[value="${value}"]`).should("be.selected");
+ beSelected: (selector: string, value: string) => {
+ this.helper.get.element(selector).find(`option[value="${value}"]`).should("be.selected");
},
+ containText: (selector: string, text: string) => {
+ this.helper.get.element(selector).should("contain.text", text);
+ }
};
}
diff --git a/cypress/e2e/keyboard.cy.ts b/cypress/e2e/keyboard.cy.ts
index 3d7a24bb..8cb5b1ce 100644
--- a/cypress/e2e/keyboard.cy.ts
+++ b/cypress/e2e/keyboard.cy.ts
@@ -1,4 +1,4 @@
-import { default as MaputnikDriver } from "./driver";
+import MaputnikDriver from "./driver";
describe("keyboard", () => {
let { beforeAndAfter, given, when, get, should } = new MaputnikDriver();
@@ -45,12 +45,12 @@ describe("keyboard", () => {
it("'i' should change map to inspect mode", () => {
when.typeKeys("i");
- should.isSelected(get.getDataAttribute("nav:inspect"), "inspect");
+ should.beSelected("nav:inspect", "inspect");
});
it("'m' should focus map", () => {
when.typeKeys("m");
- should.beFocused(".maplibregl-canvas");
+ should.canvasBeFocused();
});
it("'!' should show debug modal", () => {
diff --git a/cypress/e2e/layers.cy.ts b/cypress/e2e/layers.cy.ts
index 736a578e..41efdb43 100644
--- a/cypress/e2e/layers.cy.ts
+++ b/cypress/e2e/layers.cy.ts
@@ -1,4 +1,3 @@
-var assert = require("assert");
import { v1 as uuid } from "uuid";
import MaputnikDriver from "./driver";
@@ -134,12 +133,9 @@ describe("layers", () => {
// Setup
var id = uuid();
- when.select(
- get.getDataAttribute("add-layer.layer-type", "select"),
- "background"
- );
+ when.selectWithin("add-layer.layer-type", "background");
when.setValue(
- get.getDataAttribute("add-layer.layer-id", "input"),
+ get.dataAttribute("add-layer.layer-id", "input"),
"background:" + id
);
@@ -167,7 +163,7 @@ describe("layers", () => {
var id = uuid();
when.setValue(
- get.getDataAttribute("layer-editor.layer-id", "input"),
+ get.dataAttribute("layer-editor.layer-id", "input"),
"foobar:" + id
);
when.click("min-zoom");
@@ -188,7 +184,7 @@ describe("layers", () => {
when.click("layer-list-item:background:" + bgId);
when.setValue(
- get.getDataAttribute("min-zoom", 'input[type="text"]'),
+ get.dataAttribute("min-zoom", 'input[type="text"]'),
"1"
);
@@ -206,8 +202,8 @@ describe("layers", () => {
);
// AND RESET!
- // driver.setValue(driver.getDataAttribute("min-zoom", "input"), "")
- // driver.click(driver.getDataAttribute("max-zoom", "input"));
+ // driver.setValue(driver.get.dataAttribute("min-zoom", "input"), "")
+ // driver.click(driver.get.dataAttribute("max-zoom", "input"));
// driver.isStyleStoreEqual((a: any) => a.layers, [
// {
@@ -222,7 +218,7 @@ describe("layers", () => {
when.click("layer-list-item:background:" + bgId);
when.setValue(
- get.getDataAttribute("max-zoom", 'input[type="text"]'),
+ get.dataAttribute("max-zoom", 'input[type="text"]'),
"1"
);
@@ -245,7 +241,7 @@ describe("layers", () => {
var id = uuid();
when.click("layer-list-item:background:" + bgId);
- when.setValue(get.getDataAttribute("layer-comment", "textarea"), id);
+ when.setValue(get.dataAttribute("layer-comment", "textarea"), id);
when.click("layer-editor.layer-id");
@@ -324,9 +320,7 @@ describe("layers", () => {
when.typeKeys(
"\uE013\uE013\uE013\uE013\uE013\uE013\uE013\uE013\uE013\uE013\uE013\uE013 {"
);
- should.isExists(errorSelector);
-
- when.click("layer-editor.layer-id");
+ should.exist(errorSelector);
});
});
});
diff --git a/cypress/e2e/map.cy.ts b/cypress/e2e/map.cy.ts
index ea0b695b..afccd755 100644
--- a/cypress/e2e/map.cy.ts
+++ b/cypress/e2e/map.cy.ts
@@ -8,18 +8,16 @@ describe("map", () => {
var zoomLevel = 12.37;
when.setStyle("geojson", zoomLevel);
should.beVisible("maplibre:ctrl-zoom");
- // HM TODO
- //driver.getText(".maplibregl-ctrl-zoom") === "Zoom "+(zoomLevel);
+ should.containText("maplibre:ctrl-zoom", "Zoom: " + zoomLevel);
});
it("via map controls", () => {
var zoomLevel = 12.37;
when.setStyle("geojson", zoomLevel);
- when.click("maplibre:ctrl-zoom");
should.beVisible("maplibre:ctrl-zoom");
- // HM TODO
- //driver.getText(".maplibregl-ctrl-zoom") === "Zoom "+(zoomLevel + 1);
+ when.clickZoomin();
+ should.containText("maplibre:ctrl-zoom", "Zoom: "+(zoomLevel + 1));
});
});
});
diff --git a/cypress/e2e/modals.cy.ts b/cypress/e2e/modals.cy.ts
index a910d2c4..e5fd505f 100644
--- a/cypress/e2e/modals.cy.ts
+++ b/cypress/e2e/modals.cy.ts
@@ -20,17 +20,17 @@ describe("modals", () => {
// HM: I was not able to make the following choose file actually to select a file and close the modal...
when.chooseExampleFile();
- should.isStyleStoreEqualToExampleFileData();
+ should.styleStoreEqualToExampleFileData();
});
it("load from url", () => {
- var styleFileUrl = get.getExampleFileUrl();
+ var styleFileUrl = get.exampleFileUrl();
- when.setValue(get.getDataAttribute("modal:open.url.input"), styleFileUrl);
+ when.setValue(get.dataAttribute("modal:open.url.input"), styleFileUrl);
when.click("modal:open.url.button");
when.waitForExampleFileRequset();
- should.isStyleStoreEqualToExampleFileData();
+ should.styleStoreEqualToExampleFileData();
});
});
@@ -67,7 +67,7 @@ describe("modals", () => {
it("toggle", () => {
when.setStyle("geojson");
- when.select(get.getDataAttribute("nav:inspect", "select"), "inspect");
+ when.selectWithin("nav:inspect", "inspect");
});
});
@@ -77,20 +77,20 @@ describe("modals", () => {
});
it("name", () => {
- when.setValue(get.getDataAttribute("modal:settings.name"), "foobar");
+ when.setValue(get.dataAttribute("modal:settings.name"), "foobar");
when.click("modal:settings.owner");
should.equalStyleStore((obj) => obj.name, "foobar");
});
it("owner", () => {
- when.setValue(get.getDataAttribute("modal:settings.owner"), "foobar");
+ when.setValue(get.dataAttribute("modal:settings.owner"), "foobar");
when.click("modal:settings.name");
should.equalStyleStore((obj) => obj.owner, "foobar");
});
it("sprite url", () => {
when.setValue(
- get.getDataAttribute("modal:settings.sprite"),
+ get.dataAttribute("modal:settings.sprite"),
"http://example.com"
);
when.click("modal:settings.name");
@@ -99,7 +99,7 @@ describe("modals", () => {
});
it("glyphs url", () => {
var glyphsUrl = "http://example.com/{fontstack}/{range}.pbf";
- when.setValue(get.getDataAttribute("modal:settings.glyphs"), glyphsUrl);
+ when.setValue(get.dataAttribute("modal:settings.glyphs"), glyphsUrl);
when.click("modal:settings.name");
should.equalStyleStore((obj) => obj.glyphs, glyphsUrl);
@@ -108,7 +108,7 @@ describe("modals", () => {
it("maptiler access token", () => {
var apiKey = "testing123";
when.setValue(
- get.getDataAttribute(
+ get.dataAttribute(
"modal:settings.maputnik:openmaptiles_access_token"
),
apiKey
@@ -124,7 +124,7 @@ describe("modals", () => {
it("thunderforest access token", () => {
var apiKey = "testing123";
when.setValue(
- get.getDataAttribute(
+ get.dataAttribute(
"modal:settings.maputnik:thunderforest_access_token"
),
apiKey
@@ -139,14 +139,8 @@ describe("modals", () => {
it("style renderer", () => {
cy.on("uncaught:exception", () => false); // this is due to the fact that this is an invalid style for openlayers
- when.select(
- get.getDataAttribute("modal:settings.maputnik:renderer"),
- "ol"
- );
- should.isSelected(
- get.getDataAttribute("modal:settings.maputnik:renderer"),
- "ol"
- );
+ when.select("modal:settings.maputnik:renderer", "ol");
+ should.beSelected("modal:settings.maputnik:renderer", "ol");
when.click("modal:settings.name");
diff --git a/src/components/App.jsx b/src/components/App.jsx
index 5ef42a0d..4287f8eb 100644
--- a/src/components/App.jsx
+++ b/src/components/App.jsx
@@ -1,50 +1,43 @@
-import { arrayMoveMutable } from "array-move";
-import buffer from "buffer";
-import { unset } from "lodash";
-import clamp from "lodash.clamp";
-import cloneDeep from "lodash.clonedeep";
-import get from "lodash.get";
-import React from "react";
-import autoBind from "react-autobind";
+import autoBind from 'react-autobind';
+import React from 'react'
+import cloneDeep from 'lodash.clonedeep'
+import clamp from 'lodash.clamp'
+import buffer from 'buffer'
+import get from 'lodash.get'
+import {unset} from 'lodash'
+import {arrayMoveMutable} from 'array-move'
+import url from 'url'
import hash from "string-hash";
-import url from "url";
-import AppLayout from "./AppLayout";
-import MessagePanel from "./AppMessagePanel";
-import AppToolbar from "./AppToolbar";
-import LayerEditor from "./LayerEditor";
-import LayerList from "./LayerList";
-import MapMaplibreGl from "./MapMaplibreGl";
-import MapOpenLayers from "./MapOpenLayers";
+import MapMaplibreGl from './MapMaplibreGl'
+import MapOpenLayers from './MapOpenLayers'
+import LayerList from './LayerList'
+import LayerEditor from './LayerEditor'
+import AppToolbar from './AppToolbar'
+import AppLayout from './AppLayout'
+import MessagePanel from './AppMessagePanel'
-import ModalDebug from "./ModalDebug";
-import ModalExport from "./ModalExport";
-import ModalOpen from "./ModalOpen";
-import ModalSettings from "./ModalSettings";
-import ModalShortcuts from "./ModalShortcuts";
-import ModalSources from "./ModalSources";
-import ModalSurvey from "./ModalSurvey";
+import ModalSettings from './ModalSettings'
+import ModalExport from './ModalExport'
+import ModalSources from './ModalSources'
+import ModalOpen from './ModalOpen'
+import ModalShortcuts from './ModalShortcuts'
+import ModalSurvey from './ModalSurvey'
+import ModalDebug from './ModalDebug'
-import { latest, validate } from "@maplibre/maplibre-gl-style-spec";
-import isEqual from "lodash.isequal";
-import tokens from "../config/tokens.json";
-import { ApiStyleStore } from "../libs/apistore";
-import Debug from "../libs/debug";
-import { redoMessages, undoMessages } from "../libs/diffmessage";
-import LayerWatcher from "../libs/layerwatcher";
-import {
- downloadGlyphsMetadata,
- downloadSpriteMetadata,
-} from "../libs/metadata";
-import { RevisionStore } from "../libs/revisions";
-import style from "../libs/style";
-import { StyleStore } from "../libs/stylestore";
-import {
- initialStyleUrl,
- loadStyleUrl,
- removeStyleQuerystring,
-} from "../libs/urlopen";
-import { formatLayerId } from "../util/format";
+import { downloadGlyphsMetadata, downloadSpriteMetadata } from '../libs/metadata'
+import {latest, validate} from '@maplibre/maplibre-gl-style-spec'
+import style from '../libs/style'
+import { initialStyleUrl, loadStyleUrl, removeStyleQuerystring } from '../libs/urlopen'
+import { undoMessages, redoMessages } from '../libs/diffmessage'
+import { StyleStore } from '../libs/stylestore'
+import { ApiStyleStore } from '../libs/apistore'
+import { RevisionStore } from '../libs/revisions'
+import LayerWatcher from '../libs/layerwatcher'
+import tokens from '../config/tokens.json'
+import isEqual from 'lodash.isequal'
+import Debug from '../libs/debug'
+import {formatLayerId} from '../util/format';
// Buffer must be defined globally for @maplibre/maplibre-gl-style-spec validate() function to succeed.
window.Buffer = buffer.Buffer;
@@ -54,20 +47,18 @@ function setFetchAccessToken(url, mapStyle) {
const matchesMaptiler = url.match(/\.maptiler\.com/);
const matchesThunderforest = url.match(/\.thunderforest\.com/);
if (matchesTilehosting || matchesMaptiler) {
- const accessToken = style.getAccessToken("openmaptiles", mapStyle, {
- allowFallback: true,
- });
+ const accessToken = style.getAccessToken("openmaptiles", mapStyle, {allowFallback: true})
if (accessToken) {
- return url.replace("{key}", accessToken);
+ return url.replace('{key}', accessToken)
}
- } else if (matchesThunderforest) {
- const accessToken = style.getAccessToken("thunderforest", mapStyle, {
- allowFallback: true,
- });
+ }
+ else if (matchesThunderforest) {
+ const accessToken = style.getAccessToken("thunderforest", mapStyle, {allowFallback: true})
if (accessToken) {
- return url.replace("{key}", accessToken);
+ return url.replace('{key}', accessToken)
}
- } else {
+ }
+ else {
return url;
}
}
@@ -79,135 +70,124 @@ function updateRootSpec(spec, fieldName, newValues) {
...spec.$root,
[fieldName]: {
...spec.$root[fieldName],
- values: newValues,
- },
- },
- };
+ values: newValues
+ }
+ }
+ }
}
export default class App extends React.Component {
constructor(props) {
- super(props);
+ super(props)
autoBind(this);
- this.revisionStore = new RevisionStore();
- const params = new URLSearchParams(window.location.search.substring(1));
- let port = params.get("localport");
- if (
- port == null &&
- window.location.port != 80 &&
- window.location.port != 443
- ) {
- port = window.location.port;
+ this.revisionStore = new RevisionStore()
+ const params = new URLSearchParams(window.location.search.substring(1))
+ let port = params.get("localport")
+ if (port == null && (window.location.port != 80 && window.location.port != 443)) {
+ port = window.location.port
}
this.styleStore = new ApiStyleStore({
- onLocalStyleChange: (mapStyle) =>
- this.onStyleChanged(mapStyle, { save: false }),
+ onLocalStyleChange: mapStyle => this.onStyleChanged(mapStyle, {save: false}),
port: port,
- host: params.get("localhost"),
- });
+ host: params.get("localhost")
+ })
+
const shortcuts = [
{
key: "?",
handler: () => {
this.toggleModal("shortcuts");
- },
+ }
},
{
key: "o",
handler: () => {
this.toggleModal("open");
- },
+ }
},
{
key: "e",
handler: () => {
this.toggleModal("export");
- },
+ }
},
{
key: "d",
handler: () => {
this.toggleModal("sources");
- },
+ }
},
{
key: "s",
handler: () => {
this.toggleModal("settings");
- },
+ }
},
{
key: "i",
handler: () => {
- this.setMapState(this.state.mapState === "map" ? "inspect" : "map");
- },
+ this.setMapState(
+ this.state.mapState === "map" ? "inspect" : "map"
+ );
+ }
},
{
key: "m",
handler: () => {
document.querySelector(".maplibregl-canvas").focus();
- },
+ }
},
{
key: "!",
handler: () => {
this.toggleModal("debug");
- },
+ }
},
- ];
+ ]
document.body.addEventListener("keyup", (e) => {
- if (e.key === "Escape") {
+ if(e.key === "Escape") {
e.target.blur();
document.body.focus();
- } else if (
- this.state.isOpen.shortcuts ||
- document.activeElement === document.body
- ) {
+ }
+ else if(this.state.isOpen.shortcuts || document.activeElement === document.body) {
const shortcut = shortcuts.find((shortcut) => {
- return shortcut.key === e.key;
- });
+ return (shortcut.key === e.key)
+ })
- if (shortcut) {
+ if(shortcut) {
this.setModal("shortcuts", false);
shortcut.handler(e);
}
}
- });
+ })
- const styleUrl = initialStyleUrl();
- if (
- styleUrl &&
- window.confirm(
- "Load style from URL: " + styleUrl + " and discard current changes?"
- )
- ) {
- this.styleStore = new StyleStore();
- loadStyleUrl(styleUrl, (mapStyle) => this.onStyleChanged(mapStyle));
- removeStyleQuerystring();
+ const styleUrl = initialStyleUrl()
+ if(styleUrl && window.confirm("Load style from URL: " + styleUrl + " and discard current changes?")) {
+ this.styleStore = new StyleStore()
+ loadStyleUrl(styleUrl, mapStyle => this.onStyleChanged(mapStyle))
+ removeStyleQuerystring()
} else {
- if (styleUrl) {
- removeStyleQuerystring();
+ if(styleUrl) {
+ removeStyleQuerystring()
}
- this.styleStore.init((err) => {
- if (err) {
- console.log("Falling back to local storage for storing styles");
- this.styleStore = new StyleStore();
+ this.styleStore.init(err => {
+ if(err) {
+ console.log('Falling back to local storage for storing styles')
+ this.styleStore = new StyleStore()
}
- this.styleStore.latestStyle((mapStyle) =>
- this.onStyleChanged(mapStyle, { initialLoad: true })
- );
+ this.styleStore.latestStyle(mapStyle => this.onStyleChanged(mapStyle, {initialLoad: true}))
- if (Debug.enabled()) {
+ if(Debug.enabled()) {
Debug.set("maputnik", "styleStore", this.styleStore);
Debug.set("maputnik", "revisionStore", this.revisionStore);
}
- });
+ })
}
- if (Debug.enabled()) {
+ if(Debug.enabled()) {
Debug.set("maputnik", "revisionStore", this.revisionStore);
Debug.set("maputnik", "styleStore", this.styleStore);
}
@@ -248,32 +228,35 @@ export default class App extends React.Component {
openlayersDebugOptions: {
debugToolbox: false,
},
- };
+ }
this.layerWatcher = new LayerWatcher({
- onVectorLayersChange: (v) => this.setState({ vectorLayers: v }),
- });
+ onVectorLayersChange: v => this.setState({ vectorLayers: v })
+ })
}
handleKeyPress = (e) => {
- if (navigator.platform.toUpperCase().indexOf("MAC") >= 0) {
- if (e.metaKey && e.shiftKey && e.keyCode === 90) {
+ if(navigator.platform.toUpperCase().indexOf('MAC') >= 0) {
+ if(e.metaKey && e.shiftKey && e.keyCode === 90) {
e.preventDefault();
this.onRedo(e);
- } else if (e.metaKey && e.keyCode === 90) {
+ }
+ else if(e.metaKey && e.keyCode === 90) {
e.preventDefault();
this.onUndo(e);
}
- } else {
- if (e.ctrlKey && e.keyCode === 90) {
+ }
+ else {
+ if(e.ctrlKey && e.keyCode === 90) {
e.preventDefault();
this.onUndo(e);
- } else if (e.ctrlKey && e.keyCode === 89) {
+ }
+ else if(e.ctrlKey && e.keyCode === 89) {
e.preventDefault();
this.onRedo(e);
}
}
- };
+ }
componentDidMount() {
window.addEventListener("keydown", this.handleKeyPress);
@@ -284,38 +267,33 @@ export default class App extends React.Component {
}
saveStyle(snapshotStyle) {
- this.styleStore.save(snapshotStyle);
+ this.styleStore.save(snapshotStyle)
}
updateFonts(urlTemplate) {
- const metadata = this.state.mapStyle.metadata || {};
- const accessToken =
- metadata["maputnik:openmaptiles_access_token"] || tokens.openmaptiles;
+ const metadata = this.state.mapStyle.metadata || {}
+ const accessToken = metadata['maputnik:openmaptiles_access_token'] || tokens.openmaptiles
- let glyphUrl =
- typeof urlTemplate === "string"
- ? urlTemplate.replace("{key}", accessToken)
- : urlTemplate;
- downloadGlyphsMetadata(glyphUrl, (fonts) => {
- this.setState({ spec: updateRootSpec(this.state.spec, "glyphs", fonts) });
- });
+ let glyphUrl = (typeof urlTemplate === 'string')? urlTemplate.replace('{key}', accessToken): urlTemplate;
+ downloadGlyphsMetadata(glyphUrl, fonts => {
+ this.setState({ spec: updateRootSpec(this.state.spec, 'glyphs', fonts)})
+ })
}
updateIcons(baseUrl) {
- downloadSpriteMetadata(baseUrl, (icons) => {
- this.setState({ spec: updateRootSpec(this.state.spec, "sprite", icons) });
- });
+ downloadSpriteMetadata(baseUrl, icons => {
+ this.setState({ spec: updateRootSpec(this.state.spec, 'sprite', icons)})
+ })
}
onChangeMetadataProperty = (property, value) => {
// If we're changing renderer reset the map state.
if (
- property === "maputnik:renderer" &&
- value !==
- get(this.state.mapStyle, ["metadata", "maputnik:renderer"], "mlgljs")
+ property === 'maputnik:renderer' &&
+ value !== get(this.state.mapStyle, ['metadata', 'maputnik:renderer'], 'mlgljs')
) {
this.setState({
- mapState: "map",
+ mapState: 'map'
});
}
@@ -323,13 +301,13 @@ export default class App extends React.Component {
...this.state.mapStyle,
metadata: {
...this.state.mapStyle.metadata,
- [property]: value,
- },
- };
- this.onStyleChanged(changedStyle);
- };
+ [property]: value
+ }
+ }
+ this.onStyleChanged(changedStyle)
+ }
- onStyleChanged = (newStyle, opts = {}) => {
+ onStyleChanged = (newStyle, opts={}) => {
opts = {
save: true,
addRevision: true,
@@ -360,11 +338,9 @@ export default class App extends React.Component {
});
}
- const mappedErrors = layerErrors.concat(errors).map((error) => {
+ const mappedErrors = layerErrors.concat(errors).map(error => {
// Special case: Duplicate layer id
- const dupMatch = error.message.match(
- /layers\[(\d+)\]: (duplicate layer id "?(.*)"?, previously used)/
- );
+ const dupMatch = error.message.match(/layers\[(\d+)\]: (duplicate layer id "?(.*)"?, previously used)/);
if (dupMatch) {
const [matchStr, index, message] = dupMatch;
return {
@@ -375,15 +351,13 @@ export default class App extends React.Component {
index: parseInt(index, 10),
key: "id",
message,
- },
- },
- };
+ }
+ }
+ }
}
// Special case: Invalid source
- const invalidSourceMatch = error.message.match(
- /layers\[(\d+)\]: (source "(?:.*)" not found)/
- );
+ const invalidSourceMatch = error.message.match(/layers\[(\d+)\]: (source "(?:.*)" not found)/);
if (invalidSourceMatch) {
const [matchStr, index, message] = invalidSourceMatch;
return {
@@ -394,17 +368,15 @@ export default class App extends React.Component {
index: parseInt(index, 10),
key: "source",
message,
- },
- },
- };
+ }
+ }
+ }
}
- const layerMatch = error.message.match(
- /layers\[(\d+)\]\.(?:(\S+)\.)?(\S+): (.*)/
- );
+ const layerMatch = error.message.match(/layers\[(\d+)\]\.(?:(\S+)\.)?(\S+): (.*)/);
if (layerMatch) {
const [matchStr, index, group, property, message] = layerMatch;
- const key = group && property ? [group, property].join(".") : property;
+ const key = (group && property) ? [group, property].join(".") : property;
return {
message: error.message,
parsed: {
@@ -412,11 +384,12 @@ export default class App extends React.Component {
data: {
index: parseInt(index, 10),
key,
- message,
- },
- },
- };
- } else {
+ message
+ }
+ }
+ }
+ }
+ else {
return {
message: error.message,
};
@@ -427,26 +400,27 @@ export default class App extends React.Component {
if (errors.length > 0) {
dirtyMapStyle = cloneDeep(newStyle);
- errors.forEach((error) => {
- const { message } = error;
+ errors.forEach(error => {
+ const {message} = error;
if (message) {
try {
const objPath = message.split(":")[0];
// Errors can be deply nested for example 'layers[0].filter[1][1][0]' we only care upto the property 'layers[0].filter'
const unsetPath = objPath.match(/^\S+?\[\d+\]\.[^\[]+/)[0];
unset(dirtyMapStyle, unsetPath);
- } catch (err) {
+ }
+ catch (err) {
console.warn(err);
}
}
});
}
- if (newStyle.glyphs !== this.state.mapStyle.glyphs) {
- this.updateFonts(newStyle.glyphs);
+ if(newStyle.glyphs !== this.state.mapStyle.glyphs) {
+ this.updateFonts(newStyle.glyphs)
}
- if (newStyle.sprite !== this.state.mapStyle.sprite) {
- this.updateIcons(newStyle.sprite);
+ if(newStyle.sprite !== this.state.mapStyle.sprite) {
+ this.updateIcons(newStyle.sprite)
}
if (opts.addRevision) {
@@ -456,266 +430,251 @@ export default class App extends React.Component {
this.saveStyle(newStyle);
}
- this.setState(
- {
- mapStyle: newStyle,
- dirtyMapStyle: dirtyMapStyle,
- errors: mappedErrors,
- },
- () => {
- this.fetchSources();
- this.setStateInUrl();
- }
- );
- };
+ this.setState({
+ mapStyle: newStyle,
+ dirtyMapStyle: dirtyMapStyle,
+ errors: mappedErrors,
+ }, () => {
+ this.fetchSources();
+ this.setStateInUrl();
+ })
+
+ }
onUndo = () => {
- const activeStyle = this.revisionStore.undo();
+ const activeStyle = this.revisionStore.undo()
- const messages = undoMessages(this.state.mapStyle, activeStyle);
- this.onStyleChanged(activeStyle, { addRevision: false });
+ const messages = undoMessages(this.state.mapStyle, activeStyle)
+ this.onStyleChanged(activeStyle, {addRevision: false});
this.setState({
infos: messages,
- });
- };
+ })
+ }
onRedo = () => {
- const activeStyle = this.revisionStore.redo();
- const messages = redoMessages(this.state.mapStyle, activeStyle);
- this.onStyleChanged(activeStyle, { addRevision: false });
+ const activeStyle = this.revisionStore.redo()
+ const messages = redoMessages(this.state.mapStyle, activeStyle)
+ this.onStyleChanged(activeStyle, {addRevision: false});
this.setState({
infos: messages,
- });
- };
+ })
+ }
onMoveLayer = (move) => {
let { oldIndex, newIndex } = move;
let layers = this.state.mapStyle.layers;
- oldIndex = clamp(oldIndex, 0, layers.length - 1);
- newIndex = clamp(newIndex, 0, layers.length - 1);
- if (oldIndex === newIndex) return;
+ oldIndex = clamp(oldIndex, 0, layers.length-1);
+ newIndex = clamp(newIndex, 0, layers.length-1);
+ if(oldIndex === newIndex) return;
if (oldIndex === this.state.selectedLayerIndex) {
this.setState({
- selectedLayerIndex: newIndex,
+ selectedLayerIndex: newIndex
});
}
layers = layers.slice(0);
arrayMoveMutable(layers, oldIndex, newIndex);
this.onLayersChange(layers);
- };
+ }
onLayersChange = (changedLayers) => {
const changedStyle = {
...this.state.mapStyle,
- layers: changedLayers,
- };
- this.onStyleChanged(changedStyle);
- };
+ layers: changedLayers
+ }
+ this.onStyleChanged(changedStyle)
+ }
onLayerDestroy = (index) => {
let layers = this.state.mapStyle.layers;
const remainingLayers = layers.slice(0);
remainingLayers.splice(index, 1);
this.onLayersChange(remainingLayers);
- };
+ }
onLayerCopy = (index) => {
let layers = this.state.mapStyle.layers;
- const changedLayers = layers.slice(0);
+ const changedLayers = layers.slice(0)
- const clonedLayer = cloneDeep(changedLayers[index]);
- clonedLayer.id = clonedLayer.id + "-copy";
- changedLayers.splice(index, 0, clonedLayer);
- this.onLayersChange(changedLayers);
- };
+ const clonedLayer = cloneDeep(changedLayers[index])
+ clonedLayer.id = clonedLayer.id + "-copy"
+ changedLayers.splice(index, 0, clonedLayer)
+ this.onLayersChange(changedLayers)
+ }
onLayerVisibilityToggle = (index) => {
let layers = this.state.mapStyle.layers;
- const changedLayers = layers.slice(0);
+ const changedLayers = layers.slice(0)
- const layer = { ...changedLayers[index] };
- const changedLayout = "layout" in layer ? { ...layer.layout } : {};
- changedLayout.visibility =
- changedLayout.visibility === "none" ? "visible" : "none";
+ const layer = { ...changedLayers[index] }
+ const changedLayout = 'layout' in layer ? {...layer.layout} : {}
+ changedLayout.visibility = changedLayout.visibility === 'none' ? 'visible' : 'none'
+
+ layer.layout = changedLayout
+ changedLayers[index] = layer
+ this.onLayersChange(changedLayers)
+ }
- layer.layout = changedLayout;
- changedLayers[index] = layer;
- this.onLayersChange(changedLayers);
- };
onLayerIdChange = (index, oldId, newId) => {
- const changedLayers = this.state.mapStyle.layers.slice(0);
+ const changedLayers = this.state.mapStyle.layers.slice(0)
changedLayers[index] = {
...changedLayers[index],
- id: newId,
- };
+ id: newId
+ }
- this.onLayersChange(changedLayers);
- };
+ this.onLayersChange(changedLayers)
+ }
onLayerChanged = (index, layer) => {
- const changedLayers = this.state.mapStyle.layers.slice(0);
- changedLayers[index] = layer;
+ const changedLayers = this.state.mapStyle.layers.slice(0)
+ changedLayers[index] = layer
- this.onLayersChange(changedLayers);
- };
+ this.onLayersChange(changedLayers)
+ }
setMapState = (newState) => {
- this.setState(
- {
- mapState: newState,
- },
- this.setStateInUrl
- );
- };
+ this.setState({
+ mapState: newState
+ }, this.setStateInUrl);
+ }
setDefaultValues = (styleObj) => {
- const metadata = styleObj.metadata || {};
- if (metadata["maputnik:renderer"] === undefined) {
+ const metadata = styleObj.metadata || {}
+ if(metadata['maputnik:renderer'] === undefined) {
const changedStyle = {
...styleObj,
metadata: {
...styleObj.metadata,
- "maputnik:renderer": "mlgljs",
- },
- };
- return changedStyle;
+ 'maputnik:renderer': 'mlgljs'
+ }
+ }
+ return changedStyle
} else {
- return styleObj;
+ return styleObj
}
- };
+ }
openStyle = (styleObj) => {
- styleObj = this.setDefaultValues(styleObj);
- this.onStyleChanged(styleObj);
- };
+ styleObj = this.setDefaultValues(styleObj)
+ this.onStyleChanged(styleObj)
+ }
fetchSources() {
const sourceList = {};
- for (let [key, val] of Object.entries(this.state.mapStyle.sources)) {
- if (
+ for(let [key, val] of Object.entries(this.state.mapStyle.sources)) {
+ if(
!this.state.sources.hasOwnProperty(key) &&
val.type === "vector" &&
val.hasOwnProperty("url")
) {
sourceList[key] = {
type: val.type,
- layers: [],
+ layers: []
};
let url = val.url;
try {
- url = setFetchAccessToken(url, this.state.mapStyle);
- } catch (err) {
+ url = setFetchAccessToken(url, this.state.mapStyle)
+ } catch(err) {
console.warn("Failed to setFetchAccessToken: ", err);
}
fetch(url, {
- mode: "cors",
+ mode: 'cors',
})
- .then((response) => response.json())
- .then((json) => {
- if (!json.hasOwnProperty("vector_layers")) {
- return;
- }
+ .then(response => response.json())
+ .then(json => {
- // Create new objects before setState
- const sources = Object.assign(
- {},
- {
- [key]: this.state.sources[key],
- }
- );
+ if(!json.hasOwnProperty("vector_layers")) {
+ return;
+ }
- for (let layer of json.vector_layers) {
- sources[key].layers.push(layer.id);
- }
-
- console.debug("Updating source: " + key);
- this.setState({
- sources: sources,
- });
- })
- .catch((err) => {
- console.error("Failed to process sources for '%s'", url, err);
+ // Create new objects before setState
+ const sources = Object.assign({}, {
+ [key]: this.state.sources[key],
});
- } else {
- sourceList[key] =
- this.state.sources[key] || this.state.mapStyle.sources[key];
+
+ for(let layer of json.vector_layers) {
+ sources[key].layers.push(layer.id)
+ }
+
+ console.debug("Updating source: "+key);
+ this.setState({
+ sources: sources
+ });
+ })
+ .catch(err => {
+ console.error("Failed to process sources for '%s'", url, err);
+ });
+ }
+ else {
+ sourceList[key] = this.state.sources[key] || this.state.mapStyle.sources[key];
}
}
- if (!isEqual(this.state.sources, sourceList)) {
+ if(!isEqual(this.state.sources, sourceList)) {
console.debug("Setting sources");
this.setState({
- sources: sourceList,
- });
+ sources: sourceList
+ })
}
}
- _getRenderer() {
+ _getRenderer () {
const metadata = this.state.mapStyle.metadata || {};
- return metadata["maputnik:renderer"] || "mlgljs";
+ return metadata['maputnik:renderer'] || 'mlgljs';
}
onMapChange = (mapView) => {
this.setState({
mapView,
});
- };
+ }
mapRenderer() {
- const { mapStyle, dirtyMapStyle } = this.state;
+ const {mapStyle, dirtyMapStyle} = this.state;
const metadata = this.state.mapStyle.metadata || {};
const mapProps = {
- mapStyle: dirtyMapStyle || mapStyle,
+ mapStyle: (dirtyMapStyle || mapStyle),
replaceAccessTokens: (mapStyle) => {
return style.replaceAccessTokens(mapStyle, {
- allowFallback: true,
+ allowFallback: true
});
},
onDataChange: (e) => {
- this.layerWatcher.analyzeMap(e.map);
+ this.layerWatcher.analyzeMap(e.map)
this.fetchSources();
},
- };
+ }
const renderer = this._getRenderer();
let mapElement;
// Check if OL code has been loaded?
- if (renderer === "ol") {
- mapElement = (
-