Skip to content

Commit

Permalink
fix(customloader): vector source now have a custom loader for non url…
Browse files Browse the repository at this point in the history
… source
  • Loading branch information
pelord committed Sep 14, 2021
1 parent 051b4fd commit 6a54b73
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ import BaseEvent from 'ol/events/Event';
export class WFSDataSource extends DataSource {
public ol: olSourceVector<OlGeometry>;

public vectorLoadingListener: Listener;
public vectorLoadedListener: Listener;
public vectorErrorListener: Listener;

set ogcFilters(value: OgcFiltersOptions) {
(this.options as OgcFilterableDataSourceOptions).ogcFilters = value;
}
Expand Down Expand Up @@ -79,7 +75,6 @@ export class WFSDataSource extends DataSource {
const vectorSource = new olSourceVector({
format: getFormatFromOptions(this.options),
loader: (extent, resolution, proj: olProjection) => {
vectorSource.dispatchEvent({type: 'vectorloading'} as BaseEvent);
const paramsWFS = this.options.paramsWFS;
const wfsProj = paramsWFS.srsName ? new olProjection({ code: paramsWFS.srsName }) : proj;

Expand Down Expand Up @@ -113,9 +108,6 @@ export class WFSDataSource extends DataSource {
},
strategy: OlLoadingStrategy.bbox
});
vectorSource.addEventListener('vectorloading', this.vectorLoadingListener);
vectorSource.addEventListener('vectorloaded', this.vectorLoadedListener);
vectorSource.addEventListener('vectorloaderror', this.vectorErrorListener);
return vectorSource;
}

Expand All @@ -126,7 +118,6 @@ export class WFSDataSource extends DataSource {
this.authInterceptor.interceptXhr(xhr, url);
}
const onError = () => {
vectorSource.dispatchEvent('vectorloaderror');
vectorSource.removeLoadedExtent(extent);
};
xhr.onerror = onError;
Expand All @@ -141,7 +132,6 @@ export class WFSDataSource extends DataSource {
console.log('No more data to download at this resolution');
}*/
vectorSource.addFeatures(features);
vectorSource.dispatchEvent('vectorloaded');
} else {
onError();
}
Expand Down
142 changes: 68 additions & 74 deletions packages/geo/src/lib/layer/shared/layers/vector-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { Layer } from './layer';
import { VectorLayerOptions } from './vector-layer.interface';
import { AuthInterceptor } from '@igo2/auth';
import { MessageService } from '@igo2/core';
import { ListenerFunction } from 'ol/events';
export class VectorLayer extends Layer {
public dataSource:
| FeatureDataSource
Expand All @@ -32,10 +31,6 @@ export class VectorLayer extends Layer {
private watcher: VectorWatcher;
private trackFeatureListenerId;

private loadingListener: ListenerFunction;
private loadedListener: ListenerFunction;
private errorListener: ListenerFunction;

get browsable(): boolean {
return this.options.browsable !== false;
}
Expand Down Expand Up @@ -78,24 +73,23 @@ export class VectorLayer extends Layer {
// : vector.getSource()) as olSourceVector<OlGeometry>;
const vectorSource = vector.getSource() as olSourceVector<OlGeometry>;
const url = vectorSource.getUrl();
vectorSource.addEventListener('vectorloading', this.loadingListener);
vectorSource.addEventListener('vectorloaded', this.loadedListener);
vectorSource.addEventListener('vectorloaderror', this.errorListener);
if (url) {
const loader = (extent, resolution, proj) => {
this.customLoader(
vectorSource,
url,
this.authInterceptor,
extent,
resolution,
proj
);
};
if (loader) {
vectorSource.setLoader(loader);
}

const loader = (extent, resolution, proj, success, failure) => {
this.customLoader(
vectorSource,
url,
this.authInterceptor,
extent,
resolution,
proj,
success,
failure
);
};
if (loader) {
vectorSource.setLoader(loader);
}

return vector;
}

Expand Down Expand Up @@ -242,63 +236,63 @@ export class VectorLayer extends Layer {
interceptor,
extent,
resolution,
projection
projection,
success,
failure
) {
vectorSource.dispatchEvent({ type: 'vectorloading' });
const xhr = new XMLHttpRequest();
xhr.open(
'GET',
typeof url === 'function'
? (url = url(extent, resolution, projection))
: url
);
const format = vectorSource.getFormat();
if (format.getType() === FormatType.ARRAY_BUFFER) {
xhr.responseType = 'arraybuffer';
}
if (interceptor) {
interceptor.interceptXhr(xhr, url);
}
if (url) {
const xhr = new XMLHttpRequest();
xhr.open(
'GET',
typeof url === 'function'
? (url = url(extent, resolution, projection))
: url
);
const format = vectorSource.getFormat();
if (format.getType() === FormatType.ARRAY_BUFFER) {
xhr.responseType = 'arraybuffer';
}
if (interceptor) {
interceptor.interceptXhr(xhr, url);
}

const onError = () => {
vectorSource.dispatchEvent({ type: 'vectorloaderror' });
vectorSource.removeLoadedExtent(extent);
};
xhr.onerror = onError;
xhr.onload = () => {
// status will be 0 for file:// urls
if (!xhr.status || (xhr.status >= 200 && xhr.status < 300)) {
const type = format.getType();
let source;
if (type === FormatType.JSON || type === FormatType.TEXT) {
source = xhr.responseText;
} else if (type === FormatType.XML) {
source = xhr.responseXML;
if (!source) {
source = new DOMParser().parseFromString(
xhr.responseText,
'application/xml'
);
const onError = () => {
vectorSource.removeLoadedExtent(extent);
failure();
};
xhr.onerror = onError;
xhr.onload = () => {
// status will be 0 for file:// urls
if (!xhr.status || (xhr.status >= 200 && xhr.status < 300)) {
const type = format.getType();
let source;
if (type === FormatType.JSON || type === FormatType.TEXT) {
source = xhr.responseText;
} else if (type === FormatType.XML) {
source = xhr.responseXML;
if (!source) {
source = new DOMParser().parseFromString(
xhr.responseText,
'application/xml'
);
}
} else if (type === FormatType.ARRAY_BUFFER) {
source = xhr.response;
}
if (source) {
const features = format.readFeatures(source, { extent, featureProjection: projection })
vectorSource.addFeatures(features, format.readProjection(source));
success(features);
} else {
onError();
}
} else if (type === FormatType.ARRAY_BUFFER) {
source = xhr.response;
}
if (source) {
vectorSource.addFeatures(
format.readFeatures(source, {
extent,
featureProjection: projection
}),
format.readProjection(source)
);
vectorSource.dispatchEvent({ type: 'vectorloaded' });
} else {
onError();
}
} else {
onError();
}
};
xhr.send();
};
xhr.send();
} else {
success(vectorSource.getFeatures());
}
}
}
2 changes: 1 addition & 1 deletion packages/geo/src/lib/layer/utils/vector-watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class VectorWatcher extends Watcher {
this.status = SubjectStatus.Working;
}

private handleLoadEnd(event) {
private handleLoadEnd(event: any) {
this.loaded += 1;

const loading = this.loading;
Expand Down

0 comments on commit 6a54b73

Please sign in to comment.