-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: feature draggable directive + example
- Loading branch information
Wykks
committed
Oct 29, 2017
1 parent
40cdb28
commit 1166346
Showing
15 changed files
with
210 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
template: ` | ||
<mgl-map | ||
[style]="'mapbox://styles/mapbox/streets-v9'" | ||
[zoom]="2" | ||
[center]="[0, 0]" | ||
> | ||
<mgl-geojson-source | ||
id="point" | ||
> | ||
<mgl-feature | ||
[properties]="" | ||
[geometry]="{ | ||
'type': 'Point', | ||
'coordinates': [0, 0] | ||
}" | ||
[mglDraggable]="targetLayer" | ||
></mgl-feature> | ||
</mgl-geojson-source> | ||
<mgl-layer | ||
#targetLayer | ||
id="point" | ||
type="circle" | ||
source="point" | ||
[paint]="layerPaint" | ||
(mouseEnter)="changeColor('#3bb2d0')" | ||
(mouseLeave)="changeColor('#3887be')" | ||
></mgl-layer> | ||
</mgl-map> | ||
`, | ||
styleUrls: ['./examples.css'] | ||
}) | ||
export class NgxDragAPointComponent { | ||
layerPaint = { | ||
'circle-radius': 10, | ||
'circle-color': '#3887be' | ||
}; | ||
|
||
changeColor(color: string) { | ||
this.layerPaint = { ...this.layerPaint, 'circle-color': color }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { FeatureComponent } from './feature.component'; | ||
import { Directive, Host, Input, OnDestroy, OnInit } from '@angular/core'; | ||
import 'rxjs/add/operator/takeUntil'; | ||
import { merge } from 'rxjs/observable/merge'; | ||
import { ReplaySubject } from 'rxjs/ReplaySubject'; | ||
import { LayerComponent } from '../../layer/layer.component'; | ||
import { MapService } from '../../map/map.service'; | ||
|
||
@Directive({ | ||
selector: '[mglDraggable]' | ||
}) | ||
export class DraggableDirective implements OnInit, OnDestroy { | ||
// tslint:disable-next-line:no-input-rename | ||
@Input('mglDraggable') source: LayerComponent; | ||
|
||
private destroyed$: ReplaySubject<void> = new ReplaySubject(1); | ||
|
||
constructor( | ||
private MapService: MapService, | ||
@Host() private FeatureComponent: FeatureComponent | ||
) { } | ||
|
||
ngOnInit() { | ||
if (this.FeatureComponent.geometry.type !== 'Point') { | ||
throw new Error('mglDraggable only support point feature'); | ||
} | ||
this.MapService.mapCreated$.subscribe(() => { | ||
this.source.mouseEnter | ||
.takeUntil(this.destroyed$) | ||
.subscribe((evt) => { | ||
const feature: GeoJSON.Feature<any> = this.MapService.queryRenderedFeatures( | ||
evt.point, | ||
{ | ||
layers: [this.source.id], | ||
filter: [ | ||
'all', | ||
['==', '$type', 'Point'], | ||
['==', '$id', this.FeatureComponent.id] | ||
] | ||
} | ||
)[0]; | ||
if (!feature) { | ||
return; | ||
} | ||
this.MapService.changeCanvasCursor('move'); | ||
this.MapService.updateDragPan(false); | ||
this.MapService.mapEvents.mouseDown | ||
.takeUntil(merge(this.destroyed$, this.source.mouseLeave)) | ||
.subscribe(() => { | ||
this.MapService.mapEvents.mouseMove | ||
.takeUntil(merge(this.destroyed$, this.MapService.mapEvents.mouseUp)) | ||
.subscribe((evt) => { | ||
this.FeatureComponent.updateCoordinates([evt.lngLat.lng, evt.lngLat.lat]); | ||
}); | ||
}); | ||
}); | ||
this.source.mouseLeave | ||
.takeUntil(this.destroyed$) | ||
.subscribe(() => { | ||
this.MapService.changeCanvasCursor(''); | ||
this.MapService.updateDragPan(true); | ||
}); | ||
}); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.destroyed$.next(undefined); | ||
this.destroyed$.complete(); | ||
} | ||
} |
Oops, something went wrong.