2010-06-01 14:38:41 +02:00
|
|
|
/**
|
|
|
|
* eGroupWare API: JSON - Contains the client side javascript implementation of class.egw_json.inc.php
|
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package api
|
|
|
|
* @subpackage ajax
|
2010-06-01 20:54:31 +02:00
|
|
|
* @author Andreas Stoeckel <as@stylite.de>
|
|
|
|
* @version $Id$
|
2010-06-01 14:38:41 +02:00
|
|
|
*/
|
|
|
|
|
2011-08-15 16:26:37 +02:00
|
|
|
/*egw:uses
|
|
|
|
jsapi.jsapi; // Contains some helper functions
|
|
|
|
jquery.jquery; // Used for the ajax requests
|
|
|
|
*/
|
|
|
|
|
2010-06-01 14:38:41 +02:00
|
|
|
/* The egw_json_request is the javaScript side implementation of class.egw_json.inc.php.*/
|
|
|
|
|
2010-06-11 17:06:13 +02:00
|
|
|
function _egw_json_escape_string(input)
|
|
|
|
{
|
|
|
|
var len = input.length;
|
|
|
|
var res = "";
|
|
|
|
|
|
|
|
for (var i = 0; i < len; i++)
|
|
|
|
{
|
2010-06-14 18:04:39 +02:00
|
|
|
switch (input.charAt(i))
|
2010-06-11 17:06:13 +02:00
|
|
|
{
|
|
|
|
case '"':
|
|
|
|
res += '\\"';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\n':
|
|
|
|
res += '\\n';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\r':
|
|
|
|
res += '\\r';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\\':
|
|
|
|
res += '\\\\';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\/':
|
|
|
|
res += '\\/';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\b':
|
|
|
|
res += '\\b';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\f':
|
|
|
|
res += '\\f';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\t':
|
|
|
|
res += '\\t';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-06-14 18:04:39 +02:00
|
|
|
res += input.charAt(i);
|
2010-06-11 17:06:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _egw_json_encode_simple(input)
|
2010-06-09 15:24:59 +02:00
|
|
|
{
|
2010-06-11 12:23:13 +02:00
|
|
|
switch (input.constructor)
|
|
|
|
{
|
2010-06-09 15:24:59 +02:00
|
|
|
case String:
|
2010-06-11 17:06:13 +02:00
|
|
|
return '"' + _egw_json_escape_string(input) + '"';
|
2010-06-09 15:24:59 +02:00
|
|
|
|
|
|
|
case Number:
|
|
|
|
return input.toString();
|
|
|
|
|
|
|
|
case Boolean:
|
|
|
|
return input ? 'true' : 'false';
|
|
|
|
|
2010-06-11 12:23:13 +02:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2010-06-09 15:24:59 +02:00
|
|
|
|
2010-06-11 12:23:13 +02:00
|
|
|
function egw_json_encode(input)
|
|
|
|
{
|
2013-09-10 21:39:20 +02:00
|
|
|
egw.debug("warn", "Function %s is deprecated, use egw.jsonEncode() instead", arguments.callee.name);
|
|
|
|
return egw.jsonEncode(input);
|
2010-08-10 16:58:54 +02:00
|
|
|
if (input == null || !input && input.length == 0) return 'null';
|
2010-06-09 15:24:59 +02:00
|
|
|
|
2010-06-11 17:06:13 +02:00
|
|
|
var simple_res = _egw_json_encode_simple(input);
|
2010-06-11 12:23:13 +02:00
|
|
|
if (simple_res == null)
|
|
|
|
{
|
|
|
|
switch (input.constructor)
|
|
|
|
{
|
2011-09-20 01:19:16 +02:00
|
|
|
case Array:
|
2010-06-11 12:23:13 +02:00
|
|
|
var buf = [];
|
|
|
|
for (var k in input)
|
|
|
|
{
|
2011-02-26 19:04:28 +01:00
|
|
|
//Filter non numeric entries
|
|
|
|
if (!isNaN(k))
|
2010-06-16 14:07:50 +02:00
|
|
|
buf.push(egw_json_encode(input[k]));
|
2010-06-11 12:23:13 +02:00
|
|
|
}
|
|
|
|
return '[' + buf.join(',') + ']';
|
|
|
|
|
|
|
|
case Object:
|
|
|
|
var buf = [];
|
|
|
|
for (var k in input)
|
|
|
|
{
|
2010-06-11 17:06:13 +02:00
|
|
|
buf.push(_egw_json_encode_simple(k) + ':' + egw_json_encode(input[k]));
|
2010-06-11 12:23:13 +02:00
|
|
|
}
|
|
|
|
return '{' + buf.join(',') + '}';
|
|
|
|
|
|
|
|
default:
|
2011-09-20 01:19:16 +02:00
|
|
|
switch(typeof input)
|
|
|
|
{
|
|
|
|
case 'array':
|
|
|
|
var buf = [];
|
|
|
|
for (var k in input)
|
|
|
|
{
|
|
|
|
//Filter non numeric entries
|
|
|
|
if (!isNaN(k))
|
|
|
|
buf.push(egw_json_encode(input[k]));
|
|
|
|
}
|
|
|
|
return '[' + buf.join(',') + ']';
|
|
|
|
|
|
|
|
case 'object':
|
|
|
|
var buf = [];
|
|
|
|
for (var k in input)
|
|
|
|
{
|
|
|
|
buf.push(_egw_json_encode_simple(k) + ':' + egw_json_encode(input[k]));
|
|
|
|
}
|
|
|
|
return '{' + buf.join(',') + '}';
|
|
|
|
|
|
|
|
}
|
2010-06-11 12:23:13 +02:00
|
|
|
return 'null';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return simple_res;
|
2010-06-09 15:24:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-18 10:36:33 +02:00
|
|
|
/**
|
|
|
|
* Some variables needed to store which JS and CSS files have already be included
|
|
|
|
*/
|
|
|
|
var egw_json_files = {};
|
|
|
|
|
2011-05-22 20:42:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the egw_json_files object with all files which are already bound in
|
|
|
|
*/
|
2011-07-03 11:00:36 +02:00
|
|
|
$j(document).ready(function() {
|
|
|
|
$j("script, link").each(function() {
|
2011-05-22 20:42:23 +02:00
|
|
|
var file = false;
|
2011-07-03 11:00:36 +02:00
|
|
|
if ($j(this).attr("src")) {
|
|
|
|
file = $j(this).attr("src");
|
|
|
|
} else if ($j(this).attr("href")) {
|
|
|
|
file = $j(this).attr("href");
|
2011-05-22 20:42:23 +02:00
|
|
|
}
|
|
|
|
if (file) {
|
|
|
|
egw_json_files[file] = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2010-07-08 14:03:13 +02:00
|
|
|
/**
|
|
|
|
* Variable which stores all currently registered plugins
|
|
|
|
*/
|
|
|
|
var _egw_json_plugins = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a plugin for the egw_json handler
|
|
|
|
*/
|
|
|
|
function egw_json_register_plugin(_callback, _context)
|
|
|
|
{
|
2013-09-10 21:39:20 +02:00
|
|
|
egw.debug("warn", "Function %s is deprecated", arguments.callee.name);
|
2010-07-08 14:03:13 +02:00
|
|
|
//Default the context parameter to "window"
|
|
|
|
if (typeof _context == 'undefined') {
|
|
|
|
_context = window;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add a plugin object to the plugins array
|
|
|
|
_egw_json_plugins[_egw_json_plugins.length] = {
|
|
|
|
'callback': _callback,
|
|
|
|
'context': _context
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function used internally to pass a response to all registered plugins
|
|
|
|
*/
|
2011-08-15 16:26:37 +02:00
|
|
|
function _egw_json_plugin_handle(_type, _response, _context) {
|
2010-07-08 14:03:13 +02:00
|
|
|
for (var i = 0; i < _egw_json_plugins.length; i++)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
var plugin = _egw_json_plugins[i];
|
2011-08-15 16:26:37 +02:00
|
|
|
|
|
|
|
var context = plugin.context;
|
|
|
|
if (!plugin.context && typeof _context != "undefined")
|
|
|
|
{
|
|
|
|
context = _context;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (plugin.callback.call(context, _type, _response)) {
|
2010-07-08 14:03:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (typeof console != 'undefined')
|
|
|
|
{
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-18 10:36:33 +02:00
|
|
|
/** The constructor of the egw_json_request class.
|
2011-01-20 15:11:47 +01:00
|
|
|
*
|
|
|
|
* @param string _menuaction the menuaction function which should be called and
|
|
|
|
* which handles the actual request. If the menuaction is a full featured
|
|
|
|
* url, this one will be used instead.
|
2010-06-01 14:38:41 +02:00
|
|
|
* @param array _parameters which should be passed to the menuaction function.
|
2013-04-12 10:23:23 +02:00
|
|
|
* @param object _context is the context which will be used for the callbacks (not callback of sendRequest!)
|
2010-06-18 10:36:33 +02:00
|
|
|
*/
|
2010-06-18 16:23:51 +02:00
|
|
|
function egw_json_request(_menuaction, _parameters, _context)
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
2013-09-10 21:39:20 +02:00
|
|
|
egw.debug("warn", "Function %s is deprecated", arguments.callee.name);
|
2010-06-18 16:23:51 +02:00
|
|
|
this.context = window.document;
|
|
|
|
if (typeof _context != 'undefined')
|
|
|
|
this.context = _context;
|
|
|
|
|
2010-06-09 15:24:59 +02:00
|
|
|
if (typeof _parameters != 'undefined')
|
|
|
|
{
|
|
|
|
this.parameters = _parameters;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.parameters = new Array;
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:11:47 +01:00
|
|
|
// Check whether the supplied menuaction parameter is a full featured url
|
|
|
|
// or just a menuaction
|
|
|
|
if (_menuaction.match(/json.php\?menuaction=[a-z_0-9]*\.[a-z_0-9]*\.[a-z_0-9]*/i))
|
2010-06-09 12:40:17 +02:00
|
|
|
{
|
2011-01-20 15:11:47 +01:00
|
|
|
// Menuaction is a full featured url
|
2011-09-13 19:27:02 +02:00
|
|
|
this.url = _menuaction;
|
2011-01-20 15:11:47 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We only got a menu action, assemble the url manually.
|
|
|
|
this.url = this._assembleAjaxUrl(_menuaction);
|
2010-06-09 12:40:17 +02:00
|
|
|
}
|
2010-06-09 15:24:59 +02:00
|
|
|
|
2010-08-10 16:58:54 +02:00
|
|
|
this.request = null;
|
2010-06-01 14:38:41 +02:00
|
|
|
this.sender = null;
|
|
|
|
this.callback = null;
|
|
|
|
this.alertHandler = this.alertFunc;
|
2010-07-20 12:20:32 +02:00
|
|
|
this.onLoadFinish = null;
|
|
|
|
this.loadedJSFiles = {};
|
|
|
|
this.handleResponseDone = false;
|
2011-06-11 23:27:15 +02:00
|
|
|
this.app = null;
|
2010-06-04 10:16:45 +02:00
|
|
|
if (window.egw_alertHandler)
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
2010-06-04 10:16:45 +02:00
|
|
|
this.alertHandler = window.egw_alertHandler;
|
2010-06-01 14:38:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-11 23:27:15 +02:00
|
|
|
/**
|
|
|
|
* Sets the "application" object which is passed to egw_appWindowOpen when a redirect is done
|
|
|
|
*/
|
|
|
|
egw_json_request.prototype.setAppObject = function(_app)
|
|
|
|
{
|
|
|
|
this.app = _app;
|
|
|
|
}
|
|
|
|
|
2011-01-20 15:11:47 +01:00
|
|
|
egw_json_request.prototype._assembleAjaxUrl = function(_menuaction)
|
|
|
|
{
|
|
|
|
// Retrieve the webserver url
|
|
|
|
var webserver_url = egw_topWindow().egw_webserverUrl;
|
|
|
|
|
2011-02-04 15:28:12 +01:00
|
|
|
// Check whether the webserver_url is really set
|
|
|
|
// Don't check for !webserver_url as it might be empty.
|
|
|
|
// Thank you to Ingo Ratsdorf for reporting this.
|
|
|
|
if (typeof webserver_url == "undefined")
|
2011-01-20 15:11:47 +01:00
|
|
|
{
|
|
|
|
throw "Internal JS error, top window not found, webserver url could not be retrieved.";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the ajax relevant parts
|
|
|
|
return webserver_url + '/json.php?menuaction=' + _menuaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the AJAX JSON request.
|
|
|
|
*
|
2010-06-01 14:38:41 +02:00
|
|
|
* @param boolean _async specifies whether the request should be handeled asynchronously (true, the sendRequest function immediately returns to the caller) or asynchronously (false, the sendRequest function waits until the request is received)
|
|
|
|
* @param _callback is an additional callback function which should be called upon a "data" response is received
|
2013-04-12 10:23:23 +02:00
|
|
|
* @param _sender context (this) of _callback (different from _context param of constructor used for standard callbacks!)
|
2010-06-01 14:38:41 +02:00
|
|
|
*/
|
|
|
|
egw_json_request.prototype.sendRequest = function(_async, _callback, _sender)
|
|
|
|
{
|
2013-09-10 21:39:20 +02:00
|
|
|
egw.debug("warn", "egw_json_request is deprecated\n\
|
|
|
|
Use egw.json(menuaction, parameters [,callback, context, async, sender]).sendRequest() instead.");
|
2010-06-01 14:38:41 +02:00
|
|
|
//Store the sender and callback parameter inside this class
|
|
|
|
this.sender = _sender;
|
|
|
|
if (typeof _callback != "undefined")
|
|
|
|
this.callback = _callback;
|
|
|
|
|
|
|
|
//Copy the async parameter which defaults to "true"
|
|
|
|
var is_async = true;
|
|
|
|
if (typeof _async != "undefined")
|
|
|
|
is_async = _async;
|
|
|
|
|
2010-06-09 15:24:59 +02:00
|
|
|
//Assemble the actual request object containing the json data string
|
|
|
|
var request_obj = {
|
2013-09-10 21:39:20 +02:00
|
|
|
"json_data": egw.jsonEncode(
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
2010-06-09 15:24:59 +02:00
|
|
|
"request": {
|
|
|
|
"parameters": this.parameters
|
|
|
|
}
|
|
|
|
})
|
2010-06-30 11:13:17 +02:00
|
|
|
};
|
2010-06-01 14:38:41 +02:00
|
|
|
|
|
|
|
//Send the request via the jquery AJAX interface to the server
|
2011-07-03 11:00:36 +02:00
|
|
|
this.request = $j.ajax({url: this.url,
|
2010-06-01 14:38:41 +02:00
|
|
|
async: is_async,
|
|
|
|
context: this,
|
|
|
|
data: request_obj,
|
|
|
|
dataType: 'json',
|
|
|
|
type: 'POST',
|
2010-06-22 12:19:33 +02:00
|
|
|
success: this.handleResponse,
|
2013-09-02 15:25:00 +02:00
|
|
|
error: function(_xmlhttp,_err) {
|
|
|
|
window.console.error('Ajax request to ' + this.url + ' failed: ' + _err);
|
|
|
|
}
|
2010-06-22 12:19:33 +02:00
|
|
|
});
|
2010-06-01 14:38:41 +02:00
|
|
|
}
|
|
|
|
|
2010-08-10 16:58:54 +02:00
|
|
|
egw_json_request.prototype.abort = function()
|
|
|
|
{
|
|
|
|
this.request.abort();
|
|
|
|
}
|
|
|
|
|
2010-06-01 14:38:41 +02:00
|
|
|
egw_json_request.prototype.alertFunc = function(_message, _details)
|
|
|
|
{
|
|
|
|
alert(_message);
|
2010-07-09 15:00:46 +02:00
|
|
|
if(_details) _egw_json_debug_log(_message, _details);
|
2010-06-01 14:38:41 +02:00
|
|
|
}
|
|
|
|
|
2010-06-21 10:39:21 +02:00
|
|
|
function _egw_json_debug_log(_msg, _e)
|
2010-06-10 14:24:50 +02:00
|
|
|
{
|
|
|
|
if (typeof console != "undefined" && typeof console.log != "undefined")
|
|
|
|
{
|
2010-06-21 10:39:21 +02:00
|
|
|
console.log(_msg, _e);
|
2010-06-10 14:24:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-21 10:39:21 +02:00
|
|
|
/* Displays an json error message */
|
|
|
|
egw_json_request.prototype.jsonError = function(_msg, _e)
|
|
|
|
{
|
2012-10-24 11:52:00 +02:00
|
|
|
var msg = 'EGW JSON Error: '+_msg;
|
2010-06-21 10:39:21 +02:00
|
|
|
|
|
|
|
//Log and show the error message
|
2010-07-09 15:00:46 +02:00
|
|
|
_egw_json_debug_log(msg, _e);
|
2010-06-21 10:39:21 +02:00
|
|
|
this.alertHandler(msg);
|
|
|
|
}
|
|
|
|
|
2010-06-01 14:38:41 +02:00
|
|
|
/* Internal function which handles the response from the server */
|
|
|
|
egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRequest)
|
|
|
|
{
|
2010-07-20 12:20:32 +02:00
|
|
|
this.handleResponseDone = false;
|
2010-06-04 17:22:01 +02:00
|
|
|
if (data && data.response)
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
|
|
|
var hasResponse = false;
|
2013-04-01 20:39:03 +02:00
|
|
|
// Try to load files using API
|
|
|
|
if(egw && egw().includeJS)
|
|
|
|
{
|
|
|
|
var js_files = [];
|
|
|
|
var css_files = [];
|
|
|
|
for (var i = data.response.length - 1; i > 0; --i)
|
|
|
|
{
|
|
|
|
var res = data.response[i];
|
|
|
|
if(res.type == 'js' && typeof res.data == 'string')
|
|
|
|
{
|
|
|
|
js_files.unshift(res.data);
|
|
|
|
this.loadedJSFiles[res.data] = false;
|
|
|
|
data.response.splice(i,1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(js_files.length > 0)
|
|
|
|
{
|
|
|
|
egw().includeJS(js_files, function() {
|
|
|
|
for(var i = 0; i < js_files.length; i++)
|
|
|
|
{
|
|
|
|
this.loadedJSFiles[js_files[i]] = true;
|
|
|
|
}
|
|
|
|
this.checkLoadFinish();
|
|
|
|
},this);
|
|
|
|
}
|
|
|
|
}
|
2010-06-01 14:38:41 +02:00
|
|
|
for (var i = 0; i < data.response.length; i++)
|
|
|
|
{
|
2010-06-21 10:39:21 +02:00
|
|
|
try
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
2010-06-21 10:39:21 +02:00
|
|
|
var res = data.response[i];
|
|
|
|
|
|
|
|
switch (data.response[i].type)
|
|
|
|
{
|
|
|
|
case 'alert':
|
|
|
|
//Check whether all needed parameters have been passed and call the alertHandler function
|
|
|
|
if ((typeof res.data.message != 'undefined') &&
|
|
|
|
(typeof res.data.details != 'undefined'))
|
|
|
|
{
|
|
|
|
this.alertHandler(
|
|
|
|
res.data.message,
|
|
|
|
res.data.details)
|
2010-06-09 15:24:59 +02:00
|
|
|
hasResponse = true;
|
2010-06-21 10:39:21 +02:00
|
|
|
} else
|
|
|
|
throw 'Invalid parameters';
|
|
|
|
break;
|
|
|
|
case 'assign':
|
|
|
|
//Check whether all needed parameters have been passed and call the alertHandler function
|
|
|
|
if ((typeof res.data.id != 'undefined') &&
|
|
|
|
(typeof res.data.key != 'undefined') &&
|
|
|
|
(typeof res.data.value != 'undefined'))
|
|
|
|
{
|
|
|
|
var obj = document.getElementById(res.data.id);
|
|
|
|
if (obj)
|
|
|
|
{
|
|
|
|
obj[res.data.key] = res.data.value;
|
2011-03-17 19:00:24 +01:00
|
|
|
|
|
|
|
if (res.data.key == "innerHTML")
|
|
|
|
{
|
|
|
|
egw_insertJS(res.data.value);
|
|
|
|
}
|
|
|
|
|
2010-06-21 10:39:21 +02:00
|
|
|
hasResponse = true;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
throw 'Invalid parameters';
|
|
|
|
break;
|
|
|
|
case 'data':
|
|
|
|
//Callback the caller in order to allow him to handle the data
|
|
|
|
if (this.callback)
|
2010-06-09 15:24:59 +02:00
|
|
|
{
|
2010-06-21 10:39:21 +02:00
|
|
|
this.callback.call(this.sender, res.data);
|
|
|
|
hasResponse = true;
|
2010-06-09 16:41:40 +02:00
|
|
|
}
|
2010-06-21 10:39:21 +02:00
|
|
|
break;
|
|
|
|
case 'script':
|
|
|
|
if (typeof res.data == 'string')
|
2010-06-09 16:41:40 +02:00
|
|
|
{
|
2010-06-21 10:39:21 +02:00
|
|
|
try
|
|
|
|
{
|
2011-09-13 19:27:02 +02:00
|
|
|
var func = new Function(res.data);
|
2010-06-21 10:39:21 +02:00
|
|
|
func.call(window);
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
e.code = res.data;
|
|
|
|
_egw_json_debug_log(e);
|
|
|
|
}
|
|
|
|
hasResponse = true;
|
|
|
|
} else
|
|
|
|
throw 'Invalid parameters';
|
|
|
|
break;
|
2011-09-13 19:27:02 +02:00
|
|
|
case 'apply':
|
2013-04-12 18:23:10 +02:00
|
|
|
if (typeof res.data.func == 'string' && typeof window[res.data.func] == 'function')
|
2011-09-13 19:27:02 +02:00
|
|
|
{
|
2013-04-12 18:23:10 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
window[res.data.func].apply(window, res.data.parms);
|
|
|
|
}
|
|
|
|
catch (e)
|
2011-09-13 19:27:02 +02:00
|
|
|
{
|
2013-04-12 18:23:10 +02:00
|
|
|
_egw_json_debug_log(e, {'Function': res.data.func, 'Parameters': res.data.parms});
|
2011-09-13 19:27:02 +02:00
|
|
|
}
|
2013-04-12 18:23:10 +02:00
|
|
|
hasResponse = true;
|
|
|
|
} else if (typeof res.data.func == "string" &&
|
|
|
|
res.data.func.substr(0,4) == "app." && window.app)
|
|
|
|
{
|
|
|
|
|
|
|
|
var parts = res.data.func.split(".");
|
|
|
|
if(parts.length == 3 && typeof window.app[parts[1]] == "object" &&
|
|
|
|
typeof window.app[parts[1]][parts[2]] == "function")
|
2013-04-09 16:39:56 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-04-12 18:23:10 +02:00
|
|
|
this.context = window.app[parts[1]][parts[2]].apply(window.app[parts[1]], res.data.parms);
|
2013-04-09 16:39:56 +02:00
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
_egw_json_debug_log(e, {'Function': res.data.func, 'Parameters': res.data.parms});
|
|
|
|
}
|
|
|
|
}
|
2013-04-12 18:23:10 +02:00
|
|
|
hasResponse = true;
|
|
|
|
|
|
|
|
} else
|
2011-09-13 19:27:02 +02:00
|
|
|
throw 'Invalid parameters';
|
|
|
|
break;
|
2010-06-21 10:39:21 +02:00
|
|
|
case 'jquery':
|
|
|
|
if (typeof res.data.select == 'string' &&
|
|
|
|
typeof res.data.func == 'string')
|
2010-06-11 10:08:07 +02:00
|
|
|
{
|
2010-06-21 10:39:21 +02:00
|
|
|
try
|
|
|
|
{
|
2011-07-03 11:00:36 +02:00
|
|
|
var jQueryObject = $j(res.data.select, this.context);
|
2010-06-21 10:39:21 +02:00
|
|
|
jQueryObject[res.data.func].apply(jQueryObject, res.data.parms);
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
2011-09-16 18:11:05 +02:00
|
|
|
_egw_json_debug_log(e, {'Function': res.data.func, 'Parameters': res.data.parms});
|
2010-06-21 10:39:21 +02:00
|
|
|
}
|
|
|
|
hasResponse = true;
|
|
|
|
} else
|
|
|
|
throw 'Invalid parameters';
|
|
|
|
break;
|
|
|
|
case 'redirect':
|
2011-05-31 22:12:51 +02:00
|
|
|
//console.log(res.data.url);
|
2010-06-21 10:39:21 +02:00
|
|
|
if (typeof res.data.url == 'string' &&
|
|
|
|
typeof res.data.global == 'boolean')
|
2010-06-11 10:08:07 +02:00
|
|
|
{
|
2010-06-21 10:39:21 +02:00
|
|
|
//Special handling for framework reload
|
2011-01-20 15:11:47 +01:00
|
|
|
res.data.global |= (res.data.url.indexOf("?cd=10") > 0);
|
2010-06-21 10:39:21 +02:00
|
|
|
|
|
|
|
if (res.data.global)
|
|
|
|
{
|
|
|
|
egw_topWindow().location.href = res.data.url;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-11 23:27:15 +02:00
|
|
|
egw_appWindowOpen(this.app, res.data.url);
|
2010-06-21 10:39:21 +02:00
|
|
|
}
|
2010-06-18 10:36:33 +02:00
|
|
|
|
2010-06-21 10:39:21 +02:00
|
|
|
hasResponse = true;
|
|
|
|
} else
|
|
|
|
throw 'Invalid parameters';
|
|
|
|
break;
|
|
|
|
case 'css':
|
|
|
|
if (typeof res.data == 'string')
|
2010-06-18 10:36:33 +02:00
|
|
|
{
|
2010-06-21 10:39:21 +02:00
|
|
|
//Check whether the requested file had already be included
|
|
|
|
if (!egw_json_files[res.data])
|
|
|
|
{
|
|
|
|
egw_json_files[res.data] = true;
|
|
|
|
|
|
|
|
//Get the head node and append a new link node with the stylesheet url to it
|
|
|
|
var headID = document.getElementsByTagName('head')[0];
|
|
|
|
var cssnode = document.createElement('link');
|
|
|
|
cssnode.type = "text/css";
|
|
|
|
cssnode.rel = "stylesheet";
|
|
|
|
cssnode.href = res.data;
|
|
|
|
headID.appendChild(cssnode);
|
|
|
|
}
|
|
|
|
hasResponse = true;
|
|
|
|
} else
|
|
|
|
throw 'Invalid parameters';
|
|
|
|
break;
|
|
|
|
case 'js':
|
|
|
|
if (typeof res.data == 'string')
|
2010-06-18 10:36:33 +02:00
|
|
|
{
|
2010-06-21 10:39:21 +02:00
|
|
|
//Check whether the requested file had already be included
|
|
|
|
if (!egw_json_files[res.data])
|
|
|
|
{
|
|
|
|
egw_json_files[res.data] = true;
|
|
|
|
|
|
|
|
//Get the head node and append a new script node with the js file to it
|
|
|
|
var headID = document.getElementsByTagName('head')[0];
|
|
|
|
var scriptnode = document.createElement('script');
|
|
|
|
scriptnode.type = "text/javascript";
|
|
|
|
scriptnode.src = res.data;
|
2010-07-20 12:20:32 +02:00
|
|
|
scriptnode._originalSrc = res.data;
|
2010-06-21 10:39:21 +02:00
|
|
|
headID.appendChild(scriptnode);
|
2010-07-20 12:20:32 +02:00
|
|
|
|
|
|
|
//Increment the includedJSFiles count
|
|
|
|
this.loadedJSFiles[res.data] = false;
|
|
|
|
|
2010-07-20 12:54:56 +02:00
|
|
|
if (typeof console != 'undefined' && typeof console.log != 'undefined')
|
2011-05-22 20:42:23 +02:00
|
|
|
console.log("Requested JS file '%s' from server", res.data);
|
2010-07-20 12:20:32 +02:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
//FF, Opera, Chrome
|
|
|
|
scriptnode.onload = function(e) {
|
|
|
|
var file = e.target._originalSrc;
|
2010-07-20 12:54:56 +02:00
|
|
|
if (typeof console != 'undefined' && typeof console.log != 'undefined')
|
2011-05-22 20:42:23 +02:00
|
|
|
console.log("Retrieved JS file '%s' from server", file);
|
2010-07-20 12:20:32 +02:00
|
|
|
|
|
|
|
self.loadedJSFiles[file] = true;
|
|
|
|
self.checkLoadFinish();
|
|
|
|
};
|
|
|
|
|
|
|
|
//IE
|
2010-07-21 12:26:54 +02:00
|
|
|
if (typeof scriptnode.readyState != 'undefined')
|
|
|
|
{
|
|
|
|
if (scriptnode.readyState != 'complete' &&
|
|
|
|
scriptnode.readyState != 'loaded')
|
|
|
|
{
|
|
|
|
scriptnode.onreadystatechange = function() {
|
|
|
|
var node = window.event.srcElement;
|
2010-07-21 12:52:18 +02:00
|
|
|
if (node.readyState == 'complete' || node.readyState == 'loaded') {
|
2010-07-21 12:26:54 +02:00
|
|
|
var file = node._originalSrc;
|
|
|
|
if (typeof console != 'undefined' && typeof console.log != 'undefined')
|
|
|
|
console.log("Retrieved JS file '%s' from server", [file]);
|
|
|
|
|
|
|
|
self.loadedJSFiles[file] = true;
|
|
|
|
self.checkLoadFinish();
|
|
|
|
}
|
|
|
|
};
|
2010-07-20 12:20:32 +02:00
|
|
|
}
|
2010-07-21 12:26:54 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
this.loadedJSFiles[res.data] = true;
|
|
|
|
}
|
2010-08-10 16:58:54 +02:00
|
|
|
}
|
2010-06-21 10:39:21 +02:00
|
|
|
}
|
|
|
|
hasResponse = true;
|
|
|
|
} else
|
|
|
|
throw 'Invalid parameters';
|
|
|
|
break;
|
|
|
|
case 'error':
|
|
|
|
if (typeof res.data == 'string')
|
|
|
|
{
|
|
|
|
this.jsonError(res.data);
|
|
|
|
hasResponse = true;
|
|
|
|
} else
|
|
|
|
throw 'Invalid parameters';
|
|
|
|
break;
|
|
|
|
}
|
2010-07-08 14:03:13 +02:00
|
|
|
|
|
|
|
//Try to handle the json response with all registered plugins
|
2011-08-15 16:26:37 +02:00
|
|
|
hasResponse |= _egw_json_plugin_handle(data.response[i].type, res, this.context);
|
2010-06-21 10:39:21 +02:00
|
|
|
} catch(e) {
|
|
|
|
this.jsonError('Internal JSON handler error', e);
|
2010-06-01 14:38:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no explicit response has been specified, call the callback (if one was set) */
|
2010-08-10 16:58:54 +02:00
|
|
|
if (!hasResponse && this.callback && data.response[i])
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
|
|
|
this.callback.call(this.sender, data.response[i].data);
|
|
|
|
}
|
2010-07-20 12:20:32 +02:00
|
|
|
|
|
|
|
this.handleResponseDone = true;
|
|
|
|
|
|
|
|
this.checkLoadFinish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-20 12:54:56 +02:00
|
|
|
/**
|
|
|
|
* The "onLoadFinish" handler gets called after all JS-files have been loaded
|
|
|
|
* successfully
|
|
|
|
*/
|
2010-07-20 12:20:32 +02:00
|
|
|
egw_json_request.prototype.checkLoadFinish = function()
|
|
|
|
{
|
|
|
|
var complete = true;
|
2011-05-22 20:42:23 +02:00
|
|
|
for (var key in this.loadedJSFiles) {
|
2010-07-20 12:20:32 +02:00
|
|
|
complete = complete && this.loadedJSFiles[key];
|
2011-05-22 20:42:23 +02:00
|
|
|
}
|
2010-07-20 12:20:32 +02:00
|
|
|
|
|
|
|
if (complete && this.onLoadFinish && this.handleResponseDone)
|
|
|
|
{
|
|
|
|
this.onLoadFinish.call(this.sender);
|
2010-06-01 14:38:41 +02:00
|
|
|
}
|
|
|
|
}
|
2010-06-09 15:24:59 +02:00
|
|
|
|
2010-06-15 17:27:04 +02:00
|
|
|
function egw_json_getFormValues(_form, _filterClass)
|
2010-06-15 11:47:39 +02:00
|
|
|
{
|
2013-09-10 21:39:20 +02:00
|
|
|
egw.debug("warn", "Function %s is deprecated", arguments.callee.name);
|
2010-06-15 11:47:39 +02:00
|
|
|
var elem = null;
|
|
|
|
if (typeof _form == 'object')
|
|
|
|
{
|
|
|
|
elem = _form;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
elem = document.getElementsByName(_form)[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
var serialized = new Object;
|
|
|
|
if (typeof elem != "undefined" && elem && elem.childNodes)
|
|
|
|
{
|
2010-06-15 17:27:04 +02:00
|
|
|
if (typeof _filterClass == 'undefined')
|
2010-06-16 09:30:16 +02:00
|
|
|
_filterClass = null;
|
2010-06-15 17:27:04 +02:00
|
|
|
|
|
|
|
_egw_json_getFormValues(serialized, elem.childNodes, _filterClass)
|
2010-06-15 11:47:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return serialized;
|
|
|
|
}
|
2010-06-09 15:24:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deprecated legacy xajax wrapper functions for the new egw_json interface
|
|
|
|
*/
|
|
|
|
_xajax_doXMLHTTP = function(_async, _menuaction, _arguments)
|
|
|
|
{
|
2013-09-10 21:39:20 +02:00
|
|
|
egw.debug("warn", "Function %s is deprecated", arguments.callee.name);
|
2010-06-10 09:18:39 +02:00
|
|
|
/* Assemble the parameter array */
|
2010-06-09 15:24:59 +02:00
|
|
|
var paramarray = new Array();
|
|
|
|
for (var i = 1; i < _arguments.length; i++)
|
|
|
|
{
|
|
|
|
paramarray[paramarray.length] = _arguments[i];
|
|
|
|
}
|
|
|
|
|
2010-06-10 09:18:39 +02:00
|
|
|
/* Create a new request, passing the menuaction and the parameter array */
|
2010-06-09 15:24:59 +02:00
|
|
|
var request = new egw_json_request(_menuaction, paramarray);
|
|
|
|
|
2010-06-10 08:59:54 +02:00
|
|
|
/* Send the request */
|
2010-06-09 15:24:59 +02:00
|
|
|
request.sendRequest(_async);
|
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
|
|
|
xajax_doXMLHTTP = function(_menuaction)
|
|
|
|
{
|
|
|
|
return _xajax_doXMLHTTP(true, _menuaction, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
xajax_doXMLHTTPsync = function(_menuaction)
|
|
|
|
{
|
|
|
|
return _xajax_doXMLHTTP(false, _menuaction, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
window.xajax = {
|
|
|
|
"getFormValues": function(_form)
|
2010-06-10 11:33:00 +02:00
|
|
|
{
|
2010-06-15 11:47:39 +02:00
|
|
|
return egw_json_getFormValues(_form);
|
2010-06-10 11:33:00 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
The following code is adapted from the xajax project which is licensed under
|
|
|
|
the following license
|
|
|
|
@copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
|
|
|
|
@copyright Copyright (c) 2008-2009 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson
|
|
|
|
@license http://www.xajaxproject.org/bsd_license.txt BSD License
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* used internally by the legacy "egw_json_response.getFormValues" to recursively
|
|
|
|
* run over all form elements
|
|
|
|
* @param serialized is the object which will contain the form data
|
|
|
|
* @param children is the children node of the form we're runing over
|
2010-06-16 09:30:16 +02:00
|
|
|
* @param string _filterClass if given only return
|
2010-06-10 11:33:00 +02:00
|
|
|
*/
|
2010-06-15 17:27:04 +02:00
|
|
|
function _egw_json_getFormValues(serialized, children, _filterClass)
|
2010-06-10 11:33:00 +02:00
|
|
|
{
|
2013-09-10 21:39:20 +02:00
|
|
|
egw.debug("warn", "Function %s is deprecated", arguments.callee.name);
|
2010-06-16 09:30:16 +02:00
|
|
|
//alert('_egw_json_getFormValues(,,'+_filterClass+')');
|
2010-06-10 11:33:00 +02:00
|
|
|
for (var i = 0; i < children.length; ++i) {
|
|
|
|
var child = children[i];
|
|
|
|
|
|
|
|
if (typeof child.childNodes != "undefined")
|
2010-06-16 09:30:16 +02:00
|
|
|
_egw_json_getFormValues(serialized, child.childNodes, _filterClass);
|
2010-06-10 11:33:00 +02:00
|
|
|
|
2011-07-03 11:00:36 +02:00
|
|
|
if ((!_filterClass || $j(child).hasClass(_filterClass)) && typeof child.name != "undefined")
|
2010-06-16 09:30:16 +02:00
|
|
|
{
|
|
|
|
//alert('_egw_json_getFormValues(,,'+_filterClass+') calling _egw_json_getFormValue for name='+child.name+', class='+child.class+', value='+child.value);
|
2010-06-15 17:27:04 +02:00
|
|
|
_egw_json_getFormValue(serialized, child);
|
2010-06-16 09:30:16 +02:00
|
|
|
}
|
2010-06-10 11:33:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-11 12:23:13 +02:00
|
|
|
function _egw_json_getObjectLength(_obj)
|
|
|
|
{
|
|
|
|
var res = 0;
|
|
|
|
for (key in _obj)
|
|
|
|
{
|
|
|
|
if (_obj.hasOwnProperty(key))
|
|
|
|
res++;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-06-10 11:33:00 +02:00
|
|
|
/**
|
|
|
|
* used internally to serialize
|
|
|
|
*/
|
|
|
|
function _egw_json_getFormValue(serialized, child)
|
|
|
|
{
|
|
|
|
//Return if the child doesn't have a name, is disabled, or is a radio-/checkbox and not checked
|
2010-06-10 14:24:50 +02:00
|
|
|
if ((typeof child.name == "undefined") || (child.disabled && child.disabled == true) ||
|
2010-10-12 15:56:40 +02:00
|
|
|
(child.type && (child.type == 'radio' || child.type == 'checkbox' || child.type == 'button' || child.type == 'submit') && (!child.checked)))
|
2010-06-10 11:33:00 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var name = child.name;
|
2010-06-10 12:33:00 +02:00
|
|
|
var values = null;
|
2010-06-10 11:33:00 +02:00
|
|
|
|
|
|
|
if ('select-multiple' == child.type)
|
|
|
|
{
|
2010-06-10 12:33:00 +02:00
|
|
|
values = new Array;
|
2010-06-10 11:33:00 +02:00
|
|
|
for (var j = 0; j < child.length; ++j)
|
2010-06-09 15:24:59 +02:00
|
|
|
{
|
2010-06-10 11:33:00 +02:00
|
|
|
var option = child.options[j];
|
|
|
|
if (option.selected == true)
|
|
|
|
values.push(option.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
values = child.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Special treatment if the name of the child contains a [] - then all theese
|
|
|
|
//values are added to an array.
|
|
|
|
var keyBegin = name.indexOf('[');
|
|
|
|
if (0 <= keyBegin) {
|
|
|
|
var n = name;
|
|
|
|
var k = n.substr(0, n.indexOf('['));
|
|
|
|
var a = n.substr(n.indexOf('['));
|
|
|
|
if (typeof serialized[k] == 'undefined')
|
2010-06-10 12:33:00 +02:00
|
|
|
serialized[k] = new Object;
|
2010-06-11 12:23:13 +02:00
|
|
|
|
2010-06-10 11:33:00 +02:00
|
|
|
var p = serialized; // pointer reset
|
|
|
|
while (a.length != 0) {
|
|
|
|
var sa = a.substr(0, a.indexOf(']')+1);
|
|
|
|
|
|
|
|
var lk = k; //save last key
|
|
|
|
var lp = p; //save last pointer
|
|
|
|
|
|
|
|
a = a.substr(a.indexOf(']')+1);
|
|
|
|
p = p[k];
|
|
|
|
k = sa.substr(1, sa.length-2);
|
|
|
|
if (k == '') {
|
|
|
|
if ('select-multiple' == child.type) {
|
|
|
|
k = lk; //restore last key
|
|
|
|
p = lp;
|
|
|
|
} else {
|
2010-06-11 12:23:13 +02:00
|
|
|
k = _egw_json_getObjectLength(p);
|
2010-06-10 11:33:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (typeof p[k] == 'undefined')
|
2010-06-11 12:23:13 +02:00
|
|
|
{
|
2010-06-10 12:33:00 +02:00
|
|
|
p[k] = new Object;
|
2010-06-11 12:23:13 +02:00
|
|
|
}
|
2010-06-09 15:24:59 +02:00
|
|
|
}
|
2010-06-10 11:33:00 +02:00
|
|
|
p[k] = values;
|
|
|
|
} else {
|
|
|
|
//Add the value to the result object with the given name
|
|
|
|
if (typeof values != "undefined")
|
2010-06-09 15:24:59 +02:00
|
|
|
{
|
2010-06-10 11:33:00 +02:00
|
|
|
serialized[name] = values;
|
2010-06-09 15:24:59 +02:00
|
|
|
}
|
|
|
|
}
|
2010-06-10 11:33:00 +02:00
|
|
|
}
|