Skip to content

Code cleanup #212

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# @geoblocks/edittrack changes

## v1.3.2

- Move parseFeatures function out side of the TrackData class.

## v1.3.1

- sort POIs when updating index
- Do not clone in getPOIS, getControlPoints(), getSegments()

## v1.3.0

- publish a transpiled ES6 library + types
- add inline source maps
- finish migration to typescript
Expand Down
54 changes: 28 additions & 26 deletions src/interaction/TrackData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,6 @@ export default class TrackData {
private controlPoints: Feature<Point>[] = [];
private pois: Feature<Point>[] = [];

parseFeatures(features: Feature<Point|LineString>[]): ParsedFeatures {
const parsed: ParsedFeatures = {
segments: [],
pois: [],
controlPoints: [],
};
const {segments, pois, controlPoints} = parsed;

for (const feature of features) {
const type = feature.get('type') as FeatureType;
if (type === 'segment') {
console.assert(feature.getGeometry().getType() === 'LineString');
segments.push(feature as Feature<LineString>);
} else if (type === 'controlPoint') {
console.assert(feature.getGeometry().getType() === 'Point');
controlPoints.push(feature as Feature<Point>);
} else if (type === 'POI') {
console.assert(feature.getGeometry().getType() === 'Point');
pois.push(feature as Feature<Point>);
}
}

return parsed;
}

restoreParsedFeatures(parsedFeatures: ParsedFeatures) {
const {segments, pois, controlPoints} = parsedFeatures;
console.assert((!controlPoints.length && !segments.length) || (controlPoints.length === segments.length + 1));
Expand All @@ -75,7 +50,7 @@ export default class TrackData {
let before = undefined;
let after = undefined;
const index = this.controlPoints.indexOf(controlPoint);

console.assert(index >= 0, 'control point not found');
if (index >= 1) {
before = this.segments[index - 1];
}
Expand All @@ -88,6 +63,7 @@ export default class TrackData {

getControlPointBefore(controlPoint: Feature<Point>): Feature<Point> | null {
const index = this.controlPoints.indexOf(controlPoint);
console.assert(index >= 0, 'control point not found');
if (index > 0) {
return this.controlPoints[index - 1];
}
Expand All @@ -96,6 +72,7 @@ export default class TrackData {

getControlPointAfter(controlPoint: Feature<Point>): Feature<Point> | null {
const index = this.controlPoints.indexOf(controlPoint);
console.assert(index >= 0, 'control point not found');
if (index >= 0 && index < this.controlPoints.length - 1) {
return this.controlPoints[index + 1];
}
Expand Down Expand Up @@ -368,3 +345,28 @@ function createStraightSegment(featureFrom: Feature<Point>, featureTo: Feature<P

return segment;
}

export function parseFeatures(features: Feature<Point|LineString>[]): ParsedFeatures {
const parsed: ParsedFeatures = {
segments: [],
pois: [],
controlPoints: [],
};
const {segments, pois, controlPoints} = parsed;

for (const feature of features) {
const type = feature.get('type') as FeatureType;
if (type === 'segment') {
console.assert(feature.getGeometry().getType() === 'LineString');
segments.push(feature as Feature<LineString>);
} else if (type === 'controlPoint') {
console.assert(feature.getGeometry().getType() === 'Point');
controlPoints.push(feature as Feature<Point>);
} else if (type === 'POI') {
console.assert(feature.getGeometry().getType() === 'Point');
pois.push(feature as Feature<Point>);
}
}

return parsed;
}
3 changes: 0 additions & 3 deletions src/interaction/TrackInteractionModify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ export default class Modify extends PointerInteraction {
private overlay_: VectorLayer<VectorSource<Feature>>;
private lastPixel_ = [0, 0];
private trackData_: Options['trackData'];
/**
* @type {Feature<Point>}
*/
private pointAtCursorFeature_ = new Feature({
geometry: new Point([0, 0]),
type: 'sketch',
Expand Down
36 changes: 21 additions & 15 deletions src/interaction/TrackManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Feature from 'ol/Feature.js';
import Point from 'ol/geom/Point.js';

import TrackData from './TrackData';
import TrackData, {parseFeatures} from './TrackData';
import TrackUpdater from './TrackUpdater';
import TrackInteraction from './TrackInteraction';
import HistoryManager from './HistoryManager';
Expand Down Expand Up @@ -56,14 +56,8 @@ export interface Options {
export default class TrackManager<POIMeta> {

private map_: Map;
get map(): Map {
return this.map;
}
private source_: VectorSource;
private trackLayer_: VectorLayer<VectorSource>;
get trackLayer(): VectorLayer<VectorSource> {
return this.trackLayer_;
}
private shadowTrackLayer_: VectorLayer<VectorSource>;
private hitTolerance_: number;
public snapping = true;
Expand All @@ -75,13 +69,7 @@ export default class TrackManager<POIMeta> {
private trackHoverEventListeners_: Function[] = [];
private trackData_ = new TrackData();
private router_: Router;
get router(): Router {
return this.router_;
}
private profiler_: Profiler;
get profiler(): Profiler {
return this.profiler_;
}
private updater_: TrackUpdater;
private interaction_: TrackInteraction;
private historyManager_ = new HistoryManager<Feature<Point|LineString>[]>();
Expand Down Expand Up @@ -122,7 +110,7 @@ export default class TrackManager<POIMeta> {
// }));



// Add a control point at the end of the track
// @ts-ignore too complicate to declare proper events
this.interaction_.on('drawend',
async (event: DrawEvent) => {
Expand Down Expand Up @@ -168,6 +156,7 @@ export default class TrackManager<POIMeta> {
}
});

// Move an existing poi, control point or segment
this.interaction_.on(
// @ts-ignore too complicate to declare proper events
'modifyend',
Expand Down Expand Up @@ -209,6 +198,7 @@ export default class TrackManager<POIMeta> {
}
});

// Delete a control point or a POI
this.interaction_.on(
// @ts-ignore too complicate to declare proper events
'select',
Expand Down Expand Up @@ -262,6 +252,22 @@ export default class TrackManager<POIMeta> {
});
}

get map(): Map {
return this.map;
}

get trackLayer(): VectorLayer<VectorSource> {
return this.trackLayer_;
}

get router(): Router {
return this.router_;
}

get profiler(): Profiler {
return this.profiler_;
}

private pushNewStateToHistoryManager_() {
const segments = this.getSegments();
const controlPoints = this.getControlPoints();
Expand Down Expand Up @@ -378,7 +384,7 @@ export default class TrackManager<POIMeta> {
*/
private async restoreFeaturesInternal_(features: Feature<Point|LineString>[]): Promise<void> {
// should parse features first, compute profile, and then replace the trackdata and add history
const parsedFeatures = this.trackData_.parseFeatures(features);
const parsedFeatures = parseFeatures(features);
this.source_.addFeatures(features);
const profileRequests = parsedFeatures.segments.map(segment => this.profiler_.computeProfile(segment));
await Promise.all(profileRequests);
Expand Down
3 changes: 3 additions & 0 deletions src/interaction/TrackUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export default class TrackUpdater {

// If needed, equalize the control point, the segment before and after to all share the same coordinate.
equalizeCoordinates(controlPoint: Feature<Point>) {
if (!controlPoint) {
return;
}
Comment on lines +86 to +88
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it OK that the control point may be undefined?

const {before, after} = this.trackData.getAdjacentSegments(controlPoint);
if (before && after) {
const firstCoordinate = before.getGeometry().getLastCoordinate();
Expand Down