/www/src/Conversation.js
enyo.kind({
    name: "Conversation",
    kind: "FittableRows",
    classes: "conversation",
    messages: [],
    properties: {
        jid: '',
        name: '',
    },
    components: [
        {kind: "onyx.Toolbar", components: [
            {kind: "StatusCircle"},
            {name: "conversationJid", content: 'None'}
        ]},
        {name: "messageList", kind: "List", onSetupItem: "messageItemSetup", fit: 1, components: [
            {name: "messageItem", classes: "message-item", components: [
                {classes: "message-image-container", components: [
                    {name: "messageBuddyImage", classes: "buddy-image"}
                ]},
                {components: [
                    {name: "messageData", classes: "message-data"},
                    {name: "messageTimestamp", classes: "message-timestamp"}
                ]}
            ]}
        ]},
        {kind: "onyx.Toolbar", classes: "table-fit", components: [
            {components: [
                {kind: "onyx.Grabber"},
            ]},
            {kind: "onyx.InputDecorator", alwaysLooksFocused: true, classes: "conversation-input-decorator table-fit-fill", components: [
                {name: "conversationInput", kind: "onyx.Input", classes: "conversation-input", onkeypress: "inputKeypress"}
            ]}
        ]},
        {kind: "Signals", onMessageReceived: "messageReceived"}
    ],
    create: function() {
        this.inherited(arguments);
        this.updateConversationList();
    },
    setJid: function(jid) {
        this.jid = jid;
        this.messages = conversationStore.getConversation(jid);
        this.$.conversationJid.setContent(jid);
        this.updateConversationList();
    },
    setName: function(name) {
        this.name = name;
        this.$.conversationJid.setContent(name || this.jid);
    },
    updateConversationList: function() {
        this.$.messageList.setCount(this.messages.length);
        this.$.messageList.refresh();
        setTimeout(function() {
            this.$.messageList.scrollToBottom();
        }.bind(this));
    },
    messageReceived: function(inSender, inEvent) {
        if (inEvent.jid == this.jid) {
            var data = inEvent;
            this.messages.push(data);
            this.updateConversationList();
        }
    },
    messageItemSetup: function(inSender, inEvent) {
        var row = this.messages[inEvent.index];
        if (!row) return;
        this.$.messageData.setContent(row.message);
        var timestamp = new Date(row.receivedTimestamp).toLocaleTimeString();
        this.$.messageTimestamp.setContent(timestamp);
        this.$.messageItem.addRemoveClass('outbound', row.outbound);
    },
    inputKeypress: function(inSender, inEvent) {
        if (inEvent.keyCode == 13) {
            var message = this.$.conversationInput.getValue();
            this.$.conversationInput.setValue('');
            var data = {
                outbound: true,
                jid: this.jid,
                receivedTimestamp: (new Date()).getTime(),
                message: message
            };
            this.messages.push(data);
            enyo.Signals.send('onMessageSent', data);
            this.updateConversationList();
        }
    }
});