-kiwi.model.Application = Backbone.Model.extend(new (function () {
- var that = this;
-
- this.initialize = function () {
- // Update `that` with this new Model object
- that = this;
-
- // Set the gateway up
- kiwi.gateway = new kiwi.model.Gateway();
- this.bindGatewayCommands(kiwi.gateway);
-
- //this.initializeLogin();
- this.initializeClient();
-
- kiwi.gateway.set('nick', 'kiwi_' + Math.ceil(Math.random() * 10000).toString());
- kiwi.gateway.connect('ate.anonnet.org', 6667, false, false, function () {
- console.log('gateway connected');
- });
-
-
- };
-
- this.initializeLogin = function () {
- // TODO: this
- // Show the server selection/login screen.
- // Once connected and logged in, then open the client screen (initializeClient)
- };
-
-
- this.initializeClient = function () {
- this.view = new kiwi.view.Application({model: this, el: this.get('container')})
-
-
- /**
- * Set the UI components up
- */
- this.controlbox = new kiwi.view.ControlBox({el: $('#controlbox')[0]});
- this.bindControllboxCommands(this.controlbox);
-
- // Container for the channels
- this.panels = new kiwi.model.PanelList();
- this.panels.server.view.show();
-
- // Rejigg the UI sizes
- this.view.doLayout();
- };
-
-
-
- this.bindGatewayCommands = function (gw) {
- gw.on('onmotd', function (event) {
- that.panels.server.addMsg(event.server, event.msg, 'motd');
- });
-
-
- gw.on('onconnect', function (event) {});
-
-
- gw.on('onjoin', function (event) {
- console.log(event);
- var c, members, user;
- c = that.panels.getByName(event.channel);
- if (!c) {
- c = new kiwi.model.Channel({name: event.channel});
- that.panels.add(c);
- }
-
- members = c.get('members');
- if (!members) return;
-
- user = new kiwi.model.Member({nick: event.nick, ident: event.ident, hostname: event.hostname});
- members.add(user);
- // TODO: highlight the new channel in some way
- });
-
-
- gw.on('onpart', function (event) {
- var channel, members, user;
-
- channel = that.panels.getByName(event.channel);
- if (!channel) return;
-
- // If this is us, close the panel
- if (event.nick === kiwi.gateway.get('nick')) {
- channel.close();
- return;
- }
-
- members = channel.get('members');
- if (!members) return;
-
- user = members.getByNick(event.nick);
- if (!user) return;
-
- members.remove(user);
- });
-
-
- gw.on('onmsg', function (event) {
- var panel,
- is_pm = (event.channel == kiwi.gateway.get('nick'));
-
- if (is_pm) {
- // If a panel isn't found for this PM, create one
- panel = that.panels.getByName(event.nick);
- if (!panel) {
- panel = new kiwi.model.Channel({name: event.nick});
- that.panels.add(panel);
- }
-
- } else {
- // If a panel isn't found for this channel, reroute to the
- // server panel
- panel = that.panels.getByName(event.channel);
- if (!panel) {
- panel = that.panels.server;
- }
- }
-
- panel.addMsg(event.nick, event.msg);
- });
-
-
- gw.on('onnotice', function (event) {
- kiwi.app.panels.server.addMsg('', event.msg, 'notice');
- });
-
-
- gw.on('ontopic', function (event) {
- var c;
- c = that.panels.getByName(event.channel);
- if (!c) return;
-
- // Set the channels topic
- c.set('topic', event.topic);
-
- // If this is the active channel, update the topic bar too
- if (c.get('name') === kiwi.current_panel.get('name')) {
- that.setCurrentTopic(event.topic);
- }
- });
-
-
- gw.on('onuserlist', function (event) {
- // TODO: This *SHOULD NOT* simply replace the current nicklist!
- // 1. Set a flag to mark the nicklist is changing.
- // 2. If the flag is not set, clear the list.
- // 3. On a userlist_end event, clear the flag
- var channel, users;
- channel = that.panels.getByName(event.channel);
-
- // If we didn't find a channel for this, may aswell leave
- if (!channel) return;
-
- users = [];
- _.each(event.users, function (item) {
- var user = new kiwi.model.Member({nick: item.nick, modes: item.modes});
- users.push(user);
- });
-
- // Update the members list with the new list
- channel.get('members').reset(users);
- });
- };
-
-
-
- /**
- * Bind to certain commands that may be typed into the control box
- */
- this.bindControllboxCommands = function (controlbox) {
- controlbox.on('unknown_command', this.unknownCommand);
-
- controlbox.on('command', this.allCommands);
- controlbox.on('command_msg', this.msgCommand);
-
- controlbox.on('command_join', this.joinCommand);
- controlbox.on('command_j', this.joinCommand);
-
- controlbox.on('command_part', this.partCommand);
- controlbox.on('command_p', this.partCommand);
-
- controlbox.on('command_nick', function (ev) {
- kiwi.gateway.changeNick(ev.params[0]);
- });
-
- controlbox.on('command_css', function (ev) {
- var queryString = '?reload=' + new Date().getTime();
- $('link[rel="stylesheet"]').each(function () {
- this.href = this.href.replace(/\?.*|$/, queryString);
- });
- });
- };
-
- this.unknownCommand = function (ev) {
- kiwi.gateway.raw(ev.command + ' ' + ev.params.join(' '));
- };
-
- this.allCommands = function (ev) {
- console.log('allCommands', ev);
- };
-
- this.joinCommand = function (ev) {
- var c = new kiwi.model.Channel({name: ev.params[0]});
- kiwi.app.panels.add(c);
- c.view.show();
- kiwi.gateway.join(ev.params[0]);
- };
-
- this.msgCommand = function (ev) {
- kiwi.current_panel.addMsg(kiwi.gateway.get('nick'), ev.params.join(' '));
- kiwi.gateway.privmsg(kiwi.current_panel.get('name'), ev.params.join(' '));
- };
-
- this.partCommand = function (ev) {
- if (ev.params.length === 0) {
- kiwi.gateway.part(kiwi.current_panel.get('name'));
- } else {
- _.each(ev.params, function (channel) {
- kiwi.gateway.part(channel);
- });
- }
- //kiwi.app.panels.remove(kiwi.current_panel);
- };
-
-
-
-
-
- this.setCurrentTopic = function (new_topic) {
- $('#topic input').val(new_topic);
- };
-
+kiwi.model.Application = Backbone.Model.extend(new (function () {\r
+ var that = this;\r
+\r
+ this.initialize = function () {\r
+ // Update `that` with this new Model object\r
+ that = this;\r
+\r
+ // Set the gateway up\r
+ kiwi.gateway = new kiwi.model.Gateway();\r
+ this.bindGatewayCommands(kiwi.gateway);\r
+\r
+ //this.initializeLogin();\r
+ this.initializeClient();\r
+\r
+ kiwi.gateway.set('nick', 'kiwi_' + Math.ceil(Math.random() * 10000).toString());\r
+ kiwi.gateway.connect('ate.anonnet.org', 6667, false, false, function () {\r
+ console.log('gateway connected');\r
+ });\r
+\r
+\r
+ };\r
+\r
+ this.initializeLogin = function () {\r
+ // TODO: this\r
+ // Show the server selection/login screen.\r
+ // Once connected and logged in, then open the client screen (initializeClient)\r
+ };\r
+\r
+\r
+ this.initializeClient = function () {\r
+ this.view = new kiwi.view.Application({model: this, el: this.get('container')})\r
+\r
+ \r
+ /**\r
+ * Set the UI components up\r
+ */\r
+ this.controlbox = new kiwi.view.ControlBox({el: $('#controlbox')[0]});\r
+ this.bindControllboxCommands(this.controlbox);\r
+\r
+ // Container for the channels\r
+ this.panels = new kiwi.model.PanelList();\r
+ this.panels.server.view.show();\r
+\r
+ // Rejigg the UI sizes\r
+ this.view.doLayout();\r
+ };\r
+\r
+\r
+\r
+ this.bindGatewayCommands = function (gw) {\r
+ gw.on('onmotd', function (event) {\r
+ that.panels.server.addMsg(event.server, event.msg, 'motd');\r
+ });\r
+\r
+\r
+ gw.on('onconnect', function (event) {});\r
+\r
+\r
+ gw.on('onjoin', function (event) {\r
+ console.log(event);\r
+ var c, members, user;\r
+ c = that.panels.getByName(event.channel);\r
+ if (!c) {\r
+ c = new kiwi.model.Channel({name: event.channel});\r
+ that.panels.add(c);\r
+ }\r
+\r
+ members = c.get('members');\r
+ if (!members) return;\r
+\r
+ user = new kiwi.model.Member({nick: event.nick, ident: event.ident, hostname: event.hostname});\r
+ members.add(user);\r
+ // TODO: highlight the new channel in some way\r
+ });\r
+\r
+\r
+ gw.on('onpart', function (event) {\r
+ var channel, members, user;\r
+\r
+ channel = that.panels.getByName(event.channel);\r
+ if (!channel) return;\r
+\r
+ // If this is us, close the panel\r
+ if (event.nick === kiwi.gateway.get('nick')) {\r
+ channel.close();\r
+ return;\r
+ }\r
+\r
+ members = channel.get('members');\r
+ if (!members) return;\r
+\r
+ user = members.getByNick(event.nick);\r
+ if (!user) return;\r
+\r
+ members.remove(user);\r
+ });\r
+\r
+\r
+ gw.on('onmsg', function (event) {\r
+ var panel,\r
+ is_pm = (event.channel == kiwi.gateway.get('nick'));\r
+\r
+ if (is_pm) {\r
+ // If a panel isn't found for this PM, create one\r
+ panel = that.panels.getByName(event.nick);\r
+ if (!panel) {\r
+ panel = new kiwi.model.Channel({name: event.nick});\r
+ that.panels.add(panel);\r
+ }\r
+\r
+ } else {\r
+ // If a panel isn't found for this channel, reroute to the\r
+ // server panel\r
+ panel = that.panels.getByName(event.channel);\r
+ if (!panel) {\r
+ panel = that.panels.server;\r
+ }\r
+ }\r
+ \r
+ panel.addMsg(event.nick, event.msg);\r
+ });\r
+\r
+\r
+ gw.on('onnotice', function (event) {\r
+ kiwi.app.panels.server.addMsg('', event.msg, 'notice');\r
+ });\r
+\r
+\r
+ gw.on('ontopic', function (event) {\r
+ var c;\r
+ c = that.panels.getByName(event.channel);\r
+ if (!c) return;\r
+\r
+ // Set the channels topic\r
+ c.set('topic', event.topic);\r
+\r
+ // If this is the active channel, update the topic bar too\r
+ if (c.get('name') === kiwi.current_panel.get('name')) {\r
+ that.setCurrentTopic(event.topic);\r
+ }\r
+ });\r
+\r
+\r
+ gw.on('onuserlist', function (event) {\r
+ var channel;\r
+ channel = that.panels.getByName(event.channel);\r
+\r
+ // If we didn't find a channel for this, may aswell leave\r
+ if (!channel) return;\r
+\r
+ channel.temp_userlist = channel.temp_userlist || [];\r
+ _.each(event.users, function (item) {\r
+ var user = new kiwi.model.Member({nick: item.nick, modes: item.modes});\r
+ channel.temp_userlist.push(user);\r
+ });\r
+ });\r
+\r
+\r
+ gw.on('onuserlist_end', function (event) {\r
+ var channel;\r
+ channel = that.panels.getByName(event.channel);\r
+\r
+ // If we didn't find a channel for this, may aswell leave\r
+ if (!channel) return;\r
+\r
+ // Update the members list with the new list\r
+ channel.get('members').reset(channel.temp_userlist || []);\r
+\r
+ // Clear the temporary userlist\r
+ delete channel.temp_userlist;\r
+ });\r
+ };\r
+\r
+\r
+\r
+ /**\r
+ * Bind to certain commands that may be typed into the control box\r
+ */\r
+ this.bindControllboxCommands = function (controlbox) {\r
+ controlbox.on('unknown_command', this.unknownCommand);\r
+\r
+ controlbox.on('command', this.allCommands);\r
+ controlbox.on('command_msg', this.msgCommand);\r
+\r
+ controlbox.on('command_join', this.joinCommand);\r
+ controlbox.on('command_j', this.joinCommand);\r
+\r
+ controlbox.on('command_part', this.partCommand);\r
+ controlbox.on('command_p', this.partCommand);\r
+\r
+ controlbox.on('command_nick', function (ev) {\r
+ kiwi.gateway.changeNick(ev.params[0]);\r
+ });\r
+\r
+ controlbox.on('command_css', function (ev) {\r
+ var queryString = '?reload=' + new Date().getTime();\r
+ $('link[rel="stylesheet"]').each(function () {\r
+ this.href = this.href.replace(/\?.*|$/, queryString);\r
+ });\r
+ });\r
+ };\r
+\r
+ this.unknownCommand = function (ev) {\r
+ kiwi.gateway.raw(ev.command + ' ' + ev.params.join(' '));\r
+ };\r
+\r
+ this.allCommands = function (ev) {\r
+ console.log('allCommands', ev);\r
+ };\r
+\r
+ this.joinCommand = function (ev) {\r
+ var c = new kiwi.model.Channel({name: ev.params[0]});\r
+ kiwi.app.panels.add(c);\r
+ c.view.show();\r
+ kiwi.gateway.join(ev.params[0]);\r
+ };\r
+\r
+ this.msgCommand = function (ev) {\r
+ kiwi.current_panel.addMsg(kiwi.gateway.get('nick'), ev.params.join(' '));\r
+ kiwi.gateway.privmsg(kiwi.current_panel.get('name'), ev.params.join(' '));\r
+ };\r
+\r
+ this.partCommand = function (ev) {\r
+ if (ev.params.length === 0) {\r
+ kiwi.gateway.part(kiwi.current_panel.get('name'));\r
+ } else {\r
+ _.each(ev.params, function (channel) {\r
+ kiwi.gateway.part(channel);\r
+ });\r
+ }\r
+ //kiwi.app.panels.remove(kiwi.current_panel);\r
+ };\r
+\r
+\r
+\r
+\r
+\r
+ this.setCurrentTopic = function (new_topic) {\r
+ $('#topic input').val(new_topic);\r
+ };\r
+\r
})());
\ No newline at end of file