sync/www/assets/js/ui.js

397 lines
12 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-06-06 23:38:47 +00:00
/* Generalized show/hide function */
function generateToggle(chevron, div) {
$(chevron).click(function() {
if($(div).css("display") == "none") {
2013-06-09 18:03:41 +00:00
$(chevron).html($(chevron).html().replace(/Show/, "Hide"));
2013-06-06 23:38:47 +00:00
$(div).show();
2013-06-15 21:45:07 +00:00
$(chevron+" i").removeClass("icon-plus")
.addClass("icon-minus");
2013-06-06 23:38:47 +00:00
}
else {
2013-06-09 18:03:41 +00:00
$(chevron).html($(chevron).html().replace(/Hide/, "Show"));
2013-06-06 23:38:47 +00:00
$(div).hide();
2013-06-15 21:45:07 +00:00
$(chevron+" i").removeClass("icon-minus")
.addClass("icon-plus");
2013-06-06 23:38:47 +00:00
}
});
}
2013-06-06 21:48:21 +00:00
2013-06-07 03:13:24 +00:00
/* setup show/hide toggles */
2013-06-06 23:38:47 +00:00
generateToggle("#usercountwrap", "#userlist");
2013-06-20 04:42:20 +00:00
$("#usercountwrap").click(scrollChat);
2013-06-06 23:38:47 +00:00
generateToggle("#librarytoggle", "#librarywrap");
generateToggle("#userpltoggle", "#userplaylistwrap");
generateToggle("#playlisttoggle", "#playlist_controls");
2013-06-07 03:13:24 +00:00
/* navbar stuff */
$("#optlink").click(showOptionsMenu);
2013-06-11 23:51:00 +00:00
$("#chatonly").click(chatOnly);
2013-06-07 22:09:36 +00:00
function guestLogin() {
socket.emit("login", {
2013-06-07 03:13:24 +00:00
name: $("#guestname").val(),
});
2013-06-07 22:09:36 +00:00
}
$("#guestlogin").click(guestLogin);
$("#guestname").keydown(function(ev) {
if(ev.keyCode == 13) {
guestLogin();
}
2013-06-07 03:13:24 +00:00
});
2013-06-07 22:09:36 +00:00
2013-06-07 03:13:24 +00:00
$("#login").click(showLoginMenu);
$("#logout").click(function() {
eraseCookie("cytube_name");
eraseCookie("cytube_session");
2013-06-07 22:09:36 +00:00
document.location.reload(true);
2013-06-07 03:13:24 +00:00
});
/* chatbox */
2013-06-20 04:42:20 +00:00
$("#messagebuffer").mouseenter(function() { SCROLLCHAT = false; });
$("#messagebuffer").mouseleave(function() { SCROLLCHAT = true; });
2013-06-09 18:03:41 +00:00
$("#chatline").keydown(function(ev) {
2013-06-07 03:13:24 +00:00
if(ev.keyCode == 13) {
var msg = $("#chatline").val();
if(msg.trim()) {
if(USEROPTS.modhat && CLIENT.rank >= Rank.Moderator) {
msg = "/m " + msg;
}
socket.emit("chatMsg", {
msg: msg
});
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;
}
else if(ev.keyCode == 9) {
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();
if(name.indexOf(current) == 0 && match == null) {
match = users[i].children[1].innerHTML;
}
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;
}
else if(ev.keyCode == 38) {
if(CHATHISTIDX == CHATHIST.length) {
CHATHIST.push($("#chatline").val());
}
if(CHATHISTIDX > 0) {
CHATHISTIDX--;
$("#chatline").val(CHATHIST[CHATHISTIDX]);
}
ev.preventDefault();
return false;
}
else if(ev.keyCode == 40) {
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() {
socket.emit("searchMedia", {
source: "yt",
query: $("#library_query").val().toLowerCase()
});
});
/* user playlists */
$("#userpltoggle").click(function() {
socket.emit("listPlaylists");
});
$("#userpl_save").click(function() {
if($("#userpl_name").val().trim() == "") {
makeAlert("Invalid Name", "Playlist name cannot be empty", "alert-error")
.addClass("span12")
.insertAfter($("#userpl_save").parent());
return;
}
socket.emit("savePlaylist", {
name: $("#userpl_name").val()
});
});
/* playlist controls */
2013-06-07 22:09:36 +00:00
2013-06-11 19:41:03 +00:00
$("#queue").sortable({
start: function(ev, ui) {
PL_FROM = ui.item.prevAll().length;
},
update: function(ev, ui) {
PL_TO = ui.item.prevAll().length;
if(PL_TO != PL_FROM) {
socket.emit("moveMedia", {
from: PL_FROM,
to: PL_TO
});
2013-06-11 15:29:21 +00:00
}
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-06-07 22:09:36 +00:00
function queue(pos) {
var links = $("#mediaurl").val().split(",");
if(pos == "next") {
links = links.reverse();
}
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) {
makeAlert("Error", "Invalid link. Please double check it and remove extraneous information", "alert-error")
.addClass("span12")
.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-06-07 22:09:36 +00:00
$("#getplaylist").click(function() {
var callback = function(data) {
2013-06-11 19:41:03 +00:00
PLAYER.hide();
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++) {
var entry = formatURL(data[i]);
2013-06-07 22:09:36 +00:00
list.push(entry);
}
var urls = list.join(",");
var modal = $("<div/>").addClass("modal hide fade")
.appendTo($("body"));
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);
$("<input/>").attr("type", "text")
.val(urls)
.appendTo(body);
$("<div/>").addClass("modal-footer").appendTo(modal);
modal.on("hidden", function() {
modal.remove();
2013-06-11 19:41:03 +00:00
PLAYER.unhide();
2013-06-07 22:09:36 +00:00
});
modal.modal();
}
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 */
$(window).resize(function() {
VWIDTH = $("#ytapiplayer").parent().css("width").replace("px", "");
var VHEIGHT = ""+parseInt(parseInt(VWIDTH) * 9 / 16);
$("#messagebuffer").css("height", (VHEIGHT - 31) + "px");
$("#userlist").css("height", (VHEIGHT - 31) + "px");
$("#ytapiplayer").attr("width", VWIDTH);
$("#ytapiplayer").attr("height", VHEIGHT);
});
2013-06-20 22:15:35 +00:00
/* first time */
if(USEROPTS.first_visit) {
var al = makeAlert("Playlist Options", [
"From the Options menu, you can choose to automatically",
" hide the buttons on each entry (and show them when",
" you right click). You can also choose to use the old",
" style of playlist buttons.",
"<br>"].join(""))
.addClass("span12")
.insertBefore($("#queue"));
al.find(".close").remove();
$("<button/>").addClass("btn btn-primary")
.text("Dismiss")
.appendTo(al)
.click(function() {
USEROPTS.first_visit = false;
saveOpts();
al.hide("blind", function() {
al.remove();
});
});
}
2013-06-11 15:29:21 +00:00
/* initial YouTube api */
if(!USEROPTS.hidevid) {
var tag = document.createElement("script");
tag.src = "http://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
function onYouTubeIframeAPIReady() {
if(!PLAYER)
PLAYER = new Player({id:"", type: "yt"});
if(FLUIDLAYOUT)
fluid();
}
/* load channel */
var loc = document.location+"";
var m = loc.match(/\/r\/([a-zA-Z0-9-_]+)$/);
if(m) {
CHANNEL.name = m[1];
}
else {
var main = $("#main");
var container = $("<div/>").addClass("container").insertBefore(main);
var row = $("<div/>").addClass("row").appendTo(container);
var div = $("<div/>").addClass("span6").appendTo(row);
main.css("display", "none");
var label = $("<label/>").text("Enter Channel:").appendTo(div);
var entry = $("<input/>").attr("type", "text").appendTo(div);
entry.keydown(function(ev) {
var host = ""+document.location;
host = host.replace("http://", "");
host = host.substring(0, host.indexOf("/"));
if(ev.keyCode == 13) {
document.location = "http://" + host + "/r/" + entry.val();
socket.emit("joinChannel", {
name: entry.val()
});
container.remove();
main.css("display", "");
}
});
}
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;
});