little-library/public/js/little-library.js

67 lines
2.2 KiB
JavaScript
Raw Normal View History

$(document).ready(function() {
2018-12-27 00:48:14 +01:00
var socket = io();
2018-12-27 20:40:03 +01:00
var downloadButton;
2019-01-04 00:38:22 +01:00
socket.on('update visitors', function(visitors) {
$('#visitors').text(visitors);
});
2018-12-27 20:40:03 +01:00
socket.on('get book', function(url) {
$(downloadButton).replaceWith('<a download href="' + url + '" class="button is-success is-large">Download</a>');
});
socket.on('remove book', function(bookId) {
var book = $('#book_' + bookId);
var modal = $('<div class="modal is-active"><div class="modal-background"></div><div class="modal-content"><button class="modal-close"></button><div class="notification is-danger"><h2 class="title">Sorry!</h2><p>Someone took the book you were looking at.</h2></div></div></div>');
modal.find('.modal-background, .modal-close').click(function() {
modal.remove();
});
book.after(modal);
book.remove();
});
2018-12-28 01:10:50 +01:00
$('.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');
}
});
2018-12-27 20:40:03 +01:00
$('.modal-background, .modal-close, .modal-card-head .delete, .modal .close').click(function() {
$(this).closest('.modal').removeClass('is-active');
2018-12-27 20:40:03 +01:00
downloadButton = undefined;
});
$('.modal-button').click(function() {
var modal = $(this).data('modal');
$('#' + modal).addClass('is-active');
});
2018-12-27 20:40:03 +01:00
$('.take-book').click(function() {
var id = $(this).data('book');
$('#book_' + id).find('.box')
.removeClass('box').addClass(['notification', 'is-success'])
.attr('title', 'This can be downloaded until you leave this page');
2018-12-27 20:40:03 +01:00
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');
2018-12-27 07:36:47 +01:00
});
});