piratebox-web/js/scripts.js

163 lines
4.7 KiB
JavaScript
Raw Normal View History

2019-08-04 08:28:03 +00:00
$(document).ready(function() {
$.get('/piratebox/station-cnt', function(data) {
$('#station-cnt').text(data.count);
});
$('div#shoutbox').ajaxError(function() {
$(this).text('Triggered ajaxError handler on shoutbox');
});
$('#sb_form').submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
post_shoutbox();
});
display_diskusage();
display_shoutbox();
// Spin menu icon and toggle nav
$('#menu-icon').click(function() {
$(this).toggleClass('rotate');
$('#top-nav').slideToggle();
});
// Closes the mobile nav
$('#top-nav a').click(function() {
if ($('#top-nav').is(':visible') && $('#menu-icon').is(':visible')) {
$('#top-nav').slideUp();
$('#menu-icon').toggleClass('rotate');
}
});
// Hides the welcome
$('#thanks').click(function() {
$('#welcome').slideUp();
});
// Detects window size
$(window).resize(function() {
if ($('#menu-icon').is(':visible')) {
$('#top-nav').hide();
} else {
$('#top-nav').show();
}
});
// smooth scrolling for internal links
function filterPath(string) {
return string.replace(/^\//, '')
.replace(/(index|default).[a-zA-Z]{3,4}$/, '')
.replace(/\/$/, '');
}
var locationPath = filterPath(location.pathname);
var scrollElem = scrollableElement('html', 'body');
$('a[href*=\\#]').each(function() {
var thisPath = filterPath(this.pathname) || locationPath;
if (locationPath == thisPath &&
(location.hostname == this.hostname || !this.hostname) &&
this.hash.replace(/#/, '')) {
var $target = $(this.hash), target = this.hash;
if (target) {
var targetOffset = $target.offset().top;
$(this).click(function(event) {
event.preventDefault();
$(scrollElem)
.animate({scrollTop: targetOffset}, 400, function() {
location.hash = target;
});
});
}
}
});
// use the first element that is "scrollable"
function scrollableElement(els) {
for (var i = 0, argLength = arguments.length; i < argLength; i++) {
var el = arguments[i], $scrollElement = $(el);
if ($scrollElement.scrollTop() > 0) {
return el;
} else {
$scrollElement.scrollTop(1);
var isScrollable = $scrollElement.scrollTop() > 0;
$scrollElement.scrollTop(0);
if (isScrollable) {
return el;
}
}
}
return [];
}
});
function refresh_shoutbox() {
$.get('/piratebox/chat', function(data) {
let sb = $('div#shoutbox');
let was_bottom = false;
if (sb.get(0).scrollTop >= sb.get(0).scrollHeight - sb.get(0).clientHeight - 10) {
was_bottom = true;
}
sb.empty();
for (let msg of data) {
sb.append(render_chat_content(msg));
}
if (was_bottom) {
sb.get(0).scrollTop = sb.get(0).scrollHeight;
}
});
}
function refresh_time_sb() {
// Refresh rate in milli seconds
mytime = setTimeout('display_shoutbox()', 10000);
}
function post_shoutbox() {
$('#send-button').prop('value', 'Sending...');
$('#send-button').prop('disabled', true);
$.post('/piratebox/chat', $('#sb_form').serialize())
.success(function() {
refresh_shoutbox();
$('#send-button').prop('value', 'Send')
$('#send-button').prop('disabled', false);
});
$('#shoutbox-input .message').val('');
}
function display_shoutbox() {
refresh_shoutbox();
refresh_time_sb();
}
function refresh_diskusage() {
$.get('/piratebox/diskusage', function(data) {
let usage = data.usage;
$('div#diskusage .progress-bar').attr("aria-valuenow", usage).css("width", usage + "%");
$('div#diskusage .sr-only').text(usage + "%");
});
}
function refresh_time_du() {
// Refresh rate in milli seconds
mytimedu = setTimeout('display_diskusage()', 10000);
}
function display_diskusage() {
refresh_diskusage();
refresh_time_du();
}
function fnGetDomain(url) {
return url.match(/:\/\/(.[^/]+)/)[1];
}
function render_chat_content(item) {
return $('<div>')
.addClass('message')
.append($('<date>').text(item.date))
.append($('<name>').text(item.name + ':'))
.append($('<data>').addClass(item.color).text(item.data));
}