/*
The MIT License (MIT)
Copyright (c) 2013 Calvin Montgomery
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Format a userlist entry based on a person's rank
function formatUserlistItem(div, data) {
var name = div.children[1];
$(name).removeClass();
$(name).css("font-style", "");
$(name).addClass(getNameColor(data.rank));
$(div).find(".profile-box").remove();
var profile;
$(name).mouseenter(function(ev) {
profile = $("
")
.addClass("profile-box")
.css("top", (ev.pageY + 5) + "px")
.css("left", ev.pageX + "px")
.appendTo(div);
if(data.profile.image) {
$("").addClass("profile-image")
.attr("src", data.profile.image)
.appendTo(profile);
}
$("").text(data.name).appendTo(profile);
$("").text(data.profile.text).appendTo(profile);
});
$(name).mousemove(function(ev) {
profile.css("top", (ev.pageY + 5) + "px")
.css("left", ev.pageX + "px")
});
$(name).mouseleave(function() {
profile.remove();
});
var flair = div.children[0];
flair.innerHTML = "";
// denote current leader with a star
if(data.leader) {
$("").addClass("icon-star-empty").appendTo(flair);
}
if(data.meta && data.meta.afk) {
$(name).css("font-style", "italic");
$("").addClass("icon-time").appendTo(flair);
}
}
function getNameColor(rank) {
if(rank >= Rank.Siteadmin)
return "userlist_siteadmin";
else if(rank >= Rank.Owner)
return "userlist_owner";
else if(rank >= Rank.Moderator)
return "userlist_op";
else if(rank == Rank.Guest)
return "userlist_guest";
else
return "";
}
// Adds a dropdown with user actions (promote/demote/leader)
function addUserDropdown(entry, name) {
$(entry).find(".dropdown").remove();
$(entry).unbind();
var div = $("").addClass("dropdown").appendTo(entry);
var ul = $("
").addClass("dropdown-menu").appendTo(div);
ul.attr("role", "menu");
ul.attr("aria-labelledby", "dropdownMenu");
var ignore = $("").appendTo(ul);
var a = $("").attr("tabindex", "-1").attr("href", "javascript:void(0);").appendTo(ignore);
if(IGNORED.indexOf(name) != -1) {
a.text("Unignore User");
}
else {
a.text("Ignore User");
}
a.click(function() {
if(IGNORED.indexOf(name) != -1) {
IGNORED.splice(IGNORED.indexOf(name), 1);
this.text("Ignore User");
}
else {
IGNORED.push(name);
this.text("Unignore User");
}
}.bind(a));
if(hasPermission("kick")) {
var kick = $("").appendTo(ul);
var a = $("").attr("tabindex", "-1").attr("href", "javascript:void(0);").appendTo(kick);
a.text("Kick");
a.click(function() {
socket.emit("chatMsg", {
msg: "/kick " + name
});
});
}
if(RANK >= Rank.Moderator) {
$("").addClass("divider").appendTo(ul);
var makeLeader = $("").appendTo(ul);
var a = $("").attr("tabindex", "-1").attr("href", "javascript:void(0);").appendTo(makeLeader);
a.text("Make Leader");
a.click(function() {
socket.emit("assignLeader", {
name: name
});
});
var takeLeader = $("").appendTo(ul);
var a = $("").attr("tabindex", "-1").attr("href", "javascript:void(0);").appendTo(takeLeader);
a.text("Take Leader");
a.click(function() {
socket.emit("assignLeader", {
name: ""
});
});
var ban = $("").appendTo(ul);
var a = $("").attr("tabindex", "-1").attr("href", "javascript:void(0);").appendTo(ban);
a.text("IP Ban");
a.click(function() {
socket.emit("chatMsg", {
msg: "/ban " + name
});
});
var nameban = $("").appendTo(ul);
var a = $("").attr("tabindex", "-1").attr("href", "javascript:void(0);").appendTo(nameban);
a.text("Name Ban");
a.click(function() {
socket.emit("banName", {
name: name
});
});
$("").addClass("divider").appendTo(ul);
var promote = $("").appendTo(ul);
var a = $("").attr("tabindex", "-1").attr("href", "javascript:void(0);").appendTo(promote);
a.text("Promote");
a.click(function() {
socket.emit("promote", {
name: name
});
});
var demote = $("").appendTo(ul);
var a = $("").attr("tabindex", "-1").attr("href", "javascript:void(0);").appendTo(demote);
a.text("Demote");
a.click(function() {
socket.emit("demote", {
name: name
});
});
}
$(entry).click(function() {
if(ul.css("display") == "none") {
// Hide others
$("#userlist ul.dropdown-menu").each(function() {
if(this != ul) {
$(this).css("display", "none");
}
});
ul.css("display", "block");
}
else {
ul.css("display", "none");
}
});
return ul;
}
function addChatMessage(data) {
if(IGNORED.indexOf(data.username) != -1) {
return;
}
var div = formatChatMessage(data);
div.data("sender", data.username);
div.appendTo($("#messagebuffer"));
div.mouseover(function() {
$("#messagebuffer").children().each(function() {
var name = $(this).data("sender");
if(name == data.username) {
$(this).addClass("nick-hover");
}
});
});
div.mouseleave(function() {
$("#messagebuffer").children().each(function() {
$(this).removeClass("nick-hover");
});
});
// Cap chatbox at most recent 100 messages
if($("#messagebuffer").children().length > 100) {
$($("#messagebuffer").children()[0]).remove();
}
if(SCROLLCHAT)
$("#messagebuffer").scrollTop($("#messagebuffer").prop("scrollHeight"));
if(USEROPTS.blink_title && !FOCUSED && !TITLE_BLINK) {
TITLE_BLINK = setInterval(function() {
if(document.title == "*Chat*")
document.title = PAGETITLE;
else
document.title = "*Chat*";
}, 1000);
}
if(uname) {
if(data.msg.toUpperCase().indexOf(uname.toUpperCase()) != -1) {
div.addClass("nick-highlight");
if(!FOCUSED && !TITLE_BLINK) {
TITLE_BLINK = setInterval(function() {
if(document.title == "*Chat*")
document.title = PAGETITLE;
else
document.title = "*Chat*";
}, 1000);
}
}
}
}
function formatChatMessage(data) {
var skip = data.username == LASTCHATNAME;
if(data.msgclass == "drink" || data.msgclass == "shout") {
skip = false;
}
LASTCHATNAME = data.username;
LASTCHATTIME = data.time;
var div = $("");
if(USEROPTS.show_timestamps) {
var time = $("").addClass("timestamp").appendTo(div);
var timestamp = new Date(data.time).toTimeString().split(" ")[0];
time.text("["+timestamp+"] ");
}
var name = $("");
if(!skip) {
name.appendTo(div);
}
$("").addClass("username").text(data.username + ": ").appendTo(name);
var message = $("").appendTo(div);
message[0].innerHTML = data.msg;
if(data.modflair) {
name.addClass(getNameColor(data.modflair));
}
if(data.msgclass == "action") {
name.remove();
message.addClass("action");
message[0].innerHTML = data.username + " " + data.msg;
}
else if(data.msgclass == "drink") {
div.addClass("drink");
}
else if(data.msgclass == "shout") {
message.addClass("shout");
name.addClass("shout");
}
else {
message.addClass(data.msgclass);
}
return div;
}
// Creates and formats a queue entry
function makeQueueEntry(video) {
var li = $("");
li.attr("class", "well");
if(video.thumb) {
$("").attr("src", video.thumb.url)
.css("float", "left")
.css("clear", "both")
.appendTo(li);
}
var title = $("").addClass("qe_title").appendTo(li)
.text(video.title)
.attr("href", idToURL(video))
.attr("target", "_blank");
var time = $("").addClass("qe_time").appendTo(li);
time.text(video.duration);
var clear = $("").addClass("qe_clear").appendTo(li);
if(video.temp) {
li.addClass("alert alert-error");
}
return li;
}
// Add buttons to a queue list entry
function addQueueButtons(li) {
var btnstrip = $("").attr("class", "btn-group qe_buttons").prependTo(li);
if(hasPermission("playlistmove")) {
var btnMove = $("").addClass("btn qe_btn").appendTo(btnstrip);
$("").addClass("icon-resize-vertical").appendTo(btnMove);
// Callback time
btnMove.mousedown(function() {
GRABBEDLI = li;
OLDINDEX = $("#queue").children().index(li);
});
btnMove.mousemove(function() {
if(GRABBEDLI != null) {
var idx = $("#queue").children().index(li);
var lidx = $("#queue").children().index(GRABBEDLI);
if(idx != lidx)
moveVideo(lidx, idx, true);
}
});
}
if(hasPermission("playlistdelete")) {
var btnRemove = $("").attr("class", "btn btn-danger qe_btn").appendTo(btnstrip);
$("").attr("class", "icon-remove").appendTo(btnRemove);
$(btnRemove).click(function() {
btnstrip.remove();
var idx = $("#queue").children().index(li);
socket.emit("unqueue", { pos: idx });
});
}
if(hasPermission("playlistjump")) {
var btnPlay = $("").attr("class", "btn btn-success qe_btn").appendTo(btnstrip);
$("").attr("class", "icon-play").appendTo(btnPlay);
$(btnPlay).click(function() {
var idx = $("#queue").children().index(li);
socket.emit("jumpTo", {
pos: idx
});
});
}
if(hasPermission("playlistnext")) {
var btnNext = $("").attr("class", "btn qe_btn").appendTo(btnstrip);
$("").addClass("icon-share-alt").appendTo(btnNext);
$(btnNext).click(function() {
var idx = $("#queue").children().index(li);
var dest = idx < POSITION ? POSITION : POSITION + 1;
socket.emit("moveMedia", {
src: idx,
dest: dest
});
});
}
if(hasPermission("settemp")) {
var btnTemp = $("").attr("class", "btn qe_btn").appendTo(btnstrip);
var temp = $(li).hasClass("alert-error");
$("").addClass("icon-flag").appendTo(btnTemp);
if(temp) {
btnTemp.addClass("btn-success");
}
else {
btnTemp.addClass("btn-danger");
}
$(btnTemp).click(function() {
temp = $(li).hasClass("alert-error");
var idx = $("#queue").children().index(li);
socket.emit("setTemp", {
idx: idx,
temp: !temp
});
});
}
$(document).mouseup(function() {
if(GRABBEDLI != null) {
var idx = $("#queue").children().index(GRABBEDLI);
GRABBEDLI = null;
moveVideo(idx, OLDINDEX, true);
socket.emit("moveMedia", {
src: OLDINDEX,
dest: idx
});
}
});
}
function rebuildPlaylist() {
$("#queue li").each(function() {
$(this).find(".btn-group").remove();
addQueueButtons(this);
});
}
// Add buttons to a list entry for the library search results
function addLibraryButtons(li, id, yt) {
var btnstrip = $("").attr("class", "btn-group qe_buttons").prependTo(li);
if(hasPermission("playlistnext")) {
var btnNext = $("").addClass("btn qe_btn")
.text("Next")
.appendTo(btnstrip);
btnNext.click(function() {
if(yt) {
socket.emit("queue", {
id: id,
pos: "next",
type: "yt"
});
}
else {
socket.emit("queue", {
id: id,
pos: "next"
});
}
});
}
var btnEnd = $("").addClass("btn qe_btn").text("End").appendTo(btnstrip);
btnEnd.click(function() {
if(yt) {
socket.emit("queue", {
id: id,
pos: "end",
type: "yt"
});
}
else {
socket.emit("queue", {
id: id,
pos: "end"
});
}
});
if(RANK >= Rank.Moderator) {
var btnDelete = $("").addClass("btn qe_btn btn-danger").appendTo(btnstrip);
$("").addClass("icon-remove").appendTo(btnDelete);
btnDelete.click(function() {
socket.emit("uncache", {
id: id
});
$(li).hide("blind", function() {
$(li).remove();
});
});
}
}
function loadSearchPage(page) {
$("#library").html("");
var results = $("#library").data("entries");
var start = page * 100;
for(var i = start; i < start + 100 && i < results.length; i++) {
var li = makeQueueEntry(results[i]);
if(hasPermission("playlistadd")) {
if(results[i].thumb) {
addLibraryButtons(li, results[i].id, true);
}
else {
addLibraryButtons(li, results[i].id);
}
}
$(li).appendTo($("#library"));
}
if($("#search_pagination").length > 0) {
$("#search_pagination").find("li").each(function() {
$(this).removeClass("active");
});
$($("#search_pagination").find("li")[page]).addClass("active");
}
}
function clearSearchResults() {
$("#library").html("");
$("#search_pagination").remove();
}
function loadLoginlogPage(page) {
var entries = $("#loginlog").data("entries");
var start = page * 20;
var tbl = $("#loginlog table");
if(tbl.children().length > 1) {
$(tbl.children()[1]).remove();
}
for(var i = start; i < start + 20 && i < entries.length; i++) {
var tr = $("