From ceafa88dc830202d10fa0034d0bda0fec67be2ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Harrtell?= Date: Mon, 22 Jun 2015 10:11:48 +0200 Subject: [PATCH] Enable use of custom XHR loader for TileVector sources --- externs/olx.js | 17 ++++++++++++++--- src/ol/source/tilevectorsource.js | 29 +++++++++++++++++++++-------- src/ol/tileloadfunction.js | 11 +++++++++++ 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/externs/olx.js b/externs/olx.js index 322808dd3c..11b2e991b9 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -4022,10 +4022,11 @@ olx.source.TileImageOptions.prototype.wrapX; /** * @typedef {{attributions: (Array.|undefined), - * format: ol.format.Feature, + * format: (ol.format.Feature|undefined), * logo: (string|olx.LogoOptions|undefined), * tileGrid: ol.tilegrid.TileGrid, * tileUrlFunction: (ol.TileUrlFunctionType|undefined), + * tileLoadFunction: (ol.TileVectorLoadFunctionType|undefined), * url: (string|undefined), * urls: (Array.|undefined), * wrapX: (boolean|undefined)}} @@ -4043,8 +4044,8 @@ olx.source.TileVectorOptions.prototype.attributions; /** - * Format. - * @type {ol.format.Feature} + * Format. Required unless tileLoadFunction is used. + * @type {ol.format.Feature|undefined} * @api */ olx.source.TileVectorOptions.prototype.format; @@ -4075,6 +4076,16 @@ olx.source.TileVectorOptions.prototype.tileGrid; olx.source.TileVectorOptions.prototype.tileUrlFunction; +/** + * Optional function to override the default loading and format parsing behaviour. + * If this option is used format is ignored and the provided function will be + * responsible for data retrieval and transformation into features. + * @type {ol.TileVectorLoadFunctionType|undefined} + * @api + */ +olx.source.TileVectorOptions.prototype.tileLoadFunction; + + /** * URL template. Must include `{x}`, `{y}` or `{-y}`, and `{z}` placeholders. * @type {string|undefined} diff --git a/src/ol/source/tilevectorsource.js b/src/ol/source/tilevectorsource.js index 5e33b8d8b7..90b3dc74f1 100644 --- a/src/ol/source/tilevectorsource.js +++ b/src/ol/source/tilevectorsource.js @@ -34,12 +34,9 @@ ol.source.TileVector = function(options) { /** * @private - * @type {ol.format.Feature} + * @type {ol.format.Feature|undefined} */ - this.format_ = options.format; - - goog.asserts.assert(goog.isDefAndNotNull(this.format_), - 'ol.source.TileVector requires a format'); + this.format_ = goog.isDef(options.format) ? options.format : null; /** * @private @@ -53,6 +50,17 @@ ol.source.TileVector = function(options) { */ this.tileUrlFunction_ = ol.TileUrlFunction.nullTileUrlFunction; + /** + * @private + * @type {?ol.TileVectorLoadFunctionType} + */ + this.tileLoadFunction_ = goog.isDef(options.tileLoadFunction) ? + options.tileLoadFunction : null; + + goog.asserts.assert(!goog.isNull(this.format_) || + !goog.isNull(this.tileLoadFunction_), + 'Either format or tileLoadFunction are required'); + /** * @private * @type {Object.>} @@ -299,9 +307,14 @@ ol.source.TileVector.prototype.loadFeatures = tileUrlFunction(urlTileCoord, 1, projection); if (goog.isDef(url)) { tiles[tileKey] = []; - var loader = ol.featureloader.loadFeaturesXhr(url, this.format_, - goog.partial(success, tileKey)); - loader.call(this, extent, resolution, projection); + var tileSuccess = goog.partial(success, tileKey); + if (!goog.isNull(this.tileLoadFunction_)) { + this.tileLoadFunction_(url, tileSuccess); + } else { + var loader = ol.featureloader.loadFeaturesXhr(url, + /** @type {ol.format.Feature} */ (this.format_), tileSuccess); + loader.call(this, extent, resolution, projection); + } } } } diff --git a/src/ol/tileloadfunction.js b/src/ol/tileloadfunction.js index 887eda62f9..e459a73eb8 100644 --- a/src/ol/tileloadfunction.js +++ b/src/ol/tileloadfunction.js @@ -1,4 +1,5 @@ goog.provide('ol.TileLoadFunctionType'); +goog.provide('ol.TileVectorLoadFunctionType'); /** @@ -9,3 +10,13 @@ goog.provide('ol.TileLoadFunctionType'); * @api */ ol.TileLoadFunctionType; + + +/** + * A function that is called with a tile url for the features to load and + * a callback that takes the loaded features as argument. + * + * @typedef {function(string, function(Array.))} + * @api + */ +ol.TileVectorLoadFunctionType;