joinpeertube/app/instances.js

118 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-09-21 21:00:42 +00:00
/* eslint-disable */
2018-03-15 15:12:18 +00:00
$(function () {
2018-03-27 09:24:21 +00:00
const instancesApi = 'https://instances.joinpeertube.org/api/v1/instances'
2018-03-15 15:12:18 +00:00
const data = {
start: 0,
count: 100,
signup: true,
healthy: true
}
2018-03-15 15:36:41 +00:00
const instancesListElement = $('#instances-list')
2018-03-15 15:12:18 +00:00
2018-03-15 15:36:41 +00:00
$.get(instancesApi, data)
.done(function (res) {
2018-03-16 13:42:57 +00:00
const instances = shuffle(res.data)
2018-03-15 15:12:18 +00:00
2018-03-15 15:36:41 +00:00
const lis = []
instances.forEach(function (instance) {
2018-03-16 10:32:58 +00:00
const el = createInstanceElement(instance)
2018-03-15 15:36:41 +00:00
lis.push(el)
})
2018-03-15 15:12:18 +00:00
2018-03-15 15:36:41 +00:00
instancesListElement.append(lis)
})
2018-03-16 10:32:58 +00:00
.fail(function () {
2018-03-15 15:36:41 +00:00
$('#instances-list-error').css('display', 'block')
})
2018-03-15 15:12:18 +00:00
2018-03-16 10:32:58 +00:00
function createInstanceElement (instance) {
2018-03-15 15:12:18 +00:00
const a = $('<a>', {
class: 'list-group-item',
2018-03-16 10:32:58 +00:00
href: 'https://' + instance.host,
2018-03-15 15:12:18 +00:00
target: '_blank',
2018-03-16 10:32:58 +00:00
title: instance.host
})
const leftDiv = $('<div>', {
class: 'left-div'
})
const rightDiv = $('<div>', {
class: 'right-div'
2018-03-15 15:12:18 +00:00
})
const h4 = $('<h4>', {
2018-03-16 10:32:58 +00:00
class: 'list-group-item-heading'
})
const spanName = $('<span>', {
text: instance.name,
class: 'instance-name'
2018-03-15 15:12:18 +00:00
})
2018-03-16 10:32:58 +00:00
const spanHost = $('<small>', {
text: instance.host,
class: 'instance-host'
})
h4.append(spanName, spanHost)
leftDiv.append(h4)
2018-03-15 15:12:18 +00:00
2018-03-16 10:32:58 +00:00
if (instance.shortDescription) {
2018-03-15 15:12:18 +00:00
const p = $('<p>', {
class: 'list-group-item-text',
2018-03-16 10:32:58 +00:00
text: instance.shortDescription
2018-03-15 15:12:18 +00:00
})
2018-03-16 10:32:58 +00:00
leftDiv.append(p)
2018-03-15 15:12:18 +00:00
}
2018-03-16 10:32:58 +00:00
if (instance.totalInstanceFollowers) {
const li = $('<li>', {
text: instance.totalInstanceFollowers + ' followers'
})
rightDiv.append(li)
}
if (instance.totalInstanceFollowing) {
const li = $('<li>', {
text: 'Follows ' + instance.totalInstanceFollowing + ' instances'
})
rightDiv.append(li)
}
2018-03-28 12:40:42 +00:00
if (instance.userVideoQuota) {
const li = $('<li>', {
text: bytes(instance.userVideoQuota) + ' per user'
})
rightDiv.append(li)
}
2018-03-16 10:32:58 +00:00
a.append(leftDiv, rightDiv)
2018-03-15 15:12:18 +00:00
return a
}
2018-03-16 13:42:57 +00:00
// Thanks https://stackoverflow.com/a/6274381
function shuffle (a) {
for (var i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]]
}
return a
}
2018-03-28 12:40:42 +00:00
// https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
const dictionaryBytes = [
{ max: 1024, type: 'B' },
{ max: 1048576, type: 'KB' },
{ max: 1073741824, type: 'MB' },
{ max: 1.0995116e12, type: 'GB' }
]
function bytes (value) {
if (value === -1) return 'No quota'
const format = dictionaryBytes.find(function (d) { return value < d.max }) || dictionaryBytes[dictionaryBytes.length - 1]
const calc = Math.floor(value / (format.max / 1024)).toString()
return calc + format.type
}
2018-03-15 15:12:18 +00:00
})