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
|
|
|
*/
|
|
|
|
|
|
|
|
/* The egw_json_request is the javaScript side implementation of class.egw_json.inc.php.*/
|
|
|
|
|
2010-06-11 12:23:13 +02:00
|
|
|
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 12:23:13 +02:00
|
|
|
return '"' + 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)
|
|
|
|
{
|
|
|
|
if (!input) return 'null';
|
2010-06-09 15:24:59 +02:00
|
|
|
|
2010-06-11 12:23:13 +02:00
|
|
|
var simple_res = egw_json_encode_simple(input);
|
|
|
|
if (simple_res == null)
|
|
|
|
{
|
|
|
|
switch (input.constructor)
|
|
|
|
{
|
|
|
|
case Array:
|
|
|
|
var buf = [];
|
|
|
|
for (var k in input)
|
|
|
|
{
|
|
|
|
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(',') + '}';
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 'null';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return simple_res;
|
2010-06-09 15:24:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-01 14:38:41 +02:00
|
|
|
/* The constructor of the egw_json_request class.
|
|
|
|
* @param string _menuaction the menuaction function which should be called and which handles the actual request
|
|
|
|
* @param array _parameters which should be passed to the menuaction function.
|
|
|
|
*/
|
2010-06-02 10:10:43 +02:00
|
|
|
function egw_json_request(_menuaction, _parameters)
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
|
|
|
//Copy the supplied parameters
|
|
|
|
this.menuaction = _menuaction;
|
2010-06-09 15:24:59 +02:00
|
|
|
|
|
|
|
if (typeof _parameters != 'undefined')
|
|
|
|
{
|
|
|
|
this.parameters = _parameters;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.parameters = new Array;
|
|
|
|
}
|
|
|
|
|
2010-06-09 12:40:17 +02:00
|
|
|
var url = window.egw_webserverUrl;
|
|
|
|
|
|
|
|
// Search up to parent if the current window is in a frame
|
|
|
|
if(typeof url == "undefined")
|
|
|
|
{
|
|
|
|
url = top.egw_webserverUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.url = url + '/json.php';
|
2010-06-09 15:24:59 +02:00
|
|
|
|
2010-06-01 14:38:41 +02:00
|
|
|
this.sender = null;
|
|
|
|
this.callback = null;
|
|
|
|
this.alertHandler = this.alertFunc;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sends the AJAX JSON request.
|
|
|
|
* @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
|
|
|
|
* @param _sender is the reference object the callback function should get
|
|
|
|
*/
|
|
|
|
egw_json_request.prototype.sendRequest = function(_async, _callback, _sender)
|
|
|
|
{
|
|
|
|
//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 = {
|
|
|
|
"json_data": egw_json_encode(
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
2010-06-09 15:24:59 +02:00
|
|
|
"request": {
|
|
|
|
"parameters": this.parameters
|
|
|
|
}
|
|
|
|
})
|
2010-06-01 14:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Send the request via the jquery AJAX interface to the server
|
2010-06-01 16:20:55 +02:00
|
|
|
$.ajax({url: this.url + '?menuaction=' + this.menuaction,
|
2010-06-01 14:38:41 +02:00
|
|
|
async: is_async,
|
|
|
|
context: this,
|
|
|
|
data: request_obj,
|
|
|
|
dataType: 'json',
|
|
|
|
type: 'POST',
|
|
|
|
success: this.handleResponse});
|
|
|
|
}
|
|
|
|
|
2010-06-10 11:33:00 +02:00
|
|
|
egw_json_request.prototype.getFormValues = function(_form)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
_egw_json_getFormValues(serialized, elem.childNodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
return serialized;
|
|
|
|
}
|
|
|
|
|
2010-06-01 14:38:41 +02:00
|
|
|
egw_json_request.prototype.alertFunc = function(_message, _details)
|
|
|
|
{
|
|
|
|
alert(_message);
|
|
|
|
}
|
|
|
|
|
2010-06-10 14:24:50 +02:00
|
|
|
function _egw_json_debug_log(_msg)
|
|
|
|
{
|
|
|
|
if (typeof console != "undefined" && typeof console.log != "undefined")
|
|
|
|
{
|
|
|
|
console.log(_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-06-04 17:22:01 +02:00
|
|
|
if (data && data.response)
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
|
|
|
var hasResponse = false;
|
|
|
|
for (var i = 0; i < data.response.length; i++)
|
|
|
|
{
|
2010-06-10 14:24:50 +02:00
|
|
|
var res = data.response[i];
|
|
|
|
|
2010-06-01 14:38:41 +02:00
|
|
|
switch (data.response[i].type)
|
|
|
|
{
|
|
|
|
case 'alert':
|
|
|
|
//Check whether all needed parameters have been passed and call the alertHandler function
|
2010-06-10 14:24:50 +02:00
|
|
|
if ((typeof res.data.message != 'undefined') &&
|
|
|
|
(typeof res.data.details != 'undefined'))
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
|
|
|
this.alertHandler(
|
2010-06-10 14:24:50 +02:00
|
|
|
res.data.message,
|
|
|
|
res.data.details)
|
2010-06-01 14:38:41 +02:00
|
|
|
hasResponse = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'assign':
|
|
|
|
//Check whether all needed parameters have been passed and call the alertHandler function
|
2010-06-10 14:24:50 +02:00
|
|
|
if ((typeof res.data.id != 'undefined') &&
|
|
|
|
(typeof res.data.key != 'undefined') &&
|
|
|
|
(typeof res.data.value != 'undefined'))
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
2010-06-10 14:24:50 +02:00
|
|
|
var obj = document.getElementById(res.data.id);
|
2010-06-01 14:38:41 +02:00
|
|
|
if (obj)
|
|
|
|
{
|
2010-06-10 14:24:50 +02:00
|
|
|
obj[res.data.key] = res.data.value;
|
2010-06-09 15:24:59 +02:00
|
|
|
hasResponse = true;
|
2010-06-01 14:38:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'data':
|
|
|
|
//Callback the caller in order to allow him to handle the data
|
2010-06-01 16:20:55 +02:00
|
|
|
if (this.callback)
|
|
|
|
{
|
2010-06-10 14:24:50 +02:00
|
|
|
this.callback.call(this.sender, res.data);
|
2010-06-09 15:24:59 +02:00
|
|
|
hasResponse = true;
|
2010-06-01 16:20:55 +02:00
|
|
|
}
|
2010-06-01 14:38:41 +02:00
|
|
|
break;
|
|
|
|
case 'script':
|
2010-06-10 14:24:50 +02:00
|
|
|
if (typeof res.data == 'string')
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
2010-06-09 15:24:59 +02:00
|
|
|
try
|
|
|
|
{
|
2010-06-10 14:24:50 +02:00
|
|
|
var func = function() {eval(res.data);};
|
2010-06-09 15:24:59 +02:00
|
|
|
func.call(window);
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
2010-06-10 14:24:50 +02:00
|
|
|
e.code = res.data;
|
|
|
|
_egw_json_debug_log(e);
|
2010-06-09 15:24:59 +02:00
|
|
|
}
|
2010-06-01 14:38:41 +02:00
|
|
|
hasResponse = true;
|
|
|
|
}
|
|
|
|
break;
|
2010-06-10 14:24:50 +02:00
|
|
|
case 'jquery':
|
|
|
|
if (typeof res.data.select == 'string' &&
|
|
|
|
typeof res.data.func == 'string')
|
2010-06-09 16:41:40 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2010-06-10 14:24:50 +02:00
|
|
|
var jQueryObject = $(res.data.select);
|
|
|
|
jQueryObject[res.data.func].apply(jQueryObject, res.data.parms);
|
2010-06-09 16:41:40 +02:00
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
2010-06-10 14:24:50 +02:00
|
|
|
_egw_json_debug_log(e);
|
2010-06-09 16:41:40 +02:00
|
|
|
}
|
|
|
|
hasResponse = true;
|
|
|
|
}
|
|
|
|
break;
|
2010-06-02 10:10:43 +02:00
|
|
|
case 'redirect':
|
2010-06-11 10:08:07 +02:00
|
|
|
if (typeof res.data.url == 'string' &&
|
|
|
|
typeof res.data.global == 'boolean')
|
2010-06-02 10:10:43 +02:00
|
|
|
{
|
2010-06-11 10:08:07 +02:00
|
|
|
//Special handling for framework reload
|
|
|
|
if (res.data.url.indexOf("?cd=10") > 0)
|
|
|
|
res.data.global = true;
|
|
|
|
|
|
|
|
if (res.data.global)
|
|
|
|
{
|
|
|
|
egw_topWindow().location.href = res.data.url;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
window.location.href = res.data.url;
|
|
|
|
}
|
2010-06-02 10:10:43 +02:00
|
|
|
}
|
|
|
|
break;
|
2010-06-01 14:38:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no explicit response has been specified, call the callback (if one was set) */
|
|
|
|
if (!hasResponse && this.callback)
|
|
|
|
{
|
|
|
|
this.callback.call(this.sender, data.response[i].data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
{
|
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
|
|
|
{
|
|
|
|
return egw_json_request.prototype.getFormValues(_form);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
function _egw_json_getFormValues(serialized, children)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < children.length; ++i) {
|
|
|
|
var child = children[i];
|
|
|
|
|
|
|
|
if (typeof child.childNodes != "undefined")
|
|
|
|
_egw_json_getFormValues(serialized, child.childNodes);
|
|
|
|
|
|
|
|
_egw_json_getFormValue(serialized, child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-10 11:33:00 +02:00
|
|
|
(child.type && (child.type == 'radio' || child.type == 'checkbox') && (!child.checked)))
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|