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

397 lines
10 KiB
JavaScript
Raw Normal View History

2014-05-16 21:24:10 -07:00
(function ($, window, document) {
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-11-22 13:46:02 -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;
2014-11-28 19:40:46 -07:00
ApiClient.get(ApiClient.getUrl(url)).done(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)
}).done(function () {
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
2014-06-20 08:10:40 -07:00
Dashboard.confirm(msg, Globalize.translate('HeaderConfirmDeletion'), function (result) {
2014-05-16 21:24:10 -07:00
if (result) {
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)
}).done(function () {
2015-09-16 18:33:46 -07:00
reload(page, itemId);
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 {
Dashboard.getCurrentUser().done(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);
2014-07-01 22:16:59 -07:00
ApiClient.getJSON(url).done(function (results) {
2014-05-16 21:24:10 -07:00
renderSearchResults(page, results);
});
}
2015-09-16 18:33:46 -07:00
function reload(page, itemId) {
2014-05-16 21:24:10 -07:00
$('.noSearchResults', page).hide();
2015-09-16 18:33:46 -07:00
function onGetItem(item) {
2014-05-16 21:24:10 -07:00
currentItem = item;
fillSubtitleList(page, item);
Dashboard.hideLoadingMsg();
2015-09-16 18:33:46 -07:00
}
if (typeof itemId == 'string') {
ApiClient.getItem(Dashboard.getCurrentUserId(), itemId).done(onGetItem);
}
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-10-13 12:22:45 -07:00
HttpClient.send({
2014-05-16 21:24:10 -07:00
2015-09-16 18:33:46 -07:00
type: 'GET',
2015-09-17 09:04:04 -07:00
url: 'components/subtitleeditor/subtitleeditor.template.html'
2014-05-16 21:24:10 -07:00
2015-09-16 18:33:46 -07:00
}).done(function (template) {
2015-06-07 21:47:19 -07:00
2015-09-16 18:33:46 -07:00
ApiClient.getItem(Dashboard.getCurrentUserId(), itemId).done(function (item) {
2014-05-16 21:24:10 -07:00
2015-10-01 23:14:04 -07:00
var dlg = PaperDialogHelper.createDialog();
2014-05-16 21:24:10 -07:00
2015-09-16 18:33:46 -07:00
var html = '';
2015-09-16 19:33:45 -07:00
html += '<h2 class="dialogHeader">';
2015-10-20 22:10:02 -07:00
html += '<paper-fab icon="arrow-back" mini class="btnCloseDialog"></paper-fab>';
2015-09-16 19:33:45 -07:00
html += '<div style="display:inline-block;margin-left:.6em;vertical-align:middle;">' + item.Name + '</div>';
html += '</h2>';
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
2015-09-16 18:33:46 -07:00
$('.subtitleSearchForm', dlg).off('submit', onSearchSubmit).on('submit', onSearchSubmit);
// Has to be assigned a z-index after the call to .open()
$(dlg).on('iron-overlay-closed', onDialogClosed);
2015-09-17 09:04:04 -07:00
PaperDialogHelper.openWithHash(dlg, 'subtitleeditor');
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-09-16 19:33:45 -07:00
ApiClient.getCultures().done(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-09-29 09:29:06 -07:00
$('.btnCloseDialog', dlg).on('click', function () {
PaperDialogHelper.close(dlg);
});
2015-09-16 18:33:46 -07:00
});
2014-05-16 21:24:10 -07:00
});
2015-09-16 18:33:46 -07:00
}
function onDialogClosed() {
2015-09-16 19:33:45 -07:00
2015-09-16 18:33:46 -07:00
$(this).remove();
2015-09-16 21:19:15 -07:00
Dashboard.hideLoadingMsg();
2015-09-16 18:33:46 -07:00
}
2014-05-16 21:24:10 -07:00
2015-09-16 18:33:46 -07:00
window.SubtitleEditor = {
2015-09-17 09:04:04 -07:00
show: function (itemId) {
require(['components/paperdialoghelper'], function () {
showEditor(itemId);
});
}
2015-09-16 18:33:46 -07:00
};
2014-05-16 21:24:10 -07:00
})(jQuery, window, document);