2012-03-01 17:24:29 +01:00
|
|
|
/**
|
|
|
|
* EGroupware clientside API object
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Andreas Stöckel (as AT stylite.de)
|
|
|
|
* @author Ralf Becker <RalfBecker@outdoor-training.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
egw_core;
|
2012-03-05 14:07:38 +01:00
|
|
|
egw_debug;
|
2012-03-01 17:24:29 +01:00
|
|
|
*/
|
2021-06-05 20:39:39 +02:00
|
|
|
import './egw_core.js';
|
2012-03-01 17:24:29 +01:00
|
|
|
|
2015-08-31 14:21:11 +02:00
|
|
|
egw.extend('jsonq', egw.MODULE_GLOBAL, function()
|
|
|
|
{
|
2016-02-29 16:50:24 +01:00
|
|
|
"use strict";
|
|
|
|
|
2021-07-30 15:21:58 +02:00
|
|
|
/**
|
|
|
|
* Explicit registered push callbacks
|
|
|
|
*
|
|
|
|
* @type {Function[]}
|
|
|
|
*/
|
|
|
|
let push_callbacks = [];
|
|
|
|
|
2012-03-01 17:24:29 +01:00
|
|
|
/**
|
|
|
|
* Queued json requests (objects with attributes menuaction, parameters, context, callback, sender and callbeforesend)
|
2015-08-31 14:21:11 +02:00
|
|
|
*
|
2012-03-01 17:24:29 +01:00
|
|
|
* @access private, use jsonq method to queue requests
|
|
|
|
*/
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
const jsonq_queue = {};
|
2012-03-01 17:24:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Next uid (index) in queue
|
|
|
|
*/
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
let jsonq_uid = 0;
|
2012-03-01 17:24:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Running timer for next send of queued items
|
|
|
|
*/
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
let jsonq_timer = null;
|
2012-03-05 14:07:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the whole job-queue to the server in a single json request with menuaction=queue
|
|
|
|
*/
|
|
|
|
function jsonq_send()
|
|
|
|
{
|
|
|
|
if (jsonq_uid > 0 && typeof jsonq_queue['u'+(jsonq_uid-1)] == 'object')
|
|
|
|
{
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
const jobs_to_send = {};
|
|
|
|
let something_to_send = false;
|
|
|
|
for(let uid in jsonq_queue)
|
2012-03-05 14:07:38 +01:00
|
|
|
{
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
const job = jsonq_queue[uid];
|
2012-03-05 14:07:38 +01:00
|
|
|
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
if (job.menuaction === 'send') continue; // already send to server
|
2012-03-05 14:07:38 +01:00
|
|
|
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
// if job has a callbeforesend callback, call it to allow it to modify parameters
|
|
|
|
if (typeof job.callbeforesend === 'function')
|
2012-03-05 14:07:38 +01:00
|
|
|
{
|
|
|
|
job.callbeforesend.call(job.sender, job.parameters);
|
|
|
|
}
|
|
|
|
jobs_to_send[uid] = {
|
|
|
|
menuaction: job.menuaction,
|
|
|
|
parameters: job.parameters
|
|
|
|
};
|
|
|
|
job.menuaction = 'send';
|
|
|
|
job.parameters = null;
|
|
|
|
something_to_send = true;
|
|
|
|
}
|
|
|
|
if (something_to_send)
|
|
|
|
{
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
egw.request('api.queue', jobs_to_send).then(_data =>
|
|
|
|
{
|
|
|
|
if (typeof _data != 'object') throw "jsonq_callback called with NO object as parameter!";
|
|
|
|
|
|
|
|
const json = egw.json('none');
|
|
|
|
for(let uid in _data)
|
|
|
|
{
|
|
|
|
if (typeof jsonq_queue[uid] == 'undefined')
|
|
|
|
{
|
|
|
|
console.log("jsonq_callback received response for not existing queue uid="+uid+"!");
|
|
|
|
console.log(_data[uid]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const job = jsonq_queue[uid];
|
|
|
|
const response = _data[uid];
|
|
|
|
|
|
|
|
// The ajax request has completed, get just the data & pass it on
|
|
|
|
if(response)
|
|
|
|
{
|
|
|
|
for(let value of response)
|
|
|
|
{
|
|
|
|
if(value.type && value.type === "data" && typeof value.data !== "undefined")
|
|
|
|
{
|
|
|
|
// Data was packed in response
|
|
|
|
job.resolve(value.data);
|
|
|
|
}
|
|
|
|
else if (value && typeof value.type === "undefined" && typeof value.data === "undefined")
|
|
|
|
{
|
|
|
|
// Just raw data
|
|
|
|
job.resolve(value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// fake egw.json_request object, to call it with the current response
|
|
|
|
json.handleResponse({response: response});
|
|
|
|
}
|
|
|
|
}
|
2023-04-25 17:10:33 +02:00
|
|
|
// Response is there, but empty. Make sure to resolve it or the callback doesn't get called.
|
|
|
|
if (typeof response.length !== "undefined" && response.length == 0)
|
|
|
|
{
|
|
|
|
job.resolve();
|
|
|
|
}
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
delete jsonq_queue[uid];
|
|
|
|
}
|
|
|
|
// if nothing left in queue, stop interval-timer to give browser a rest
|
|
|
|
if (jsonq_timer && typeof jsonq_queue['u'+(jsonq_uid-1)] != 'object')
|
|
|
|
{
|
|
|
|
window.clearInterval(jsonq_timer);
|
|
|
|
jsonq_timer = null;
|
|
|
|
}
|
|
|
|
});
|
2012-03-05 14:07:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-01 17:24:29 +01:00
|
|
|
return {
|
|
|
|
/**
|
|
|
|
* Send a queued JSON call to the server
|
2015-08-31 14:21:11 +02:00
|
|
|
*
|
|
|
|
* @param {string} _menuaction the menuaction function which should be called and
|
2012-03-01 17:24:29 +01:00
|
|
|
* which handles the actual request. If the menuaction is a full featured
|
|
|
|
* url, this one will be used instead.
|
2015-08-31 14:21:11 +02:00
|
|
|
* @param {array} _parameters which should be passed to the menuaction function.
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
* @param {function|undefined} _callback callback function which should be called upon a "data" response is received
|
|
|
|
* @param {object|undefined} _sender is the reference object the callback function should get
|
|
|
|
* @param {function|undefined} _callbeforesend optional callback function which can modify the parameters, eg. to do some own queuing
|
|
|
|
* @return Promise
|
2012-03-01 17:24:29 +01:00
|
|
|
*/
|
|
|
|
jsonq: function(_menuaction, _parameters, _callback, _sender, _callbeforesend)
|
|
|
|
{
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
const uid = 'u'+(jsonq_uid++);
|
2012-03-01 17:24:29 +01:00
|
|
|
jsonq_queue[uid] = {
|
|
|
|
menuaction: _menuaction,
|
2015-08-31 14:21:11 +02:00
|
|
|
// IE JSON-serializes arrays passed in from different window contextx (eg. popups)
|
|
|
|
// as objects (it looses object-type of array), causing them to be JSON serialized
|
|
|
|
// as objects and loosing parameters which are undefined
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
// JSON.stringify([123,undefined]) --> '{"0":123}' instead of '[123,null]'
|
2015-08-31 14:21:11 +02:00
|
|
|
parameters: _parameters ? [].concat(_parameters) : [],
|
2022-05-02 16:21:23 +02:00
|
|
|
callbeforesend: _callbeforesend && _sender ? _callbeforesend.bind(_sender) : _callbeforesend,
|
2012-03-01 17:24:29 +01:00
|
|
|
};
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
let promise = new Promise(resolve => {
|
|
|
|
jsonq_queue[uid].resolve = resolve;
|
|
|
|
});
|
|
|
|
if (typeof _callback === 'function')
|
|
|
|
{
|
2022-05-02 16:21:23 +02:00
|
|
|
const callback = _callback.bind(_sender);
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
promise = promise.then(_data => {
|
2022-05-02 16:21:23 +02:00
|
|
|
callback(_data);
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
return _data;
|
|
|
|
});
|
|
|
|
}
|
2015-08-31 14:21:11 +02:00
|
|
|
|
2012-03-01 17:24:29 +01:00
|
|
|
if (jsonq_timer == null)
|
|
|
|
{
|
|
|
|
// check / send queue every N ms
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
jsonq_timer = window.setInterval(() => jsonq_send(), 100);
|
2012-03-01 17:24:29 +01:00
|
|
|
}
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
return promise;
|
2021-07-30 15:21:58 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a callback to receive push broadcasts eg. in a popup or iframe
|
|
|
|
*
|
|
|
|
* It's also used internally by egw_message's push method to dispatch to the registered callbacks.
|
|
|
|
*
|
|
|
|
* @param {Function|PushData} data callback (with bound context) or PushData to dispatch to callbacks
|
|
|
|
*/
|
|
|
|
registerPush: function(data)
|
|
|
|
{
|
|
|
|
if (typeof data === "function")
|
|
|
|
{
|
|
|
|
push_callbacks.push(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (let n in push_callbacks)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
push_callbacks[n].call(this, data);
|
|
|
|
}
|
|
|
|
// if we get an exception, we assume the callback is no longer available and remove it
|
|
|
|
catch (ex) {
|
|
|
|
push_callbacks.splice(n, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-05 14:07:38 +01:00
|
|
|
}
|
2012-03-01 17:24:29 +01:00
|
|
|
|
|
|
|
};
|
change egw.jsonq() and egw.link_title() to return promises
* egw.jsonq() now always returns a promise like egw.request(), still supporting old callback syntax
* egw.link_title(_app, _id, _callback, _context, _force_reload) supports boolean values for _callback (to not break existing code)
- false: just a cache lookup (like current call with just 2 parameters), returning null, if no title is cached, or the title
- true: always return a promise, which might already be resolved, if title was cached
- function: also returns a promise and calls the callback
--> calling egw.link_title(_app, _id) without 3rd parameter is deprecated now (gives a console.trace), to be changed in future to always return a promise, unless called with false, to just return a cache-lookup
* Et2SelectAccountReadonly and et2_link_widget is changed to use the new/updated syntax with promises
2022-05-02 11:27:33 +02:00
|
|
|
});
|