Author: bree
Date: Tue Apr 18 19:05:38 2006
New Revision: 2875

Modified:
   azax/branches/plugin/plugins/json/browser/jsonkukit.js
Log:
Implement registry for new style command plugins

Modified: azax/branches/plugin/plugins/json/browser/jsonkukit.js
==============================================================================
--- azax/branches/plugin/plugins/json/browser/jsonkukit.js      (original)
+++ azax/branches/plugin/plugins/json/browser/jsonkukit.js      Tue Apr 18 
19:05:38 2006
@@ -4,16 +4,33 @@
 *
 * This gets included when json plugins are registered.
 *
-* We overwrite notifyServer and at one point we return to the
-* normal execution chain (after results are received)
+* This transparently replaces the normal transport with JSON.
+* To do this, we overwrite notifyServer and at one point we 
+* return to the normal execution chain (after results are received).
+* (We need to do this because even an old style event plugin may
+* invoke new style command plugins as a response.)
+*
+* To manually call the server from an event plugin, the
+* makeJSONKukitMethod function should be used to create a
+* proxy method.
 */
 
+kukit.makeJSONKukitMethod = function(url, methodName, timeout) {
+       // url and methodName can be used, 
+       // or methodName can be set to null and url will be used as a full url.
+       if (typeof(timeout) == 'undefined' || timeout == null) {
+       // default timeout is 4 sec... a sane choice
+               timeout = 4000;
+       }
+    return makeJSONRPCMethod(url, methodName, kukit.jsonCallback, 
kukit.jsonError,
+                       4000, null, null, kukit.requestManager);
+}
+
+// OVERWRITE kukit.js
 kukit.notifyServer = function(url, target) {
-    // sending form
+    // sending form, with standard form parameters.
     var form_data = kukit.extractFormQuery(target).toDict();
-    // XXX set a standard 4 sec timeout... a sane choice
-    var method = makeJSONRPCMethod(url, '', kukit.jsonCallback, 
kukit.jsonError,
-                       4000, null, null, kukit.requestManager);
+    var method = kukit.makeJSONKukitMethod(url, null);
        method(PythonKw(form_data));
 }
 
@@ -29,51 +46,89 @@
 
 /* Command execution */
 
+// OVERWRITE kukit.js
 kukit.CommandProcessor.prototype.parseCommand = function(command) {
-    // For the sake of old commands compatibility, we need to convert
-    // all the commands to DOM.
-    var new_params = {};
-    for (var name in command.params) {
-        var param = command.params[name];
-
-               //
-        // now convert to dom
-               //
-               
-               //param = 
"<option>test_xmlentity</option><option>tic</option><option>tac&#32;toe</option>";
-               // ***BROKEN*** param = 
"<option>test_htmlentity</option><option>tic</option><option>tac&nbsp;toe</option>";
-               //param = 
"<option>test_utf8</option><option>tic</option><option>tacűtoe</option>";
-
-               // This is a good solution since it does not do magic to
-               // our html - BUT html is parsed as xml
-               // so we currently preprocess it to remove html entities
-               // from it.
-               //
-               var root_txt = '<html 
xmlns="http://www.w3.org/1999/xhtml";><div>' + param + '</div></html>';
-       var doc = (new DOMParser()).parseFromString(root_txt, "text/xml");
-               var root = doc.getElementsByTagName('div')[0];
-
-               // This would be the perfect solution: insert our stuff
-               // into the main document that is, since it is html,
-               // will parse html in correctly. But the problem is
-               // that is also sensitive to context which we have not
-               // at this point yet. So: <option> will not work since
-               // we have no embedding select, etc...
-               // -->> gets broken with both MOZILLA and IE
-               //
-               //var root = document.createElement('html');
-               //root.innerHTML = param;
-               //
-               // XXX Sarissa bug; html docs would not have a
-               // working serialize, and so importNodes would fail on them.
-               // XXX Fixed in: Revision 1.23  - Sun Jul 10 18:53:53 2005 UTC
-               // use at least 0.9.6.1
-               
-        new_params[name] = root;
-               //alert(Sarissa.serialize(root));
-    }
-    command.params = new_params;
     // Add the command.
     this.addCommand(command);
 } 
 
+kukit.CommandRegistry.prototype.forceJsonRegistry = function() {
+       if (typeof(this.jsonContent) == 'undefined') {
+               this.jsonContent = {};
+               this.jsonNr = 0; 
+       }
+}
+
+kukit.CommandRegistry.prototype.registerJson = function(name, func) {
+       this.forceJsonRegistry();
+       // Register a new style (json) command.
+       if (this.jsonContent[name] || this.content[name]) {
+        // Do not allow redefinition
+        kukit.logError('Error : redefinition attempt of command ' + name);
+        return;
+        }
+    this.jsonContent[name] = func;
+    this.jsonNr = this.jsonNr + 1;
+}
+
+// OVERWRITE kukit.js
+kukit.CommandRegistry.prototype.getFunc = function(name) {
+       this.forceJsonRegistry();
+    var func = this.content[name];
+    var jsonFunc = this.jsonContent[name];
+       var stub = null;
+    if (jsonFunc) {
+               // json commands receive the parameters "as are"
+               stub = jsonFunc;
+    } else if (func) {
+               // old style commands need all the parameters converted to DOM
+               stub = function(node, command_data) {
+                       var new_params = {};
+                       for (var key in command_data) {
+                               var param = command_data[key];
+
+                               //
+                               // now convert to dom
+                               //
+                               
+                               //param = 
"<option>test_xmlentity</option><option>tic</option><option>tac&#32;toe</option>";
+                               // ***BROKEN*** param = 
"<option>test_htmlentity</option><option>tic</option><option>tac&nbsp;toe</option>";
+                               //param = 
"<option>test_utf8</option><option>tic</option><option>tacűtoe</option>";
+
+                               // This is a good solution since it does not do 
magic to
+                               // our html - BUT html is parsed as xml
+                               // so we currently preprocess it on the server
+                               // and remove html named entities from it.
+                               //
+                               var root_txt = '<html 
xmlns="http://www.w3.org/1999/xhtml";><div>' + param + '</div></html>';
+                               var doc = (new 
DOMParser()).parseFromString(root_txt, "text/xml");
+                               var root = doc.getElementsByTagName('div')[0];
+
+                               // This would be the perfect solution: insert 
our stuff
+                               // into the main document that is, since it is 
html,
+                               // will parse html in correctly. But the 
problem is
+                               // that is also sensitive to context which we 
have not
+                               // at this point yet. So: <option> will not 
work since
+                               // we have no embedding select, etc...
+                               // -->> gets broken with both MOZILLA and IE
+                               //
+                               //var root = document.createElement('html');
+                               //root.innerHTML = param;
+                               //
+                               // XXX Sarissa bug; html docs would not have a
+                               // working serialize, and so importNodes would 
fail on them.
+                               // XXX Fixed in: Revision 1.23  - Sun Jul 10 
18:53:53 2005 UTC
+                               // use at least 0.9.6.1
+                               
+                               new_params[key] = root;
+                               //alert(Sarissa.serialize(root));
+                       }
+                       // now call the original function with the converted 
parameters
+                       return func(node, new_params);
+               }
+       } else {
+        // not found
+        kukit.logError('Error : undefined command ' + name);
+        }
+    return stub;
+}
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to