also augmenting Promise returned from egw.request() with an abort method and using that in link-widget

This commit is contained in:
Ralf Becker 2021-08-23 09:37:18 +02:00
parent 6c641f824b
commit 0203ce5a1f
2 changed files with 29 additions and 31 deletions

View File

@ -639,7 +639,6 @@ export class et2_link_entry extends et2_inputWidget
protected search: JQuery; protected search: JQuery;
protected clear: JQuery; protected clear: JQuery;
protected link_button: JQuery; protected link_button: JQuery;
private response: any;
private request: any; private request: any;
private last_search: string; private last_search: string;
processing: boolean = false; processing: boolean = false;
@ -1190,18 +1189,29 @@ export class et2_link_entry extends et2_inputWidget
return response(this.cache[request.term]); return response(this.cache[request.term]);
} }
// Remember callback
this.response = response;
this.search.addClass("loading"); this.search.addClass("loading");
// Remove specific display and revert to CSS file // Remove specific display and revert to CSS file
// show() would use inline, should be inline-block // show() would use inline, should be inline-block
this.clear.css('display', ''); this.clear.css('display', '');
this.request = egw.json("EGroupware\\Api\\Etemplate\\Widget\\Link::ajax_link_search",
[this.app_select.val(), '', request.term, request.options], this.request = egw.request("EGroupware\\Api\\Etemplate\\Widget\\Link::ajax_link_search",
this._results, [this.app_select.val(), '', request.term, request.options]);
this, true, this
).sendRequest(); this.request.then((data) =>
{
if (this.request)
{
this.request = null;
}
this.search.removeClass("loading");
let result = [];
for (var id in data)
{
result.push({"value": id, "label": data[id]});
}
this.cache[this.search.val()] = result;
response(result);
});
} }
/** /**
@ -1246,27 +1256,6 @@ export class et2_link_entry extends et2_inputWidget
}, event.data)); }, event.data));
} }
/**
* Server found some results
*
* @param {Array} data
*/
_results(data)
{
if (this.request)
{
this.request = null;
}
this.search.removeClass("loading");
var result = [];
for (var id in data)
{
result.push({"value": id, "label": data[id]});
}
this.cache[this.search.val()] = result;
this.response(result);
}
/** /**
* Create a link using the current internal values * Create a link using the current internal values
* *

View File

@ -189,6 +189,7 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd)
* @param {function} error option error callback(_xmlhttp, _err) used instead our default this.error * @param {function} error option error callback(_xmlhttp, _err) used instead our default this.error
* *
* @return {Promise|boolean} Promise or for async==="keepalive" boolean is returned * @return {Promise|boolean} Promise or for async==="keepalive" boolean is returned
* Promise.abort() allows to abort the pending request
*/ */
json_request.prototype.sendRequest = function(async, method, error) json_request.prototype.sendRequest = function(async, method, error)
{ {
@ -502,11 +503,13 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd)
* @param {any[]} _parameters * @param {any[]} _parameters
* *
* @return Promise resolving to data part (not full response, which can contain other parts) * @return Promise resolving to data part (not full response, which can contain other parts)
* Promise.abort() allows to abort the pending request
*/ */
request: function(_menuaction, _parameters) request: function(_menuaction, _parameters)
{ {
const request = new json_request(_menuaction, _parameters, null, this, true, this, this); const request = new json_request(_menuaction, _parameters, null, this, true, this, this);
return request.sendRequest().then(function(response) const response = request.sendRequest();
let promise = response.then(function(response)
{ {
// The ajax request has completed, get just the data & pass it on // The ajax request has completed, get just the data & pass it on
if(response && response.response) if(response && response.response)
@ -527,6 +530,12 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd)
} }
return undefined; return undefined;
}); });
// pass abort method to returned response
if (typeof response.abort === 'function')
{
promise.abort = response.abort;
}
return promise;
}, },
/** /**