$(document).ready(function() { var socket = io(); var downloadButton; socket.on('update visitors', function(visitors) { $('#visitors').text(visitors); }); socket.on('get book', function(url) { $(downloadButton).replaceWith('Download'); }); socket.on('remove book', function(bookId) { var book = $('#book_' + bookId); var modal = $(''); modal.find('.modal-background, .modal-close').click(function() { modal.remove(); }); book.after(modal); book.remove(); }); $('.navbar-burger').click(function() { if ($(this).hasClass('is-active')) { $(this).removeClass('is-active'); $(this).attr('aria-expanded', 'false'); $('.navbar-menu').removeClass('is-active'); } else { $(this).addClass('is-active'); $(this).attr('aria-expanded', 'true'); $('.navbar-menu').addClass('is-active'); } }); $('#readableToggle').click(function() { var useReadable = getCookieValue('useReadable'); document.cookie = 'useReadable=' + (useReadable !== 'yes' ? 'yes' : 'no'); window.location.reload(); }); $('.modal-background, .modal-close, .modal-card-head .delete, .modal .close').click(function() { $(this).closest('.modal').removeClass('is-active'); downloadButton = undefined; }); $('.modal-button').click(function() { var modal = $(this).data('modal'); $('#' + modal).addClass('is-active'); }); $('.take-book').click(function() { var id = $(this).data('book'); var book = $('#book_' + id).find('.spine'); if (book) { book.addClass('is-success'); } else { book = $('#book_' + id).find('.box'); book.removeClass('box').addClass(['notification', 'is-success']) } book.attr('title', 'This can be downloaded until you leave this page'); socket.emit('take book', id); downloadButton = this; $(this).addClass('is-loading'); }); $('#book').change(function() { var fileName = $(this).val(); if (fileName) { const lastIndexOfSlash = fileName.lastIndexOf('\\'); if (lastIndexOfSlash < 0) { lastIndexOfSlash = fileName.lastIndexOf('/'); } fileName = fileName.substr(lastIndexOfSlash + 1); } $('#bookFileName').text(fileName ? fileName : 'None Selected'); }); }); function getCookieValue(key) { var matches = document.cookie.match('(^|;)\\s*' + key + '\\s*=\\s*([^;]+)'); return matches ? matches.pop() : '' }