-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientResources.swift
816 lines (718 loc) · 22.5 KB
/
ClientResources.swift
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
// Generated from Model/ChatItem.js
// on Fri Jun 8 16:03:12 CEST 2018
//
let rsrc_ChatItem_js =
"""
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-nio-irc open source project
//
// Copyright (c) 2018 ZeeZide GmbH. and the swift-nio-irc project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIOIRC project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
// Our model object for something we display in a chat list
const ChatItem = function(message, sender, time, isSystem, isRead) {
this.message = message || "";
this.sender = sender || "✭";
this.time = time || new Date();
this.isSystem = isSystem || false;
this.isRead = isRead || false;
};
"""
// Generated from Model/ClientConnection.js
// on Fri Jun 8 16:03:12 CEST 2018
//
let rsrc_ClientConnection_js =
"""
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-nio-irc open source project
//
// Copyright (c) 2018 ZeeZide GmbH. and the swift-nio-irc project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIOIRC project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
/// This corresponds to the NIOIRC.IRCMessage, though we don't split out the
/// IRCCommand value.
/// Also: We use some non-standard commands ...
const IRCMessage = function(origin, target, command, args) {
this.time = new Date();
this.origin = origin || null;
this.target = target || null;
this.command = command || "X-MISSING-COMMAND";
this.arguments = args || new Array();
};
/// Wraps the WebSocket connection to the IRC bridge.
const Connection = function(webSockEndPointURL, onMessage) {
const self = this;
self.onMessage = onMessage;
self.connect = function(onConnect) {
self.onConnect = onConnect;
self.isFirstReceive = true;
self.socket = new WebSocket(webSockEndPointURL);
self.socket.onmessage = function(msg) {
let ircMessage = null;
try {
const json = JSON.parse(msg.data);
ircMessage = new IRCMessage(json["origin"], json["target"],
json["command"], json["arguments"])
}
catch(error) {
console.error("failed to parse incoming message:", error);
}
self.onMessage(ircMessage);
if (self.isFirstReceive) {
if (self.onConnect !== undefined && ircMessage["command"] === "NICK") {
// wait for 1st NICK
self.isFirstReceive = false;
self.onConnect();
}
}
}
};
self.json = function(object) {
self.socket.send(JSON.stringify(object));
};
self.call = function(command, ...args) {
self.json({ "command": command, "arguments": args });
};
self.send = function(stringMessage) { // TODO: remove me
self.call("NOTICE", "*", stringMessage);
};
}
"""
// Generated from Model/ClientUtils.js
// on Fri Jun 8 16:03:12 CEST 2018
//
let rsrc_ClientUtils_js =
"""
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-nio-irc open source project
//
// Copyright (c) 2018 ZeeZide GmbH. and the swift-nio-irc project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIOIRC project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
function formatDate(date) {
const now = new Date();
if (now.getYear() != date.getYear() ||
now.getMonth() != date.getMonth() ||
now.getDate() != date.getDate())
return date.toString();
const m = date.getMinutes();
return `${date.getHours()}:${m < 10 ? "0" + m : m}`
}
function htmlEscape(str) {
const div = document.createElement("div");
div.innerText = str;
return div.innerHTML;
}
"""
// Generated from ViewControllers/MainVC.js
// on Fri Jun 8 16:03:12 CEST 2018
//
let rsrc_MainVC_js =
"""
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-nio-irc open source project
//
// Copyright (c) 2018 ZeeZide GmbH. and the swift-nio-irc project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIOIRC project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
const MainVC = function(nick, webSockEndPointURL) {
const self = this;
self.serverTarget = "server";
self.targets = {
"server": Array()
};
self.activeTarget = self.serverTarget;
self.loadView = function() {
const self = this;
self.view = document.querySelector(".chat");
self.sidebar = new SidebarVC(self.onTargetChange);
self.messages = new MessagesVC(self.onLine);
self.sidebar.loadView(self.view);
self.messages.loadView(self.view);
};
self.systemNick = "✭";
self.nick = null; // nick is assigned by the server
self.commandMap = {
"/nick": function(name) {
self.connection.call("NICK", name);
},
"/join": function(target) {
if (!target.startsWith("#")) target = "#" + target;
const lc = target.toLowerCase();
self.connection.call("JOIN", target);
if (self.targets[lc] === undefined) self.targets[lc] = new Array();
self.sidebar.addChannelView(target, true);
},
"/part": function(name) {
if (name === undefined) {
if (!self.activeTarget.startsWith("#")) return;
name = self.activeTarget;
}
self.connection.call("PART", name);
},
"/msg": function(target, ...message) {
self.sendMessageToTarget(target, message.join(" "));
},
"/help": function() {
self.addItem(self.activeTarget, self.systemNick,
`Available commands:
/msg [nick or channel] message
/nick [nickname] - change your nickname, e.g. /nick Grandmaster
/join [channel] - join a channel, e.g. /join #ZeeZide
/part [channel]? - leave a channel, e.g. current /leave
`, false, true);
}
};
self.messageMap = {
"NOTICE": function(message) {
self.submit(this.systemNick, message.arguments[1] || "?", true);
},
"NICK": function(message) {
if (message.arguments.length == 0) return;
const name = message.arguments[0];
self.nick = name;
self.notice(`Changed nick to: ${name}`)
self.sidebar.setNick(name);
},
"JOIN": function(message) {
if (message.arguments.length < 1) return;
const channel = message.arguments[0];
if (message.origin === self.nick) {
const lc = channel.toLowerCase();
if (self.targets[lc] === undefined) self.targets[lc] = new Array();
self.sidebar.addChannelView(channel, false);
}
else {
self.addItem(channel, message.origin,
`${message.origin} joined ${channel}.`, true);
}
},
"PART": function(message) {
if (message.arguments.length < 1) return;
const channel = message.arguments[0];
if (message.origin === self.nick)
self.sidebar.removeChannelView(channel);
else {
self.addItem(channel, message.origin,
`${message.origin} left ${channel}.`, true, true);
}
},
"PRIVMSG": function(message) {
if (message.arguments.length < 2) return;
const target = message.arguments[0];
const text = message.arguments[1];
const sender = message.origin || "???";
if (target.toLowerCase() === self.nick.toLowerCase()) {
if (self.targets[sender.toLowerCase()] === undefined)
self.sidebar.addQueryView(sender, true);
self.addItem(sender, sender, text);
}
else
self.addItem(target, sender, text);
},
"Z-CHANNEL-TOPIC": function(message) {
// TODO: set topic of a channel.
}
};
// Add a chat message to the target arrays, and if the target is visible,
// to the list of chats. If not visible, update the unread count.
self.addItem = function(target, author, message, isSystem, isRead) {
const lc = target.toLowerCase();
const item = new ChatItem(message, author, new Date(), isSystem, isRead);
if (self.targets[lc] === undefined) self.targets[lc] = new Array();
self.targets[lc].push(item);
if (self.activeTarget === lc) {
self.messages.addItem(item);
}
else {
let unreadCount = 0;
self.targets[lc].forEach(function(item) {
if (!item.isRead) unreadCount++;
});
self.sidebar.updateUnreadCount(target, unreadCount);
}
}
self.submit = function(author, message, isSystem ) { // DEPRECATED
self.addItem(self.serverTarget, author, message, isSystem || false)
};
self.notice = function(message) { // DEPRECATED
self.addItem(self.serverTarget, this.systemNick, message, true)
};
// Called by the SidebarVC when a new target is clicked.
self.onTargetChange = function(newTarget) {
const lc = newTarget.toLowerCase()
if (self.activeTarget === lc) return;
self.activeTarget = lc;
self.messages.setTitle(newTarget);
self.messages.setChatItems(self.targets[lc]);
self.sidebar.updateUnreadCount(newTarget, 0);
};
// Called by the MessagesVC if the user enters a command line.
self.onLine = function(line) {
if (line.startsWith("/")) {
const pair = line.split(" ");
const cmd = pair[0].toLowerCase();
if (self.commandMap[cmd] !== undefined) {
// TODO: slice&apply
self.commandMap[cmd](pair[1], pair[2], pair[3], pair[4], pair[5]);
}
else {
// TBD: we could still send this to the server!
self.addItem(self.serverTarget, this.systemNick,
`unknown command: ${cmd}`, true);
}
return;
}
if (self.activeTarget === self.serverTarget) {
self.addItem(self.serverTarget, this.systemNick,
"Cannot send message to server itself, /help for help!",
true);
}
else {
self.sendMessageToTarget(self.activeTarget, line);
}
};
self.sendMessageToTarget = function(target, message) {
if (self.targets[target.toLowerCase()] === undefined) {
if (target.startsWith("#"))
return self.notice("You did not join this channel yet!");
self.sidebar.addQueryView(target, true);
}
self.connection.call("PRIVMSG", target || self.activeTarget, message);
self.addItem(target || self.activeTarget, self.nick, message, false, true);
};
// This is called when we receive a new IRC message from the bridge.
self.onMessage = function(message) {
if (self.messageMap[message.command] !== undefined)
self.messageMap[message.command](message);
else
self.submit(self.systemNick, `unknown message: ${message.command}`, true);
};
self.connection = new Connection(
webSockEndPointURL || "ws://localhost:1337/websocket",
self.onMessage
);
self.connect = function() {
self.notice("Connecting to IRC bridge ...");
self.connection.connect(function() {
{{script.vc.MainVC.onConnect}}
});
};
self.viewDidAppear = function() {
const self = this;
self.messages.viewDidAppear();
{{script.app.onStartup}}
self.connect();
};
}
"""
// Generated from ViewControllers/MessagesVC.js
// on Fri Jun 8 16:03:12 CEST 2018
//
let rsrc_MessagesVC_js =
"""
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-nio-irc open source project
//
// Copyright (c) 2018 ZeeZide GmbH. and the swift-nio-irc project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIOIRC project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
const MessagesVC = function(onLine) {
const self = this;
self.onLine = onLine;
self.loadView = function(parent) {
self.view = parent.querySelector("main");
self.form = self.view.querySelector("footer form");
self.messageListView = self.view.querySelector("section");
};
self.viewDidAppear = function() {
const self = this;
self.form.querySelector("input").focus();
self.form.addEventListener("submit",
function(e) {
e.preventDefault()
self.onFormSubmit();
}
);
};
self.setTitle = function(newTitle) {
self.view.querySelector("#chatTitle").innerText = newTitle;
};
self.clear = function() {
while (self.messageListView.firstChild) {
self.messageListView.removeChild(self.messageListView.firstChild);
}
};
/// Clear the whole history and set the new items.
self.setChatItems = function(newItems) {
self.clear();
if (newItems === undefined) return;
newItems.forEach(function(item) { self.addItem(item); });
};
self.onFormSubmit = function() {
const field = self.form.querySelector("input");
const line = field.value;
field.value = "";
if (self.onLine !== undefined) { self.onLine(line) }
};
self.addItem = function(item) {
const self = this;
const escapedAuthor = htmlEscape(item.sender);
const escapedMessage = htmlEscape(item.message);
const formattedDate = formatDate(item.time);
const timestamp = item.time.toString();
const newArticle = document.createElement("article")
newArticle.innerHTML = `
<svg data-jdenticon-value="${escapedAuthor}" class="avatar"></svg>
<div class="content">
<div class="info">
<span class="author">${escapedAuthor}</span>
<span class="date" value="${timestamp}">${formattedDate}</span>
</div>
<p>${escapedMessage}</p>
</div>
`;
if (item.isSystem) { newArticle.classList.add('system'); }
self.messageListView.appendChild(newArticle);
jdenticon.update(newArticle.querySelector("svg"));
if (newArticle.scrollIntoView) newArticle.scrollIntoView();
item.isRead = true;
};
};
"""
// Generated from ViewControllers/SidebarVC.js
// on Fri Jun 8 16:03:12 CEST 2018
//
let rsrc_SidebarVC_js =
"""
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-nio-irc open source project
//
// Copyright (c) 2018 ZeeZide GmbH. and the swift-nio-irc project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIOIRC project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
const SidebarVC = function(onTargetChange) {
const self = this;
self.onTargetChange = onTargetChange;
self.activeTarget = "server";
self.idPrefix = "";
self.loadView = function(parent) {
self.view = parent.querySelector("aside");
self.channeList = self.view.querySelector("#channelList");
self.queryList = self.view.querySelector("#queryList");
document.getElementById("server").addEventListener("click", function(e) {
e.preventDefault()
self.selectTarget("Server");
});
}
self.setNick = function(newNick) {
const nickView = self.view.querySelector("aside .info .username");
nickView.innerText = newNick;
};
self.selectTarget = function(newTarget) {
if (self.activeTarget === newTarget) { return }
self.view.querySelectorAll("section li").forEach(function(item) {
item.classList.remove('selected');
});
const lc = newTarget.toLowerCase();
const selection = document.getElementById(self.idPrefix + lc);
if (selection !== null) selection.classList.add('selected');
self.activeTarget = lc;
if (self.onTargetChange !== undefined)
self.onTargetChange(newTarget);
};
self.updateUnreadCount = function(target, count) {
const lc = target.toLowerCase();
const targetView = document.getElementById(self.idPrefix + lc);
if (targetView === null) return;
const countView = targetView.querySelector(".unreadcount");
if (countView === null) return;
if (count > 0) countView.innerText = "(" + count + ")"
else countView.innerText = "";
};
self.addTargetView = function(list, newTarget) {
const elementID = self.idPrefix + newTarget.toLowerCase();
if (document.getElementById(elementID))
return;
const childView = document.createElement("li")
childView.innerHTML = `
<span class="name"></span>
<span class="unreadcount"></span>
`;
childView.setAttribute("id", elementID);
const nameView = childView.querySelector(".name");
nameView.innerText = newTarget;
list.appendChild(childView);
childView.addEventListener("click", function(e) {
e.preventDefault()
self.selectTarget(newTarget);
});
};
self.removeTargetView = function(target) {
const lc = target.toLowerCase();
const targetView = document.getElementById(self.idPrefix + lc);
if (targetView === null) return;
targetView.parentNode.removeChild(targetView);
if (self.activeTarget === lc)
self.selectTarget("server");
};
self.removeChannelView = self.removeTargetView;
self.addChannelView = function(channel, select) {
self.addTargetView(self.channeList, channel);
self.view.querySelector("section.channels.list")
.classList.remove("hidden");
if (select) self.selectTarget(channel);
};
self.addQueryView = function(user, select) {
self.addTargetView(self.queryList, user);
self.view.querySelector("section.queries.list")
.classList.remove("hidden");
if (select) self.selectTarget(user);
};
};
"""
// Generated from Styles/Client.css
// on Fri Jun 8 16:03:12 CEST 2018
//
let rsrc_Client_css =
"""
/*
Thanks go to [@slashmo](https://github.com/slashmo) for the original
flexbox layout and CSS setup!
*/
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Helvetica Neue', sans-serif;
}
.chat {
height: 100vh;
width: 100%;
display: flex;
}
/* sidebar */
.chat aside {
width: 240px;
background: #13202D;
flex-shrink: 0;
color: white;
padding: 1em 0;
}
.chat aside section.info {
padding: 0 10px;
}
.chat aside section.info h1 {
font-size: 1.2em;
text-align: center;
color: #EEE;
}
.chat aside section h2 {
font-size: 0.9em;
padding-left: 1em;
color: #999;
text-align: center;
}
.chat aside section.info .username {
margin-top: 2px;
font-size: 0.8em;
text-align: center;
}
.chat aside section.list {
margin-top: 10px;
}
.chat aside section ul li {
list-style: none;
font-size: 0.9em;
padding: 6px 1.5em;
color: rgba(255, 255, 255, 0.6);
}
.chat aside section ul li.selected {
background: #52AC65;
color: white;
}
.chat aside section ul li:hover {
background: #666;
color: white;
}
.chat aside section ul li:hover.selected {
background: #428C45;
}
/* main */
.chat main {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
}
.chat main header {
padding: 0.7em 1em;
border-bottom: 1px solid #DDD;
}
.chat main header h1 {
font-size: 1.2em;
}
.chat main footer {
width: 100%;
padding: 10px;
border-top: 1px solid #DDD;
}
.chat footer input {
width: 100%;
border: 2px solid #EEE;
border-radius: 4px;
padding: 10px;
outline: none;
font-size: 14px;
}
/* body */
.chat main section {
padding: 10px 0;
flex-grow: 1;
overflow-y: scroll;
}
.chat main section article {
padding: 10px 20px;
display: flex;
}
.chat main section article .avatar {
width: 38px;
height: 38px;
background: #EEE;
border-radius: 4px;
border: 1px solid #DDD;
flex-shrink: 0;
}
.chat main section article.system {
color: #AAA;
}
.chat main section article .content {
margin-left: 10px;
}
.chat main section article .content .info {
font-size: 0.8em;
}
.chat main section article .content .info .author {
font-weight: 700;
}
.chat main section article .content p {
font-size: 0.9em;
margin-top: 4px;
}
.chat main section article .content p .mention {
background: #ECF5FB;
color: #3275B4;
padding: 2px;
border-radius: 4px;
}
.hidden {
visibility: hidden;
}
"""
// Generated from ClientInline.html
// on Fri Jun 8 16:03:12 CEST 2018
//
let rsrc_ClientInline_html =
"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>{{title}}</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<style>{{style}}</style>
</head>
<body>
<div class="chat">
<aside>
<section class="info">
<h1>{{title}}</h1>
<div class="username"></div>
</section>
<section class="server list">
<ul id='serverList'>
<li id="server" class="selected">Server</li>
</ul>
</section>
<section class="channels list hidden">
<h2>Channels</h2>
<ul id='channelList'>
</ul>
</section>
<section class="queries list hidden">
<h2>Direct Messages</h2>
<ul id='queryList'>
</ul>
</section>
</aside>
<main>
<header>
<h1 id='chatTitle'>Server</h1>
</header>
<section>
</section>
<footer>
<form>
<input type="text" placeholder="Enter IRC commands (or /help)" />
</form>
</footer>
</main>
</div>
<script>
(function() {
{{script.model.ClientUtils}}
{{script.model.ClientConnection}}
{{script.model.ChatItem}}
{{script.vc.SidebarVC}}
{{script.vc.MessagesVC}}
{{script.vc.MainVC}}
var controller = new MainVC("{{defaultNick}}", "{{endpoint}}");
controller.loadView();
controller.viewDidAppear();
}())
</script>
</body>
</html>
"""