basic router

This commit is contained in:
Fedor Katurov 2018-08-16 12:29:58 +07:00
parent 0d8a507620
commit e477f50283
12 changed files with 215 additions and 15 deletions

View file

@ -1,7 +1,8 @@
import { Map } from '$modules/Map';
import { Poly } from "$modules/Poly";
import { Poly } from '$modules/Poly';
import { MODES } from '$constants/modes';
import { Stickers } from '$modules/Stickers';
import { Router } from '$modules/Router';
export class Editor {
constructor({
@ -10,8 +11,12 @@ export class Editor {
setMode
}) {
this.map = new Map({ container });
const { lockMapClicks, map: { map } } = this;
this.poly = new Poly({ map: this.map.map });
this.stickers = new Stickers({ map: this.map.map });
this.stickers = new Stickers({ map, lockMapClicks });
this.router = new Router({ map, lockMapClicks });
this.setMode = setMode;
this.mode = mode;
@ -20,14 +25,20 @@ export class Editor {
[MODES.POLY]: {
start: this.poly.continue,
stop: this.poly.stop,
},
[MODES.ROUTER]: {
start: this.routerSetStart,
}
};
this.clickHandlers = {
[MODES.STICKERS]: this.stickers.createOnClick
[MODES.STICKERS]: this.stickers.createOnClick,
[MODES.ROUTER]: this.router.pushWaypointOnClick,
};
this.map.map.addEventListener('mousedown', this.onClick);
map.addEventListener('mouseup', this.onClick);
map.addEventListener('dragstart', () => lockMapClicks(true));
map.addEventListener('dragstop', () => lockMapClicks(false));
}
changeMode = mode => {
@ -52,6 +63,29 @@ export class Editor {
};
onClick = e => {
if (e.originalEvent.which === 3) return; // skip right click
if (this.clickHandlers[this.mode]) this.clickHandlers[this.mode](e);
};
lockMapClicks = lock => {
if (lock) {
this.map.map.removeEventListener('mouseup', this.onClick);
this.map.map.addEventListener('mouseup', this.unlockMapClicks);
} else {
this.map.map.removeEventListener('mouseup', this.unlockMapClicks);
this.map.map.addEventListener('mouseup', this.onClick);
}
};
unlockMapClicks = () => {
this.lockMapClicks(false);
};
routerSetStart = () => {
const { latlngs } = this.poly;
if (!latlngs || !latlngs.length) return;
this.router.startFrom(latlngs.pop());
};
}