/usr/share/games/flightgear/Phi/lib/knockprops.js is in flightgear-phi 2016.4.2+dfsg1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | /**
###########################################################################
# knockprops - knockout.js <-> flightgear properties bridge
# (c) 2015 Torsten Dreyer
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
############################################################################
*/
define(['knockout'], function(ko) {
function KnockProps() {
var self = this;
self.initWebsocket = function() {
self.ws = new WebSocket('ws://' + location.host + '/PropertyListener');
self.ws.onclose = function(ev) {
var msg = 'Lost connection to FlightGear. Should I try to reconnect?';
if (confirm(msg)) {
// try reconnect
self.initWebsocket();
} else {
throw new Error(msg);
}
}
self.ws.onerror = function(ev) {
var msg = 'Error communicating with FlightGear. Please reload this page and/or restart FlightGear.';
alert(msg);
throw new Error(msg);
}
self.ws.onmessage = function(ev) {
try {
self.fire(JSON.parse(ev.data));
} catch (e) {
}
};
self.openCache = [];
self.ws.onopen = function(ev) {
// send subscriptions when the socket is open
var c = self.openCache;
delete self.openCache;
c.forEach(function(e) {
self.addListener(e.prop, e.koObservable);
});
for ( var p in self.listeners) {
self.addListener(p, self.listeners[p]);
}
};
}
self.initWebsocket();
self.fire = function(json) {
var value = json.value;
var listeners = self.listeners[json.path] || [];
listeners.forEach(function(koObservable) {
koObservable(value)
});
koObservable(json.value);
}
function resolvePropertyPath(self, pathOrAlias) {
if (pathOrAlias in self.aliases)
return self.aliases[pathOrAlias];
if (pathOrAlias.charAt(0) == '/')
return pathOrAlias;
return null;
}
self.listeners = {}
self.removeListener = function(pathOrAlias, koObservable) {
var path = resolvePropertyPath(self, pathOrAlias);
if (path == null) {
console.log("can't remove listener for " + pathOrAlias + ": unknown alias or invalid path.");
return self;
}
var listeners = self.listeners[path] || [];
var idx = listeners.indexOf(koObservable);
if (idx == -1) {
console.log("can't remove listener for " + path + ": not a listener.");
return self;
}
listeners.splice(idx, 1);
if (0 == listeners.length) {
self.ws.send(JSON.stringify({
command : 'removeListener',
node : path
}));
}
return self;
}
self.addListener = function(alias, koObservable) {
if (self.openCache) {
// socket not yet open, just cache the request
self.openCache.push({
"prop" : alias,
"koObservable" : koObservable
});
return self;
}
var path = resolvePropertyPath(self, alias);
if (path == null) {
console.log("can't listen to " + alias + ": unknown alias or invalid path.");
return self;
}
var listeners = self.listeners[path] = (self.listeners[path] || []);
if (listeners.indexOf(koObservable) != -1) {
console.log("won't listen to " + path + ": duplicate.");
return self;
}
koObservable.fgPropertyPath = path;
koObservable.fgBaseDispose = koObservable.dispose;
koObservable.dispose = function() {
if (this.fgPropertyPath) {
self.removeListener(this.fgPropertyPath, this);
}
this.fgBaseDispose.call(this);
}
listeners.push(koObservable);
koObservable.fgSetPropertyValue = function(value) {
self.setPropertyValue(this.fgPropertyPath, value);
}
if (1 == listeners.length) {
self.ws.send(JSON.stringify({
command : 'addListener',
node : path
}));
}
self.ws.send(JSON.stringify({
command : 'get',
node : path
}));
return self;
}
self.aliases = {};
self.setAliases = function(arg) {
if( Object.prototype.toString.call( arg ) === '[object Array]' ) {
// [
// [ shortcut, propertypath ],
// [ othercut, otherproperty ],
// ]
arg.forEach(function(a) {
self.aliases[a[0]] = a[1];
});
} else {
self.aliases = arg;
}
}
self.addAliases = function(arg) {
self.aliases = self.aliases || {};
for( var p in arg ) {
if( self.aliases.hasOwnProperty(p) ) {
console.log(p + " is already a property alias. Skipping.");
continue;
}
self.aliases[p] = arg[p];
}
}
self.makeObservablesForAllProperties = function(target, aliases ) {
aliases = aliases || self.aliases;
for( var p in aliases ) {
if( aliases.hasOwnProperty(p) ) {
target[p] = ko.observable().extend({
fgprop : p
}).extend({
rateLimit: 40
});
}
}
}
self.props = {};
self.get = function(target, prop) {
if (self.props[prop]) {
return self.props[prop];
}
return (self.props[prop] = self.observedProperty(target, prop));
}
self.observedProperty = function(target, prop) {
var reply = ko.pureComputed({
read : target,
write : function(newValue) {
if (newValue == target())
return;
target(newValue);
target.notifySubscribers(newValue);
}
});
self.addListener(prop, reply);
return reply;
}
self.write = function(prop, value) {
var path = this.aliases[prop] || "";
if (path.length == 0) {
console.log("can't write " + prop + ": unknown alias.");
return;
}
self.setPropertyValue(path, value);
}
self.setPropertyValue = function(path, value) {
this.ws.send(JSON.stringify({
command : 'set',
node : path,
value : value
}));
}
self.propsToObject = function(prop, map, result) {
result = result || {}
prop.children.forEach(function(prop) {
var target = map[prop.name] || null;
if (target) {
if (typeof (result[target]) === 'function') {
result[target](prop.value);
} else {
result[target] = prop.value;
}
}
});
return result;
}
}
ko.utils.knockprops = new KnockProps();
ko.extenders.fgprop = function(target, prop) {
return ko.utils.knockprops.get(target, prop);
};
ko.extenders.observedProperty = function(target, prop) {
return ko.utils.knockprops.observedProperty(target, prop);
};
/*
ko.extenders.fgPropertyGetSet = function(target, option) {
fgCommand.getPropertyValue(option, function(value) {
target(value);
}, self);
var p = ko.pureComputed({
read : target,
write : function(newValue) {
if (newValue == target())
return;
target(newValue);
target.notifySubscribers(newValue);
fgCommand.setPropertyValue(option, newValue);
}
});
return p;
}
*/
// don't return anything, use ko.extenders or ku.utils.knockprops
});
|