Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/web/xocom.js
blob: bb346e894485a7c8f822776572b2c10df5c9654e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * XOCom Javascript Source
 *
 * This source creates a global XO object that communicates
 * with the python XPCom code running this activity.
 *
 * Your HTML file should register several callbacks with 
 * the XO object to handle requests from the activity:
 *
 * Example: A handler for the activity read_file and write_file
 *
 *  <script type="text/javascript">
 *      XO.register('read', function(content) {
 *          // Your code to consume the supplied content
 *      })
 *      XO.register('write', function() {
 *          // Your code to return the content to save
 *          return 'monkey'
 *      })
 *  </script>
 *
 *  Some commands for debugging are put in an element with the 
 *  id 'xo-status' if it exists.
 *
 *
 * Copiado de SocialCalc
 *
 *
 */

var XO = window.XO = {
    callbacks: { },
    set_status: function (msg) {
        jQuery('#xo-status').html(msg)
    },
    observer: {
      observe: function(req_obj,topic,command) {
        now = new Date().getTime()
        XO.set_status("(" + now + ") Handling " + command)
        try {
          // We need access to use the XPCom functions below
          netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect")

          // Unwrap the XPCom objects to get at the data passed to us
          req_obj = req_obj.QueryInterface(Components.interfaces.nsIMutableArray)
          var command_arg = undefined
          if (req_obj.length) {
              var iter = req_obj.enumerate()
              xp_arg = iter.getNext()
              xp_arg = xp_arg.QueryInterface(Components.interfaces.nsISupportsString)
              command_arg = xp_arg.toString()
          }

          // Execute the registered callback method
          return_value = XO.callbacks[command](command_arg) || ''

          // Wrap the return value back into the XPCom object
          var result = Components.classes["@mozilla.org/supports-string;1"].createInstance(
                Components.interfaces.nsISupportsString)
          result.data = return_value
          req_obj.clear()
          req_obj.appendElement(result, false)
          XO.set_status("(" + now + ") Handled " + command + ": (" + return_value + ")")
        }
        catch (err) {
          XO.set_status("Error handling event: " + err)
        }
      }
    },
    register: function(command, callback) {
        XO.callbacks[command] = callback
    }
}

/*
 * This snippet registers the XO observer to receive commands
 * from the python XPCom code
 */
try {
  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  var observerService = Components.classes["@mozilla.org/observer-service;1"]
      .getService(Components.interfaces.nsIObserverService);
  observerService.addObserver(XO.observer, 'xo-message', false);
}
catch(err) {
    // Wait a bit to show this error, so the page has time to load up.
    setTimeout( function() {
        jQuery('#xo-status', 'JS Error: ' + err);
    }, 1000)
}