From 8c308c2bf7f658945d80be9d5880361238635f5b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 9 Jun 2015 17:41:40 +0200 Subject: Spawn --- public/javascripts/index.js | 221 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 public/javascripts/index.js (limited to 'public/javascripts') diff --git a/public/javascripts/index.js b/public/javascripts/index.js new file mode 100644 index 000000000..95caf7eb4 --- /dev/null +++ b/public/javascripts/index.js @@ -0,0 +1,221 @@ +;(function () { + 'use strict' + + var $ = require('jquery') + require('blueimp-file-upload') + + var WebTorrent = require('webtorrent') + var client = new WebTorrent({ dht: false }) + + var $content = $('#ajax_load') + + // Webtorrent events + client.on('error', function (err) { + console.error(err) + }) + + client.on('warning', function (err) { + console.warning(err) + }) + + // Events of the panel + $('#panel_get_videos').on('click', function () { + getVideos() + }) + + $('#panel_upload_video').on('click', function () { + uploadVideo() + }) + + $('#panel_make_friends').on('click', function () { + makeFriends() + }) + + $('#search_video').on('keyup', function (e) { + var search = $(this).val() + + if (search === '') return + + if (e.keyCode === 13) { + $.ajax({ + url: '/api/videos/search/' + search, + type: 'GET', + dataType: 'json', + success: function (videos) { + printVideos(videos) + } + }) + } + }) + + // Join a new network + function makeFriends () { + $.ajax({ + url: '/api/pods/makefriends', + type: 'GET', + dataType: 'json', + success: function () { + alert('Made friends!') + } + }) + } + + function printVideos (videos) { + $content.empty() + + if (videos.length === 0) { + $content.text('There is no videos.') + } + + videos.forEach(function (video) { + var $video = $('
').addClass('video') + + var $video_name = $('').addClass('video_name').text(video.name) + var $video_pod = $('').addClass('video_pod_url').text(video.podUrl) + var $remove = $('').addClass('span_action glyphicon glyphicon-remove') + var $header = $('
').append([ $video_name, $video_pod, $remove ]) + + var $video_description = $('
').addClass('video_description').text(video.description) + + // Get the video + $video_name.on('click', function () { + getVideo(video) + }) + + // Remove the video + $remove.on('click', function () { + // TODO + if (!confirm('Are you sure ?')) return + + removeVideo(video) + }) + + if (!video.magnetUri) { + $remove.css('display', 'none') + } + + $video.append([ $header, $video_description ]) + $content.append($video) + }) + } + + // Upload the video, the server will seed it + function uploadVideo () { + // Creating all the elements + var $video_label = $('').attr('for', 'name').text('Video name') + var $video_name = $('').addClass('form-control').attr({ + name: 'name', + id: 'name' + }) + var $video_block = $('
').addClass('form-group').append([ $video_label, $video_name ]) + + var $title = $('

').text('Upload a video') + + var $button_text = $('').text('Select the video...') + var $input_video = $('').attr({ + type: 'file', + name: 'input_video', + id: 'input_video' + }) + var $button = $('
').addClass('btn btn-default btn-file').append([ $button_text, $input_video ]) + + var $description_label = $('').attr('for', 'description').text('Description') + var $description_text = $('').addClass('form-control').attr({ + name: 'description', + id: 'description', + placeholder: 'Description...' + }) + var $description = $('
').addClass('form-group').append([ $description_label, $description_text ]) + + var $bar = $('
').attr('id', 'progress').append($bar) + + var $input_submit = $('').addClass('btn btn-default').attr({ + type: 'button', + value: 'Upload' + }) + + // JQuery plugin + var $form_video = $('
').append([ $video_block, $button, $progress_bar, $description, $input_submit ]) + $form_video.fileupload({ + singleFileUploads: true, + multipart: true, + url: '/api/videos', + autoupload: false, + add: function (e, data) { + var $text = $('').addClass('name_file').text(data['files'][0]['name']) + $text.insertAfter($button) + $input_submit.off('click').on('click', function () { + $bar.css('display', 'block') + data.formData = $form_video.serializeArray() + data.submit() + }) + }, + progressall: function (e, data) { + $bar.attr({ + value: data.loaded, + max: data.total + }) + }, + done: function (e, data) { + // Print all the videos once it's finished + getVideos() + } + }) + + $content.empty() + $content.append([ $title, $form_video ]) + } + + // Print the list of all the videos + function getVideos () { + $.ajax({ + url: '/api/videos/', + dataType: 'json', + type: 'GET', + success: function (videos) { + printVideos(videos) + } + }) + } + + function removeVideo (video) { + $.ajax({ + url: '/api/videos/' + video._id, + type: 'DELETE', + success: function (response, status) { + getVideos() + } + }) + } + + // Get the video: add the torrent file and stream it into a video tag + function getVideo (video) { + var $waiting = $('').addClass('center-block loading').attr('src', '/images/loading.gif') + $content.empty() + $content.append($waiting) + + console.log('Getting ' + video) + client.add(video.magnetUri, function (torrent) { + var $embed = $('
').addClass('embed-responsive embed-responsive-16by9') + + $content.empty() + $content.append($embed) + + // Got torrent metadata! + console.log('Torrent info hash:', torrent.infoHash) + + // Let's say the first file is a webm (vp8) or mp4 (h264) video... + var file = torrent.files[0] + + file.appendTo($embed.get(0), function (err) { + if (err) { + alert('Cannot append the file.') + console.err(err) + } + }) + }) + } + + getVideos() +})() -- cgit v1.2.3