2014-03-12 21:45:35 +00:00
|
|
|
var PropertyChangeListenerObjects = {
|
2014-03-14 20:15:51 +00:00
|
|
|
_ws : null,
|
2014-03-26 21:07:00 +00:00
|
|
|
_listeners : {},
|
2014-03-12 21:45:35 +00:00
|
|
|
};
|
|
|
|
|
2014-03-14 20:15:51 +00:00
|
|
|
var PropertyChangeListener = function(callback) {
|
2014-03-12 21:45:35 +00:00
|
|
|
PropertyChangeListenerObjects._ws = new WebSocket('ws://' + location.host + '/PropertyListener');
|
|
|
|
PropertyChangeListenerObjects._ws.onopen = callback;
|
|
|
|
PropertyChangeListenerObjects._ws.onclose = function(ev) {
|
2014-03-14 20:15:51 +00:00
|
|
|
alert('Lost connection to FlightGear. Please reload this page and/or restart FlightGear.');
|
|
|
|
PropertyChangeListenerObjects._ws = null;
|
|
|
|
};
|
|
|
|
PropertyChangeListenerObjects._ws.onerror = function(ev) {
|
|
|
|
alert('Error communicating with FlightGear. Please reload this page and/or restart FlightGear.');
|
2014-03-12 21:45:35 +00:00
|
|
|
PropertyChangeListenerObjects._ws = null;
|
|
|
|
};
|
|
|
|
PropertyChangeListenerObjects._ws.onmessage = function(ev) {
|
|
|
|
try {
|
|
|
|
var node = JSON.parse(ev.data);
|
|
|
|
var cb = PropertyChangeListenerObjects._listeners[node.path];
|
2014-03-26 21:07:00 +00:00
|
|
|
for (var i = 0; i < cb.length; i++) {
|
|
|
|
var o = cb[i];
|
|
|
|
o.context ? o.context.call(o.cb(node)) : o.cb(node);
|
|
|
|
}
|
2014-03-12 21:45:35 +00:00
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-03-26 21:07:00 +00:00
|
|
|
var NextListenerId = 0;
|
|
|
|
|
|
|
|
var SetListener = function(path, callback, context ) {
|
2014-03-12 21:45:35 +00:00
|
|
|
var o = PropertyChangeListenerObjects._listeners[path];
|
|
|
|
if (typeof (o) == 'undefined') {
|
|
|
|
o = new Array();
|
|
|
|
PropertyChangeListenerObjects._listeners[path] = o;
|
|
|
|
PropertyChangeListenerObjects._ws.send(JSON.stringify({
|
|
|
|
command : 'addListener',
|
|
|
|
node : path
|
|
|
|
}));
|
2014-09-16 09:16:15 +00:00
|
|
|
PropertyChangeListenerObjects._ws.send(JSON.stringify({
|
|
|
|
command : 'get',
|
|
|
|
node : path
|
|
|
|
}));
|
2014-03-12 21:45:35 +00:00
|
|
|
}
|
2014-03-26 21:07:00 +00:00
|
|
|
o.push({ cb: callback, ctx: context, id: NextListenerId });
|
|
|
|
return NextListenerId++;
|
2014-03-12 21:45:35 +00:00
|
|
|
};
|
2014-03-26 21:07:00 +00:00
|
|
|
|
|
|
|
var RemoveListener = function(id) {
|
|
|
|
// send unsubscribe over the socket.
|
|
|
|
var a = PropertyChangeListenerObjects._listeners;
|
|
|
|
for( var k in a ) {
|
|
|
|
var o = a[k];
|
|
|
|
for( var i = 0; i < o.length; i++ ) {
|
|
|
|
if( o[i].id == id ) {
|
|
|
|
//remote element from array
|
|
|
|
var rest = o.slice(i+1);
|
|
|
|
o.length = i;
|
|
|
|
o.push.apply(o,rest);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( o.length == 0 ) {
|
|
|
|
//TODO: send removeListener
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|