Homogenize indentation (dom and messaging were using spaces) to tabs
This commit is contained in:
parent
242b97e745
commit
fff6ac34a1
2 changed files with 78 additions and 78 deletions
52
src/dom.js
52
src/dom.js
|
@ -1,36 +1,36 @@
|
|||
function Dom() {
|
||||
return {
|
||||
clear: clear,
|
||||
make: make
|
||||
}
|
||||
return {
|
||||
clear: clear,
|
||||
make: make
|
||||
}
|
||||
|
||||
function clear(elem) {
|
||||
while(elem.firstChild) {
|
||||
elem.removeChild(elem.firstChild);
|
||||
}
|
||||
}
|
||||
function clear(elem) {
|
||||
while(elem.firstChild) {
|
||||
elem.removeChild(elem.firstChild);
|
||||
}
|
||||
}
|
||||
|
||||
function make(tag, properties, children) {
|
||||
var e = document.createElement(tag);
|
||||
function make(tag, properties, children) {
|
||||
var e = document.createElement(tag);
|
||||
properties = properties || {};
|
||||
children = children || [];
|
||||
for(key in properties) {
|
||||
var value = properties[key];
|
||||
switch(key) {
|
||||
case "class":
|
||||
e.className = Array.isArray(value) ? value.join(' ') : value;
|
||||
break;;
|
||||
case "onClick":
|
||||
e.addEventListener("click", value);
|
||||
break;;
|
||||
default:
|
||||
e[key] = value;
|
||||
}
|
||||
}
|
||||
for(key in properties) {
|
||||
var value = properties[key];
|
||||
switch(key) {
|
||||
case "class":
|
||||
e.className = Array.isArray(value) ? value.join(' ') : value;
|
||||
break;;
|
||||
case "onClick":
|
||||
e.addEventListener("click", value);
|
||||
break;;
|
||||
default:
|
||||
e[key] = value;
|
||||
}
|
||||
}
|
||||
for(var i = 0; i < children.length; i++) {
|
||||
e.appendChild(children[i]);
|
||||
}
|
||||
return e;
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
104
src/messaging.js
104
src/messaging.js
|
@ -1,62 +1,62 @@
|
|||
function Messaging() {
|
||||
var ws = new WebSocket('ws://' + window.location.hostname + '/play/');
|
||||
var keepAlivePeriod = 20000;
|
||||
var routes = {callbacks: [], children: {}};
|
||||
var ws = new WebSocket('ws://' + window.location.hostname + '/play/');
|
||||
var keepAlivePeriod = 20000;
|
||||
var routes = {callbacks: [], children: {}};
|
||||
|
||||
return {
|
||||
addEventListener: addEventListener,
|
||||
send: send,
|
||||
start: start
|
||||
}
|
||||
return {
|
||||
addEventListener: addEventListener,
|
||||
send: send,
|
||||
start: start
|
||||
}
|
||||
|
||||
function get(obj, path, write) {
|
||||
write = write || false;
|
||||
if(path.length < 1) {
|
||||
return obj;
|
||||
} else {
|
||||
if(obj.children[path[0]] == undefined && write) {
|
||||
obj.children[path[0]] = {callbacks: [], children: {}};
|
||||
}
|
||||
if(obj.children[path[0]] != undefined) {
|
||||
return get(obj.children[path[0]], path.slice(1), write);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
function get(obj, path, write) {
|
||||
write = write || false;
|
||||
if(path.length < 1) {
|
||||
return obj;
|
||||
} else {
|
||||
if(obj.children[path[0]] == undefined && write) {
|
||||
obj.children[path[0]] = {callbacks: [], children: {}};
|
||||
}
|
||||
if(obj.children[path[0]] != undefined) {
|
||||
return get(obj.children[path[0]], path.slice(1), write);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function addEventListener(path, callback) {
|
||||
var route = get(routes, path, true);
|
||||
route.callbacks.push(callback);
|
||||
}
|
||||
function addEventListener(path, callback) {
|
||||
var route = get(routes, path, true);
|
||||
route.callbacks.push(callback);
|
||||
}
|
||||
|
||||
function messageListener(event) {
|
||||
var o = JSON.parse(event.data);
|
||||
var path = [];
|
||||
var tmp = o;
|
||||
while(tmp != undefined && tmp.tag != undefined) {
|
||||
path.push(tmp.tag);
|
||||
tmp = tmp.message;
|
||||
}
|
||||
var route = get(routes, path);
|
||||
if(route != undefined && route.callbacks != undefined) {
|
||||
route.callbacks.forEach(function(f) {f(o);});
|
||||
} else {
|
||||
debug.textContent = event.data;
|
||||
}
|
||||
};
|
||||
function messageListener(event) {
|
||||
var o = JSON.parse(event.data);
|
||||
var path = [];
|
||||
var tmp = o;
|
||||
while(tmp != undefined && tmp.tag != undefined) {
|
||||
path.push(tmp.tag);
|
||||
tmp = tmp.message;
|
||||
}
|
||||
var route = get(routes, path);
|
||||
if(route != undefined && route.callbacks != undefined) {
|
||||
route.callbacks.forEach(function(f) {f(o);});
|
||||
} else {
|
||||
debug.textContent = event.data;
|
||||
}
|
||||
};
|
||||
|
||||
function start() {
|
||||
function start() {
|
||||
//ping();
|
||||
addEventListener(["Pong"], ping);
|
||||
ws.addEventListener('message', messageListener);
|
||||
}
|
||||
addEventListener(["Pong"], ping);
|
||||
ws.addEventListener('message', messageListener);
|
||||
}
|
||||
|
||||
function send(o) {
|
||||
ws.send(JSON.stringify(o));
|
||||
}
|
||||
function send(o) {
|
||||
ws.send(JSON.stringify(o));
|
||||
}
|
||||
|
||||
function ping() {
|
||||
setTimeout(function() {send({tag: "Ping"});}, keepAlivePeriod);
|
||||
}
|
||||
function ping() {
|
||||
setTimeout(function() {send({tag: "Ping"});}, keepAlivePeriod);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue