sync/www/assets/js/ui.js

493 lines
15 KiB
JavaScript
Raw Normal View History

2013-06-19 21:56:05 +00:00
/*
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.
*/
2013-06-20 04:42:20 +00:00
/* window focus/blur */
$(window).focus(function() {
FOCUSED = true;
clearInterval(TITLE_BLINK);
TITLE_BLINK = false;
document.title = PAGETITLE;
}).blur(function() {
FOCUSED = false;
});
2013-10-03 03:26:28 +00:00
$("#togglemotd").click(function () {
var hidden = $("#motd").css("display") === "none";
$("#motd").toggle();
if (hidden) {
$("#togglemotd").find(".icon-plus")
.removeClass("icon-plus")
.addClass("icon-minus");
} else {
$("#togglemotd").find(".icon-minus")
.removeClass("icon-minus")
.addClass("icon-plus");
}
});
2013-06-07 03:13:24 +00:00
/* chatbox */
$("#modflair").click(function () {
var m = $("#modflair");
if (m.hasClass("label-success")) {
USEROPTS.modhat = false;
m.removeClass("label-success")
.addClass("label-default");
} else {
USEROPTS.modhat = true;
m.removeClass("label-default")
.addClass("label-success");
}
});
$("#adminflair").click(function () {
var m = $("#adminflair");
2013-12-19 17:14:48 +00:00
if (m.hasClass("label-danger")) {
USEROPTS.adminhat = false;
2013-12-19 17:14:48 +00:00
m.removeClass("label-danger")
.addClass("label-default");
} else {
USEROPTS.adminhat = true;
m.removeClass("label-default")
2013-12-19 17:14:48 +00:00
.addClass("label-danger");
}
});
$("#usercount").mouseenter(function (ev) {
var breakdown = calcUserBreakdown();
// re-using profile-box class for convenience
var popup = $("<div/>")
.addClass("profile-box")
.css("top", (ev.pageY + 5) + "px")
.css("left", (ev.pageX) + "px")
.appendTo($("#usercount"));
var contents = "";
for(var key in breakdown) {
contents += "<strong>" + key + ":&nbsp;</strong>" + breakdown[key];
contents += "<br>"
}
popup.html(contents);
});
$("#usercount").mousemove(function (ev) {
var popup = $("#usercount").find(".profile-box");
if(popup.length == 0)
return;
popup.css("top", (ev.pageY + 5) + "px");
popup.css("left", (ev.pageX) + "px");
});
$("#usercount").mouseleave(function () {
$("#usercount").find(".profile-box").remove();
});
2013-06-20 04:42:20 +00:00
$("#messagebuffer").mouseenter(function() { SCROLLCHAT = false; });
$("#messagebuffer").mouseleave(function() { SCROLLCHAT = true; });
2014-01-06 15:55:12 +00:00
$("#guestname").keydown(function (ev) {
if (ev.keyCode === 13) {
socket.emit("login", {
name: $("#guestname").val()
});
}
});
2013-06-09 18:03:41 +00:00
$("#chatline").keydown(function(ev) {
2013-12-15 03:59:47 +00:00
// Enter/return
2013-06-07 03:13:24 +00:00
if(ev.keyCode == 13) {
2013-11-19 21:14:40 +00:00
if (CHATTHROTTLE) {
return;
}
2013-06-07 03:13:24 +00:00
var msg = $("#chatline").val();
if(msg.trim()) {
2013-11-17 19:12:56 +00:00
var meta = {};
if (USEROPTS.adminhat && CLIENT.rank >= 255) {
msg = "/a " + msg;
2013-11-17 19:12:56 +00:00
} else if (USEROPTS.modhat && CLIENT.rank >= Rank.Moderator) {
meta.modflair = CLIENT.rank;
2013-06-07 03:13:24 +00:00
}
2013-12-15 03:59:47 +00:00
// The /m command no longer exists, so emulate it clientside
2013-11-17 19:12:56 +00:00
if (CLIENT.rank >= 2 && msg.indexOf("/m ") === 0) {
meta.modflair = CLIENT.rank;
2013-11-21 23:46:33 +00:00
msg = msg.substring(3);
2013-11-17 19:12:56 +00:00
}
2013-06-07 03:13:24 +00:00
socket.emit("chatMsg", {
2013-11-17 19:12:56 +00:00
msg: msg,
meta: meta
2013-06-07 03:13:24 +00:00
});
CHATHIST.push($("#chatline").val());
2013-06-19 21:54:27 +00:00
CHATHISTIDX = CHATHIST.length;
2013-06-07 03:13:24 +00:00
$("#chatline").val("");
}
return;
}
2013-12-15 03:59:47 +00:00
else if(ev.keyCode == 9) { // Tab completion
2013-06-07 03:13:24 +00:00
var words = $("#chatline").val().split(" ");
var current = words[words.length - 1].toLowerCase();
var users = $("#userlist").children();
var match = null;
for(var i = 0; i < users.length; i++) {
var name = users[i].children[1].innerHTML.toLowerCase();
2013-12-15 03:59:47 +00:00
// Last word is a unique match for a userlist name
2013-06-07 03:13:24 +00:00
if(name.indexOf(current) == 0 && match == null) {
match = users[i].children[1].innerHTML;
}
2013-12-15 03:59:47 +00:00
// Last word is NOT a unique match- a match has already
// been found. Bail because no unique completion is possible.
2013-06-07 03:13:24 +00:00
else if(name.indexOf(current) == 0) {
match = null;
break;
}
}
if(match != null) {
words[words.length - 1] = match;
if(words.length == 1)
words[0] += ": ";
else
words[words.length - 1] += " ";
$("#chatline").val(words.join(" "));
}
ev.preventDefault();
return false;
}
2013-12-15 03:59:47 +00:00
else if(ev.keyCode == 38) { // Up arrow (input history)
2013-06-07 03:13:24 +00:00
if(CHATHISTIDX == CHATHIST.length) {
CHATHIST.push($("#chatline").val());
}
if(CHATHISTIDX > 0) {
CHATHISTIDX--;
$("#chatline").val(CHATHIST[CHATHISTIDX]);
}
ev.preventDefault();
return false;
}
2013-12-15 03:59:47 +00:00
else if(ev.keyCode == 40) { // Down arrow (input history)
2013-06-07 03:13:24 +00:00
if(CHATHISTIDX < CHATHIST.length - 1) {
CHATHISTIDX++;
$("#chatline").val(CHATHIST[CHATHISTIDX]);
}
ev.preventDefault();
return false;
}
});
/* poll controls */
$("#newpollbtn").click(showPollMenu);
/* search controls */
$("#library_search").click(function() {
socket.emit("searchMedia", {
source: "library",
query: $("#library_query").val().toLowerCase()
});
});
$("#library_query").keydown(function(ev) {
if(ev.keyCode == 13) {
socket.emit("searchMedia", {
source: "library",
query: $("#library_query").val().toLowerCase()
});
}
});
$("#youtube_search").click(function () {
var query = $("#library_query").val().toLowerCase();
if(parseMediaLink(query).type !== null) {
makeAlert("Media Link", "If you already have the link, paste it " +
"in the 'Media URL' box under Playlist Controls. This "+
"searchbar works like YouTube's search function.",
2013-12-15 03:59:47 +00:00
"alert-danger")
.insertBefore($("#library"));
}
2013-06-07 03:13:24 +00:00
socket.emit("searchMedia", {
source: "yt",
query: query
2013-06-07 03:13:24 +00:00
});
});
/* user playlists */
2013-12-15 03:59:47 +00:00
$("#showplaylistmanager").click(function() {
2013-06-07 03:13:24 +00:00
socket.emit("listPlaylists");
});
$("#userpl_save").click(function() {
if($("#userpl_name").val().trim() == "") {
2013-12-15 03:59:47 +00:00
makeAlert("Invalid Name", "Playlist name cannot be empty", "alert-danger")
2013-06-07 03:13:24 +00:00
.insertAfter($("#userpl_save").parent());
return;
}
socket.emit("savePlaylist", {
name: $("#userpl_name").val()
});
});
2013-06-23 18:21:21 +00:00
/* video controls */
2013-12-15 03:59:47 +00:00
// TODO this is ugly, change it?
2013-06-23 18:21:21 +00:00
(function() {
function qualHandler(select, preset) {
$(select).click(function() {
VIDEOQUALITY = preset;
2013-08-15 04:26:50 +00:00
USEROPTS.default_quality = select;
2013-12-25 21:18:21 +00:00
storeOpts();
2013-12-15 03:59:47 +00:00
var btn = $("#qdrop");
2013-06-23 18:21:21 +00:00
var caret = btn.find(".caret").detach();
btn.text($(select).text());
caret.appendTo(btn);
2013-06-30 19:56:41 +00:00
if(PLAYER.type == "yt" && PLAYER.player.setPlaybackQuality)
PLAYER.player.setPlaybackQuality(VIDEOQUALITY);
2013-06-23 18:21:21 +00:00
});
}
2013-08-15 04:26:50 +00:00
qualHandler("#quality_auto", "");
2013-06-23 18:21:21 +00:00
qualHandler("#quality_240p", "small");
qualHandler("#quality_360p", "medium");
qualHandler("#quality_480p", "large");
qualHandler("#quality_720p", "hd720");
qualHandler("#quality_1080p", "hd1080");
2013-08-15 04:26:50 +00:00
if($(USEROPTS.default_quality).length > 0)
$(USEROPTS.default_quality).click();
2013-06-23 18:21:21 +00:00
})();
2013-06-30 19:56:41 +00:00
$("#mediarefresh").click(function() {
PLAYER.type = "";
PLAYER.id = "";
2013-12-15 03:59:47 +00:00
// playerReady triggers the server to send a changeMedia.
// the changeMedia handler then reloads the player
2013-06-30 19:56:41 +00:00
socket.emit("playerReady");
});
2013-06-07 03:13:24 +00:00
/* playlist controls */
2013-06-07 22:09:36 +00:00
2013-06-11 19:41:03 +00:00
$("#queue").sortable({
start: function(ev, ui) {
2013-06-29 22:09:20 +00:00
PL_FROM = ui.item.data("uid");
2013-06-11 19:41:03 +00:00
},
update: function(ev, ui) {
var prev = ui.item.prevAll();
if(prev.length == 0)
2013-06-29 22:09:20 +00:00
PL_AFTER = "prepend";
else
2013-06-29 22:09:20 +00:00
PL_AFTER = $(prev[0]).data("uid");
socket.emit("moveMedia", {
from: PL_FROM,
after: PL_AFTER
});
2013-10-04 03:11:47 +00:00
$("#queue").sortable("cancel");
2013-06-11 19:41:03 +00:00
}
2013-06-09 18:03:41 +00:00
});
2013-06-11 19:41:03 +00:00
$("#queue").disableSelection();
2013-06-09 18:03:41 +00:00
2013-12-15 03:59:47 +00:00
// TODO no more comma separated queueing.
2013-06-07 22:09:36 +00:00
function queue(pos) {
2013-08-03 19:10:06 +00:00
if($("#customembed_code").val()) {
var title = false;
if($("#customembed_title").val()) {
title = $("#customembed_title").val();
}
2013-08-03 19:10:06 +00:00
socket.emit("queue", {
id: $("#customembed_code").val(),
title: title,
2013-08-03 19:10:06 +00:00
type: "cu",
pos: pos
});
$("#customembed_code").val("");
$("#customembed_title").val("");
2013-08-03 19:10:06 +00:00
return;
}
2013-06-07 22:09:36 +00:00
var links = $("#mediaurl").val().split(",");
var parsed = [];
2013-06-07 22:09:36 +00:00
links.forEach(function(link) {
var data = parseMediaLink(link);
if(data.id === null || data.type === null) {
2013-12-15 03:59:47 +00:00
makeAlert("Error", "Invalid link. Please double check it and remove extraneous information", "alert-danger")
.insertBefore($("#extended_controls"));
}
else {
$("#mediaurl").val("");
}
parsed.push({
2013-06-07 22:09:36 +00:00
id: data.id,
type: data.type
2013-06-07 22:09:36 +00:00
});
});
if(parsed.length > 1) {
socket.emit("queue", {
id: false,
list: parsed,
type: "list",
pos: pos
});
}
else {
parsed[0].pos = pos;
socket.emit("queue", parsed[0]);
}
2013-06-07 22:09:36 +00:00
}
$("#queue_next").click(function() {
queue("next");
});
$("#queue_end").click(function() {
queue("end");
});
$("#mediaurl").keydown(function(ev) {
if(ev.keyCode == 13) {
queue("end");
}
});
$("#qlockbtn").click(function() {
socket.emit("togglePlaylistLock");
});
2013-06-18 20:18:41 +00:00
$("#voteskip").click(function() {
socket.emit("voteskip");
2013-07-24 19:11:50 +00:00
$("#voteskip").attr("disabled", true);
2013-06-18 20:18:41 +00:00
});
2013-08-03 19:10:06 +00:00
$("#customembed_btn").click(function () {
if($("#customembed_entry").css("display") == "none")
$("#customembed_entry").show("blind");
else
$("#customembed_entry").hide("blind");
});
2013-06-07 22:09:36 +00:00
$("#getplaylist").click(function() {
var callback = function(data) {
hidePlayer();
2013-06-07 22:09:36 +00:00
socket.listeners("playlist").splice(
socket.listeners("playlist").indexOf(callback)
);
var list = [];
2013-06-11 19:41:03 +00:00
for(var i = 0; i < data.length; i++) {
2013-07-12 20:10:06 +00:00
var entry = formatURL(data[i].media);
2013-06-07 22:09:36 +00:00
list.push(entry);
}
var urls = list.join(",");
2013-12-15 03:59:47 +00:00
var outer = $("<div/>").addClass("modal fade")
2013-06-07 22:09:36 +00:00
.appendTo($("body"));
2013-12-15 03:59:47 +00:00
modal = $("<div/>").addClass("modal-dialog").appendTo(outer);
modal = $("<div/>").addClass("modal-content").appendTo(modal);
2013-06-07 22:09:36 +00:00
var head = $("<div/>").addClass("modal-header")
.appendTo(modal);
$("<button/>").addClass("close")
.attr("data-dismiss", "modal")
.attr("aria-hidden", "true")
.html("&times;")
.appendTo(head);
$("<h3/>").text("Playlist URLs").appendTo(head);
var body = $("<div/>").addClass("modal-body").appendTo(modal);
2013-12-15 03:59:47 +00:00
$("<input/>").addClass("form-control").attr("type", "text")
2013-06-07 22:09:36 +00:00
.val(urls)
.appendTo(body);
$("<div/>").addClass("modal-footer").appendTo(modal);
2013-12-15 03:59:47 +00:00
outer.on("hidden", function() {
outer.remove();
unhidePlayer();
2013-06-07 22:09:36 +00:00
});
2013-12-15 03:59:47 +00:00
outer.modal();
};
2013-06-07 22:09:36 +00:00
socket.on("playlist", callback);
socket.emit("requestPlaylist");
});
$("#clearplaylist").click(function() {
var clear = confirm("Are you sure you want to clear the playlist?");
if(clear) {
socket.emit("clearPlaylist");
}
});
$("#shuffleplaylist").click(function() {
2013-06-11 19:41:03 +00:00
var shuffle = confirm("Are you sure you want to shuffle the playlist?");
if(shuffle) {
2013-06-07 22:09:36 +00:00
socket.emit("shufflePlaylist");
}
});
2013-06-11 15:29:21 +00:00
/* layout stuff */
2013-12-15 03:59:47 +00:00
/*
2013-06-11 15:29:21 +00:00
$(window).resize(function() {
VWIDTH = $("#videowidth").css("width").replace("px", "");
2013-07-04 22:50:15 +00:00
VHEIGHT = ""+parseInt(parseInt(VWIDTH) * 9 / 16);
2013-06-11 15:29:21 +00:00
$("#messagebuffer").css("height", (VHEIGHT - 31) + "px");
$("#userlist").css("height", (VHEIGHT - 31) + "px");
2013-07-04 22:50:15 +00:00
if($("#ytapiplayer").length > 0) {
$("#ytapiplayer").attr("width", VWIDTH);
$("#ytapiplayer").attr("height", VHEIGHT);
}
2013-06-11 15:29:21 +00:00
});
2013-12-15 03:59:47 +00:00
*/
2013-06-11 15:29:21 +00:00
/* load channel */
var loc = document.location+"";
2013-12-25 21:18:21 +00:00
var m = loc.match(/\/r\/([a-zA-Z0-9-_#]+)$/);
2013-06-11 15:29:21 +00:00
if(m) {
CHANNEL.name = m[1];
2013-12-25 21:18:21 +00:00
if (CHANNEL.name.indexOf("#") !== -1) {
CHANNEL.name = CHANNEL.name.substring(0, CHANNEL.name.indexOf("#"));
}
2013-06-11 15:29:21 +00:00
}
2013-12-15 03:59:47 +00:00
/*
2013-06-11 15:29:21 +00:00
else {
var main = $("#main");
var container = $("<div/>").addClass("container").insertBefore(main);
var row = $("<div/>").addClass("row").appendTo(container);
2013-12-15 03:59:47 +00:00
var div = $("<div/>").addClass("col-lg-6 col-md-6").appendTo(row);
2013-06-11 15:29:21 +00:00
main.css("display", "none");
var label = $("<label/>").text("Enter Channel:").appendTo(div);
var entry = $("<input/>").attr("type", "text").appendTo(div);
entry.keydown(function(ev) {
2013-09-09 22:16:41 +00:00
var host = document.protocol + "//" + document.host + "/";
2013-06-11 15:29:21 +00:00
if(ev.keyCode == 13) {
2013-09-09 22:16:41 +00:00
document.location = host + "r/" + entry.val();
2013-06-11 15:29:21 +00:00
container.remove();
main.css("display", "");
}
});
}
2013-12-15 03:59:47 +00:00
*/
2013-06-19 21:54:27 +00:00
/* custom footer */
$("#sitefooter").load("footer.html");
2013-06-19 21:54:27 +00:00
/* oh internet explorer, how I hate thee */
$(":input:not(textarea)").keypress(function(ev) {
return ev.keyCode != 13;
});
if (location.protocol === "https:") {
var title = "Warning";
var text = "You connected to this page via HTTPS. Due to browser "+
"security policy, certain media players may throw warnings,"+
" while others may not work at all due to only being "+
"available over plain HTTP.<br>To encrypt your websocket "+
"traffic and API calls (logins, account management, etc) "+
"while loading this page over plain HTTP, enable the SSL "+
"option from the Options menu.";
makeAlert(title, text, "alert-warning")
.appendTo($("#announcements"));
}