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
19 changes: 19 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [getWaypoints](#getwaypoints)
- [removeRoutes](#removeroutes)
- [on](#on)
- [off](#off)

## MapboxDirections

Expand Down Expand Up @@ -185,3 +186,21 @@ Subscribe to events that happen within the plugin.
- `fn` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** function that's called when the event is emitted.

Returns **[MapboxDirections](#mapboxdirections)** this;

### off

Unsubscribe to events previously registered.

**Parameters**

- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** name of event. Available events and the data passed into their respective event objects are:
- **clear** `{ type: } Type is one of 'origin' or 'destination'`
- **loading** `{ type: } Type is one of 'origin' or 'destination'`
- **profile** `{ profile } Profile is one of 'driving', 'walking', or 'cycling'`
- **origin** `{ feature } Fired when origin is set`
- **destination** `{ feature } Fired when destination is set`
- **route** `{ route } Fired when a route is updated`
- **error** \`{ error } Error as string
- `fn` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** function used when the event was registered.

Returns **[MapboxDirections](#mapboxdirections)** this;
32 changes: 30 additions & 2 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ function fetchDirections() {
dispatch(destinationPoint(data.waypoints[data.waypoints.length - 1].location));
} else {
dispatch(setDirections([]));
return dispatch(setError(JSON.parse(request.responseText).message));
return dispatch(setError(JSON.parse(request.responseText || '{"message": "Undefined error"}').message));
}
};

request.onerror = () => {
dispatch(setDirections([]));
return dispatch(setError(JSON.parse(request.responseText).message));
return dispatch(setError(JSON.parse(request.responseText || '{"message": "Undefined error"}').message));
};

request.send();
Expand Down Expand Up @@ -321,6 +321,34 @@ export function eventSubscribe(type, fn) {
};
}

export function eventUnsubscribe(type, fn) {
return (dispatch, getState) => {
const { events } = getState();

if (!events[type]) {
return {
type: types.EVENTS,
events
};
}

if (fn) {
// Drop the event if it exists in the events object
const fnToDelete = events[type].indexOf(fn);
if (fnToDelete >= 0) {
events[type].splice(fnToDelete, 1);
}
} else {
delete events[type];
}

return {
type: types.EVENTS,
events: events
};
};
}

export function eventEmit(type, data) {
return (dispatch, getState) => {
const { events } = getState();
Expand Down
9 changes: 6 additions & 3 deletions src/controls/inputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@ export default class Inputs {
}

animateToCoordinates(mode, coords) {
const { origin, destination, routePadding } = this.store.getState();
const { origin, destination, waypoints, routePadding } = this.store.getState();

if (origin.geometry &&
destination.geometry &&
!isEqual(origin.geometry, destination.geometry)) {

const ways = waypoints.filter((waypoint) => (waypoint.geometry))

// Animate map to fit bounds.
const bb = extent({
type: 'FeatureCollection',
features: [origin, destination]
features: [origin, destination, ...ways]
});

this._map.fitBounds([[bb[0], bb[1]], [bb[2], bb[3]]], {padding: routePadding});
Expand Down
3 changes: 2 additions & 1 deletion src/controls/instructions.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default class Instructions {

if (directions.length && shouldRender) {
const direction = this.directions = directions[routeIndex];
const allSteps = direction.legs.reduce((legs, leg) => legs.concat(leg.steps), []);

if (compile) {
direction.legs.forEach(function(leg) {
Expand All @@ -50,7 +51,7 @@ export default class Instructions {
this.container.innerHTML = instructionsTemplate({
routeIndex,
routes: directions.length,
steps: direction.legs[0].steps, // Todo: Respect all legs,
steps: allSteps,
format: utils.format[unit],
duration: utils.format[unit](direction.distance),
distance: utils.format.duration(direction.duration)
Expand Down
12 changes: 11 additions & 1 deletion src/directions.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ export default class MapboxDirections {
}
} else {
this.actions.setDestinationFromCoordinates(coords);
this._map.flyTo({ center: coords });
}
}
}
Expand Down Expand Up @@ -558,4 +557,15 @@ export default class MapboxDirections {
this.actions.eventSubscribe(type, fn);
return this;
}

/**
* Unsubscribe to events
* @param {String} type name of event. Available events are outlined in `on`
* @param {Function} fn optional. The function that's called when the event is emitted.
* @returns {Directions} this;
*/
off(type, fn) {
this.actions.eventUnsubscribe(type, fn);
return this;
}
}