diff --git a/webclient/showbot.js b/webclient/showbot.js index 46fbb12..da91b75 100644 --- a/webclient/showbot.js +++ b/webclient/showbot.js @@ -4,30 +4,30 @@ window.Showbot = window.Showbot || {}; Showbot.Bot = (function ($) { - // Templates - var titleRowTemplate = null; + // Templates + var titleRowTemplate = null; var linkRowTemplate = null; - var connectingTemplate = null; + var connectingTemplate = null; - // State - var connection = null; + // State + var connection = null; - function init() { - $(function () { + function init() { + $(function () { titleRowTemplate = titleRowTemplate || Handlebars.compile($("#titleRow").html()); linkRowTemplate = linkRowTemplate || Handlebars.compile($('#linkRow').html()); - connectingTemplate = connectingTemplate || Handlebars.compile($('#connectingMessage').html()); + connectingTemplate = connectingTemplate || Handlebars.compile($('#connectingMessage').html()); - Handlebars.registerHelper('timeAgo', function (date) { + Handlebars.registerHelper('timeAgo', function (date) { return moment(date).fromNow(); - }); + }); - connectSocket(); + connectSocket(); - // Show the connecting message - $(".message").html(connectingTemplate()); - }); - } + // Show the connecting message + $(".message").html(connectingTemplate()); + }); + } function voteHandler(anchor) { var id = $(anchor).closest('tr').data('id'); @@ -47,11 +47,11 @@ Showbot.Bot = (function ($) { return false; } - function resetToDefault(){ - $('.message').fadeOut(function () { - $('.tables').fadeIn(); - }); - } + function resetToDefault(){ + $('.message').fadeOut(function () { + $('.tables').fadeIn(); + }); + } function ping() { if (connection != null) { @@ -64,47 +64,47 @@ Showbot.Bot = (function ($) { }); } - function connectSocket() { - if (connection == null || connection.readyState == 3) { - // Connect to the server and await feedback. + function connectSocket() { + if (connection == null || connection.readyState == 3) { + // Connect to the server and await feedback. if (window.location.hostname == 'localhost' || window.location.hostname == '') { connection = new WebSocket('ws://localhost:5001'); } else { - connection = new WebSocket('ws://thawing-bayou-3232.herokuapp.com:80'); + connection = new WebSocket('ws://thawing-bayou-3232.herokuapp.com:80'); } - connection.onopen = function (event) { - resetToDefault(); + connection.onopen = function (event) { + resetToDefault(); setInterval(ping, 30000); - }; + }; - connection.onmessage = function (message) { - var packet = JSON.parse(message.data); + connection.onmessage = function (message) { + var packet = JSON.parse(message.data); //console.log(JSON.stringify(packet)); if (packet.operation == 'REFRESH') { // Refresh everything var titles = packet.titles; - $('.titles tbody').empty(); + $('.titles tbody').empty(); var html = ""; - // Create all the rows - var titlesAlreadyVoted = []; + // Create all the rows + var titlesAlreadyVoted = []; for (var i=0; i < titles.length; ++i) { html += titleRowTemplate(titles[i]); - if (titles[i].voted) { - titlesAlreadyVoted.push(titles[i].id); - } + if (titles[i].voted) { + titlesAlreadyVoted.push(titles[i].id); + } } - // Add to the table + // Add to the table $('.titles tbody').html(html); - // Remove all the anchors for already voted titles - console.log(JSON.stringify(titlesAlreadyVoted)); - for (var i=0; i < titlesAlreadyVoted.length; ++i) { - $('tr[data-id=' + titlesAlreadyVoted[i] + ']').find('a').remove(); - } + // Remove all the anchors for already voted titles + console.log(JSON.stringify(titlesAlreadyVoted)); + for (var i=0; i < titlesAlreadyVoted.length; ++i) { + $('tr[data-id=' + titlesAlreadyVoted[i] + ']').find('a').remove(); + } - // Show the table + // Show the table if (!$('.titles').is(':visible')) { $('.titles').fadeIn(); } @@ -130,27 +130,27 @@ Showbot.Bot = (function ($) { } else if (packet.operation == 'PONG') { // NOOP } - }; + }; - connection.onclose = function (event) { - $('.tables').fadeOut(function () { + connection.onclose = function (event) { + $('.tables').fadeOut(function () { $('.message').fadeIn(); }); setTimeout(connectSocket, 5000); clearInterval(ping); - }; - - connection.onerror = function (error) { - console.log("Error: " + JSON.stringify(error)); - }; - } else { - setTimeout(connectSocket, 5000); - } - } - - return { - init: init, + }; + + connection.onerror = function (error) { + console.log("Error: " + JSON.stringify(error)); + }; + } else { + setTimeout(connectSocket, 5000); + } + } + + return { + init: init, voteHandler: voteHandler, linkHandler: linkHandler - }; + }; })(jQuery);