var ChatPage; var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; ChatPage = (function() { function ChatPage(session) { this.session = session; this.session.onRoster(__bind(function() { return this.roster(); }, this)); this.session.onCard(__bind(function(c) { return this.card(c); }, this)); this.session.onMessage(__bind(function(m) { return this.message(m); }, this)); this.session.onPresence(__bind(function(p) { return this.presence(p); }, this)); this.chats = {}; this.currentContact = null; } ChatPage.prototype.datef = function(millis) { var d, hour, meridian, minutes; d = new Date(millis); meridian = d.getHours() >= 12 ? ' pm' : ' am'; hour = d.getHours() > 12 ? d.getHours() - 12 : d.getHours(); if (hour === 0) { hour = 12; } minutes = d.getMinutes() + ''; if (minutes.length === 1) { minutes = '0' + minutes; } return hour + ':' + minutes + meridian; }; ChatPage.prototype.card = function(card) { return this.eachContact(card.jid, __bind(function(node) { return $('.vcard-img', node).attr('src', this.session.avatar(card.jid)); }, this)); }; ChatPage.prototype.roster = function() { var contact, found, jid, node, roster, setName, _ref, _results; roster = $('#roster'); $('li', roster).each(__bind(function(ix, node) { var jid; jid = $(node).attr('data-jid'); if (!this.session.roster[jid]) { return $(node).remove(); } }, this)); setName = function(node, contact) { $('.text', node).text(contact.name || contact.jid); return node.attr('data-name', contact.name || ''); }; _ref = this.session.roster; _results = []; for (jid in _ref) { contact = _ref[jid]; found = $("#roster li[data-jid='" + jid + "']"); setName(found, contact); _results.push(found.length === 0 ? (node = $("
  • \n \n Offline\n \n \""\n
  • ").appendTo(roster), setName(node, contact), node.click(__bind(function(event) { return this.selectContact(event); }, this))) : void 0); } return _results; }; ChatPage.prototype.message = function(message) { var bottom, chat, from, me; this.queueMessage(message); me = message.from === this.session.jid(); from = message.from.split('/')[0]; if (me || from === this.currentContact) { bottom = this.atBottom(); this.appendMessage(message); if (bottom) { return this.scroll(); } } else { chat = this.chat(message.from); chat.unread++; return this.eachContact(from, function(node) { return $('.unread', node).text(chat.unread).show(); }); } }; ChatPage.prototype.eachContact = function(jid, callback) { var node, _i, _len, _ref, _results; _ref = $("#roster li[data-jid='" + jid + "']").get(); _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { node = _ref[_i]; _results.push(callback($(node))); } return _results; }; ChatPage.prototype.appendMessage = function(message) { var contact, from, name, node; from = message.from.split('/')[0]; contact = this.session.roster[from]; name = contact ? contact.name || from : from; if (message.from === this.session.jid()) { name = 'Me'; } node = $("
  • \n

    \n \""\n \n
  • ").appendTo('#messages'); $('p', node).text(message.text); $('.author', node).text(name); return node.fadeIn(200); }; ChatPage.prototype.queueMessage = function(message) { var chat, full, me; me = message.from === this.session.jid(); full = message[me ? 'to' : 'from']; chat = this.chat(full); chat.jid = full; return chat.messages.push(message); }; ChatPage.prototype.chat = function(jid) { var bare, chat; bare = jid.split('/')[0]; chat = this.chats[bare]; if (!chat) { chat = { jid: jid, messages: [], unread: 0 }; this.chats[bare] = chat; } return chat; }; ChatPage.prototype.presence = function(presence) { var contact, from, node; from = presence.from.split('/')[0]; if (from === this.session.bareJid()) { return; } if (!presence.type || presence.offline) { contact = this.session.roster[from]; this.eachContact(from, function(node) { $('.status-msg', node).text(contact.status()); if (contact.offline()) { return node.addClass('offline'); } else { return node.removeClass('offline'); } }); } if (presence.offline) { this.chat(from).jid = from; } if (presence.type === 'subscribe') { node = $("
  • \n
    \n

    Buddy Approval

    \n

    " + presence.from + " wants to add you as a buddy.

    \n
    \n \n \n
    \n
    \n
  • ").appendTo('#notifications'); node.fadeIn(200); $('form', node).submit(__bind(function() { return this.acceptContact(node, presence.from); }, this)); return $('input[type="button"]', node).click(__bind(function() { return this.rejectContact(node, presence.from); }, this)); } }; ChatPage.prototype.acceptContact = function(node, jid) { node.fadeOut(200, function() { return node.remove(); }); this.session.sendSubscribed(jid); this.session.sendSubscribe(jid); return false; }; ChatPage.prototype.rejectContact = function(node, jid) { node.fadeOut(200, function() { return node.remove(); }); return this.session.sendUnsubscribed(jid); }; ChatPage.prototype.selectContact = function(event) { var chat, contact, jid, messages, msg, _i, _len; jid = $(event.currentTarget).attr('data-jid'); contact = this.session.roster[jid]; if (this.currentContact === jid) { return; } this.currentContact = jid; $('#roster li').removeClass('selected'); $(event.currentTarget).addClass('selected'); $('#chat-title').text('Chat with ' + (contact.name || contact.jid)); $('#messages').empty(); chat = this.chats[jid]; messages = []; if (chat) { messages = chat.messages; chat.unread = 0; this.eachContact(jid, function(node) { return $('.unread', node).text('').hide(); }); } for (_i = 0, _len = messages.length; _i < _len; _i++) { msg = messages[_i]; this.appendMessage(msg); } this.scroll(); $('#remove-contact-msg').html("Are you sure you want to remove " + ("" + this.currentContact + " from your buddy list?")); $('#remove-contact-form .buttons').fadeIn(200); $('#edit-contact-jid').text(this.currentContact); $('#edit-contact-name').val(this.session.roster[this.currentContact].name); $('#edit-contact-form input').fadeIn(200); return $('#edit-contact-form .buttons').fadeIn(200); }; ChatPage.prototype.scroll = function() { var msgs; msgs = $('#messages'); return msgs.animate({ scrollTop: msgs.prop('scrollHeight') }, 400); }; ChatPage.prototype.atBottom = function() { var bottom, msgs; msgs = $('#messages'); bottom = msgs.prop('scrollHeight') - msgs.outerHeight(); return msgs.scrollTop() >= bottom; }; ChatPage.prototype.send = function() { var chat, input, jid, text; if (!this.currentContact) { return false; } input = $('#message'); text = input.val().trim(); if (text) { chat = this.chats[this.currentContact]; jid = chat ? chat.jid : this.currentContact; this.message({ from: this.session.jid(), text: text, to: jid, received: new Date() }); this.session.sendMessage(jid, text); } input.val(''); return false; }; ChatPage.prototype.addContact = function() { var contact; this.toggleForm('#add-contact-form'); contact = { jid: $('#add-contact-jid').val(), name: $('#add-contact-name').val(), groups: ['Buddies'] }; if (contact.jid) { this.session.updateContact(contact, true); } return false; }; ChatPage.prototype.removeContact = function() { this.toggleForm('#remove-contact-form'); this.session.removeContact(this.currentContact); this.currentContact = null; $('#chat-title').text('Select a buddy to chat'); $('#messages').empty(); $('#remove-contact-msg').html("Select a buddy in the list above to remove."); $('#remove-contact-form .buttons').hide(); $('#edit-contact-jid').text("Select a buddy in the list above to update."); $('#edit-contact-name').val(''); $('#edit-contact-form input').hide(); $('#edit-contact-form .buttons').hide(); return false; }; ChatPage.prototype.updateContact = function() { var contact; this.toggleForm('#edit-contact-form'); contact = { jid: this.currentContact, name: $('#edit-contact-name').val(), groups: this.session.roster[this.currentContact].groups }; this.session.updateContact(contact); return false; }; ChatPage.prototype.toggleForm = function(form, fn) { form = $(form); $('form.overlay').each(function() { if (this.id !== form.attr('id')) { return $(this).hide(); } }); if (form.is(':hidden')) { if (fn) { fn(); } return form.fadeIn(100); } else { return form.fadeOut(100, function() { form[0].reset(); if (fn) { return fn(); } }); } }; ChatPage.prototype.draw = function() { var fn, layout; if (!this.session.connected()) { window.location.hash = ''; return; } $('body').attr('id', 'chat-page'); $('#container').hide().empty(); $("
    \n

    Buddies

    \n
    \n \n
    \n
    \n
    \n
    \n
    \n
    \n

    Add Buddy

    \n \n \n
    \n \n \n
    \n
    \n
    \n

    Remove Buddy

    \n

    Select a buddy in the list above to remove.

    \n
    \n \n \n
    \n
    \n
    \n

    Update Profile

    \n

    Select a buddy in the list above to update.

    \n \n
    \n \n \n
    \n
    \n
    \n
    \n

    Select a buddy to chat

    \n \n
    \n \n
    \n
    \n
    \n

    Notifications

    \n \n
    \n
    \n
    \n
    ").appendTo('#container'); this.roster(); new Button('#clear-notices', ICONS.no); new Button('#add-contact', ICONS.plus); new Button('#remove-contact', ICONS.minus); new Button('#edit-contact', ICONS.user); $('#message').focus(function() { return $('form.overlay').fadeOut(); }); $('#message-form').submit(__bind(function() { return this.send(); }, this)); $('#clear-notices').click(function() { return $('#notifications li').fadeOut(200); }); $('#add-contact').click(__bind(function() { return this.toggleForm('#add-contact-form'); }, this)); $('#remove-contact').click(__bind(function() { return this.toggleForm('#remove-contact-form'); }, this)); $('#edit-contact').click(__bind(function() { return this.toggleForm('#edit-contact-form', __bind(function() { if (this.currentContact) { $('#edit-contact-jid').text(this.currentContact); return $('#edit-contact-name').val(this.session.roster[this.currentContact].name); } }, this)); }, this)); $('#add-contact-cancel').click(__bind(function() { return this.toggleForm('#add-contact-form'); }, this)); $('#remove-contact-cancel').click(__bind(function() { return this.toggleForm('#remove-contact-form'); }, this)); $('#edit-contact-cancel').click(__bind(function() { return this.toggleForm('#edit-contact-form'); }, this)); $('#add-contact-form').submit(__bind(function() { return this.addContact(); }, this)); $('#remove-contact-form').submit(__bind(function() { return this.removeContact(); }, this)); $('#edit-contact-form').submit(__bind(function() { return this.updateContact(); }, this)); $('#container').fadeIn(200); layout = this.resize(); fn = function() { layout.resize(); return layout.resize(); }; return new Filter({ list: '#roster', icon: '#search-roster-icon', form: '#search-roster-form', attrs: ['data-jid', 'data-name'], open: fn, close: fn }); }; ChatPage.prototype.resize = function() { var a, b, c, form, msg; a = $('#alpha'); b = $('#beta'); c = $('#charlie'); msg = $('#message'); form = $('#message-form'); return new Layout(function() { c.css('left', a.width() + b.width()); return msg.width(form.width() - 32); }); }; return ChatPage; })();