Commit | Line | Data |
---|---|---|
2dd6a025 D |
1 | kiwi.model.Application = Backbone.Model.extend(new (function () { |
2 | var that = this; | |
3 | ||
4 | this.initialize = function () { | |
5 | // Update `that` with this new Model object | |
6 | that = this; | |
7 | ||
8 | // Set the gateway up | |
9 | kiwi.gateway = new kiwi.model.Gateway(); | |
10 | this.bindGatewayCommands(kiwi.gateway); | |
11 | ||
12 | //this.initializeLogin(); | |
13 | this.initializeClient(); | |
14 | ||
15 | kiwi.gateway.set('nick', 'kiwi_' + Math.ceil(Math.random() * 10000).toString()); | |
16 | kiwi.gateway.connect('ate.anonnet.org', 6667, false, false, function () { | |
17 | console.log('gateway connected'); | |
18 | }); | |
19 | ||
20 | ||
21 | }; | |
22 | ||
23 | this.initializeLogin = function () { | |
24 | // TODO: this | |
25 | // Show the server selection/login screen. | |
26 | // Once connected and logged in, then open the client screen (initializeClient) | |
27 | }; | |
28 | ||
29 | ||
30 | this.initializeClient = function () { | |
31 | this.view = new kiwi.view.Application({model: this, el: this.get('container')}) | |
32 | ||
33 | ||
34 | /** | |
35 | * Set the UI components up | |
36 | */ | |
37 | this.controlbox = new kiwi.view.ControlBox({el: $('#controlbox')[0]}); | |
38 | this.bindControllboxCommands(this.controlbox); | |
39 | ||
40 | // Container for the channels | |
41 | this.panels = new kiwi.model.PanelList(); | |
42 | this.panels.server.view.show(); | |
43 | ||
44 | // Rejigg the UI sizes | |
45 | this.view.doLayout(); | |
46 | }; | |
47 | ||
48 | ||
49 | ||
50 | this.bindGatewayCommands = function (gw) { | |
51 | gw.on('onmotd', function (event) { | |
52 | that.panels.server.addMsg(event.server, event.msg, 'motd'); | |
53 | }); | |
54 | ||
55 | ||
56 | gw.on('onconnect', function (event) {}); | |
57 | ||
58 | ||
59 | gw.on('onjoin', function (event) { | |
60 | console.log(event); | |
61 | var c, members, user; | |
62 | c = that.panels.getByName(event.channel); | |
63 | if (!c) { | |
64 | c = new kiwi.model.Channel({name: event.channel}); | |
65 | that.panels.add(c); | |
66 | } | |
67 | ||
68 | members = c.get('members'); | |
69 | if (!members) return; | |
70 | ||
71 | user = new kiwi.model.Member({nick: event.nick, ident: event.ident, hostname: event.hostname}); | |
72 | members.add(user); | |
73 | // TODO: highlight the new channel in some way | |
74 | }); | |
75 | ||
76 | ||
77 | gw.on('onpart', function (event) { | |
78 | var channel, members, user; | |
79 | ||
80 | channel = that.panels.getByName(event.channel); | |
81 | if (!channel) return; | |
82 | ||
83 | members = channel.get('members'); | |
84 | if (!members) return; | |
85 | ||
86 | user = members.getByNick(event.nick); | |
87 | if (!user) return; | |
88 | ||
89 | members.remove(user); | |
90 | }); | |
91 | ||
92 | ||
93 | gw.on('onmsg', function (event) { | |
94 | var panel, | |
95 | is_pm = (event.channel == kiwi.gateway.get('nick')); | |
96 | ||
97 | if (is_pm) { | |
98 | // If a panel isn't found for this PM, create one | |
99 | panel = that.panels.getByName(event.nick); | |
100 | if (!panel) { | |
101 | panel = new kiwi.model.Channel({name: event.nick}); | |
102 | that.panels.add(panel); | |
103 | } | |
104 | ||
105 | } else { | |
106 | // If a panel isn't found for this channel, reroute to the | |
107 | // server panel | |
108 | panel = that.panels.getByName(event.channel); | |
109 | if (!panel) { | |
110 | panel = that.panels.server; | |
111 | } | |
112 | } | |
113 | ||
114 | panel.addMsg(event.nick, event.msg); | |
115 | }); | |
116 | ||
117 | ||
118 | gw.on('onnotice', function (event) { | |
119 | kiwi.app.panels.server.addMsg('', event.msg, 'notice'); | |
120 | }); | |
121 | ||
122 | ||
123 | gw.on('ontopic', function (event) { | |
124 | var c; | |
125 | c = that.panels.getByName(event.channel); | |
126 | if (!c) return; | |
127 | ||
128 | // Set the channels topic | |
129 | c.set('topic', event.topic); | |
130 | ||
131 | // If this is the active channel, update the topic bar too | |
132 | if (c.get('name') === kiwi.current_panel.get('name')) { | |
133 | that.setCurrentTopic(event.topic); | |
134 | } | |
135 | }); | |
136 | ||
137 | ||
138 | gw.on('onuserlist', function (event) { | |
139 | // TODO: This *SHOULD NOT* simply replace the current nicklist! | |
140 | // 1. Set a flag to mark the nicklist is changing. | |
141 | // 2. If the flag is not set, clear the list. | |
142 | // 3. On a userlist_end event, clear the flag | |
143 | var channel, users; | |
144 | channel = that.panels.getByName(event.channel); | |
145 | ||
146 | // If we didn't find a channel for this, may aswell leave | |
147 | if (!channel) return; | |
148 | ||
149 | users = []; | |
150 | _.each(event.users, function (item) { | |
151 | var user = new kiwi.model.Member({nick: item.nick, modes: item.modes}); | |
152 | users.push(user); | |
153 | }); | |
154 | ||
155 | // Update the members list with the new list | |
156 | channel.get('members').reset(users); | |
157 | }); | |
158 | }; | |
159 | ||
160 | ||
161 | ||
162 | /** | |
163 | * Bind to certain commands that may be typed into the control box | |
164 | */ | |
165 | this.bindControllboxCommands = function (controlbox) { | |
166 | controlbox.on('command', this.allCommands); | |
167 | controlbox.on('command_msg', this.msgCommand); | |
168 | ||
169 | controlbox.on('command_join', this.joinCommand); | |
170 | controlbox.on('command_j', this.joinCommand); | |
171 | ||
172 | controlbox.on('command_part', this.partCommand); | |
173 | controlbox.on('command_p', this.partCommand); | |
174 | ||
175 | controlbox.on('command_nick', function (ev) { | |
176 | kiwi.gateway.changeNick(ev.params[0]); | |
177 | }); | |
178 | ||
179 | controlbox.on('command_css', function (ev) { | |
180 | var queryString = '?reload=' + new Date().getTime(); | |
181 | $('link[rel="stylesheet"]').each(function () { | |
182 | this.href = this.href.replace(/\?.*|$/, queryString); | |
183 | }); | |
184 | }); | |
185 | }; | |
186 | this.allCommands = function (ev) { | |
187 | console.log('allCommands', ev); | |
188 | }; | |
189 | ||
190 | this.joinCommand = function (ev) { | |
191 | var c = new kiwi.model.Channel({name: ev.params[0]}); | |
192 | kiwi.app.panels.add(c); | |
193 | c.view.show(); | |
194 | kiwi.gateway.join(ev.params[0]); | |
195 | }; | |
196 | ||
197 | this.msgCommand = function (ev) { | |
198 | kiwi.current_panel.addMsg(kiwi.gateway.get('nick'), ev.params.join(' ')); | |
199 | kiwi.gateway.privmsg(kiwi.current_panel.get('name'), ev.params.join(' ')); | |
200 | }; | |
201 | ||
202 | this.partCommand = function (ev) { | |
203 | if (ev.params.length === 0) { | |
204 | kiwi.gateway.part(kiwi.current_panel.get('name')); | |
205 | } else { | |
206 | _.each(ev.params, function (channel) { | |
207 | kiwi.gateway.part(channel); | |
208 | }); | |
209 | } | |
210 | //kiwi.app.panels.remove(kiwi.current_panel); | |
211 | }; | |
212 | ||
213 | ||
214 | ||
215 | ||
216 | ||
217 | this.setCurrentTopic = function (new_topic) { | |
218 | $('#topic input').val(new_topic); | |
219 | }; | |
220 | ||
221 | })()); |