Skip to content

Commit

Permalink
feat(map): add a directive that change url to path depending on the n…
Browse files Browse the repository at this point in the history
…etwork status (#384)

* feat(network): new network service that return the network status

* feat(map):
* new feature that change the url to a path depending on the network status.
* the query can now select features from layers.

* feat(map): add a directive that change url to path depending on the network status

* fix(datasource): remove useless attributes

* Delete mapOffline.directive.spec.ts

* Update datasource.ts

* Update datasource.ts
  • Loading branch information
drekss authored and mbarbeau committed Aug 12, 2019
1 parent 712074f commit ad5b5de
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DataService } from './data.service';
import { generateIdFromSourceOptions } from '../../utils/id-generator';

export abstract class DataSource {

public id: string;
public ol: olSource;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface FeatureDataSourceOptions extends DataSourceOptions {
features?: olFeature[];
format?: olFormatFeature;
url?: string;
pathOffline?: string;

ol?: olSourceVector;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface MVTDataSourceOptions extends DataSourceOptions {
projection?: string;
attributions?: olAttribution;
format?: any;
url?: string;
ol?: olSourceVectorTile;
url?: string;
pathOffline?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { DataSourceOptions } from './datasource.interface';
export interface XYZDataSourceOptions extends DataSourceOptions {
// type?: 'xyz';
projection?: string;
ol?: olSourceXYZ;
url?: string;
urls?: string[];
ol?: olSourceXYZ;
pathOffline?: string;
}
7 changes: 5 additions & 2 deletions packages/geo/src/lib/map/map.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { GeolocateButtonComponent } from './geolocate-button/geolocate-button.co
import { RotationButtonComponent } from './rotation-button/rotation-button.component';
import { BaseLayersSwitcherComponent } from './baselayers-switcher/baselayers-switcher.component';
import { MiniBaseMapComponent } from './baselayers-switcher/mini-basemap.component';
import { MapOfflineDirective } from './shared/mapOffline.directive';

@NgModule({
imports: [
Expand All @@ -32,15 +33,17 @@ import { MiniBaseMapComponent } from './baselayers-switcher/mini-basemap.compone
GeolocateButtonComponent,
RotationButtonComponent,
BaseLayersSwitcherComponent,
MiniBaseMapComponent
MiniBaseMapComponent,
MapOfflineDirective
],
declarations: [
MapBrowserComponent,
ZoomButtonComponent,
GeolocateButtonComponent,
RotationButtonComponent,
BaseLayersSwitcherComponent,
MiniBaseMapComponent
MiniBaseMapComponent,
MapOfflineDirective
]
})
export class IgoMapModule {}
1 change: 1 addition & 0 deletions packages/geo/src/lib/map/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './map.enums';
export * from './map.interface';
export * from './map.service';
export * from './map.utils';
export * from './mapOffline.directive';
export * from './projection.interfaces';
export * from './projection.service';
export * from './controllers';
72 changes: 72 additions & 0 deletions packages/geo/src/lib/map/shared/mapOffline.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Directive, AfterViewInit } from '@angular/core';
import { IgoMap } from './map';
import { MapBrowserComponent } from '../map-browser/map-browser.component';
import { NetworkService, ConnectionState } from '@igo2/core';
import { Subscription } from 'rxjs';
import { Layer } from '../../layer/shared/layers/layer';
import { MVTDataSourceOptions, XYZDataSourceOptions, FeatureDataSourceOptions } from '../../datasource';

@Directive({
selector: '[igoMapOffline]'
})
export class MapOfflineDirective implements AfterViewInit {

private context$$: Subscription;
private state: ConnectionState;
private component: MapBrowserComponent;

get map(): IgoMap {
return this.component.map;
}

constructor(
component: MapBrowserComponent,
private networkService: NetworkService
) {
this.component = component;
}

ngAfterViewInit() {
this.networkService.currentState().subscribe((state: ConnectionState) => {
console.log(state);
this.state = state;
this.changeLayer();
});

this.map.layers$.subscribe((layers: Layer[]) => {
this.changeLayer();
});
}

private changeLayer() {
let sourceOptions;
const layerList = this.map.layers$.value;
layerList.forEach(layer => {
if (layer.options.sourceOptions.type === 'mvt') {
sourceOptions = (layer.options.sourceOptions as MVTDataSourceOptions);
} else if (layer.options.sourceOptions.type === 'xyz') {
sourceOptions = (layer.options.sourceOptions as XYZDataSourceOptions);
} else if (layer.options.sourceOptions.type === 'vector') {
sourceOptions = (layer.options.sourceOptions as FeatureDataSourceOptions);
} else {
return;
}
if (sourceOptions.pathOffline &&
this.state.connection === false) {
if (sourceOptions.excludeAttributeOffline) {
sourceOptions.excludeAttributeBackUp = sourceOptions.excludeAttribute;
sourceOptions.excludeAttribute = sourceOptions.excludeAttributeOffline;
}
layer.ol.getSource().clear();
layer.ol.getSource().setUrl(sourceOptions.pathOffline);
} else if (sourceOptions.pathOffline &&
this.state.connection === true) {
if (sourceOptions.excludeAttributeBackUp) {
sourceOptions.excludeAttribute = sourceOptions.excludeAttributeBackUp;
}
layer.ol.getSource().clear();
layer.ol.getSource().setUrl(sourceOptions.url);
}
});
}
}

0 comments on commit ad5b5de

Please sign in to comment.