]> git.mar77i.info Git - hublib/blob - hubapps/first/common.js
functioning basic chat app
[hublib] / hubapps / first / common.js
1 (function () {
2 window.common = {
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] +
10 "//" +
11 window.location.host +
12 "/" +
13 ws_uri
14 );
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);
19 },
20 "get_input": function (on_enter) {
21 var input = document.createElement("input");
22 input.addEventListener("keydown", function (event) {
23 var keycode;
24 if (!event) {
25 event = window.event;
26 }
27 keycode = event.code || event.key;
28 if (keycode === "Enter" || keycode === "NumpadEnter") {
29 on_enter();
30 }
31 });
32 return input;
33 },
34 "write": function (output, msg) {
35 if (output.childNodes.length > 0) {
36 output.append(document.createElement("br"));
37 }
38 if (typeof msg !== "string") {
39 msg = String(msg);
40 }
41 output.append(document.createTextNode(msg));
42 },
43 "setup": function (callback) {
44 document.addEventListener("readystatechange", function (event) {
45 if (!event) {
46 event = window.event;
47 }
48 if (event.target.readyState !== "complete") {
49 return;
50 }
51 callback();
52 });
53 },
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();
59 };
60 this.get_name = function (client_id) {
61 var input = this.get_checkbox(client_id);
62 if (input !== null) {
63 return input.parentNode.childNodes[1].textContent;
64 }
65 return this.get_label(client_id);
66 };
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;
73 input.checked = true;
74 li.append(input);
75 li.append(document.createTextNode(client_label));
76 this.ul.append(li);
77 };
78 this.get_checkbox = function (client_id) {
79 return document.getElementById("checkbox-" + this.get_label(client_id));
80 };
81 this.remove = function (client_id) {
82 var input = this.get_checkbox(client_id);
83 if (input === null) {
84 return;
85 }
86 input.parentNode.remove();
87 };
88 this.set_name = function(client_id, new_name) {
89 var input = this.get_checkbox(client_id);
90 var old_name;
91 if (input === null) {
92 return;
93 }
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() + "]")
100 );
101 return old_name;
102 };
103 this.all = function () {
104 var result = [];
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]));
108 });
109 return result;
110 };
111 this.selected = function () {
112 var result = [];
113 Array.prototype.forEach.call(this.ul.children, function (child) {
114 var input = child.getElementsByTagName("input")[0];
115 var id_parts;
116 if (input.checked) {
117 id_parts = input.id.split("-");
118 result.push(Number(id_parts[id_parts.length - 1]));
119 }
120 });
121 return result;
122 };
123 },
124 };
125 }());