This repository has been archived by the owner on Apr 13, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channels.go
304 lines (257 loc) · 8.5 KB
/
channels.go
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
// mauIRC - The original mauIRC web frontend
// Copyright (C) 2016 Tulir Asokan
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package ui contains UI-related functions
package ui
import (
"fmt"
"github.com/gopherjs/jquery"
"maunium.net/go/mauirc-common/messages"
"maunium.net/go/mauirc/data"
"maunium.net/go/mauirc/templates"
"maunium.net/go/mauirc/util/console"
"regexp"
"strings"
)
// GetActiveChannel gets the name of the active channel
func GetActiveChannel() string {
active := jq(".channel-switcher.active > .channel-switcher-name")
if active.Length > 0 {
return active.Text()
}
return ""
}
// GetActiveNetwork gets the name of the active network
func GetActiveNetwork() string {
active := jq(".network-switcher.activenet > .network-switcher-name")
if active.Length > 0 {
return strings.TrimSpace(active.Text())
}
return ""
}
// GetActiveChannelObj gets the active channel container
func GetActiveChannelObj() jquery.JQuery {
return jq(".channel-container:not(.hidden)")
}
// GetActiveNetworkObj gets the active network container
func GetActiveNetworkObj() jquery.JQuery {
return jq(".network-container:not(.hidden)")
}
// GetChannel returns the container for the channel with the given name
func GetChannel(network, channel string) jquery.JQuery {
return jq(fmt.Sprintf("#chan-%s-%s", NetworkFilter(network), ChannelFilter(channel)))
}
// GetNetwork returns the container for the network with the given name
func GetNetwork(network string) jquery.JQuery {
return jq(fmt.Sprintf("#net-%s", NetworkFilter(network)))
}
var chanfilter = regexp.MustCompile(`([\#\*\.])`)
// ChannelFilter filters the given channel name into a string usable in jQuery
func ChannelFilter(channel string) string {
return strings.ToLower(chanfilter.ReplaceAllString(channel, "\\$1"))
}
// NetworkFilter filters the given network name into a string usable in jQuery
func NetworkFilter(network string) string {
return strings.ToLower(network)
}
// OpenChannelAdder opens the channel adder for the given network
func OpenChannelAdder(network string) {
network = NetworkFilter(network)
var oldAdder = jq(fmt.Sprintf("#channel-adder-%s", network))
if oldAdder.Length != 0 {
oldAdder.Focus()
return
}
templates.Append("channel-adder", fmt.Sprintf("#chanswitchers-%s", network), network)
jq(fmt.Sprintf("#add-channel-%s", network)).AddClass("hidden")
adder := jq(fmt.Sprintf("#channel-adder-%s", network))
adder.Underlying().Call("easyAutocomplete", map[string]interface{}{
"data": data.MustGetNetwork(network).ChannelNames,
"placeholder": "Nick or #channel",
"list": map[string]interface{}{
"maxNumberOfElements": 10,
"match": map[string]interface{}{"enabled": true},
"sort": map[string]interface{}{"enabled": true},
"onChooseEvent": func() {
FinishChannelAdding(network)
},
"showAnimation": map[string]interface{}{
"type": "slide",
"time": 400,
},
},
})
adder.Focus()
}
// CancelChannelAdding closes the channel adder
func CancelChannelAdding(network string) {
jq(fmt.Sprintf("#channel-adder-wrapper-%s", network)).Remove()
jq(fmt.Sprintf("#add-channel-%s", network)).RemoveClass("hidden")
}
// FinishChannelAdding finishes adding a channel
func FinishChannelAdding(network string) {
adder := jq(fmt.Sprintf("#channel-adder-%s", network))
if adder.Length == 0 {
return
}
name := strings.TrimSpace(adder.Val())
jq(fmt.Sprintf("#channel-adder-wrapper-%s", network)).Remove()
if len(name) == 0 {
return
}
if name[0] == '#' {
data.Messages <- messages.Container{
Type: messages.MsgMessage,
Object: messages.Message{
Network: network,
Channel: name,
Command: "join",
Message: "Joining",
},
}
}
jq(fmt.Sprintf("#add-channel-%s", network)).RemoveClass("hidden")
OpenChannel(network, name, true)
SwitchTo(network, name)
}
// OpenNetwork opens a network
func OpenNetwork(network string) {
network = NetworkFilter(network)
if jq(fmt.Sprintf("#net-%s", network)).Length != 0 {
return
}
templates.Append("network", "#messages", network)
templates.Append("network-switcher", "#networks", network)
CreateRawIO(network)
}
// OpenChannel opens a channel
func OpenChannel(network, channel string, byUser bool) {
network = NetworkFilter(network)
chanLower := strings.ToLower(channel)
netObj := GetNetwork(network)
if netObj.Length == 0 {
OpenNetwork(network)
netObj = GetNetwork(network)
}
if GetChannel(network, channel).Length != 0 {
return
}
if byUser {
data.Messages <- messages.Container{
Type: messages.MsgOpen,
Object: messages.OpenCloseChannel{
Network: network,
Channel: channel,
},
}
}
templates.AppendObj("channel", netObj, fmt.Sprintf("chan-%s-%s", network, chanLower))
templates.Append("channel-switcher", fmt.Sprintf("#chanswitchers-%s", network), map[string]interface{}{
"Channel": chanLower,
"ChannelReal": channel,
"Network": network,
})
}
// CloseChannel closes a channel
func CloseChannel(network, channel string) {
network = NetworkFilter(network)
chanFiltered := ChannelFilter(channel)
netObj := GetNetwork(network)
if netObj.Length == 0 {
return
}
chanObj := GetChannel(network, channel)
if chanObj.Length == 0 {
return
}
if GetActiveNetwork() == network && GetActiveChannel() == channel {
SwitchToClear()
}
chanObj.Remove()
jq(fmt.Sprintf("#switchto-%s-%s", network, chanFiltered)).Remove()
}
// SwitchToClear switches to a empty message view
func SwitchToClear() {
GetActiveChannelObj().AddClass("hidden")
GetActiveNetworkObj().AddClass("hidden")
jq(".channel-switcher.active").RemoveClass("active")
jq(".network-switcher.activenet").RemoveClass("activenet")
jq("#title").SetText("")
OpenMessageView()
}
// SwitchTo switches to the given channel on the given network
func SwitchTo(network, channel string) {
console.Log("Switching to channel %s @ %s", channel, network)
SwitchToClear()
jq("#message-text").Focus()
chanObj := GetChannel(network, channel)
if !data.MustGetChannel(network, channel).HistoryFetched && chanObj.Find(".invite-wrapper").Length == 0 {
GetHistory(network, channel, 512)
}
jq(fmt.Sprintf("#switchnet-%s", network)).AddClass("activenet")
GetNetwork(network).RemoveClass("hidden")
chanObj.RemoveClass("hidden")
chanSwitcher := jq(fmt.Sprintf("#switchto-%s-%s", NetworkFilter(network), ChannelFilter(channel)))
chanSwitcher.RemoveClass("new-messages")
chanSwitcher.AddClass("active")
UpdateTitle()
UpdateUserlist()
ScrollDown()
}
// OpenPM opens a private query with the given user on the given network
func OpenPM(network, user string) {
OpenChannel(network, user, true)
SwitchTo(network, user)
}
// OpenMessageView switches to the message view
func OpenMessageView() {
jq("#networks").AddClass("hidden-medium-down")
jq("#messaging").RemoveClass("hidden-medium-down")
jq("#userlist").AddClass("hidden-medium-down")
}
// OpenNetworksView switches to the network list view
func OpenNetworksView() {
jq("#networks").RemoveClass("hidden-medium-down")
jq("#messaging").AddClass("hidden-medium-down")
jq("#userlist").AddClass("hidden-medium-down")
}
// OpenUserlistView switches to the userlist view
func OpenUserlistView() {
jq("#networks").AddClass("hidden-medium-down")
jq("#messaging").AddClass("hidden-medium-down")
jq("#userlist").RemoveClass("hidden-medium-down")
}
// ToggleNetworksView toggles between the network and message view
func ToggleNetworksView() {
if jq("#networks").HasClass("hidden-medium-down") {
OpenNetworksView()
} else {
OpenMessageView()
}
}
// ToggleUserlistView toggles between the userlist and message view
func ToggleUserlistView() {
if jq("#userlist").HasClass("hidden-medium-down") {
OpenUserlistView()
} else {
OpenMessageView()
}
}
// SetNetworkConnected sets the connected status of a network
func SetNetworkConnected(net string, connected bool) {
net = NetworkFilter(net)
if connected {
jq(fmt.Sprintf("#switchnet-%s", net)).RemoveClass("disconnected")
} else {
jq(fmt.Sprintf("#switchnet-%s", net)).AddClass("disconnected")
}
}