spee.ch/views/index.handlebars

154 lines
5.2 KiB
Handlebars
Raw Normal View History

2017-06-11 03:33:03 +02:00
<div class="container">
<div class="row">
<div class="col-md-12">
2017-06-11 04:10:46 +02:00
<div class="card card-block default-color-dark white-text">
<div class="card-title">
2017-06-11 03:33:03 +02:00
<h1>Spee.ch</h1>
</div>
2017-06-11 04:10:46 +02:00
<p>Spee.ch is a single-serving site that reads and publishes images to and from the <a class="white-text" href="https://lbry.io">LBRY</a> blockchain.</p>
2017-06-11 03:33:03 +02:00
</div>
</div>
</div>
<div class="row">
{{> examples}}
{{> publish}}
</div>
<div class="row">
{{> documentation}}
</div>
<div class="row">
{{> contribute}}
{{> bugs}}
</div>
</div>
2017-06-10 01:46:57 +02:00
<script src="/socket.io/socket.io.js"></script>
<script src="/siofu/client.js"></script>
<script>
2017-06-10 05:39:07 +02:00
// define variables
2017-06-10 01:46:57 +02:00
var socket = io();
var uploader = new SocketIOFileUpload(socket);
2017-06-10 05:39:07 +02:00
var stagedFile = null;
2017-06-10 01:46:57 +02:00
2017-06-10 05:39:07 +02:00
/* helper functions */
// create a progress animation
function createProgressBar(element, size){
2017-06-10 01:46:57 +02:00
var x = 1;
var adder = 1;
function addOne(){
var bars = '<p>|';
2017-06-10 05:46:19 +02:00
for (var i = 0; i < x; i++){ bars += ' | '; }
2017-06-10 01:46:57 +02:00
bars += '</p>';
element.innerHTML = bars;
if (x === size){
adder = -1;
} else if ( x === 0){
adder = 1;
}
x += adder;
};
setInterval(addOne, 300);
}
2017-06-10 05:39:07 +02:00
// preview file and stage the image for upload
function previewAndStageFile(selectedFile){
var preview = document.getElementById('image-preview');
var dropzone = document.getElementById('drop-zone');
2017-06-10 01:46:57 +02:00
var previewReader = new FileReader();
2017-06-10 05:39:07 +02:00
preview.style.display = 'block';
dropzone.style.display = 'none';
2017-06-10 01:46:57 +02:00
previewReader.onloadend = function () {
preview.src = previewReader.result;
};
2017-06-10 05:39:07 +02:00
2017-06-10 01:46:57 +02:00
if (selectedFile) {
previewReader.readAsDataURL(selectedFile); // reads the data and sets the img src
2017-06-10 05:39:07 +02:00
document.getElementById('publish-name').value = selectedFile.name.substring(0, selectedFile.name.indexOf('.')); // updates metadata inputs
stagedFile = [selectedFile]; // stores the selected file for upload
2017-06-10 01:46:57 +02:00
} else {
preview.src = '';
}
}
2017-06-10 05:39:07 +02:00
// update the publish status
2017-06-10 01:46:57 +02:00
function updatePublishStatus(msg){
document.getElementById('publish-status').innerHTML = msg;
}
2017-06-10 05:39:07 +02:00
// process the drop-zone drop
function drop_handler(ev) {
console.log("drop");
ev.preventDefault();
// if dropped items aren't files, reject them
var dt = ev.dataTransfer;
if (dt.items) {
if (dt.items[0].kind == 'file') {
var droppedFile = dt.items[0].getAsFile();
previewAndStageFile(droppedFile);
} else {
console.log("no files were found")
}
} else {
console.log("no items were found")
}
}
// prevent the browser's default drag behavior
function dragover_handler(ev) {
ev.preventDefault();
}
// remove all of the drag data
function dragend_handler(ev) {
var dt = ev.dataTransfer;
if (dt.items) {
for (var i = 0; i < dt.items.length; i++) {
dt.items.remove(i);
}
} else {
ev.dataTransfer.clearData();
}
}
2017-06-10 01:46:57 +02:00
2017-06-10 05:39:07 +02:00
/* configure the submit button */
2017-06-10 01:46:57 +02:00
document.getElementById('publish-submit').addEventListener('click', function(event){
event.preventDefault();
2017-06-10 05:39:07 +02:00
if (stagedFile) {
uploader.submitFiles(stagedFile);
};
2017-06-10 01:46:57 +02:00
})
2017-06-10 05:39:07 +02:00
/* socketio-file-upload listeners */
2017-06-10 01:46:57 +02:00
uploader.addEventListener('start', function(event){
var name = document.getElementById('publish-name').value;
var license = document.getElementById('publish-license').value;
var nsfw = document.getElementById('publish-nsfw').value;
event.file.meta.name = name;
event.file.meta.license = license;
event.file.meta.nsfw = nsfw;
2017-06-10 05:39:07 +02:00
// re-set the html in the publish area
2017-06-10 05:46:19 +02:00
document.getElementById('publish-active-area').innerHTML = '<div id="publish-status"></div><div id="progress-bar"></div>';
2017-06-10 05:39:07 +02:00
// start a progress animation
2017-06-10 01:46:57 +02:00
createProgressBar(document.getElementById('progress-bar'), 12);
});
uploader.addEventListener('progress', function(event){
var percent = event.bytesLoaded / event.file.size * 100;
updatePublishStatus('File is ' + percent.toFixed(2) + '% loaded to the server');
2017-06-10 05:39:07 +02:00
});
2017-06-10 01:46:57 +02:00
2017-06-10 05:39:07 +02:00
/* socket.io message listeners */
2017-06-10 01:46:57 +02:00
socket.on('publish-status', function(msg){
updatePublishStatus(msg);
2017-06-10 05:39:07 +02:00
});
2017-06-10 01:46:57 +02:00
socket.on('publish-failure', function(msg){
2017-06-10 23:15:02 +02:00
document.getElementById('publish-active-area').innerHTML = '<p>' + JSON.stringify(msg) + '</p><p> --(✖╭╮✖)→ </p>';
2017-06-10 05:39:07 +02:00
});
2017-06-10 01:46:57 +02:00
socket.on('publish-complete', function(msg){
var publishResults = '<p>Your publish is complete!</p>';
publishResults += '<p>Your Claim ID is: ' + msg.result.claim_id + '</p>';
publishResults += '<p>Your Transaction ID is: <a href="https://explorer.lbry.io/#!/transaction?id=' + msg.result.txid + '">' + msg.result.txid + '</a></p>';
publishResults += '<p>Here is a link to the claim where your asset was published: <a href="https://spee.ch/' + msg.name + '">spee.ch/' + msg.name + '</a></p>';
publishResults += '<p>Here is a direct link to your asset: <a href="https://spee.ch/' + msg.name + '/' + msg.result.claim_id + '">spee.ch/' + msg.name + '/' + msg.result.claim_id + '</a></p>';
publishResults += '<p><i>NOTE: the transaction still needs to be mined by the network before you can access it! This may take a few minutes. To to view the transaction on the blockchain explorer click the Transaction ID link above.</i></p>';
publishResults += '<p><a href="/">Reload to publish another asset</a></p>';
2017-06-10 05:46:19 +02:00
document.getElementById('publish-active-area').innerHTML = publishResults;
2017-06-10 05:39:07 +02:00
});
2017-06-10 01:46:57 +02:00
</script>