new clientside API object currently supporting the following methods:

- egw.preferences(_name, _app='common') return preference _name of _app (only common prefs loaded currently)
- egw.open() allowing to open app-entries utilising the link registry, deprecating egw_open from jsapi.js
- egw.lang(_msg, _arg1, ..., _argN) placeholders are not yet implemented
This commit is contained in:
Ralf Becker 2011-08-23 14:29:56 +00:00
parent 76af8cb134
commit 24e34f7927
4 changed files with 243 additions and 86 deletions

View File

@ -94,6 +94,7 @@ abstract class egw_framework
'/phpgwapi/js/./egw_json.js',
// always include javascript helper functions
'/phpgwapi/js/jsapi/jsapi.js',
'/phpgwapi/js/jsapi/egw.js',
));
}
@ -757,18 +758,6 @@ abstract class egw_framework
$java_script .= "<script type=\"text/javascript\">\nvar enable_ie_dropdownmenuhack=1;\n</script>\n";
}
// set webserver_url for json
$java_script .= "<script type=\"text/javascript\">\nwindow.egw_webserverUrl = '".
($GLOBALS['egw_info']['server']['enforce_ssl'] && substr($GLOBALS['egw_info']['server']['webserver_url'],0,8) != 'https://' ? 'https://'.$_SERVER['HTTP_HOST'] : '').
$GLOBALS['egw_info']['server']['webserver_url']."';\n";
// add link registry to non-popup windows, if explicit requested (idots_framework::navbar() loads it, if not explicit specified!)
if ($GLOBALS['egw_info']['flags']['js_link_registry'])
{
$java_script .= 'window.egw_link_registry='.egw_link::json_registry().';';
}
$java_script .= "</script>\n";
/* this flag is for all javascript code that has to be put before other jscode.
Think of conf vars etc... (pim@lingewoud.nl) */
if (isset($GLOBALS['egw_info']['flags']['java_script_thirst']))
@ -778,6 +767,19 @@ abstract class egw_framework
$java_script .= self::get_script_links();
// set webserver_url for json
$java_script .= "<script type=\"text/javascript\">\nwindow.egw_webserverUrl = '".
($GLOBALS['egw_info']['server']['enforce_ssl'] && substr($GLOBALS['egw_info']['server']['webserver_url'],0,8) != 'https://' ? 'https://'.$_SERVER['HTTP_HOST'] : '').
$GLOBALS['egw_info']['server']['webserver_url']."';\n";
// add link registry to non-popup windows, if explicit requested (idots_framework::navbar() loads it, if not explicit specified!)
if ($GLOBALS['egw_info']['flags']['js_link_registry'])
{
$java_script .= 'egw.set_link_registry('.egw_link::json_registry().");\n";
$java_script .= 'egw.set_preferences('.json_encode($GLOBALS['egw_info']['user']['preferences']['common']).', "common");';
}
$java_script .= "</script>\n";
if(@isset($_GET['menuaction']))
{
list($app,$class,$method) = explode('.',$_GET['menuaction']);

220
phpgwapi/js/jsapi/egw.js Normal file
View File

@ -0,0 +1,220 @@
/**
* 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$
*/
"use strict";
var egw;
/**
* Central object providing all kinds of api services on clientside:
* - preferences
* - configuration
* - link registry
*/
if (window.opener && typeof window.opener.egw == 'object')
{
egw = window.opener.egw;
}
else if (window.top == 'object' && window.top.egw == 'object')
{
egw = window.top.egw;
}
else
{
egw = {
/**
* Object holding the prefences as 2-dim. associative array, use egw.preference(name[,app]) to access it
*/
prefs: {
common: {
dateformat: "Y-m-d",
timeformat: 24,
lang: "en"
}
},
/**
* Setting prefs for an app or 'common'
*
* @param object _data object with name: value pairs to set
* @param string _app application name, 'common' or undefined to prefes of all apps at once
*/
set_preferences: function(_data, _app)
{
if (typeof _app == 'undefined')
{
this.prefs = _data;
}
else
{
this.prefs[_app] = _data;
}
},
/**
* Query an EGroupware user preference
*
* @param string _name name of the preference, eg. 'dateformat'
* @param string _app='common'
* @return string preference value
*/
preference: function(_name, _app)
{
if (typeof app == 'undefined') _app = 'common';
if (typeof this.prefs[_app] == 'undefined')
{
throw 'Prefs for application "'+_app+'" are NOT loaded!';
}
return this.prefs[_app][_name];
},
/**
* Translations
*/
lang_arr: {},
/**
* Set translation for a given application
*
* @param string _app
* @param object _message message => translation pairs
*/
set_lang_arr: function(_app, _messages)
{
this.lang_arr[_app] = _messages;
},
/**
* Translate a given phrase replacing optional placeholders
*
* @param string _msg message to translate
* @param string _arg1 ... _argN
*/
lang: function(_msg, _arg1)
{
return _msg;
},
/**
* View an EGroupware entry: opens a popup of correct size or redirects window.location to requested url
*
* Examples:
* - egw.open(123,'infolog') or egw.open('infolog:123') opens popup to edit or view (if no edit rights) infolog entry 123
* - egw.open('infolog:123','timesheet','add') opens popup to add new timesheet linked to infolog entry 123
* - egw.open(123,'addressbook','view') opens addressbook view for entry 123 (showing linked infologs)
* - egw.open('','addressbook','view_list',{ search: 'Becker' }) opens list of addresses containing 'Becker'
*
* @param string|int id either just the id or "app:id" if app==""
* @param string app app-name or empty (app is part of id)
* @param string type default "edit", possible "view", "view_list", "edit" (falls back to "view") and "add"
* @param object|string extra extra url parameters to append as object or string
* @param string target target of window to open
*/
open: function(id, app, type, extra, target)
{
if (typeof this.link_registry != 'object')
{
alert('egw.open() link registry is NOT defined!');
return;
}
if (!app)
{
var app_id = id.split(':',2);
app = app_id[0];
id = app_id[1];
}
if (!app || typeof this.link_registry[app] != 'object')
{
alert('egw.open() app "'+app+'" NOT defined in link registry!');
return;
}
var app_registry = this.link_registry[app];
if (typeof type == 'undefined') type = 'edit';
if (type == 'edit' && typeof app_registry.edit == 'undefined') type = 'view';
if (typeof app_registry[type] == 'undefined')
{
alert('egw.open() type "'+type+'" is NOT defined in link registry for app "'+app+'"!');
return;
}
var url = egw_webserverUrl+'/index.php';
var delimiter = '?';
var params = app_registry[type];
if (type == 'view' || type == 'edit') // add id parameter for type view or edit
{
params[app_registry[type+'_id']] = id;
}
else if (type == 'add' && id) // add add_app and app_id parameters, if given for add
{
var app_id = id.split(':',2);
params[app_registry.add_app] = app_id[0];
params[app_registry.add_id] = app_id[1];
}
for(var attr in params)
{
url += delimiter+attr+'='+encodeURIComponent(params[attr]);
delimiter = '&';
}
if (typeof extra == 'object')
{
for(var attr in extra)
{
url += delimiter+attr+'='+encodeURIComponent(extra[attr]);
}
}
else if (typeof extra == 'string')
{
url += delimiter + extra;
}
if (typeof app_registry[type+'_popup'] == 'undefined')
{
if (target)
{
window.open(url, target);
}
else
{
egw_appWindowOpen(app, url);
}
}
else
{
var w_h = app_registry[type+'_popup'].split('x');
if (w_h[1] == 'egw_getWindowOuterHeight()') w_h[1] = egw_getWindowOuterHeight();
egw_openWindowCentered2(url, target, w_h[0], w_h[1], 'yes', app, false);
}
},
/**
* Link registry
*/
link_registry: null,
/**
* Set link registry
*
* @param object _registry whole registry or entries for just one app
* @param string _app
*/
set_link_registry: function (_registry, _app)
{
if (typeof _app == 'undefined')
{
this.link_registry = _registry;
}
else
{
this.link_registry[_app] = _registry;
}
}
};
}

View File

@ -261,80 +261,11 @@ function egw_refresh(_msg, _app, _id, _type, _targetapp, _replace, _with)
* @param string type default "edit", possible "view", "view_list", "edit" (falls back to "view") and "add"
* @param object|string extra extra url parameters to append as object or string
* @param string target target of window to open
* @deprecated use egw.open()
*/
function egw_open(id, app, type, extra, target)
{
var registry = egw_topWindow().egw_link_registry;
if (typeof registry != 'object')
{
alert('egw_open() link registry is NOT defined!');
return;
}
if (!app)
{
var app_id = id.split(':',2);
app = app_id[0];
id = app_id[1];
}
if (!app || typeof registry[app] != 'object')
{
alert('egw_open() app "'+app+'" NOT defined in link registry!');
return;
}
var app_registry = registry[app];
if (typeof type == 'undefined') type = 'edit';
if (type == 'edit' && typeof app_registry.edit == 'undefined') type = 'view';
if (typeof app_registry[type] == 'undefined')
{
alert('egw_open() type "'+type+'" is NOT defined in link registry for app "'+app+'"!');
return;
}
var url = egw_webserverUrl+'/index.php';
var delimiter = '?';
var params = app_registry[type];
if (type == 'view' || type == 'edit') // add id parameter for type view or edit
{
params[app_registry[type+'_id']] = id;
}
else if (type == 'add' && id) // add add_app and app_id parameters, if given for add
{
var app_id = id.split(':',2);
params[app_registry.add_app] = app_id[0];
params[app_registry.add_id] = app_id[1];
}
for(var attr in params)
{
url += delimiter+attr+'='+encodeURIComponent(params[attr]);
delimiter = '&';
}
if (typeof extra == 'object')
{
for(var attr in extra)
{
url += delimiter+attr+'='+encodeURIComponent(extra[attr]);
}
}
else if (typeof extra == 'string')
{
url += delimiter + extra;
}
if (typeof app_registry[type+'_popup'] == 'undefined')
{
if (target)
{
window.open(url, target);
}
else
{
egw_appWindowOpen(app, url);
}
}
else
{
var w_h = app_registry[type+'_popup'].split('x');
if (w_h[1] == 'egw_getWindowOuterHeight()') w_h[1] = egw_getWindowOuterHeight();
egw_openWindowCentered2(url, target, w_h[0], w_h[1], 'yes', app, false);
}
window.egw.open();
}
window.egw_getFramework = function()

View File

@ -87,7 +87,7 @@ class idots_framework extends egw_framework
// make sure header is output only once
if (self::$header_done) return '';
self::$header_done = true;
error_log(__METHOD__."() this->tpl=".array2string($this->tpl).' '.function_backtrace());
// add a content-type header to overwrite an existing default charset in apache (AddDefaultCharset directiv)
header('Content-type: text/html; charset='.translation::charset());
@ -97,7 +97,8 @@ class idots_framework extends egw_framework
// the instanciation of the template has to be here and not in the constructor,
// as the old Template class has problems if restored from the session (php-restore)
if (!is_object($this->tpl)) $this->tpl = new Template(EGW_TEMPLATE_DIR,'keep');
if (!is_object($this->tpl)) ;
$this->tpl = new Template(EGW_TEMPLATE_DIR,'keep');
$this->tpl->set_file(array('_head' => 'head.tpl'));
$this->tpl->set_block('_head','head');
@ -162,7 +163,10 @@ class idots_framework extends egw_framework
// add link registry to non-popup windows
if (!isset($GLOBALS['egw_info']['flags']['js_link_registry']))
{
$content .= '<script type="text/javascript">'."\nwindow.egw_link_registry=".egw_link::json_registry().";\n</script>\n";
$content .= '<script type="text/javascript">
egw.set_link_registry('.egw_link::json_registry().');
egw.set_preferences('.json_encode($GLOBALS['egw_info']['user']['preferences']['common']).', "common");
</script>'."\n";
}
if($GLOBALS['egw_info']['user']['preferences']['common']['show_general_menu'] != 'sidebox' && !html::$ua_mobile)
{