User = {
root: '#users',
preffix: 'user-',
el: function(id){ return $('#'+this.preffix+id) },
count: function(id) { return this.el.data('count') },
add: function(id, name){
console.log(this.el(id).data('count'))
if (!this.el(id).length){
console.log('newwwww')
$('<li/>').attr('id', this.preffix+id).text(name).appendTo(this.root);
this.el(id).data('count', 0);
};
this.el(id).data('count', this.count(id) + 1)
},
rm: function(id){
this.el(id).data('count', this.count(id) - 1);
if (this.cound(id) <= 0) this.el(id).remove()
},
mv: function(id, name){ this.el(id).text(name) },
name: function(id){ return this.el(id).text() }
};
Chat = {
root: '#messages',
preffix: 'messages-',
lastUser: function(){
var class = $(this.root).find('li:last').attr('class');
if (class) {
var id = class.match(/messages-(\d*)/) || '';
return id[1]
} else {
return ''
}
},
add: function(message, class){ return $('<li/>').text(message).addClass(this.preffix+class) },
sys: function(message, class){ this.add(message, class||'service').appendTo(this.root) },
write: function(user, message){
var message = this.add(message,user);
if (typeof(this.lastUser()) == 'undefined' || (this.lastUser() != String(user))){
$('<span/>').addClass('author').text(User.name(user)+': ').prependTo(message);
}
message.appendTo(this.root)
},
};
Preved = {
me: $.cookie('user'),
message: function(text){ this.send("POST", { message: text }) },
theme: function(name){ this.send("PUT", { theme: name }) },
user: function(name){ this.send("PUT", name) },
send: function(method, data){
if (!data) data = {};
if ('DELETE' == method || 'PUT' == method) {
data['_method'] = method;
method = 'POST'
}
data.authenticity_token = window.token;
$.ajax({
type: method,
cache: false,
data: data
});
},
server: {
receive: function(data){ this[data.command](data) },
connect: function(params){
User.add(params.user, params.name);
Chat.sys(params.name + " logged in.", 'system in')
},
disconnect: function(params){
Chat.sys(User.name(params.user) + " logged out.")
User.rm(params.user, 'system out')
},
message: function(params){ Chat.write(params.user, params.text) },
user: function(params){ User.mv(params.user, params.name) }
},
};
Juggernaut.fn.receiveData = function(e) {
var msg = Juggernaut.parseJSON(unescape(e.toString()));
this.currentMsgId = msg.id;
this.currentSignature = msg.signature;
this.logger("Received data:\n" + msg.body + "\n");
Preved.server.receive(Juggernaut.parseJSON(msg.body))
};
$(document).ready(function() {
$('#users > li').data('count', 0)
// Bind events
$('#new textarea').keypress(function(e) {
// Safari sends ASCII 3 on Enter
if (13 == e.keyCode || 3 == e.keyCode) {
if (e.ctrlKey || e.metaKey) {
//TODO
} else {
$('#new').submit();
}
}
});
$('#new textarea').keyup(function(e) {
if (13 == e.keyCode || 3 == e.keyCode) {
$('#new textarea').val('');
}
});
$('#new').submit(function() {
var text = $('#new textarea').val();
if ('' == text) return false;
$('#new textarea').val('');
Preved.message(text);
return false;
});
$('#new textarea').focus();
});
$(window).unload(function(){
Preved.send('DELETE');
});