Refine setAjaxHeaders

- Allow null to clear headers (same as empty object)
- Add TiledImage._updateAjaxHeaders
- Add error message in case of invalid headers
This commit is contained in:
Uwe Schmidt 2023-05-05 01:12:23 +02:00
parent 182c11481e
commit e51aa4a9c5
3 changed files with 28 additions and 16 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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);
};