]>
git.mar77i.info Git - hublib/blob - hubapps/first/common.js
3 "HubClient": function (ws_uri
, open
, close
, message
) {
4 var ws_protocols
= {"http:": "ws:", "https:": "wss:"};
5 this.close
= close
.bind(this);
6 this.message
= message
.bind(this);
7 this.open
= open
.bind(this);
8 this.ws
= new WebSocket(
9 ws_protocols
[window
.location
.protocol
] +
11 window
.location
.host
+
15 this.ws
.addEventListener("close", this.close
);
16 this.ws
.addEventListener("message", this.message
);
17 this.ws
.addEventListener("open", this.open
);
18 this.send
= this.ws
.send
.bind(this.ws
);
20 "get_input": function (on_enter
) {
21 var input
= document
.createElement("input");
22 input
.addEventListener("keydown", function (event
) {
27 keycode
= event
.code
|| event
.key
;
28 if (keycode
=== "Enter" || keycode
=== "NumpadEnter") {
34 "write": function (output
, msg
) {
35 if (output
.childNodes
.length
> 0) {
36 output
.append(document
.createElement("br"));
38 if (typeof msg
!== "string") {
41 output
.append(document
.createTextNode(msg
));
43 "setup": function (callback
) {
44 document
.addEventListener("readystatechange", function (event
) {
48 if (event
.target
.readyState
!== "complete") {
54 "ClientsList": function () {
55 this.ul
= document
.createElement("ul");
56 document
.body
.append(this.ul
);
57 this.get_label = function (client_id
) {
58 return "client-" + client_id
.toString();
60 this.get_name = function (client_id
) {
61 var input
= this.get_checkbox(client_id
);
63 return input
.parentNode
.childNodes
[1].textContent
;
65 return this.get_label(client_id
);
67 this.append = function (client_id
) {
68 var li
= document
.createElement("li");
69 var input
= document
.createElement("input");
70 var client_label
= this.get_label(client_id
);
71 input
.type
= "checkbox";
72 input
.id
= "checkbox-" + client_label
;
75 li
.append(document
.createTextNode(client_label
));
78 this.get_checkbox = function (client_id
) {
79 return document
.getElementById("checkbox-" + this.get_label(client_id
));
81 this.remove = function (client_id
) {
82 var input
= this.get_checkbox(client_id
);
86 input
.parentNode
.remove();
88 this.set_name = function(client_id
, new_name
) {
89 var input
= this.get_checkbox(client_id
);
94 old_name
= input
.parentNode
.childNodes
[1].textContent
;
95 while (input
.parentNode
.childNodes
.length
> 1)
96 input
.parentNode
.childNodes
[1].remove();
97 input
.parentNode
.append(document
.createTextNode(new_name
));
98 input
.parentNode
.append(
99 document
.createTextNode(" [" + client_id
.toString() + "]")
103 this.all = function () {
105 Array
.prototype.forEach
.call(this.ul
.children
, function (child
) {
106 var id_parts
= child
.getElementsByTagName("input")[0].id
.split("-");
107 result
.push(Number(id_parts
[id_parts
.length
- 1]));
111 this.selected = function () {
113 Array
.prototype.forEach
.call(this.ul
.children
, function (child
) {
114 var input
= child
.getElementsByTagName("input")[0];
117 id_parts
= input
.id
.split("-");
118 result
.push(Number(id_parts
[id_parts
.length
- 1]));