home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / res / html / pythonbridge.js < prev    next >
Encoding:
Text File  |  2010-12-06  |  3.9 KB  |  139 lines

  1. function DigsbyClass() {
  2.     this.callbacks = {};
  3. }
  4.  
  5. DigsbyClass.prototype = {
  6.     requestOut: function(method, params, id, success, error, specifier) {
  7.         var self = this;
  8.         //var cmd = new Command();
  9.         //cmd.go = function(){
  10.  
  11.         if (id) {
  12.             //need to store error/success callbacks - if they exist
  13.             self.callbacks[id] = {success:success, error:error};
  14.         }
  15. //        console.log(['requestOut', method, params, id]);
  16.         self.jsonOut({'method':method, 'params':params, 'id':id, 'specifier':specifier});
  17.  
  18.         //};
  19.         //cmd.syncGo();
  20.     },
  21.  
  22.     requestIn: function(args) {
  23.         //var cmd = new Command();
  24.         //cmd.go = function(){
  25.             window[args.method](args.params, args.id);
  26.         //};
  27.         //cmd.syncGo();
  28.     },
  29.  
  30.     resultOut: function(result, error, id) {
  31.         //var cmd = new Command();
  32.         var self = this;
  33.         //cmd.go = function(){
  34.             self.jsonOut({'result':result, 'error':error, 'id':id});
  35.         //};
  36.         //cmd.syncGo();
  37.     },
  38.  
  39.     successOut: function(result, id) {
  40.         this.resultOut(result, null, id);
  41.     },
  42.  
  43.     errorOut: function(error, id) {
  44.         this.resultOut(null, error, id);
  45.     },
  46.  
  47.     jsonOut: function(json_obj) {
  48.         var url = "digsby://digsbyjsonrpc/json=" + encodeURIComponent(JSON.stringify(json_obj));
  49.         document.title = url;
  50.         document.title = "digsby://digsbyjsonrpc/clear";
  51.     },
  52.  
  53.     resultIn: function(res) {
  54.         var self = this;
  55.         //var cmd = new Command();
  56.         //cmd.go = function(){
  57.  
  58.         var result = res.result;
  59.         var error  = res.error;
  60.         var id     = res.id;
  61. //        console.log('resultIn: ' + id);
  62.         if (result !== null) {
  63.             self.successIn(result, id);
  64.             if (error !== null) {
  65.                 console.log("got a result and error:" + result + "," + error + "," + id);
  66.             }
  67.             return;
  68.         }
  69.         if (error !== null) {
  70.             self.errorIn(error, id);
  71.             return;
  72.         }
  73.         console.log("got no result or error:" + id);
  74.  
  75.         //};
  76.         //cmd.syncGo();
  77.     },
  78.  
  79.     errorIn: function(error, id) {
  80.         if (this.callbacks[id]){
  81.             this.callbacks[id].error(error);
  82.             delete this.callbacks[id];
  83.         }
  84.     },
  85.  
  86.     successIn: function(result, id) {
  87.         if (this.callbacks[id]) {
  88.             this.callbacks[id].success(result);
  89.             delete this.callbacks[id];
  90.         }
  91.     }
  92. };
  93.  
  94. var Digsby = new DigsbyClass();
  95.  
  96. function DCallbacks(specifier) {
  97.     this.id_count  = 0;
  98.     this.specifier = specifier === undefined ? null : specifier;
  99. }
  100.  
  101. function callbackCall(params, id) {
  102.     params = params[0];
  103.     var method = params.method;
  104.     var args   = params.args;
  105.  
  106.     function success(result) { Digsby.successOut(result, id); }
  107.     function error(error_obj) { Digsby.errorOut(error_obj, id); }
  108.  
  109.     args.success = success;
  110.     args.error = error;
  111.  
  112.     window[method](args);
  113. }
  114.  
  115. //convenience functions
  116. DCallbacks.prototype = {
  117.  
  118.     notify: function(method, params){
  119.         Digsby.requestOut(method, [params], null, null, null, this.specifier);
  120.     },
  121.  
  122.     rpc: function(method, params, success, error) {
  123.         var id = (new Date()).getTime() + '_' + this.specifier + '_' + this.id_count++;
  124.         //call digsby, wraps w/ an id and puts params in a list
  125.         //also, unwraps the args dict from it's parent list for success
  126.         Digsby.requestOut(method, [params], id, function(args) { success(args[0]); }, error, this.specifier);
  127.     },
  128.  
  129.     ajax: function(args) {
  130.         if (args.type != "JSON")
  131.             console.log("ERROR: Digsby only does JSON");
  132.         else
  133.             this.rpc('ajax', {url:args.url, type:args.type}, args.success, args.error);
  134.     }
  135. };
  136.  
  137. var D = new DCallbacks();
  138.  
  139.