diff --git a/src/core/chatRoom.js b/src/core/chatRoom.js index 2d842bd1..87cc227a 100644 --- a/src/core/chatRoom.js +++ b/src/core/chatRoom.js @@ -4,6 +4,7 @@ * Authors: * - Patrick Stadler * - Michael Weibel + * - Melissa Noelle * * Copyright: * (c) 2011 Amiado Group AG. All rights reserved. @@ -19,24 +20,91 @@ * Parameters: * (String) roomJid - Room jid */ -Candy.Core.ChatRoom = function(roomJid) { - /** Object: room - * Object containing roomJid and name. - */ - this.room = { - jid: roomJid, - name: Strophe.getNodeFromJid(roomJid) - }; - - /** Variable: user - * Current local user of this room. - */ - this.user = null; - - /** Variable: Roster - * Candy.Core.ChatRoster instance - */ - this.roster = new Candy.Core.ChatRoster(); +Candy.Core.ChatRoom = function(options) { + /** Object: room + * (String) jid + * (String) name + * (String) type (chat|groupchat) + * (String) topic + * (Integer) messageCount + */ + this.room = { + jid: options.roomJid, + name: options.roomName || Strophe.getNodeFromJid(options.roomJid), + type: options.roomType, + topic: null, + messageCount: 0 + }; + + /** Variable: user + * Current local user of this room. + */ + this.user = null; + + /** Variable: Roster + * Candy.Core.ChatRoster instance + */ + this.roster = new Candy.Core.ChatRoster(); + + // DOM-related information for this room. + this.dom = { + id: options.domId, + scrollPosition: options.scrollPosition || -1, + tab: null, + form: null + } +}; + +Candy.Core.ChatRoom.prototype.setTopic = function(topic) { + this.room.topic = topic; +}; + +Candy.Core.ChatRoom.prototype.getType = function() { + return this.room.type; +}; + +Candy.Core.ChatRoom.prototype.incrementMessageCount = function() { + this.room.messageCount += 1; + return this.room.messageCount; +}; + +Candy.Core.ChatRoom.prototype.getUserCount = function() { + return Object.keys(this.roster.items).length; +}; + +Candy.Core.ChatRoom.prototype.getDomElement = function() { + return this.Room.getPane(this.room.jid); +}; + +Candy.Core.ChatRoom.prototype.getDomTab = function() { + return this.Chat.getTab(roomJid); +}; + +Candy.Core.ChatRoom.prototype.getDomForm = function() { + return this.Room.getPane(roomJid, '.message-form'); +}; + +Candy.Core.ChatRoom.prototype.addRoomDom = function(chatRoomContainerSelector) { + $(chatRoomContainerSelector).append(Mustache.to_html(Candy.View.Template.Room.pane, { + roomId: this.dom.id, + roomJid: this.room.jid, + roomType: this.room.type, + form: { + _messageSubmit: $.i18n._('messageSubmit') + }, + roster: { + _userOnline: $.i18n._('userOnline') + } + }, { + roster: Candy.View.Template.Roster.pane, + messages: Candy.View.Template.Message.pane, + form: Candy.View.Template.Room.form + })); + + this.Chat.addTab(this.room.jid, roomName, roomType); + this.Room.getPane(this.room.jid, '.message-form').submit(Candy.View.Pane.Message.submit); + + return this.getDomElement(); }; /** Function: setUser @@ -46,7 +114,7 @@ Candy.Core.ChatRoom = function(roomJid) { * (Candy.Core.ChatUser) user - Chat user */ Candy.Core.ChatRoom.prototype.setUser = function(user) { - this.user = user; + this.user = user; }; /** Function: getUser @@ -56,7 +124,7 @@ Candy.Core.ChatRoom.prototype.setUser = function(user) { * (Object) - Candy.Core.ChatUser instance or null */ Candy.Core.ChatRoom.prototype.getUser = function() { - return this.user; + return this.user; }; /** Function: getJid @@ -66,7 +134,7 @@ Candy.Core.ChatRoom.prototype.getUser = function() { * (String) - Room jid */ Candy.Core.ChatRoom.prototype.getJid = function() { - return this.room.jid; + return this.room.jid; }; /** Function: setName @@ -76,7 +144,7 @@ Candy.Core.ChatRoom.prototype.getJid = function() { * (String) name - Room name */ Candy.Core.ChatRoom.prototype.setName = function(name) { - this.room.name = name; + this.room.name = name; }; /** Function: getName @@ -86,7 +154,7 @@ Candy.Core.ChatRoom.prototype.setName = function(name) { * (String) - Room name */ Candy.Core.ChatRoom.prototype.getName = function() { - return this.room.name; + return this.room.name; }; /** Function: setRoster @@ -96,7 +164,7 @@ Candy.Core.ChatRoom.prototype.getName = function() { * (Candy.Core.ChatRoster) roster - Chat roster */ Candy.Core.ChatRoom.prototype.setRoster = function(roster) { - this.roster = roster; + this.roster = roster; }; /** Function: getRoster @@ -106,5 +174,5 @@ Candy.Core.ChatRoom.prototype.setRoster = function(roster) { * (Candy.Core.ChatRoster) - instance */ Candy.Core.ChatRoom.prototype.getRoster = function() { - return this.roster; + return this.roster; }; diff --git a/src/view/pane/room.js b/src/view/pane/room.js index d4d4148d..4f1f4a6b 100644 --- a/src/view/pane/room.js +++ b/src/view/pane/room.js @@ -73,27 +73,17 @@ Candy.View.Pane = (function(self, $) { } var roomId = Candy.Util.jidToId(roomJid); - self.Chat.rooms[roomJid] = {id: roomId, usercount: 0, name: roomName, type: roomType, messageCount: 0, scrollPosition: -1, targetJid: roomJid}; - $('#chat-rooms').append(Mustache.to_html(Candy.View.Template.Room.pane, { - roomId: roomId, + var roomObject = new Candy.Core.ChatRoom({ roomJid: roomJid, + roomName: roomName, roomType: roomType, - form: { - _messageSubmit: $.i18n._('messageSubmit') - }, - roster: { - _userOnline: $.i18n._('userOnline') - } - }, { - roster: Candy.View.Template.Roster.pane, - messages: Candy.View.Template.Message.pane, - form: Candy.View.Template.Room.form - })); - self.Chat.addTab(roomJid, roomName, roomType); - self.Room.getPane(roomJid, '.message-form').submit(self.Message.submit); - - evtData.element = self.Room.getPane(roomJid); + domId: roomId, + }); + + self.Chat.rooms[roomJid] = roomObject; + + roomObject.addRoomDom('#chat-rooms'); /** Event: candy:view.room.after-add * After initialising a room