-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgadgetemulator.js
executable file
·108 lines (93 loc) · 2.75 KB
/
gadgetemulator.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Gadget emulator that fakes just enough of the Google Wave gadget API
// to let a gadget run in two iframes.
function Wave() {
}
function Gadgets() {
}
Gadgets.prototype = {
_add_participant: function(id,part, name, thumb) {
if (!this._participants) { this._participants = new Array(); }
this._participants.push({id: id, part: part, displayName: name, thumbnailUrl: thumb});
return id;
},
_load: function(url) {
$.ajax({type: "GET", url: url, dataType: "xml", success: function(xml) {
var text = $(xml).find("Content").text();
for (var p in gadgets._participants) {
var part = gadgets._participants[p];
var doc = part.part.document;
doc.write("<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js\"></script>");
doc.write("<script>document._participant_id="+part.id+";</script>");
doc.write("<script src=\"gadgetemulator.js\"></script>");
doc.write("<h3>Participant "+part.id+" - " +part.displayName+"</h3>");
doc.write(text);
doc.write("<script>gadgets.util._ready("+part.id+")</script>");
}
}, error: function() { alert("Load failed"); }
})
},
// --- To be called in the participant frames ---
_sendState: function() {
var s = this._rpc["wave_gadget_state"];
var state = this._state || {};
$('#state').val(wave.util.printJson(state, true, 2));
for (var cb in s) {
s[cb].cb(state);
}
},
_sendParticipants: function() {
var s = this._rpc["wave_participants"];
var p = this._participants;
var part = [];
for (var i in p) {
part.push(p[i]);
}
var ob = {myId: 0, authorId: 0, participants: part};
for (var cb in s) {
ob.myId = s[cb].part;
s[cb].cb(ob);
}
},
_call: function(cmd,a) {
if (cmd == "wave_gadget_state") {
var dirty = false;
this._state = this._state || {};
for (var i in a) {
if (a[i] != this._state[i]) {
this._state[i] = a[i];
dirty = true;
}
}
if (dirty) this._sendState();
} else if (cmd == "wave_enable") {
this._sendState();
this._sendParticipants();
} else {
alert("Unhandled RPC command: "+cmd);
}
},
_register: function(partid, endpoint, cb) {
this._rpc = this._rpc || {};
this._rpc[endpoint] = this._rpc[endpoint] || [];
this._rpc[endpoint].push({part: partid, cb: cb});
},
util: {
registerOnLoadHandler: function(f) {
document._gadgets_onload_handler = f;
f();
},
_ready: function(pid) {
if (document._gadgets_onload_handler) document._gadgets_onload_handler();
},
getUrlParameters: function() { return {wave: new Wave()}; }
},
rpc: {
call: function(arg1, cmd, arg2, params) {
window.top.gadgets._call(cmd,params);
},
register: function(endpoint, cb) {
window.top.gadgets._register(document._participant_id,endpoint,cb);
}
}
}
var gadgets = new Gadgets();