1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/dashboard-ui/subtitleeditor/edititemsubtitles.js

388 lines
10 KiB
JavaScript
Raw Normal View History

2014-05-17 00:24:10 -04:00
(function ($, window, document) {
var currentItem;
2015-09-16 21:33:46 -04:00
var currentDialog;
2014-05-17 00:24:10 -04:00
function showLocalSubtitles(page, index) {
Dashboard.showLoadingMsg();
var popup = $('.popupSubtitleViewer', page).popup('open');
$('.subtitleContent', page).html('');
var url = 'Videos/' + currentItem.Id + '/Subtitles/' + index;
$.get(ApiClient.getUrl(url)).done(function (result) {
$('.subtitleContent', page).html(result);
Dashboard.hideLoadingMsg();
popup.popup('reposition', {});
});
}
function showRemoteSubtitles(page, id) {
Dashboard.showLoadingMsg();
var popup = $('.popupSubtitleViewer', page).popup('open');
$('.subtitleContent', page).html('\nLoading...\n\n\n');
var url = 'Providers/Subtitles/Subtitles/' + id;
2014-11-28 21:40:46 -05:00
ApiClient.get(ApiClient.getUrl(url)).done(function (result) {
2014-05-17 00:24:10 -04:00
$('.subtitleContent', page).html(result);
Dashboard.hideLoadingMsg();
popup.popup('reposition', {});
});
}
function downloadRemoteSubtitles(page, id) {
var url = 'Items/' + currentItem.Id + '/RemoteSearch/Subtitles/' + id;
2014-07-02 01:16:59 -04:00
ApiClient.ajax({
2014-05-17 00:24:10 -04:00
type: "POST",
url: ApiClient.getUrl(url)
}).done(function () {
2014-06-20 11:10:40 -04:00
Dashboard.alert(Globalize.translate('MessageDownloadQueued'));
2014-05-17 00:24:10 -04:00
});
}
function deleteLocalSubtitle(page, index) {
2014-06-20 11:10:40 -04:00
var msg = Globalize.translate('MessageAreYouSureDeleteSubtitles');
2014-05-17 00:24:10 -04:00
2014-06-20 11:10:40 -04:00
Dashboard.confirm(msg, Globalize.translate('HeaderConfirmDeletion'), function (result) {
2014-05-17 00:24:10 -04:00
if (result) {
Dashboard.showLoadingMsg();
2015-09-16 21:33:46 -04:00
var itemId = currentItem.Id;
var url = 'Videos/' + itemId + '/Subtitles/' + index;
2014-05-17 00:24:10 -04:00
2014-07-02 01:16:59 -04:00
ApiClient.ajax({
2014-05-17 00:24:10 -04:00
type: "DELETE",
url: ApiClient.getUrl(url)
}).done(function () {
2015-09-16 21:33:46 -04:00
reload(page, itemId);
2014-05-17 00:24:10 -04:00
});
}
});
}
function fillSubtitleList(page, item) {
var streams = item.MediaStreams || [];
2014-06-10 13:36:06 -04:00
var subs = streams.filter(function (s) {
2014-05-17 00:24:10 -04:00
return s.Type == 'Subtitle';
2014-06-10 13:36:06 -04:00
});
2014-05-17 00:24:10 -04:00
2014-06-10 13:36:06 -04:00
var html = '';
2014-05-17 00:24:10 -04:00
2014-06-10 13:36:06 -04:00
if (subs.length) {
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
html += '<h1>' + Globalize.translate('HeaderCurrentSubtitles') + '</h1>';
html += '<div class="paperList">';
2014-05-17 00:24:10 -04:00
2014-06-10 13:36:06 -04:00
html += subs.map(function (s) {
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
var itemHtml = '';
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
itemHtml += '<paper-icon-item>';
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
itemHtml += '<paper-fab class="listAvatar blue" icon="closed-caption" item-icon></paper-fab>';
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
itemHtml += '<paper-item-body three-line>';
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
itemHtml += '<div>';
itemHtml += (s.Language || Globalize.translate('LabelUnknownLanaguage'));
itemHtml += '</div>';
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
var atts = [];
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
atts.push(s.Codec);
if (s.IsDefault) {
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
atts.push('Default');
}
if (s.IsForced) {
2014-06-10 13:36:06 -04:00
2015-09-16 21:33:46 -04:00
atts.push('Forced');
2014-06-10 13:36:06 -04:00
}
2015-09-16 21:33:46 -04:00
itemHtml += '<div secondary>' + atts.join(', ') + '</div>';
2014-06-10 13:36:06 -04:00
if (s.Path) {
2015-09-16 21:33:46 -04:00
itemHtml += '<div secondary>' + (s.Path) + '</div>';
2014-06-10 13:36:06 -04:00
}
2015-09-16 21:33:46 -04:00
html += '</a>';
itemHtml += '</paper-item-body>';
2014-06-10 13:36:06 -04:00
if (s.Path) {
2015-09-16 21:33:46 -04:00
itemHtml += '<paper-icon-button icon="delete" data-index="' + s.Index + '" title="' + Globalize.translate('Delete') + '" class="btnDelete"></paper-icon-button>';
2014-06-10 13:36:06 -04:00
}
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
itemHtml += '</paper-icon-item>';
2014-05-17 00:24:10 -04:00
2014-06-10 13:36:06 -04:00
return itemHtml;
2014-05-30 17:06:57 -04:00
2014-06-10 13:36:06 -04:00
}).join('');
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
html += '</div>';
2014-06-10 13:36:06 -04:00
}
2014-05-17 00:24:10 -04: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 13:01:51 -04:00
}));
2014-05-17 00:24:10 -04:00
Dashboard.getCurrentUser().done(function (user) {
var lang = user.Configuration.SubtitleLanguagePreference;
if (lang) {
2015-09-03 13:01:51 -04:00
$('#selectLanguage', page).val(lang);
2014-05-17 00:24:10 -04:00
}
});
}
function renderSearchResults(page, results) {
var lastProvider = '';
var html = '';
if (!results.length) {
$('.noSearchResults', page).show();
$('.subtitleResults', page).html('');
Dashboard.hideLoadingMsg();
return;
}
$('.noSearchResults', page).hide();
html += '<ul data-role="listview">';
for (var i = 0, length = results.length; i < length; i++) {
var result = results[i];
var provider = result.ProviderName;
if (provider != lastProvider) {
2014-05-30 17:06:57 -04:00
html += '<li data-role="list-divider">' + provider + '<span class="ui-li-count ui-body-inherit">' + Globalize.translate('HeaderRatingsDownloads') + '</span></li>';
2014-05-17 00:24:10 -04:00
lastProvider = provider;
}
html += '<li><a class="btnViewSubtitle" href="#" data-subid="' + result.Id + '">';
2014-10-06 19:58:46 -04:00
html += '<h3>' + (result.Name) + '</h3>';
2014-05-17 00:24:10 -04:00
html += '<p>' + (result.Format) + '</p>';
if (result.Comment) {
html += '<p>' + (result.Comment) + '</p>';
}
html += '<div class="ui-li-count">' + (result.CommunityRating || 0) + ' / ' + (result.DownloadCount || 0) + '</div>';
html += '</a>';
2014-06-20 11:10:40 -04:00
html += '<a href="#" class="btnDownload" data-icon="plus" data-subid="' + result.Id + '">' + Globalize.translate('ButtonDownload') + '</a>';
2014-05-17 00:24:10 -04:00
html += '</li>';
}
html += '</ul>';
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) {
Dashboard.showLoadingMsg();
var url = ApiClient.getUrl('Items/' + currentItem.Id + '/RemoteSearch/Subtitles/' + language);
2014-07-02 01:16:59 -04:00
ApiClient.getJSON(url).done(function (results) {
2014-05-17 00:24:10 -04:00
renderSearchResults(page, results);
});
}
2015-09-16 21:33:46 -04:00
function reload(page, itemId) {
2014-05-17 00:24:10 -04:00
$('.noSearchResults', page).hide();
2015-09-16 21:33:46 -04:00
function onGetItem(item) {
2014-05-17 00:24:10 -04:00
currentItem = item;
fillSubtitleList(page, item);
Dashboard.hideLoadingMsg();
2015-09-16 21:33:46 -04:00
}
if (typeof itemId == 'string') {
ApiClient.getItem(Dashboard.getCurrentUserId(), itemId).done(onGetItem);
}
else {
onGetItem(itemId);
}
2014-05-17 00:24:10 -04:00
}
2015-06-08 00:47:19 -04:00
function onSearchSubmit() {
var form = this;
var lang = $('#selectLanguage', form).val();
2015-09-16 21:33:46 -04:00
searchForSubtitles($(form).parents('.editorContent'), lang);
2015-06-08 00:47:19 -04:00
return false;
}
2015-09-16 21:33:46 -04:00
function showEditor(itemId) {
Dashboard.showLoadingMsg();
ApiClient.ajax({
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
type: 'GET',
url: 'subtitleeditor/subtitleeditor.template.html'
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
}).done(function (template) {
2015-06-08 00:47:19 -04:00
2015-09-16 21:33:46 -04:00
ApiClient.getItem(Dashboard.getCurrentUserId(), itemId).done(function (item) {
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
var dlg = document.createElement('paper-dialog');
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
dlg.entryAnimation = 'scale-up-animation';
dlg.exitAnimation = 'fade-out-animation';
dlg.classList.add('fullscreen-editor-paper-dialog');
dlg.classList.add('ui-body-b');
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
var html = '';
html += '<h2 class="dialogHeader">' + item.Name + '</h2>';
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
html += '<div class="editorContent">';
html += Globalize.translateDocument(template);
html += '</div>';
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
dlg.innerHTML = html;
document.body.appendChild(dlg);
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04: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);
document.body.classList.add('bodyWithPopupOpen');
dlg.open();
window.location.hash = 'subtitleeditor?id=' + itemId;
// We need to use a timeout or onHashChange will fire immediately while opening
setTimeout(function () {
window.addEventListener('hashchange', onHashChange);
currentDialog = dlg;
var editorContent = dlg.querySelector('.editorContent');
reload(editorContent, item);
fillLanguages(editorContent);
}, 0);
});
2014-05-17 00:24:10 -04:00
});
2015-09-16 21:33:46 -04:00
}
function onHashChange() {
if (currentDialog) {
closeDialog(false);
}
}
function closeDialog(updateHash) {
window.removeEventListener('hashchange', onHashChange);
if (updateHash) {
window.location.hash = '';
}
if (currentDialog) {
currentDialog.close();
}
}
function onDialogClosed() {
currentDialog = null;
window.removeEventListener('hashchange', onHashChange);
document.body.classList.remove('bodyWithPopupOpen');
$(this).remove();
}
function fillLanguages(editorContent) {
ApiClient.getCultures().done(function (languages) {
fillLanguages(editorContent, languages);
});
}
2014-05-17 00:24:10 -04:00
2015-09-16 21:33:46 -04:00
window.SubtitleEditor = {
show: showEditor
};
2014-05-17 00:24:10 -04:00
})(jQuery, window, document);