Synchronized json code from trunk with 10.1

This commit is contained in:
Andreas Stöckel 2010-06-11 08:38:36 +00:00
parent 911bd7bd00
commit 327324c95e
2 changed files with 57 additions and 36 deletions

View File

@ -279,14 +279,14 @@ class egw_json_response
*
* @param string $script the script code which should be executed upon receiving
*/
public function each($selector,$method,array $parameters=array())
public function jquery($selector,$method,array $parameters=array())
{
if (is_string($selector) && is_string($method))
{
$this->addGeneric('each', array(
$this->addGeneric('jquery', array(
'select' => $selector,
'func' => $method,
'params' => $parameters,
'parms' => $parameters,
));
}
else
@ -323,10 +323,16 @@ class egw_json_response
*
* @param string $url
*/
public function redirect($url)
public function redirect($url, $global = false)
{
//self::script("location.href = '$url';");
$this->addGeneric('redirect', $url);
if (is_string($url) && is_bool($global))
{
//self::script("location.href = '$url';");
$this->addGeneric('redirect', array(
'url' => $url,
'global' => $global,
));
}
}
/**

View File

@ -25,7 +25,7 @@ function egw_json_encode(input)
case Boolean:
return input ? 'true' : 'false';
case Array :
case Array:
var buf = [];
for (var i = 0; i < input.length; i++)
buf.push(egw_json_encode(input[i]));
@ -143,6 +143,14 @@ egw_json_request.prototype.alertFunc = function(_message, _details)
alert(_message);
}
function _egw_json_debug_log(_msg)
{
if (typeof console != "undefined" && typeof console.log != "undefined")
{
console.log(_msg);
}
}
/* Internal function which handles the response from the server */
egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRequest)
{
@ -151,29 +159,31 @@ egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRe
var hasResponse = false;
for (var i = 0; i < data.response.length; i++)
{
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 data.response[i].data.message != 'undefined') &&
(typeof data.response[i].data.details != 'undefined'))
if ((typeof res.data.message != 'undefined') &&
(typeof res.data.details != 'undefined'))
{
this.alertHandler(
data.response[i].data.message,
data.response[i].data.details)
res.data.message,
res.data.details)
hasResponse = true;
}
break;
case 'assign':
//Check whether all needed parameters have been passed and call the alertHandler function
if ((typeof data.response[i].data.id != 'undefined') &&
(typeof data.response[i].data.key != 'undefined') &&
(typeof data.response[i].data.value != 'undefined'))
if ((typeof res.data.id != 'undefined') &&
(typeof res.data.key != 'undefined') &&
(typeof res.data.value != 'undefined'))
{
var obj = document.getElementById(data.response[i].data.id);
var obj = document.getElementById(res.data.id);
if (obj)
{
obj[data.response[i].data.key] = data.response[i].data.value;
obj[res.data.key] = res.data.value;
hasResponse = true;
}
}
@ -182,53 +192,58 @@ egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRe
//Callback the caller in order to allow him to handle the data
if (this.callback)
{
this.callback.call(this.sender, data.response[i].data);
this.callback.call(this.sender, res.data);
hasResponse = true;
}
break;
case 'script':
if (typeof data.response[i].data == 'string')
if (typeof res.data == 'string')
{
try
{
var func = function() {eval(data.response[i].data);};
var func = function() {eval(res.data);};
func.call(window);
}
catch (e)
{
if (typeof console != "undefined" && typeof console.log != "undefined")
{
e.code = data.response[i].data;
console.log(e);
}
e.code = res.data;
_egw_json_debug_log(e);
}
hasResponse = true;
}
break;
case 'each':
if (typeof data.response[i].select == 'string' && typeof data.response[i].func == 'string')
case 'jquery':
if (typeof res.data.select == 'string' &&
typeof res.data.func == 'string')
{
try
{
var func = data.response[i].func;
// todo: for N>2
$(data.response[i].select).each(func.call(data.response[i].parms[0],data.response[i].parms[1],data.response[i].parms[2]));
var jQueryObject = $(res.data.select);
jQueryObject[res.data.func].apply(jQueryObject, res.data.parms);
}
catch (e)
{
if (typeof console != "undefined" && typeof console.log != "undefined")
{
e.code = data.response[i];
console.log(e);
}
_egw_json_debug_log(e);
}
hasResponse = true;
}
break;
case 'redirect':
if (typeof data.response[i].data == 'string')
if (typeof res.data.url == 'string' &&
typeof res.data.global == 'boolean')
{
window.location.href = data.response[i].data;
//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;
}
}
break;
}