From 62eb8e00f23237b5d53bc9c66a42233ede75ea94 Mon Sep 17 00:00:00 2001 From: Nathan Gray Date: Thu, 4 Feb 2016 19:26:37 +0000 Subject: [PATCH] Add support for global json handlers, and register a global handler for storing data entries --- phpgwapi/js/jsapi/egw_data.js | 19 ++++++++ phpgwapi/js/jsapi/egw_json.js | 83 +++++++++++++++++++++++------------ 2 files changed, 75 insertions(+), 27 deletions(-) diff --git a/phpgwapi/js/jsapi/egw_data.js b/phpgwapi/js/jsapi/egw_data.js index eafea22e06..ac89668fb1 100644 --- a/phpgwapi/js/jsapi/egw_data.js +++ b/phpgwapi/js/jsapi/egw_data.js @@ -519,6 +519,25 @@ egw.extend("data_storage", egw.MODULE_GLOBAL, function (_app, _wnd) { */ var registeredCallbacks = {}; + + + /** + * Register the "data" plugin globally for single uids + * Multiple UIDs such as nextmatch results are still handled by egw.data + * using dataFetch() && parseServerResponse(), above. Both update the + * GLOBAL data cache though this one is registered globally, and the above + * is registered app local. + */ + egw.registerJSONPlugin(function(type, res, req) { + if ((typeof res.data.uid != 'undefined') && + (typeof res.data.data != 'undefined')) + { + // Store it, which will call all registered listeners + this.dataStoreUID(res.data.uid, res.data.data); + return true; + } + }, egw, 'data',true); + /** * Uids and timers used for querying data uids, hashed by the first few * bytes of the _execId, stored as an object of the form diff --git a/phpgwapi/js/jsapi/egw_json.js b/phpgwapi/js/jsapi/egw_json.js index 623ceb3f2f..28fc3588ec 100644 --- a/phpgwapi/js/jsapi/egw_json.js +++ b/phpgwapi/js/jsapi/egw_json.js @@ -31,6 +31,15 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd) { */ var plugins = {}; + /** + * Global json handlers are from global modules, not window level + */ + if(typeof egw._global_json_handlers == 'undefined') + { + egw._global_json_handlers = {} + } + var global_plugins = egw._global_json_handlers; + /** * Internal implementation of the JSON request object. * @@ -165,24 +174,29 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd) { if(typeof res.type == 'string' && res.type != 'data') only_data = false; // Check whether a plugin for the given type exists - if (typeof plugins[res.type] !== 'undefined') + var handlers = [plugins, global_plugins]; + for(var handler_idx = 0; handler_idx < handlers.length; handler_idx++) { - for (var j = 0; j < plugins[res.type].length; j++) { - try { - // Get a reference to the plugin - var plugin = plugins[res.type][j]; + var handler_level = handlers[handler_idx]; + if (typeof handler_level[res.type] !== 'undefined') + { + for (var j = 0; j < handler_level[res.type].length; j++) { + try { + // Get a reference to the plugin + var plugin = handler_level[res.type][j]; - // Call the plugin callback - plugin.callback.call( - plugin.context ? plugin.context : this.context, - res.type, res, this - ); - } catch(e) { - var msg = e.message ? e.message : e + ''; - var stack = e.stack ? "\n-- Stack trace --\n" + e.stack : ""; - this.egw.debug('error', 'Exception "' + msg + '" while handling JSON response from ' + - this.url + ' [' + JSON.stringify(this.parameters) + '] type "' + res.type + - '", plugin', plugin, 'response', res, stack); + // Call the plugin callback + plugin.callback.call( + plugin.context ? plugin.context : this.context, + res.type, res, this + ); + } catch(e) { + var msg = e.message ? e.message : e + ''; + var stack = e.stack ? "\n-- Stack trace --\n" + e.stack : ""; + this.egw.debug('error', 'Exception "' + msg + '" while handling JSON response from ' + + this.url + ' [' + JSON.stringify(this.parameters) + '] type "' + res.type + + '", plugin', plugin, 'response', res, stack); + } } } } @@ -229,23 +243,32 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd) { * @param _type is an optional parameter defaulting to 'global'. * it describes the response type which this plugin should be * handling. + * @param {boolean} [_global=false] Register the handler globally or + * locally. Global handlers must stay around, so should be used + * for global modules. */ - registerJSONPlugin: function(_callback, _context, _type) + registerJSONPlugin: function(_callback, _context, _type, _global) { // _type defaults to 'global' if (typeof _type === 'undefined') { _type = 'global'; } + // _global defaults to false + if (typeof _global === 'undefined') + { + _global = false; + } + var scoped = _global ? global_plugins : plugins; // Create an array for the given category inside the plugins object - if (typeof plugins[_type] === 'undefined') + if (typeof scoped[_type] === 'undefined') { - plugins[_type] = []; + scoped[_type] = []; } // Add the entry - plugins[_type].push({ + scoped[_type].push({ 'callback': _callback, 'context': _context }); @@ -261,22 +284,28 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd) { * @param _type is an optional parameter defaulting to 'global'. * it describes the response type which this plugin should be * handling. + * @param {boolean} [_global=false] Remove a global or local handler. */ - unregisterJSONPlugin: function(_callback, _context, _type) + unregisterJSONPlugin: function(_callback, _context, _type, _global) { // _type defaults to 'global' if (typeof _type === 'undefined') { _type = 'global'; } - - if (typeof plugins[_type] !== 'undefined') { - for (var i = 0; i < plugins[_type].length; i++) + // _global defaults to false + if (typeof _global === 'undefined') + { + _global = false; + } + var scoped = _global ? global_plugins : plugins; + if (typeof scoped[_type] !== 'undefined') { + for (var i = 0; i < scoped[_type].length; i++) { - if (plugins[_type][i].callback == _callback && - plugins[_type][i].context == _context) + if (scoped[_type][i].callback == _callback && + scoped[_type][i].context == _context) { - plugins[_type].slice(i, 1); + scoped[_type].slice(i, 1); break; } }