jellyfin-web/dashboard-ui/components/subtitleeditor/subtitleeditor.js

404 lines
11 KiB
JavaScript
Raw Normal View History

2016-01-30 12:31:22 -07:00
define(['paperdialoghelper', 'paper-fab', 'paper-item-body', 'paper-icon-item'], function (paperDialogHelper) {
2014-05-16 21:24:10 -07:00
var currentItem;
function showLocalSubtitles(page, index) {
Dashboard.showLoadingMsg();
var popup = $('.popupSubtitleViewer', page).popup('open');
$('.subtitleContent', page).html('');
var url = 'Videos/' + currentItem.Id + '/Subtitles/' + index;
2015-12-14 08:43:03 -07:00
ApiClient.ajax({
type: 'GET',
url: url
}).then(function (result) {
2014-05-16 21:24:10 -07:00
$('.subtitleContent', page).html(result);
Dashboard.hideLoadingMsg();
popup.popup('reposition', {});
});
}
function showRemoteSubtitles(page, id) {
Dashboard.showLoadingMsg();
var popup = $('.popupSubtitleViewer', page).popup('open');
2015-09-28 20:35:50 -07:00
$('.subtitleContent', page).html('');
2014-05-16 21:24:10 -07:00
var url = 'Providers/Subtitles/Subtitles/' + id;
2015-12-14 08:43:03 -07:00
ApiClient.get(ApiClient.getUrl(url)).then(function (result) {
2014-05-16 21:24:10 -07:00
$('.subtitleContent', page).html(result);
Dashboard.hideLoadingMsg();
popup.popup('reposition', {});
});
}
function downloadRemoteSubtitles(page, id) {
var url = 'Items/' + currentItem.Id + '/RemoteSearch/Subtitles/' + id;
2014-07-01 22:16:59 -07:00
ApiClient.ajax({
2014-05-16 21:24:10 -07:00
type: "POST",
url: ApiClient.getUrl(url)
2015-12-14 08:43:03 -07:00
}).then(function () {
2014-05-16 21:24:10 -07:00
2014-06-20 08:10:40 -07:00
Dashboard.alert(Globalize.translate('MessageDownloadQueued'));
2014-05-16 21:24:10 -07:00
});
}
function deleteLocalSubtitle(page, index) {
2014-06-20 08:10:40 -07:00
var msg = Globalize.translate('MessageAreYouSureDeleteSubtitles');
2014-05-16 21:24:10 -07:00
2016-02-22 12:12:06 -07:00
require(['confirm'], function (confirm) {
2014-05-16 21:24:10 -07:00
2016-02-22 12:12:06 -07:00
confirm(msg, Globalize.translate('HeaderConfirmDeletion')).then(function () {
2014-05-16 21:24:10 -07:00
Dashboard.showLoadingMsg();
2015-09-16 18:33:46 -07:00
var itemId = currentItem.Id;
var url = 'Videos/' + itemId + '/Subtitles/' + index;
2014-05-16 21:24:10 -07:00
2014-07-01 22:16:59 -07:00
ApiClient.ajax({
2014-05-16 21:24:10 -07:00
type: "DELETE",
url: ApiClient.getUrl(url)
2015-12-14 08:43:03 -07:00
}).then(function () {
2014-05-16 21:24:10 -07:00
2015-09-16 18:33:46 -07:00
reload(page, itemId);
2014-05-16 21:24:10 -07:00
});
2016-02-22 12:12:06 -07:00
});
2014-05-16 21:24:10 -07:00
});
}
function fillSubtitleList(page, item) {
var streams = item.MediaStreams || [];
2014-06-10 10:36:06 -07:00
var subs = streams.filter(function (s) {
2014-05-16 21:24:10 -07:00
return s.Type == 'Subtitle';
2014-06-10 10:36:06 -07:00
});
2014-05-16 21:24:10 -07:00
2014-06-10 10:36:06 -07:00
var html = '';
2014-05-16 21:24:10 -07:00
2014-06-10 10:36:06 -07:00
if (subs.length) {
2014-05-16 21:24:10 -07:00
2015-09-16 19:33:45 -07:00
html += '<h1 style="margin-top:1.5em;">' + Globalize.translate('HeaderCurrentSubtitles') + '</h1>';
2015-09-16 18:33:46 -07:00
html += '<div class="paperList">';
2014-05-16 21:24:10 -07:00
2014-06-10 10:36:06 -07:00
html += subs.map(function (s) {
2014-05-16 21:24:10 -07:00
2015-09-16 18:33:46 -07:00
var itemHtml = '';
2014-05-16 21:24:10 -07:00
2015-09-16 18:33:46 -07:00
itemHtml += '<paper-icon-item>';
2014-05-16 21:24:10 -07:00
2015-10-26 11:55:46 -07:00
itemHtml += '<paper-fab mini class="blue" icon="closed-caption" item-icon></paper-fab>';
2014-05-16 21:24:10 -07:00
2015-09-16 18:33:46 -07:00
var atts = [];
2014-05-16 21:24:10 -07:00
2015-09-16 18:33:46 -07:00
atts.push(s.Codec);
if (s.IsDefault) {
2014-05-16 21:24:10 -07:00
2015-09-16 18:33:46 -07:00
atts.push('Default');
}
if (s.IsForced) {
2014-06-10 10:36:06 -07:00
2015-09-16 18:33:46 -07:00
atts.push('Forced');
2014-06-10 10:36:06 -07:00
}
2015-09-16 21:19:15 -07:00
if (atts.length == 3) {
itemHtml += '<paper-item-body three-line>';
}
else {
itemHtml += '<paper-item-body two-line>';
}
itemHtml += '<div>';
itemHtml += (s.Language || Globalize.translate('LabelUnknownLanaguage'));
itemHtml += '</div>';
itemHtml += '<div secondary>' + atts.join(' - ') + '</div>';
2015-09-16 18:33:46 -07:00
2014-06-10 10:36:06 -07:00
if (s.Path) {
2015-09-16 18:33:46 -07:00
itemHtml += '<div secondary>' + (s.Path) + '</div>';
2014-06-10 10:36:06 -07:00
}
2015-09-16 18:33:46 -07:00
html += '</a>';
itemHtml += '</paper-item-body>';
2014-06-10 10:36:06 -07:00
if (s.Path) {
2015-09-16 18:33:46 -07:00
itemHtml += '<paper-icon-button icon="delete" data-index="' + s.Index + '" title="' + Globalize.translate('Delete') + '" class="btnDelete"></paper-icon-button>';
2014-06-10 10:36:06 -07:00
}
2014-05-16 21:24:10 -07:00
2015-09-16 18:33:46 -07:00
itemHtml += '</paper-icon-item>';
2014-05-16 21:24:10 -07:00
2014-06-10 10:36:06 -07:00
return itemHtml;
2014-05-30 14:06:57 -07:00
2014-06-10 10:36:06 -07:00
}).join('');
2014-05-16 21:24:10 -07:00
2015-09-16 18:33:46 -07:00
html += '</div>';
2014-06-10 10:36:06 -07:00
}
2014-05-16 21:24:10 -07:00
var elem = $('.subtitleList', page).html(html).trigger('create');
$('.btnViewSubtitles', elem).on('click', function () {
var index = this.getAttribute('data-index');
showLocalSubtitles(page, index);
});
$('.btnDelete', elem).on('click', function () {
var index = this.getAttribute('data-index');
deleteLocalSubtitle(page, index);
});
}
function fillLanguages(page, languages) {
$('#selectLanguage', page).html(languages.map(function (l) {
return '<option value="' + l.ThreeLetterISOLanguageName + '">' + l.DisplayName + '</option>';
2015-09-03 10:01:51 -07:00
}));
2014-05-16 21:24:10 -07:00
2015-09-17 09:04:04 -07:00
var lastLanguage = appStorage.getItem('subtitleeditor-language');
if (lastLanguage) {
$('#selectLanguage', page).val(lastLanguage);
}
else {
2015-12-14 08:43:03 -07:00
Dashboard.getCurrentUser().then(function (user) {
2014-05-16 21:24:10 -07:00
2015-09-17 09:04:04 -07:00
var lang = user.Configuration.SubtitleLanguagePreference;
2014-05-16 21:24:10 -07:00
2015-09-17 09:04:04 -07:00
if (lang) {
$('#selectLanguage', page).val(lang);
}
});
}
2014-05-16 21:24:10 -07:00
}
function renderSearchResults(page, results) {
var lastProvider = '';
var html = '';
if (!results.length) {
$('.noSearchResults', page).show();
$('.subtitleResults', page).html('');
Dashboard.hideLoadingMsg();
return;
}
$('.noSearchResults', page).hide();
for (var i = 0, length = results.length; i < length; i++) {
var result = results[i];
var provider = result.ProviderName;
if (provider != lastProvider) {
2015-09-16 21:19:15 -07:00
if (i > 0) {
html += '</div>';
}
html += '<h1>' + provider + '</h1>';
html += '<div class="paperList">';
2014-05-16 21:24:10 -07:00
lastProvider = provider;
}
2015-09-16 21:19:15 -07:00
html += '<paper-icon-item>';
2015-10-26 11:55:46 -07:00
html += '<paper-fab mini class="blue" icon="closed-caption" item-icon></paper-fab>';
2015-09-16 21:19:15 -07:00
if (result.Comment) {
html += '<paper-item-body three-line>';
}
else {
html += '<paper-item-body two-line>';
}
//html += '<a class="btnViewSubtitle" href="#" data-subid="' + result.Id + '">';
2014-05-16 21:24:10 -07:00
2015-09-16 21:19:15 -07:00
html += '<div>' + (result.Name) + '</div>';
html += '<div secondary>' + (result.Format) + '</div>';
2014-05-16 21:24:10 -07:00
if (result.Comment) {
2015-09-16 21:19:15 -07:00
html += '<div secondary>' + (result.Comment) + '</div>';
2014-05-16 21:24:10 -07:00
}
2015-09-16 21:19:15 -07:00
//html += '</a>';
2014-05-16 21:24:10 -07:00
2015-09-16 21:19:15 -07:00
html += '</paper-item-body>';
2014-05-16 21:24:10 -07:00
2015-09-16 21:19:15 -07:00
html += '<div style="font-size:86%;opacity:.7;">' + /*(result.CommunityRating || 0) + ' / ' +*/ (result.DownloadCount || 0) + '</div>';
2014-05-16 21:24:10 -07:00
2015-09-16 21:19:15 -07:00
html += '<paper-icon-button icon="cloud-download" data-subid="' + result.Id + '" title="' + Globalize.translate('ButtonDownload') + '" class="btnDownload"></paper-icon-button>';
html += '</paper-icon-item>';
}
if (results.length) {
html += '</div>';
2014-05-16 21:24:10 -07:00
}
var elem = $('.subtitleResults', page).html(html).trigger('create');
$('.btnViewSubtitle', elem).on('click', function () {
var id = this.getAttribute('data-subid');
showRemoteSubtitles(page, id);
});
$('.btnDownload', elem).on('click', function () {
var id = this.getAttribute('data-subid');
downloadRemoteSubtitles(page, id);
});
Dashboard.hideLoadingMsg();
}
function searchForSubtitles(page, language) {
2015-09-17 09:04:04 -07:00
appStorage.setItem('subtitleeditor-language', language);
2014-05-16 21:24:10 -07:00
Dashboard.showLoadingMsg();
var url = ApiClient.getUrl('Items/' + currentItem.Id + '/RemoteSearch/Subtitles/' + language);
2015-12-14 08:43:03 -07:00
ApiClient.getJSON(url).then(function (results) {
2014-05-16 21:24:10 -07:00
renderSearchResults(page, results);
});
}
function reload(context, itemId) {
2014-05-16 21:24:10 -07:00
$('.noSearchResults', context).hide();
2014-05-16 21:24:10 -07:00
2015-09-16 18:33:46 -07:00
function onGetItem(item) {
2014-05-16 21:24:10 -07:00
currentItem = item;
fillSubtitleList(context, item);
var file = item.Path || '';
var index = Math.max(file.lastIndexOf('/'), file.lastIndexOf('\\'));
if (index > -1) {
file = file.substring(index + 1);
}
if (file) {
context.querySelector('.pathValue').innerHTML = file;
context.querySelector('.originalFile').classList.remove('hide');
} else {
context.querySelector('.pathValue').innerHTML = '';
context.querySelector('.originalFile').classList.add('hide');
}
2014-05-16 21:24:10 -07:00
Dashboard.hideLoadingMsg();
2015-09-16 18:33:46 -07:00
}
if (typeof itemId == 'string') {
2015-12-14 08:43:03 -07:00
ApiClient.getItem(Dashboard.getCurrentUserId(), itemId).then(onGetItem);
2015-09-16 18:33:46 -07:00
}
else {
onGetItem(itemId);
}
2014-05-16 21:24:10 -07:00
}
2015-06-07 21:47:19 -07:00
function onSearchSubmit() {
var form = this;
var lang = $('#selectLanguage', form).val();
2015-09-16 18:33:46 -07:00
searchForSubtitles($(form).parents('.editorContent'), lang);
2015-06-07 21:47:19 -07:00
return false;
}
2015-09-16 18:33:46 -07:00
function showEditor(itemId) {
Dashboard.showLoadingMsg();
2015-12-14 08:43:03 -07:00
var xhr = new XMLHttpRequest();
xhr.open('GET', 'components/subtitleeditor/subtitleeditor.template.html', true);
2014-05-16 21:24:10 -07:00
2015-12-14 08:43:03 -07:00
xhr.onload = function (e) {
2015-06-07 21:47:19 -07:00
2015-12-14 08:43:03 -07:00
var template = this.response;
ApiClient.getItem(Dashboard.getCurrentUserId(), itemId).then(function (item) {
2014-05-16 21:24:10 -07:00
2015-12-15 22:30:14 -07:00
var dlg = paperDialogHelper.createDialog({
size: 'small',
removeOnClose: true
});
2014-05-16 21:24:10 -07:00
2016-01-30 13:59:09 -07:00
dlg.classList.add('ui-body-b');
dlg.classList.add('background-theme-b');
2015-09-16 18:33:46 -07:00
var html = '';
2015-12-15 22:30:14 -07:00
html += '<div class="dialogHeader">';
2016-01-30 13:59:09 -07:00
html += '<paper-icon-button icon="arrow-back" class="btnCancel" tabindex="-1"></paper-icon-button>';
2015-12-15 22:30:14 -07:00
html += '<div class="dialogHeaderTitle">';
html += item.Name;
html += '</div>';
html += '</div>';
2014-05-16 21:24:10 -07:00
2015-09-16 18:33:46 -07:00
html += '<div class="editorContent">';
html += Globalize.translateDocument(template);
html += '</div>';
2014-05-16 21:24:10 -07:00
2015-09-16 18:33:46 -07:00
dlg.innerHTML = html;
document.body.appendChild(dlg);
2014-05-16 21:24:10 -07:00
dlg.querySelector('.pathLabel').innerHTML = Globalize.translate('MediaInfoFile');
2015-09-16 18:33:46 -07:00
$('.subtitleSearchForm', dlg).off('submit', onSearchSubmit).on('submit', onSearchSubmit);
2015-12-14 08:43:03 -07:00
paperDialogHelper.open(dlg);
2015-09-16 18:33:46 -07:00
2015-09-16 19:33:45 -07:00
var editorContent = dlg.querySelector('.editorContent');
reload(editorContent, item);
2015-09-16 18:33:46 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.getCultures().then(function (languages) {
2015-09-16 18:33:46 -07:00
2015-09-16 19:33:45 -07:00
fillLanguages(editorContent, languages);
});
2015-09-16 18:33:46 -07:00
2015-12-15 22:30:14 -07:00
$('.btnCancel', dlg).on('click', function () {
2015-09-29 09:29:06 -07:00
2015-12-14 08:43:03 -07:00
paperDialogHelper.close(dlg);
2015-09-29 09:29:06 -07:00
});
2015-09-16 18:33:46 -07:00
});
2015-12-14 08:43:03 -07:00
}
xhr.send();
2015-09-16 18:33:46 -07:00
}
2015-12-14 08:43:03 -07:00
return {
show: showEditor
2015-09-16 18:33:46 -07:00
};
2015-12-14 08:43:03 -07:00
});