mirror of
https://github.com/EGroupware/egroupware.git
synced 2024-12-28 17:48:56 +01:00
Finished egw json api
This commit is contained in:
parent
d310b14ecf
commit
1bc16c1b8e
@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
/*egw:uses
|
/*egw:uses
|
||||||
egw_core;
|
egw_core;
|
||||||
|
egw_debug;
|
||||||
egw_preferences;
|
egw_preferences;
|
||||||
egw_lang;
|
egw_lang;
|
||||||
egw_links;
|
egw_links;
|
||||||
@ -22,5 +23,6 @@
|
|||||||
egw_images;
|
egw_images;
|
||||||
egw_jsonq;
|
egw_jsonq;
|
||||||
egw_files;
|
egw_files;
|
||||||
|
egw_json;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
egw_debug;
|
egw_debug;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
egw.extend('files', egw.MODULE_GLOBAL, function() {
|
egw.extend('files', egw.MODULE_WND_LOCAL, function() {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Array which contains all currently bound in javascript and css files.
|
* Array which contains all currently bound in javascript and css files.
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
egw_core;
|
egw_core;
|
||||||
egw_utils;
|
egw_utils;
|
||||||
egw_files;
|
egw_files;
|
||||||
|
egw_debug;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
egw.extend('json', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
|
egw.extend('json', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
|
||||||
@ -33,49 +34,87 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
|
|||||||
/**
|
/**
|
||||||
* Internal implementation of the JSON request object.
|
* Internal implementation of the JSON request object.
|
||||||
*/
|
*/
|
||||||
function json_request(_menuaction, _parameters, _callback, _context, _egw)
|
function json_request(_menuaction, _parameters, _callback, _context,
|
||||||
|
_sender, _async, _egw)
|
||||||
{
|
{
|
||||||
// Initialize undefined parameters
|
|
||||||
if (typeof _parameters === 'undefined')
|
|
||||||
{
|
|
||||||
_parameters = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof _callback === 'undefined')
|
|
||||||
{
|
|
||||||
_callback = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof _context === 'undefined')
|
|
||||||
{
|
|
||||||
_context = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy the parameters
|
// Copy the parameters
|
||||||
this.parameters = _parameters;
|
this.url = _egw.ajaxUrl(_menuaction);
|
||||||
|
this.parameters = _parameters ? _parameters : [];
|
||||||
|
this.async = _async ? _async : false;
|
||||||
|
this.callback = _callback ? _callback : null;
|
||||||
|
this.context = _context ? _context : null;
|
||||||
|
this.sender = _sender ? _sender : null;
|
||||||
this.egw = _egw;
|
this.egw = _egw;
|
||||||
this.callback = _callback;
|
|
||||||
this.context = _context;
|
|
||||||
|
|
||||||
|
// We currently don't have a request object
|
||||||
this.request = null;
|
this.request = null;
|
||||||
this.sender = null;
|
|
||||||
this.callback = null;
|
|
||||||
|
|
||||||
|
// Some variables needed for notification about a JS files done loading
|
||||||
this.onLoadFinish = null;
|
this.onLoadFinish = null;
|
||||||
this.jsFiles = 0;
|
this.jsFiles = 0;
|
||||||
this.jsCount = 0;
|
this.jsCount = 0;
|
||||||
|
|
||||||
this.alertHandler = this.alertFunc;
|
// Function which is currently used to display alerts -- may be replaced by
|
||||||
|
// some API function.
|
||||||
|
this.alertHandler = function(_message, _details) {
|
||||||
|
alert(_message);
|
||||||
|
|
||||||
|
if (_details)
|
||||||
|
{
|
||||||
|
_egw.debug('info', _message, _details);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
json_request.prototype.sendRequest = function() {
|
||||||
* Function which is currently used to display alerts -- may be replaced by
|
// Assemble the complete request
|
||||||
* some API function.
|
var request_obj = {
|
||||||
*/
|
'json_data': this.egw.jsonEncode({
|
||||||
json_request.prototype.alertFunc = function(_message, _details)
|
'request': {
|
||||||
|
'parameters': this.parameters
|
||||||
|
}
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
// Send the request via AJAX using the jquery ajax function
|
||||||
|
$j.ajax({
|
||||||
|
url: this.url,
|
||||||
|
async: this.async,
|
||||||
|
context: this,
|
||||||
|
data: request_obj,
|
||||||
|
dataType: 'json',
|
||||||
|
type: 'POST',
|
||||||
|
success: this.handleResponse,
|
||||||
|
error: function(_xmlhttp, _err) {
|
||||||
|
this.egw.debug('error', 'Ajax request to', this.url, ' failed:',
|
||||||
|
_err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
json_request.prototype.handleResponse = function(data) {
|
||||||
|
if (data && data.response)
|
||||||
{
|
{
|
||||||
alert(_message);
|
for (var i = 0; i < data.response.length; i++)
|
||||||
if(_details) _egw_json_debug_log(_message, _details);
|
{
|
||||||
|
for (var key in plugins) {
|
||||||
|
try {
|
||||||
|
// Get a reference to the plugin
|
||||||
|
var plugin = plugins[key];
|
||||||
|
|
||||||
|
// Call the plugin callback
|
||||||
|
plugin.callback.call(
|
||||||
|
plugin.context ? plugin.context : this.context,
|
||||||
|
data.response[i].type,
|
||||||
|
data.response[i],
|
||||||
|
this
|
||||||
|
);
|
||||||
|
|
||||||
|
} catch(e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var json = {
|
var json = {
|
||||||
@ -86,15 +125,19 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
|
|||||||
* which handles the actual request. If the menuaction is a full featured
|
* which handles the actual request. If the menuaction is a full featured
|
||||||
* url, this one will be used instead.
|
* url, this one will be used instead.
|
||||||
* @param _parameters which should be passed to the menuaction function.
|
* @param _parameters which should be passed to the menuaction function.
|
||||||
|
* @param _async specifies whether the request should be asynchronous or
|
||||||
|
* not.
|
||||||
* @param _callback specifies the callback function which should be
|
* @param _callback specifies the callback function which should be
|
||||||
* called, once the request has been sucessfully executed.
|
* called, once the request has been sucessfully executed.
|
||||||
* @param _context is the context which will be used for the callback function
|
* @param _context is the context which will be used for the callback function
|
||||||
|
* @param _sender is a parameter being passed to the _callback function
|
||||||
*/
|
*/
|
||||||
request: function(_menuaction, _parameters, _callback, _context)
|
json: function(_menuaction, _parameters, _callback, _context, _async,
|
||||||
|
_sender)
|
||||||
{
|
{
|
||||||
return new json_request(_menuaction, _parameters, _callback,
|
return new json_request(_menuaction, _parameters, _callback,
|
||||||
_context, this);
|
_context, _async, _sender, this);
|
||||||
}
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a new handler plugin.
|
* Registers a new handler plugin.
|
||||||
@ -103,7 +146,7 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
|
|||||||
* whenever a response is comming from the server.
|
* whenever a response is comming from the server.
|
||||||
* @param _context is the context in which the callback function should
|
* @param _context is the context in which the callback function should
|
||||||
* be called. If null is given, the plugin is executed in the context
|
* be called. If null is given, the plugin is executed in the context
|
||||||
* of the request object.
|
* of the request object context.
|
||||||
* @param _type is an optional parameter defaulting to 'global'.
|
* @param _type is an optional parameter defaulting to 'global'.
|
||||||
* it describes the response type which this plugin should be
|
* it describes the response type which this plugin should be
|
||||||
* handling.
|
* handling.
|
||||||
@ -163,12 +206,12 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Regisert the "alert" plugin
|
// Regisert the "alert" plugin
|
||||||
json.registerPlugin(function(type, res) {
|
json.registerJSONPlugin(function(type, res, req) {
|
||||||
//Check whether all needed parameters have been passed and call the alertHandler function
|
//Check whether all needed parameters have been passed and call the alertHandler function
|
||||||
if ((typeof res.data.message != 'undefined') &&
|
if ((typeof res.data.message != 'undefined') &&
|
||||||
(typeof res.data.details != 'undefined'))
|
(typeof res.data.details != 'undefined'))
|
||||||
{
|
{
|
||||||
this.alertHandler(
|
req.alertHandler(
|
||||||
res.data.message,
|
res.data.message,
|
||||||
res.data.details)
|
res.data.details)
|
||||||
return true;
|
return true;
|
||||||
@ -177,7 +220,7 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
|
|||||||
}, null, 'alert');
|
}, null, 'alert');
|
||||||
|
|
||||||
// Register the "assign" plugin
|
// Register the "assign" plugin
|
||||||
json.registerPlugin(function(type, res) {
|
json.registerJSONPlugin(function(type, res, req) {
|
||||||
//Check whether all needed parameters have been passed and call the alertHandler function
|
//Check whether all needed parameters have been passed and call the alertHandler function
|
||||||
if ((typeof res.data.id != 'undefined') &&
|
if ((typeof res.data.id != 'undefined') &&
|
||||||
(typeof res.data.key != 'undefined') &&
|
(typeof res.data.key != 'undefined') &&
|
||||||
@ -202,17 +245,17 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
|
|||||||
}, null, 'assign');
|
}, null, 'assign');
|
||||||
|
|
||||||
// Register the "data" plugin
|
// Register the "data" plugin
|
||||||
json.registerPlugin(function(type, res) {
|
json.registerJSONPlugin(function(type, res, req) {
|
||||||
//Callback the caller in order to allow him to handle the data
|
//Callback the caller in order to allow him to handle the data
|
||||||
if (this.callback)
|
if (req.callback)
|
||||||
{
|
{
|
||||||
this.callback.call(this.sender, res.data);
|
req.callback.call(req.sender, res.data);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}, null, 'data');
|
}, null, 'data');
|
||||||
|
|
||||||
// Register the "script" plugin
|
// Register the "script" plugin
|
||||||
json.registerPlugin(function(type, res) {
|
json.registerJSONPlugin(function(type, res, req) {
|
||||||
if (typeof res.data == 'string')
|
if (typeof res.data == 'string')
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -222,7 +265,7 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
|
|||||||
}
|
}
|
||||||
catch (e)
|
catch (e)
|
||||||
{
|
{
|
||||||
this.egw.debug('error', 'Error while executing script: ',
|
req.egw.debug('error', 'Error while executing script: ',
|
||||||
res.data)
|
res.data)
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -231,7 +274,7 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
|
|||||||
}, null, 'script');
|
}, null, 'script');
|
||||||
|
|
||||||
// Register the "apply" plugin
|
// Register the "apply" plugin
|
||||||
json.registerPlugin(function(type, res) {
|
json.registerJSONPlugin(function(type, res, req) {
|
||||||
if (typeof res.data.func == 'string' &&
|
if (typeof res.data.func == 'string' &&
|
||||||
typeof window[res.data.func] == 'function')
|
typeof window[res.data.func] == 'function')
|
||||||
{
|
{
|
||||||
@ -241,7 +284,7 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
|
|||||||
}
|
}
|
||||||
catch (e)
|
catch (e)
|
||||||
{
|
{
|
||||||
this.egw.debug('error', 'Function', res.data.func,
|
req.egw.debug('error', 'Function', res.data.func,
|
||||||
'Parameters', res.data.parms);
|
'Parameters', res.data.parms);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -250,18 +293,18 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
|
|||||||
}, null, 'apply');
|
}, null, 'apply');
|
||||||
|
|
||||||
// Register the "jquery" plugin
|
// Register the "jquery" plugin
|
||||||
json.registerPlugin(function(type, res) {
|
json.registerJSONPlugin(function(type, res, req) {
|
||||||
if (typeof res.data.select == 'string' &&
|
if (typeof res.data.select == 'string' &&
|
||||||
typeof res.data.func == 'string')
|
typeof res.data.func == 'string')
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var jQueryObject = $j(res.data.select, this.context);
|
var jQueryObject = $j(res.data.select, req.context);
|
||||||
jQueryObject[res.data.func].apply(jQueryObject, res.data.parms);
|
jQueryObject[res.data.func].apply(jQueryObject, res.data.parms);
|
||||||
}
|
}
|
||||||
catch (e)
|
catch (e)
|
||||||
{
|
{
|
||||||
this.egw.debug('error', 'Function', res.data.func,
|
req.egw.debug('error', 'Function', res.data.func,
|
||||||
'Parameters', res.data.parms);
|
'Parameters', res.data.parms);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -270,7 +313,7 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
|
|||||||
}, null, 'jquery');
|
}, null, 'jquery');
|
||||||
|
|
||||||
// Register the "redirect" plugin
|
// Register the "redirect" plugin
|
||||||
json.registerPlugin(function(type, res) {
|
json.registerJSONPlugin(function(type, res, req) {
|
||||||
//console.log(res.data.url);
|
//console.log(res.data.url);
|
||||||
if (typeof res.data.url == 'string' &&
|
if (typeof res.data.url == 'string' &&
|
||||||
typeof res.data.global == 'boolean')
|
typeof res.data.global == 'boolean')
|
||||||
@ -284,7 +327,7 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
egw_appWindowOpen(this.app, res.data.url);
|
egw_appWindowOpen(req.app, res.data.url);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -292,27 +335,25 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_egw, _wnd) {
|
|||||||
}, null, 'redirect');
|
}, null, 'redirect');
|
||||||
|
|
||||||
// Register the 'css' plugin
|
// Register the 'css' plugin
|
||||||
json.registerPlugin(function(type, res) {
|
json.registerJSONPlugin(function(type, res, req) {
|
||||||
if (typeof res.data == 'string')
|
if (typeof res.data == 'string')
|
||||||
{
|
{
|
||||||
this.egw.includeCSS(res.data);
|
req.egw.includeCSS(res.data);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
throw 'Invalid parameters';
|
throw 'Invalid parameters';
|
||||||
}, null, 'css');
|
}, null, 'css');
|
||||||
|
|
||||||
// Register the 'js' plugin
|
// Register the 'js' plugin
|
||||||
json.registerPlugin(function(type, res) {
|
json.registerJSONPlugin(function(type, res, req) {
|
||||||
if (typeof res.data == 'string')
|
if (typeof res.data == 'string')
|
||||||
{
|
{
|
||||||
this.jsCount++;
|
req.jsCount++;
|
||||||
var self = this;
|
req.egw.includeJS(res.data, function() {
|
||||||
|
req.jsFiles++;
|
||||||
this.egw.includeJS(res.data, function() {
|
if (req.jsFiles == req.jsCount && req.onLoadFinish)
|
||||||
self.jsFiles++;
|
|
||||||
if (self.jsFiles == self.jsCount && this.onLoadFinish)
|
|
||||||
{
|
{
|
||||||
this.onLoadFinish.call(this.sender);
|
req.onLoadFinish.call(req.sender);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -145,7 +145,13 @@ egw.extend('utils', egw.MODULE_GLOBAL, function() {
|
|||||||
|
|
||||||
// Create the utils object which contains references to all functions
|
// Create the utils object which contains references to all functions
|
||||||
// covered by it.
|
// covered by it.
|
||||||
var utils = {};
|
var utils = {
|
||||||
|
|
||||||
|
ajaxUrl: function(_menuaction) {
|
||||||
|
return this.webserverUrl + '/json.php?menuaction=' + _menuaction;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
// Check whether the browser already supports encoding JSON -- if yes, use
|
// Check whether the browser already supports encoding JSON -- if yes, use
|
||||||
// its implementation, otherwise our own
|
// its implementation, otherwise our own
|
||||||
@ -159,7 +165,7 @@ egw.extend('utils', egw.MODULE_GLOBAL, function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Return the extension
|
// Return the extension
|
||||||
return {"utils": utils};
|
return utils;
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user