From aacae1ca3fd010f050bf1715fd1ce21685dc0fef Mon Sep 17 00:00:00 2001 From: Jirka Date: Wed, 12 Jan 2022 22:31:13 +0100 Subject: [PATCH] Reverted .json configuration changes made by npm. Added tests for PUSH functionality, new flag 'allowPost' to explicitly enable POST data, remove (-1) in the URL splitting. --- src/openseadragon.js | 10 ++ src/tile.js | 4 +- src/tiledimage.js | 4 +- src/tilesource.js | 24 ++- src/viewer.js | 1 + test/coverage.html | 1 + test/modules/ajax-post-data.js | 276 +++++++++++++++++++++++++++++++++ test/test.html | 1 + 8 files changed, 311 insertions(+), 10 deletions(-) create mode 100644 test/modules/ajax-post-data.js diff --git a/src/openseadragon.js b/src/openseadragon.js index 30663b4b..73fb912b 100644 --- a/src/openseadragon.js +++ b/src/openseadragon.js @@ -659,6 +659,15 @@ * @property {Object} [ajaxHeaders={}] * A set of headers to include when making AJAX requests for tile sources or tiles. * + * @property {Boolean} [splitHashDataForPost=false] + * Allows to treat _first_ hash ('#') symbol as a separator for POST data: + * URL to be opened by a {@link OpenSeadragon.TileSource} can thus look like: http://some.url#postdata=here . + * The URL is split to 'http://some.url' and 'postdata=here'; post data is given to the + * {@link OpenSeadragon.TileSource} of the choice and can be further used within tile requests + * (see TileSource methods). {@link OpenSeadragon.TileSource.prototype.configure} return value + * should contain the post data so that it is given to its subclass in the constructor. + * NOTE: post data is expected to be ampersand-separated (just like GET parameters), and is not used + * to fetch tile image data if loadTilesWithAjax=false (but it is still used for the initial request). */ /** @@ -1140,6 +1149,7 @@ function OpenSeadragon( options ){ ajaxWithCredentials: false, loadTilesWithAjax: false, ajaxHeaders: {}, + splitHashDataForPost: false, //PAN AND ZOOM SETTINGS AND CONSTRAINTS panHorizontal: true, diff --git a/src/tile.js b/src/tile.js index c0740eb4..ba4a5575 100644 --- a/src/tile.js +++ b/src/tile.js @@ -45,16 +45,16 @@ * @param {Boolean} exists Is this tile a part of a sparse image? ( Also has * this tile failed to load? ) * @param {String} url The URL of this tile's image. - * @param {String} postData post parameters or null * @param {CanvasRenderingContext2D} context2D The context2D of this tile if it * is provided directly by the tile source. * @param {Boolean} loadWithAjax Whether this tile image should be loaded with an AJAX request . * @param {Object} ajaxHeaders The headers to send with this tile's AJAX request (if applicable). * @param {OpenSeadragon.Rect} sourceBounds The portion of the tile to use as the source of the + * @param {String} postData post parameters or null * drawing operation, in pixels. Note that this only works when drawing with canvas; when drawing * with HTML the entire tile is always used. */ -$.Tile = function(level, x, y, bounds, exists, url, postData, context2D, loadWithAjax, ajaxHeaders, sourceBounds) { +$.Tile = function(level, x, y, bounds, exists, url, context2D, loadWithAjax, ajaxHeaders, sourceBounds, postData) { /** * The zoom level this tile belongs to. * @member {Number} level diff --git a/src/tiledimage.js b/src/tiledimage.js index f1b28fa2..76f3eead 100644 --- a/src/tiledimage.js +++ b/src/tiledimage.js @@ -1542,11 +1542,11 @@ function getTile( bounds, exists, url, - post, context2D, tiledImage.loadTilesWithAjax, ajaxHeaders, - sourceBounds + sourceBounds, + post ); if (tiledImage.getFlip()) { diff --git a/src/tilesource.js b/src/tilesource.js index a915ac2c..4d24070d 100644 --- a/src/tilesource.js +++ b/src/tilesource.js @@ -69,6 +69,9 @@ * the XHR's withCredentials (for accessing secure data). * @param {Object} [options.ajaxHeaders] * A set of headers to include in AJAX requests. + * @param {Boolean} [options.splitHashDataForPost] + * First occurrence of '#' in the options.url is used to split URL + * and the latter part is treated as POST data (applies to getImageInfo(...)) * @param {Number} [options.width] * Width of the source image at max resolution in pixels. * @param {Number} [options.height] @@ -446,10 +449,16 @@ $.TileSource.prototype = { } var postData = null; - var hashIdx = url.indexOf("#"); - if (hashIdx !== -1) { - postData = url.substring(hashIdx + 1); - url = url.substr(0, hashIdx - 1); + if (this.allowPost) { + if (!this.loadTilesWithAjax) { + console.warn("Ajax is not enabled, but post data are used. Post data is ignored " + + "without ajax in subsequent tile requests."); + } + var hashIdx = url.indexOf("#"); + if (hashIdx !== -1) { + postData = url.substring(hashIdx + 1); + url = url.substr(0, hashIdx); + } } callback = function( data ){ @@ -545,11 +554,13 @@ $.TileSource.prototype = { * @property {OpenSeadragon.TileSource} eventSource - A reference to the TileSource which raised the event. * @property {String} message * @property {String} source + * @property {String} postData or null * @property {?Object} userData - Arbitrary subscriber-defined object. */ _this.raiseEvent( 'open-failed', { message: msg, - source: url + source: url, + postData: postData }); } }); @@ -588,7 +599,8 @@ $.TileSource.prototype = { * from if any. * @param {String} postData value obtained from the url after '#' sign or null * @return {Object} options - A dictionary of keyword arguments sufficient - * to configure this tile sources constructor. + * to configure the tile source constructor (include all values you want to + * instantiate the TileSource subclass with - what _options_ object should contain). * @throws {Error} */ configure: function( data, url, postData ) { diff --git a/src/viewer.js b/src/viewer.js index b1a60446..b4199a42 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -2432,6 +2432,7 @@ function getTileSourceImplementation( viewer, tileSource, imgOptions, successCal ajaxWithCredentials: viewer.ajaxWithCredentials, ajaxHeaders: imgOptions.ajaxHeaders ? imgOptions.ajaxHeaders : viewer.ajaxHeaders, + splitHashDataForPost: viewer.splitHashDataForPost, useCanvas: viewer.useCanvas, success: function( event ) { successCallback( event.tileSource ); diff --git a/test/coverage.html b/test/coverage.html index df0e9781..6d392209 100644 --- a/test/coverage.html +++ b/test/coverage.html @@ -86,6 +86,7 @@ +