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