platypush/platypush/backend/http/static/js/video.omxplayer.js

151 lines
4.6 KiB
JavaScript
Raw Normal View History

2018-04-24 01:03:50 +02:00
$(document).ready(function() {
var $container = $('#video-container'),
2018-04-24 01:23:06 +02:00
$searchForm = $('#video-search'),
$videoResults = $('#video-results'),
$volumeCtrl = $('#video-volume-ctrl'),
$ctrlForm = $('#video-ctrl'),
prevVolume = undefined;
var updateVideoResults = function(videos) {
$videoResults.html('');
for (var video of videos) {
var $videoResult = $('<div></div>')
.addClass('video-result')
.attr('data-url', video['url'])
.html('title' in video ? video['title'] : video['url']);
var $icon = getVideoIconByUrl(video['url']);
$icon.prependTo($videoResult);
$videoResult.appendTo($videoResults);
}
};
2018-04-24 01:03:50 +02:00
var getVideoIconByUrl = function(url) {
var $icon = $('<i class="fa"></i>');
if (url.startsWith('file://')) {
2018-04-26 15:29:10 +02:00
$icon.addClass('fa-download');
} else if (url.startsWith('https://www.youtube.com/')) {
$icon.addClass('fa-youtube');
} else if (url.startsWith('magnet:?')) {
2018-04-26 15:29:10 +02:00
$icon.addClass('fa-film');
} else {
$icon.addClass('fa-video');
}
var $iconContainer = $('<span></span>').addClass('video-icon-container');
$icon.appendTo($iconContainer);
return $iconContainer;
};
2018-04-24 01:03:50 +02:00
var initBindings = function() {
2018-04-24 01:23:06 +02:00
$searchForm.on('submit', function(event) {
var $input = $(this).find('input[name=video-search-text]');
var resource = $input.val();
var request = {}
var onSuccess = function() {};
var onError = function() {};
var onComplete = function() {
$input.prop('disabled', false);
};
2018-04-24 01:03:50 +02:00
$input.prop('disabled', true);
$videoResults.text('Searching...');
2018-04-24 01:03:50 +02:00
if (resource.match(new RegExp('^https?://')) ||
resource.match(new RegExp('^file://'))) {
var videos = [{ url: resource }];
updateVideoResults(videos);
request = {
2018-04-24 01:03:50 +02:00
type: 'request',
action: 'video.omxplayer.play',
args: { resource: resource }
};
} else {
request = {
type: 'request',
action: 'video.omxplayer.search',
args: { query: resource }
};
2018-04-24 01:03:50 +02:00
onSuccess = function(response) {
var videos = response.response.output;
updateVideoResults(videos);
};
}
2018-04-24 01:03:50 +02:00
execute(request, onSuccess, onError, onComplete)
2018-04-24 01:03:50 +02:00
return false;
});
2018-04-24 01:23:06 +02:00
2018-04-24 01:30:04 +02:00
$ctrlForm.on('submit', function() { return false; });
$ctrlForm.find('button[data-action]').on('click touch', function(evt) {
2018-04-24 01:23:06 +02:00
var action = $(this).data('action');
var $btn = $(this);
execute(
{
type: 'request',
action: 'video.omxplayer.' + action,
}
);
});
$volumeCtrl.on('mousedown touchstart', function(event) {
prevVolume = $(this).val();
});
$volumeCtrl.on('mouseup touchend', function(event) {
execute(
{
type: 'request',
action: 'video.omxplayer.set_volume',
args: { volume: $(this).val() }
},
onSuccess=undefined,
onError = function() {
$volumeCtrl.val(prevVolume);
}
);
});
$videoResults.on('click touch', '.video-result', function(evt) {
var results = $videoResults.html();
var $item = $(this);
if (!$item.hasClass('selected')) {
$item.siblings().removeClass('selected');
$item.addClass('selected');
return false;
}
$videoResults.text('Loading video...');
execute(
{
type: 'request',
action: 'video.omxplayer.play',
args: { resource: $item.data('url') },
},
function() {
$videoResults.html(results);
$item.siblings().removeClass('active');
$item.addClass('active');
},
function() {
$videoResults.html(results);
2018-04-29 12:32:25 +02:00
}
);
});
2018-04-24 01:03:50 +02:00
};
var init = function() {
initBindings();
};
init();
});