hardlounge/client/js/chat.js

267 lines
5.2 KiB
JavaScript
Raw Normal View History

2014-06-30 01:20:54 +00:00
$(function() {
var socket = io();
var commands = [
"/close",
"/connect",
"/deop",
"/devoice",
"/disconnect",
"/invite",
"/join",
"/kick",
"/leave",
"/mode",
"/msg",
"/nick",
"/notice",
"/op",
"/part",
"/query",
"/quit",
"/raw",
"/say",
"/send",
"/server",
"/slap",
"/topic",
"/voice",
"/whois"
];
2014-07-07 00:44:31 +00:00
var sidebar = $("#sidebar");
2014-06-30 01:20:54 +00:00
var chat = $("#chat");
var tpl = [];
function render(name, data) {
tpl[name] = tpl[name] || Handlebars.compile($("#templates ." + name).html());
return tpl[name](data);
}
socket.on("auth", function(data) {
console.log(data);
});
socket.on("init", function(data) {
2014-07-07 12:42:46 +00:00
var channels = $.map(data.networks, function(n) {
2014-06-30 01:20:54 +00:00
return n.channels;
});
2014-07-07 12:42:46 +00:00
sidebar.find(".networks").html(
2014-06-30 01:20:54 +00:00
render("networks", {
networks: data.networks
})
2014-07-07 12:42:46 +00:00
);
chat.html(
render("chat", {
channels: channels
})
);
var id = $.cookie("target");
var target = sidebar.find("[data-target=" + id + "]");
if (target.length !== 0) {
target.trigger("click");
} else {
sidebar.find(".chan")
.eq(0)
.trigger("click");
2014-07-07 00:44:31 +00:00
}
2014-06-30 01:20:54 +00:00
});
socket.on("join", function(data) {
2014-07-07 00:44:31 +00:00
var id = data.network;
2014-07-07 12:42:46 +00:00
var network = sidebar.find("#network-" + id);
2014-06-30 01:20:54 +00:00
network.append(
render("channels", {
channels: [data.chan]
})
);
2014-07-07 12:42:46 +00:00
chat.append(
render("chat", {
channels: [data.chan]
})
);
sidebar.find(".chan")
2014-07-05 00:31:20 +00:00
.last()
.trigger("click");
2014-06-30 01:20:54 +00:00
});
socket.on("msg", function(data) {
2014-07-07 12:42:46 +00:00
chat.find("#chan-" + data.chan)
.find(".messages")
.append(render("messages", {messages: [data.msg]}));
2014-06-30 01:20:54 +00:00
});
socket.on("network", function(data) {
2014-07-07 12:42:46 +00:00
sidebar.find(".networks").append(
2014-06-30 01:20:54 +00:00
render("networks", {
networks: [data.network]
})
);
2014-07-07 12:42:46 +00:00
sidebar.find(".chan")
2014-07-07 00:44:31 +00:00
.last()
.trigger("click");
2014-06-30 01:20:54 +00:00
});
socket.on("nick", function(data) {
console.log(data);
});
socket.on("part", function(data) {
2014-07-07 00:44:31 +00:00
var id = data.chan;
2014-07-07 12:42:46 +00:00
sidebar.find("[data-target=#chan-" + id + "]")
2014-07-04 23:32:40 +00:00
.remove()
.end()
.find(".chan")
.eq(0)
.trigger("click");
2014-06-30 01:20:54 +00:00
});
socket.on("quit", function(data) {
2014-07-07 00:44:31 +00:00
var id = data.network;
2014-07-07 12:42:46 +00:00
sidebar.find("#network-" + id)
2014-07-04 23:32:40 +00:00
.remove()
.end()
.find(".chan")
.eq(0)
.trigger("click");
2014-06-30 01:20:54 +00:00
});
socket.on("users", function(data) {
2014-07-07 12:42:46 +00:00
chat.find("#chan-" + data.chan)
.find(".users")
.html(render("users", data));
2014-06-30 01:20:54 +00:00
});
2014-07-07 21:44:49 +00:00
var viewport = $("#viewport");
$("#rt, #lt").on("click", function(e) {
var self = $(this);
viewport.toggleClass(self.attr("id"));
if (viewport.is(".lt, .rt")) {
e.stopPropagation();
chat.find(".chat").one("click", function() {
viewport.removeClass("lt rt");
});
}
});
2014-07-06 15:58:57 +00:00
var input = $("#input")
.history()
.tab(complete, {hint: false});
2014-06-30 01:20:54 +00:00
var form = $("#form").on("submit", function(e) {
e.preventDefault();
2014-07-07 21:44:49 +00:00
var text = input.val();
2014-06-30 01:20:54 +00:00
input.val("");
socket.emit("input", {
2014-07-07 12:42:46 +00:00
target: chat.data("id"),
2014-07-07 21:44:49 +00:00
text: text
2014-06-30 01:20:54 +00:00
});
});
2014-07-07 12:42:46 +00:00
var top = 1;
2014-07-07 00:44:31 +00:00
sidebar.on("click", "button:not(.active)", function() {
2014-07-07 12:42:46 +00:00
var self = $(this);
var target = self.data("target");
if (!target) {
return;
}
2014-07-04 23:32:40 +00:00
2014-07-07 12:42:46 +00:00
$.cookie("target", target);
chat.data(
"id",
self.data("id")
);
2014-07-04 23:32:40 +00:00
2014-07-07 00:44:31 +00:00
sidebar.find(".active").removeClass("active");
2014-07-07 12:42:46 +00:00
self.addClass("active")
.find(".badge")
.removeClass("highlight")
.empty();
2014-07-07 00:44:31 +00:00
2014-07-07 21:44:49 +00:00
$("#rt").toggle(self.hasClass("chan"));
$("#header").find("h1").html(self.data("title"));
viewport.removeClass();
2014-07-07 12:42:46 +00:00
var chan = $(target)
.css("z-index", top++)
.find(".chat")
.sticky();
2014-07-02 22:00:11 +00:00
});
2014-07-07 12:42:46 +00:00
chat.on("input", ".search", function() {
2014-07-07 21:44:49 +00:00
var value = $(this).val();
2014-07-07 12:42:46 +00:00
var names = $(this).closest(".users").find(".names");
names.find("button").each(function() {
2014-07-04 23:32:40 +00:00
var btn = $(this);
2014-07-07 21:44:49 +00:00
if (btn.text().toLowerCase().indexOf(value) === 0) {
2014-07-04 23:32:40 +00:00
btn.show();
} else {
btn.hide();
}
});
});
2014-07-07 00:44:31 +00:00
chat.on("click", ".user", function() {
2014-07-07 21:44:49 +00:00
var user = $(this).html().trim().replace(/[+%@~]/, "");
2014-07-07 00:44:31 +00:00
if (user.indexOf("#") !== -1) {
return;
}
2014-07-07 21:44:49 +00:00
var text = "/whois " + user;
console.log({
target: chat.data("id"),
text: text
});
2014-07-07 00:44:31 +00:00
socket.emit("input", {
2014-07-07 21:44:49 +00:00
target: chat.data("id"),
text: text
2014-07-07 00:44:31 +00:00
});
});
2014-06-30 01:20:54 +00:00
function isActive(chan) {
2014-07-07 00:44:31 +00:00
return active !== null && chan == active;
2014-06-30 01:20:54 +00:00
}
function complete(word) {
return $.grep(
commands,
function(w) {
return !w.toLowerCase().indexOf(word.toLowerCase());
}
);
}
function escape(text) {
var e = {
"<": "&lt;",
">": "&gt;"
};
return text.replace(/[<>]/g, function (c) {
return e[c];
});
}
Handlebars.registerHelper(
"partial", function(id) {
return new Handlebars.SafeString(render(id, this));
}
);
Handlebars.registerHelper(
"uri", function(text) {
var urls = [];
text = URI.withinString(text, function(url) {
urls.push(url);
return "$(" + (urls.length - 1) + ")";
});
text = escape(text);
for (var i in urls) {
var url = escape(urls[i]);
text = text.replace(
"$(" + i + ")",
"<a href='" + url.replace(/^www/, "//www") + "' target='_blank'>" + url + "</a>"
);
}
return text;
}
);
});