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>
|
|
|
|
|
* @version $Id$
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
|
egw_core;
|
2012-03-05 14:07:38 +01:00
|
|
|
|
egw_debug;
|
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";
|
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
var jsonq_queue = {};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Next uid (index) in queue
|
|
|
|
|
*/
|
|
|
|
|
var jsonq_uid = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Running timer for next send of queued items
|
|
|
|
|
*/
|
|
|
|
|
var jsonq_timer = null;
|
|
|
|
|
|
2012-03-05 14:07:38 +01:00
|
|
|
|
/**
|
|
|
|
|
* Dispatch responses received
|
2015-08-31 14:21:11 +02:00
|
|
|
|
*
|
|
|
|
|
* @param {object} _data uid => response pairs
|
2012-03-05 14:07:38 +01:00
|
|
|
|
*/
|
|
|
|
|
function jsonq_callback(_data)
|
|
|
|
|
{
|
|
|
|
|
if (typeof _data != 'object') throw "jsonq_callback called with NO object as parameter!";
|
2015-08-31 14:21:11 +02:00
|
|
|
|
|
2014-01-11 15:47:31 +01:00
|
|
|
|
// Abort if type is set (multi-response support)
|
|
|
|
|
if (typeof _data.type != 'undefined') return;
|
2012-03-05 14:07:38 +01:00
|
|
|
|
|
2013-09-05 00:01:33 +02:00
|
|
|
|
var json = egw.json('none');
|
2012-03-05 14:07:38 +01:00
|
|
|
|
for(var 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;
|
|
|
|
|
}
|
|
|
|
|
var job = jsonq_queue[uid];
|
|
|
|
|
var response = _data[uid];
|
2015-08-31 14:21:11 +02:00
|
|
|
|
|
2013-09-05 00:01:33 +02:00
|
|
|
|
// fake egw.json_request object, to call it with the current response
|
2012-03-05 14:07:38 +01:00
|
|
|
|
json.callback = job.callback;
|
|
|
|
|
json.sender = job.sender;
|
|
|
|
|
json.handleResponse({response: response});
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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')
|
|
|
|
|
{
|
|
|
|
|
var jobs_to_send = {};
|
|
|
|
|
var something_to_send = false;
|
|
|
|
|
for(var uid in jsonq_queue)
|
|
|
|
|
{
|
|
|
|
|
var job = jsonq_queue[uid];
|
|
|
|
|
|
|
|
|
|
if (job.menuaction == 'send') continue; // already send to server
|
|
|
|
|
|
|
|
|
|
// if job has a callbeforesend callback, call it to allow it to modify pararmeters
|
|
|
|
|
if (typeof job.callbeforesend == 'function')
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
2016-05-01 11:57:48 +02:00
|
|
|
|
var request = egw.json('api.queue', jobs_to_send, jsonq_callback, this);
|
2013-11-21 00:40:26 +01:00
|
|
|
|
request.sendRequest(true);
|
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.
|
|
|
|
|
* @param {function} _callback callback function which should be called upon a "data" response is received
|
|
|
|
|
* @param {object} _sender is the reference object the callback function should get
|
|
|
|
|
* @param {function} _callbeforesend optional callback function which can modify the parameters, eg. to do some own queuing
|
2012-03-01 17:24:29 +01:00
|
|
|
|
* @return string uid of the queued request
|
|
|
|
|
*/
|
|
|
|
|
jsonq: function(_menuaction, _parameters, _callback, _sender, _callbeforesend)
|
|
|
|
|
{
|
|
|
|
|
var uid = 'u'+(jsonq_uid++);
|
|
|
|
|
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
|
|
|
|
|
// JSON.strigify([123,undefined]) --> '{"0":123}' instead of '[123,null]'
|
|
|
|
|
parameters: _parameters ? [].concat(_parameters) : [],
|
2012-03-01 17:24:29 +01:00
|
|
|
|
callback: _callback,
|
|
|
|
|
sender: _sender,
|
|
|
|
|
callbeforesend: _callbeforesend
|
|
|
|
|
};
|
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
|
|
|
|
|
var self = this;
|
|
|
|
|
jsonq_timer = window.setInterval(function(){
|
2012-03-05 14:07:38 +01:00
|
|
|
|
jsonq_send.call(self);
|
2012-03-01 17:24:29 +01:00
|
|
|
|
}, 100);
|
|
|
|
|
}
|
|
|
|
|
return uid;
|
2012-03-05 14:07:38 +01:00
|
|
|
|
}
|
2012-03-01 17:24:29 +01:00
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
});
|