Skip to content

Commit

Permalink
fix(contextmenu): handle ios mobile device (#1119)
Browse files Browse the repository at this point in the history
  • Loading branch information
pelord authored Nov 3, 2022
1 parent 61bf9da commit c26b81e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 31 deletions.
3 changes: 2 additions & 1 deletion demo/src/app/geo/search/search.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</span>
</mat-card-content>

<igo-map-browser #mapBrowser [map]="map" [view]="view" igoOverlay [igoContextMenu]=actionbarMenu
<igo-map-browser #mapBrowser [map]="map" [view]="view" igoOverlay [igoContextMenu]=actionbarMenu igoLongPress

igoSearchPointerSummary
[igoSearchPointerSummaryDelay]="500"
Expand All @@ -25,6 +25,7 @@

<igo-panel title="Search">
<igo-search-bar
[term]="term"
(pointerSummaryStatus)="onPointerSummaryStatusChange($event)"
[searchSettings]="true"
[store]="searchStore"
Expand Down
36 changes: 10 additions & 26 deletions demo/src/app/geo/search/search.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import {
Layer,
ProjectionService,
Research,
SearchResult,
SearchService
SearchResult
} from '@igo2/geo';

import { SearchState } from '@igo2/integration';
Expand Down Expand Up @@ -79,7 +78,6 @@ export class AppSearchComponent implements OnInit, OnDestroy {
private mapService: MapService,
private layerService: LayerService,
private searchState: SearchState,
private searchService: SearchService,
private mediaService: MediaService
) {
this.mapService.setMap(this.map);
Expand Down Expand Up @@ -158,18 +156,17 @@ export class AppSearchComponent implements OnInit, OnDestroy {
{
id: 'coordinates',
title: 'coordinates',
handler: this.onSearchCoordinate.bind(this)
handler: () => this.searchCoordinate(this.lonlat)
},
{
id: 'googleMaps',
title: 'googleMap',
handler: this.onOpenGoogleMaps.bind(this),
args: ['1']
handler: () => this.openGoogleMaps(this.lonlat),
},
{
id: 'googleStreetView',
title: 'googleStreetView',
handler: this.onOpenGoogleStreetView.bind(this)
handler: () => this.openGoogleStreetView(this.lonlat)
}
]);
}
Expand Down Expand Up @@ -206,31 +203,18 @@ export class AppSearchComponent implements OnInit, OnDestroy {
return position;
}

onPointerSearch(event) {
this.lonlat = event;
this.onSearchCoordinate();
}

onSearchCoordinate() {
searchCoordinate(lonlat) {
this.searchStore.clear();
const results = this.searchService.reverseSearch(this.lonlat);

for (const i in results) {
if (results.length > 0) {
results[i].request.subscribe((_results: SearchResult<Feature>[]) => {
this.onSearch({ research: results[i], results: _results });
});
}
}
this.term = lonlat.map((c) => c.toFixed(6)).join(', ');
}

onOpenGoogleMaps() {
window.open(GoogleLinks.getGoogleMapsCoordLink(this.lonlat[0], this.lonlat[1]));
openGoogleMaps(lonlat) {
window.open(GoogleLinks.getGoogleMapsCoordLink(lonlat[0], lonlat[1]));
}

onOpenGoogleStreetView() {
openGoogleStreetView(lonlat) {
window.open(
GoogleLinks.getGoogleStreetViewLink(this.lonlat[0], this.lonlat[1])
GoogleLinks.getGoogleStreetViewLink(lonlat[0], lonlat[1])
);
}
}
17 changes: 15 additions & 2 deletions packages/common/src/lib/context-menu/context-menu.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,22 @@ export class ContextMenuDirective {
private elementRef: ElementRef
) {}

@HostListener('longpress', ['$event'])
@HostListener('contextmenu', ['$event'])
public onContextMenu(e: MouseEvent): void {
const {x, y} = e;
public onContextMenu(e: MouseEvent | TouchEvent): void {
let x;
let y;
if (e instanceof TouchEvent) {
x = e.touches[0].pageX;
y = e.touches[0].pageY;
} else if (e instanceof MouseEvent) {
x = e.x;
y = e.y;
}
if (!x || !y) {
return;
}

this.close();
e.preventDefault();
this.menuPosition.emit({ x, y });
Expand Down
5 changes: 3 additions & 2 deletions packages/common/src/lib/context-menu/context-menu.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { NgModule, ModuleWithProviders } from '@angular/core';
import { ContextMenuDirective } from './context-menu.directive';
import { LongPressDirective } from './long-press.directive';

@NgModule({
imports: [],
declarations: [ContextMenuDirective],
exports: [ContextMenuDirective]
declarations: [ContextMenuDirective, LongPressDirective],
exports: [ContextMenuDirective, LongPressDirective]
})
export class IgoContextMenuModule {
static forRoot(): ModuleWithProviders<IgoContextMenuModule> {
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/lib/context-menu/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './context-menu.directive';
export * from './long-press.directive';
30 changes: 30 additions & 0 deletions packages/common/src/lib/context-menu/long-press.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Directive, HostListener, Output, EventEmitter } from '@angular/core';

@Directive({
selector: '[igoLongPress]'
})
export class LongPressDirective {
private touchTimeout: any;
@Output() longpress = new EventEmitter();

constructor() { }


@HostListener('touchstart', ['$event'])
public touchstart(e: TouchEvent) {
this.touchTimeout = setTimeout(() => {
this.longpress.emit(e);
}, 400);
}
@HostListener('touchmove')
@HostListener('touchcancel')
@HostListener('touchend')
public touchend() {
this.touchEnd();
}


private touchEnd() {
clearTimeout(this.touchTimeout);
}
}

0 comments on commit c26b81e

Please sign in to comment.