From e51aa4a9c5ddf5817cf52bc720c7d3574f2c4e26 Mon Sep 17 00:00:00 2001 From: Uwe Schmidt Date: Fri, 5 May 2023 01:12:23 +0200 Subject: [PATCH] Refine setAjaxHeaders - Allow null to clear headers (same as empty object) - Add TiledImage._updateAjaxHeaders - Add error message in case of invalid headers --- src/tiledimage.js | 27 +++++++++++++++++---------- src/viewer.js | 15 ++++++++++----- test/modules/ajax-tiles.js | 2 +- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/tiledimage.js b/src/tiledimage.js index 24583e4d..60e61827 100644 --- a/src/tiledimage.js +++ b/src/tiledimage.js @@ -1012,23 +1012,30 @@ $.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadrag /** * TODO */ - setAjaxHeaders: function(ajaxHeaders, propagate){ + setAjaxHeaders: function(ajaxHeaders, propagate) { + if (ajaxHeaders === null) { + ajaxHeaders = {}; + } + if (!$.isPlainObject(ajaxHeaders)) { + console.error('[TiledImage.setAjaxHeaders] Ignoring invalid headers, must be a plain object'); + return; + } + + this._ownAjaxHeaders = ajaxHeaders; + this._updateAjaxHeaders(propagate); + }, + + // private + _updateAjaxHeaders: function(propagate) { if (propagate === undefined) { propagate = true; } - // use same headers if provided 'ajaxHeaders' is invalid (useful for propagation) - if ($.isPlainObject(ajaxHeaders)) { - this._ownAjaxHeaders = ajaxHeaders; - } else { - ajaxHeaders = this._ownAjaxHeaders; - } - // merge with viewer's headers if ($.isPlainObject(this.viewer.ajaxHeaders)) { - this.ajaxHeaders = $.extend({}, this.viewer.ajaxHeaders, ajaxHeaders); + this.ajaxHeaders = $.extend({}, this.viewer.ajaxHeaders, this._ownAjaxHeaders); } else { - this.ajaxHeaders = ajaxHeaders; + this.ajaxHeaders = this._ownAjaxHeaders; } // propagate header updates to all tiles and queued imageloader jobs diff --git a/src/viewer.js b/src/viewer.js index 096bc597..5f835488 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -981,18 +981,23 @@ $.extend( $.Viewer.prototype, $.EventSource.prototype, $.ControlDock.prototype, /** * TODO */ - setAjaxHeaders: function(ajaxHeaders, propagate){ + setAjaxHeaders: function(ajaxHeaders, propagate) { + if (ajaxHeaders === null) { + ajaxHeaders = {}; + } + if (!$.isPlainObject(ajaxHeaders)) { + console.error('[Viewer.setAjaxHeaders] Ignoring invalid headers, must be a plain object'); + return; + } if (propagate === undefined) { propagate = true; } - if ($.isPlainObject(ajaxHeaders)) { - this.ajaxHeaders = ajaxHeaders; - } + this.ajaxHeaders = ajaxHeaders; if (propagate) { for (var i = 0; i < this.world.getItemCount(); i++) { - this.world.getItemAt(i).setAjaxHeaders(null, true); + this.world.getItemAt(i)._updateAjaxHeaders(true); } if (this.referenceStrip && this.referenceStrip.miniViewers) { diff --git a/test/modules/ajax-tiles.js b/test/modules/ajax-tiles.js index c18ec282..676a250a 100644 --- a/test/modules/ajax-tiles.js +++ b/test/modules/ajax-tiles.js @@ -346,7 +346,7 @@ assert.deepEqual(evt.tiledImage.ajaxHeaders, OpenSeadragon.extend({}, viewer.ajaxHeaders, newHeaders2)); assert.deepEqual(evt.tile.ajaxHeaders, OpenSeadragon.extend({}, viewer.ajaxHeaders, newHeaders2, tileSourceHeaders)); // set new TiledImage headers but do not propagate to Tile - evt.tiledImage.setAjaxHeaders({}, false); + evt.tiledImage.setAjaxHeaders(null, false); viewer.addHandler('tile-loaded', tileLoaded4); };