Added includeScript and includeCSS functions to json classes

This commit is contained in:
Andreas Stöckel 2010-06-18 08:36:33 +00:00
parent 7d44c81805
commit 3da1f7b585
2 changed files with 88 additions and 3 deletions

View File

@ -317,6 +317,8 @@ class egw_json_response
* Redirect to given url
*
* @param string $url
* @param boolean $global specifies whether to redirect the whole framework
* or only the current application
*/
public function redirect($url, $global = false)
{
@ -330,6 +332,34 @@ class egw_json_response
}
}
/**
* Includes the given CSS file. Every url can only be included once.
*
* @param string $url specifies the url to the css file to include
*/
public function includeCSS($url)
{
if (is_string($url))
{
$this->addGeneric('css', $url);
}
}
/**
* Includes the given JS file. Every url can only be included once.
*
* @param string $url specifies the url to the css file to include
*/
public function includeScript($url)
{
if (is_string($url))
{
$this->addGeneric('js', $url);
}
}
/**
* Returns the actual JSON code generated by calling the above "add" function.
*
@ -382,7 +412,18 @@ class xajaxResponse extends egw_json_response
$args = func_get_args();
$func = array_shift($args);
$this->script("window['".$func."'].apply(window, ".json_encode($args).");");
$this->script("try{window['".$func."'].apply(window, ".json_encode($args).");} catch(e) {_egw_json_debug_log(e);}");
}
public function addIncludeCSS($url)
{
$this->includeCSS($url);
}
public function addIncludeScript($url)
{
$this->includeScript($url);
}
public function getXML()

View File

@ -116,7 +116,12 @@ function egw_json_encode(input)
}
/* The constructor of the egw_json_request class.
/**
* Some variables needed to store which JS and CSS files have already be included
*/
var egw_json_files = {};
/** 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.
*/
@ -296,6 +301,45 @@ egw_json_request.prototype.handleResponse = function(data, textStatus, XMLHttpRe
{
window.location.href = res.data.url;
}
hasResponse = true;
}
break;
case 'css':
if (typeof res.data == 'string')
{
//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;
}
break;
case 'js':
if (typeof res.data == 'string')
{
//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;
headID.appendChild(scriptnode);
}
hasResponse = true;
}
break;
}