Created
November 5, 2013 11:57
-
-
Save eef/7318010 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() { | |
this.notifier = new Notifier(); | |
this.tab_status = new TabStatus(); | |
this.join(); | |
this.open(); | |
this.leave(); | |
}; | |
Chat.prototype.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); | |
} | |
} | |
}; | |
Chat.prototype.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(); | |
}); | |
} | |
}); | |
} | |
Chat.prototype.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(); | |
}); | |
}; | |
Chat.prototype.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(); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment