diff --git a/src/dom.js b/src/dom.js index 4dbad9b..54d842e 100644 --- a/src/dom.js +++ b/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; + } } diff --git a/src/messaging.js b/src/messaging.js index d8ef319..fcc7a46 100644 --- a/src/messaging.js +++ b/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); + } }