-
-
Save carcer/7318063 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Chat(notifier, tabStatus) { | |
this.notifier = notifier; | |
this.tab_status = tabStatus; | |
}; | |
_.extend(Chat.prototype, { | |
init: function() { | |
this.join(); | |
this.open(); | |
this.leave(); | |
}, | |
update : function(data, chat) { | |
if(data.hasOwnProperty("server")) { | |
$("#"+chat+" tbody").append("<tr><td style='width: 100px;'>server: </td><td>"+data.content+"</td></tr>"); | |
} else { | |
$("#"+chat+" tbody").append("<tr><td style='width: 100px;'>"+data.author_nickname+"</td><td>"+data.content+"</td></tr>"); | |
} | |
if(this.tab_status.is_hidden() || !window_active) { | |
if(data.hasOwnProperty("server")) { | |
this.notifier.notify('http://i.imgur.com/K2KzQM6.jpg', '', data.content); | |
} else { | |
this.notifier.notify('http://i.imgur.com/K2KzQM6.jpg', 'New chat message', data.author_nickname + ': ' + data.content); | |
} | |
} | |
}, | |
open: function() { | |
$(document).on('click', '.open-chat', function(e) { | |
e.preventDefault(); | |
var target = $(e.currentTarget); | |
$(".chat").hide(); | |
if($.inArray(target.data("chat-id"), current_chats) != -1) { | |
$("#"+target.data("chat-id")).show(); | |
} else { | |
var request = $.ajax({ | |
type: 'GET', | |
url: "/chats/" + target.data("chat-id"), | |
dataType: "html" | |
}); | |
request.done(function(data){ | |
current_chats.push(target.data("chat-id")); | |
$("#all-chats").append("<div class='chat' style='display: none;' id="+target.data("chat-id")+">"+data+"</div> "); | |
$(".chat").hide(); | |
$("#"+target.data("chat-id")).show(); | |
}); | |
} | |
}); | |
}, | |
leave: function() { | |
$(document).on('ajax:success', '.leave-chat', function(e, data) { | |
var target = $(e.currentTarget); | |
$("#chat-list").html(data); | |
target.parent().html("<a href='/chats/"+target.data("chat-id")+"/join' data-remote='true' data-chat-id='"+target.data("chat-id")+"' class='join-chat'>Join</a>"); | |
$("#"+target.data("chat-id")).remove(); | |
$("#lobby").show(); | |
}); | |
}, | |
join : function() { | |
$(document).on('ajax:success', '.join-chat', function(e, data) { | |
var target = $(e.currentTarget); | |
$("#chat-list").html(data); | |
target.parent().html("<a href='/chats/"+target.data("chat-id")+"'>Enter</a> | <a href='/chats/"+target.data("chat-id")+"/leave' class='leave-chat' data-remote='true' data-chat-id='"+target.data("chat-id")+"'>Leave</a>"); | |
var request = $.ajax({ | |
type: 'GET', | |
url: "/chats/" + target.data("chat-id"), | |
dataType: "html" | |
}); | |
request.done(function(data){ | |
current_chats.push(target.data("chat-id")); | |
$("#all-chats").append("<div class='chat' style='display: none;' id="+target.data("chat-id")+">"+data+"</div> "); | |
$(".chat").hide(); | |
faye.subscribe('/chats/'+target.data("chat-id"), function (data) { | |
new Chat().update(data, target.data("chat-id")); | |
}); | |
$("#"+target.data("chat-id")).show(); | |
}); | |
}); | |
} | |
}); | |
// usuage | |
var chat = new Chat(new Notifier(), new TabStatus()); | |
chat.init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment