Skip to content

Commit

Permalink
Fix event listener types for Annotation and Overlay (#2)
Browse files Browse the repository at this point in the history
* fix addEventListener types of mapkit.Annotation

* simplify AnnotationEvent type

* fix listner types for mapkit.Overlay

* update mapkit.Map select/deselect event

* rename type for clarity
  • Loading branch information
wsmd authored Feb 22, 2019
1 parent 8ea6c25 commit a3d0c50
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 21 deletions.
25 changes: 12 additions & 13 deletions mapkit/mapkit.Annotation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ declare namespace mapkit {
* @param thisObject An object to be set as the this keyword on the
* listener function.
*/
addEventListener<K extends keyof AnnotationEvents<this>>(
type: K,
listener: () => void,
addEventListener(
type: AnnotationEventType,
listener: (event: EventBase<Map>) => void,
thisObject?: object,
): void;
/**
Expand All @@ -44,9 +44,9 @@ declare namespace mapkit {
* @param thisObject An object to be set as the this keyword on the listener
* function.
*/
removeEventListener<K extends keyof AnnotationEvents<this>>(
type: K,
listener: () => void,
removeEventListener(
type: AnnotationEventType,
listener: (event: EventBase<Map>) => void,
thisObject?: object,
): void;
/**
Expand Down Expand Up @@ -381,11 +381,10 @@ declare namespace mapkit {
}

// prettier-ignore
interface AnnotationEvents<T> {
'select': EventBase<T> & { annotation: Annotation; overlay: Overlay };
'deselect': EventBase<T> & { annotation: Annotation; overlay: Overlay };
'drag-start': EventBase<T> & { annotation: Annotation };
'dragging': EventBase<T> & { annotation: Annotation; coordinate: Coordinate };
'drag-end': EventBase<T> & { annotation: Annotation };
}
type AnnotationEventType =
| 'select'
| 'deselect'
| 'drag-start'
| 'dragging'
| 'drag-end';
}
21 changes: 15 additions & 6 deletions mapkit/mapkit.Map.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,19 +462,28 @@ declare namespace mapkit {
target: T;
}

interface UserLocationEvents<T> {
'user-location-change': EventBase<T> & { coordinate: Coordinate; timestamp: Date };
'user-location-error': EventBase<T> & { code: number; message: string };
}

interface MapEvents<T> extends AnnotationEvents<T>, UserLocationEvents<T> {
// prettier-ignore
interface MapEvents<T> {
'region-change-start': EventBase<T>;
'region-change-end': EventBase<T>;
'scroll-start': EventBase<T>;
'scroll-end': EventBase<T>;
'zoom-start': EventBase<T>;
'zoom-end': EventBase<T>;
'map-type-change': EventBase<T>;

// Annotation Events

'select': EventBase<T> & { annotation?: Annotation; overlay?: Overlay };
'deselect': EventBase<T> & { annotation?: Annotation; overlay?: Overlay };
'drag-start': EventBase<T> & { annotation: Annotation };
'dragging': EventBase<T> & { annotation: Annotation; coordinate: Coordinate };
'drag-end': EventBase<T> & { annotation: Annotation };

// User Location Events

'user-location-change': EventBase<T> & { coordinate: Coordinate; timestamp: Date };
'user-location-error': EventBase<T> & { code: number; message: string };
}

/**
Expand Down
14 changes: 12 additions & 2 deletions mapkit/mapkit.Overlay.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ declare namespace mapkit {
/**
* Starts listening for the specified type of event.
*/
addEventListener(type: string, listener: () => {}, thisObject?: object): void;
addEventListener(
type: OverlayEventType,
listener: (event: EventBase<this>) => void,
thisObject?: object,
): void;
/**
* Stops listening for the specified type of event.
*/
addEventListener(type: string, listener: () => {}, thisObject?: object): void;
removeEventListener(
type: OverlayEventType,
listener: (event: EventBase<this>) => void,
thisObject?: object,
): void;
/**
* Custom data to associate with this overlay.
*/
Expand Down Expand Up @@ -129,4 +137,6 @@ declare namespace mapkit {
interface StylesOverlayOptions extends OverlayOptions {
style?: mapkit.Style;
}

type OverlayEventType = 'select' | 'deselect';
}
36 changes: 36 additions & 0 deletions test/mapkit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ mapkit.addEventListener('configuration-change', event => {

const map = new mapkit.Map(document.createElement('div'));

map.addEventListener('select', event => {
const annotations: mapkit.Annotation[] = event.target.annotations;
if (event.annotation) {
const annotation: mapkit.Annotation = event.annotation;
}
if (event.overlay) {
const overlay: mapkit.Overlay = event.overlay;
}
});

mapkit.removeEventListener('configuration-change', () => {});

const annotation = new mapkit.ImageAnnotation(new mapkit.Coordinate(1, 1), {
Expand Down Expand Up @@ -201,6 +211,26 @@ people.forEach(function(person) {
data: { role: person.role, building: person.building },
};
var annotation = new mapkit.Annotation(person.coordinate, factory, options);
annotation.addEventListener('deselect', event => {
let map: mapkit.Map = event.target;
let type: string = event.type;
});
annotation.addEventListener('select', event => {
let map: mapkit.Map = event.target;
let type: string = event.type;
});
annotation.addEventListener('dragging', event => {
let map: mapkit.Map = event.target;
let type: string = event.type;
});
annotation.addEventListener('drag-end', event => {
let map: mapkit.Map = event.target;
let type: string = event.type;
});
annotation.addEventListener('drag-start', event => {
let map: mapkit.Map = event.target;
let type: string = event.type;
});
map.addAnnotation(annotation);
});

Expand Down Expand Up @@ -229,3 +259,9 @@ new mapkit.MarkerAnnotation(new mapkit.Coordinate(40.6892, -74.0445), {
subtitle: 'Subtitle',
callout: calloutDelegate,
});

const overlay = new mapkit.CircleOverlay(coordinate, 1000);

overlay.addEventListener('select', event => {
let radius: number = event.target.radius;
});

0 comments on commit a3d0c50

Please sign in to comment.