jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/subtitleeditor/subtitleeditor.js

507 lines
15 KiB
JavaScript
Raw Normal View History

2016-07-26 22:19:56 -07:00
define(['dialogHelper', 'require', 'layoutManager', 'globalize', 'appStorage', 'connectionManager', 'loading', 'focusManager', 'dom', 'apphost', 'emby-select', 'listViewStyle', 'paper-icon-button-light', 'css!./../formdialog', 'material-icons', 'css!./subtitleeditor', 'emby-button'], function (dialogHelper, require, layoutManager, globalize, appStorage, connectionManager, loading, focusManager, dom, appHost) {
2016-05-30 13:46:18 -07:00
var currentItem;
2016-05-31 08:29:00 -07:00
var hasChanges;
2016-05-30 13:46:18 -07:00
function showLocalSubtitles(context, index) {
loading.show();
var subtitleContent = context.querySelector('.subtitleContent');
subtitleContent.innerHTML = '';
var apiClient = connectionManager.getApiClient(currentItem.ServerId);
var url = 'Videos/' + currentItem.Id + '/Subtitles/' + index;
apiClient.ajax({
type: 'GET',
url: url
}).then(function (result) {
subtitleContent.innerHTML = result;
loading.hide();
});
}
function showRemoteSubtitles(context, id) {
loading.show();
var url = 'Providers/Subtitles/Subtitles/' + id;
ApiClient.get(ApiClient.getUrl(url)).then(function (result) {
// show result
loading.hide();
});
}
function downloadRemoteSubtitles(context, id) {
var url = 'Items/' + currentItem.Id + '/RemoteSearch/Subtitles/' + id;
var apiClient = connectionManager.getApiClient(currentItem.ServerId);
apiClient.ajax({
type: "POST",
url: apiClient.getUrl(url)
}).then(function () {
2016-05-31 08:29:00 -07:00
hasChanges = true;
2016-05-30 13:46:18 -07:00
require(['toast'], function (toast) {
2016-05-31 08:29:00 -07:00
toast(globalize.translate('sharedcomponents#MessageDownloadQueued'));
2016-05-30 13:46:18 -07:00
});
2016-05-31 08:29:00 -07:00
focusManager.autoFocus(context);
2016-05-30 13:46:18 -07:00
});
}
function deleteLocalSubtitle(context, index) {
2016-05-31 08:29:00 -07:00
var msg = globalize.translate('sharedcomponents#MessageAreYouSureDeleteSubtitles');
2016-05-30 13:46:18 -07:00
require(['confirm'], function (confirm) {
2016-09-17 22:52:10 -07:00
confirm({
title: globalize.translate('sharedcomponents#ConfirmDeletion'),
text: msg,
confirmText: globalize.translate('sharedcomponents#Delete'),
primary: 'cancel'
}).then(function () {
2016-05-30 13:46:18 -07:00
loading.show();
var itemId = currentItem.Id;
var url = 'Videos/' + itemId + '/Subtitles/' + index;
var apiClient = connectionManager.getApiClient(currentItem.ServerId);
apiClient.ajax({
type: "DELETE",
url: apiClient.getUrl(url)
}).then(function () {
2016-05-31 08:29:00 -07:00
hasChanges = true;
2016-05-30 13:46:18 -07:00
reload(context, apiClient, itemId);
});
});
});
}
function fillSubtitleList(context, item) {
var streams = item.MediaStreams || [];
var subs = streams.filter(function (s) {
return s.Type == 'Subtitle';
});
var html = '';
if (subs.length) {
2016-05-31 08:29:00 -07:00
html += '<h1>' + globalize.translate('sharedcomponents#MySubtitles') + '</h1>';
2016-05-30 13:46:18 -07:00
if (layoutManager.tv) {
2016-08-07 12:43:52 -07:00
html += '<div class="paperList paperList-clear">';
2016-05-30 13:46:18 -07:00
} else {
html += '<div class="paperList">';
}
html += subs.map(function (s) {
var itemHtml = '';
2016-05-31 08:29:00 -07:00
var tagName = layoutManager.tv ? 'button' : 'div';
2016-08-16 11:54:08 -07:00
var className = layoutManager.tv && s.Path ? 'listItem btnDelete' : 'listItem';
if (layoutManager.tv) {
className += ' listItem-focusscale listItem-button';
}
2016-08-08 11:13:52 -07:00
className += ' listItem-noborder';
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
itemHtml += '<' + tagName + ' class="' + className + '" data-index="' + s.Index + '">';
2016-05-30 13:46:18 -07:00
2016-06-09 23:54:03 -07:00
itemHtml += '<i class="listItemIcon md-icon">closed_caption</i>';
2016-05-30 13:46:18 -07:00
2016-06-26 21:19:10 -07:00
itemHtml += '<div class="listItemBody two-line">';
2016-05-30 13:46:18 -07:00
2016-07-21 10:23:25 -07:00
itemHtml += '<div>';
2016-05-31 08:29:00 -07:00
itemHtml += s.DisplayTitle || '';
2016-07-21 10:23:25 -07:00
itemHtml += '</div>';
2016-05-30 13:46:18 -07:00
if (s.Path) {
itemHtml += '<div class="secondary listItemBodyText">' + (s.Path) + '</div>';
}
itemHtml += '</a>';
itemHtml += '</div>';
2016-05-31 08:29:00 -07:00
if (!layoutManager.tv) {
if (s.Path) {
2016-08-07 14:08:07 -07:00
itemHtml += '<button is="paper-icon-button-light" data-index="' + s.Index + '" title="' + globalize.translate('sharedcomponents#Delete') + '" class="btnDelete listItemButton"><i class="md-icon">delete</i></button>';
2016-05-31 08:29:00 -07:00
}
2016-05-30 13:46:18 -07:00
}
2016-05-31 08:29:00 -07:00
itemHtml += '</' + tagName + '>';
2016-05-30 13:46:18 -07:00
return itemHtml;
}).join('');
html += '</div>';
}
var elem = context.querySelector('.subtitleList');
if (subs.length) {
elem.classList.remove('hide');
} else {
elem.classList.add('hide');
}
elem.innerHTML = html;
//('.btnViewSubtitles', elem).on('click', function () {
// var index = this.getAttribute('data-index');
// showLocalSubtitles(context, index);
//});
}
function fillLanguages(context, apiClient, languages) {
var selectLanguage = context.querySelector('#selectLanguage');
selectLanguage.innerHTML = languages.map(function (l) {
return '<option value="' + l.ThreeLetterISOLanguageName + '">' + l.DisplayName + '</option>';
});
var lastLanguage = appStorage.getItem('subtitleeditor-language');
if (lastLanguage) {
selectLanguage.value = lastLanguage;
}
else {
apiClient.getCurrentUser().then(function (user) {
var lang = user.Configuration.SubtitleLanguagePreference;
if (lang) {
selectLanguage.value = lang;
}
});
}
}
function renderSearchResults(context, results) {
var lastProvider = '';
var html = '';
if (!results.length) {
context.querySelector('.noSearchResults').classList.remove('hide');
context.querySelector('.subtitleResults').innerHTML = '';
loading.hide();
return;
}
context.querySelector('.noSearchResults').classList.add('hide');
2016-07-21 13:51:43 -07:00
var moreIcon = appHost.moreIcon == 'dots-horiz' ? '&#xE5D3;' : '&#xE5D4;';
2016-05-30 13:46:18 -07:00
for (var i = 0, length = results.length; i < length; i++) {
var result = results[i];
var provider = result.ProviderName;
if (provider != lastProvider) {
if (i > 0) {
html += '</div>';
}
html += '<h1>' + provider + '</h1>';
if (layoutManager.tv) {
2016-08-07 12:43:52 -07:00
html += '<div class="paperList paperList-clear">';
2016-05-30 13:46:18 -07:00
} else {
html += '<div class="paperList">';
}
lastProvider = provider;
}
2016-05-31 08:29:00 -07:00
var tagName = layoutManager.tv ? 'button' : 'div';
var className = layoutManager.tv ? 'listItem btnOptions' : 'listItem';
2016-08-16 11:54:08 -07:00
if (layoutManager.tv) {
className += ' listItem-focusscale listItem-button';
}
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
html += '<' + tagName + ' class="' + className + '" data-subid="' + result.Id + '">';
2016-06-09 23:54:03 -07:00
html += '<i class="listItemIcon md-icon">closed_caption</i>';
2016-05-30 13:46:18 -07:00
2016-06-26 21:19:10 -07:00
html += '<div class="listItemBody two-line">';
2016-05-30 13:46:18 -07:00
//html += '<a class="btnViewSubtitle" href="#" data-subid="' + result.Id + '">';
2016-07-21 10:23:25 -07:00
html += '<div>' + (result.Name) + '</div>';
2016-05-30 13:46:18 -07:00
html += '<div class="secondary listItemBodyText">' + (result.Format) + '</div>';
if (result.Comment) {
html += '<div class="secondary listItemBodyText">' + (result.Comment) + '</div>';
}
//html += '</a>';
html += '</div>';
2016-08-07 14:08:07 -07:00
html += '<div class="secondary listItemAside">' + /*(result.CommunityRating || 0) + ' / ' +*/ (result.DownloadCount || 0) + '</div>';
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
if (!layoutManager.tv) {
2016-08-07 14:08:07 -07:00
html += '<button type="button" is="paper-icon-button-light" data-subid="' + result.Id + '" class="btnOptions listItemButton"><i class="md-icon">' + moreIcon + '</i></button>';
2016-05-31 08:29:00 -07:00
}
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
html += '</' + tagName + '>';
2016-05-30 13:46:18 -07:00
}
if (results.length) {
html += '</div>';
}
var elem = context.querySelector('.subtitleResults');
elem.innerHTML = html;
//('.btnViewSubtitle', elem).on('click', function () {
// var id = this.getAttribute('data-subid');
// showRemoteSubtitles(context, id);
//});
loading.hide();
}
function searchForSubtitles(context, language) {
appStorage.setItem('subtitleeditor-language', language);
loading.show();
var apiClient = connectionManager.getApiClient(currentItem.ServerId);
var url = apiClient.getUrl('Items/' + currentItem.Id + '/RemoteSearch/Subtitles/' + language);
apiClient.getJSON(url).then(function (results) {
renderSearchResults(context, results);
});
}
function reload(context, apiClient, itemId) {
context.querySelector('.noSearchResults').classList.add('hide');
function onGetItem(item) {
2016-05-31 08:29:00 -07:00
2016-05-30 13:46:18 -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');
}
loading.hide();
}
if (typeof itemId == 'string') {
apiClient.getItem(apiClient.getCurrentUserId(), itemId).then(onGetItem);
}
else {
onGetItem(itemId);
}
}
function onSearchSubmit(e) {
var form = this;
var lang = form.querySelector('#selectLanguage', form).value;
2016-08-06 23:15:03 -07:00
searchForSubtitles(dom.parentWithClass(form, 'formDialogContent'), lang);
2016-05-30 13:46:18 -07:00
e.preventDefault();
return false;
}
function onSubtitleListClick(e) {
2016-07-18 11:58:17 -07:00
var btnDelete = dom.parentWithClass(e.target, 'btnDelete');
2016-05-30 13:46:18 -07:00
if (btnDelete) {
var index = btnDelete.getAttribute('data-index');
2016-07-18 11:58:17 -07:00
var context = dom.parentWithClass(btnDelete, 'subtitleEditorDialog');
2016-05-30 13:46:18 -07:00
deleteLocalSubtitle(context, index);
}
}
function onSubtitleResultsClick(e) {
2016-07-18 11:58:17 -07:00
var btnOptions = dom.parentWithClass(e.target, 'btnOptions');
2016-05-31 08:29:00 -07:00
if (btnOptions) {
var subtitleId = btnOptions.getAttribute('data-subid');
2016-07-18 11:58:17 -07:00
var context = dom.parentWithClass(btnOptions, 'subtitleEditorDialog');
2016-05-31 08:29:00 -07:00
showDownloadOptions(btnOptions, context, subtitleId);
2016-05-30 13:46:18 -07:00
}
}
2016-05-31 08:29:00 -07:00
function showDownloadOptions(button, context, subtitleId) {
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
var items = [];
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
items.push({
name: Globalize.translate('sharedcomponents#Download'),
id: 'download'
});
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
require(['actionsheet'], function (actionsheet) {
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
actionsheet.show({
items: items,
positionTo: button
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
}).then(function (id) {
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
switch (id) {
case 'download':
downloadRemoteSubtitles(context, subtitleId);
break;
default:
break;
2016-05-30 13:46:18 -07:00
}
2016-05-31 08:29:00 -07:00
});
});
}
2016-05-30 13:46:18 -07:00
2016-07-26 22:19:56 -07:00
function centerFocus(elem, horiz, on) {
require(['scrollHelper'], function (scrollHelper) {
var fn = on ? 'on' : 'off';
scrollHelper.centerFocus[fn](elem, horiz);
});
}
2016-05-31 08:29:00 -07:00
function showEditorInternal(itemId, serverId, template) {
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
hasChanges = false;
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
var apiClient = connectionManager.getApiClient(serverId);
return apiClient.getItem(apiClient.getCurrentUserId(), itemId).then(function (item) {
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
var dialogOptions = {
2016-07-26 22:19:56 -07:00
removeOnClose: true,
scrollY: false
2016-05-31 08:29:00 -07:00
};
if (layoutManager.tv) {
dialogOptions.size = 'fullscreen';
} else {
dialogOptions.size = 'small';
}
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
var dlg = dialogHelper.createDialog(dialogOptions);
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
dlg.classList.add('formDialog');
dlg.classList.add('subtitleEditorDialog');
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
dlg.innerHTML = globalize.translateDocument(template, 'sharedcomponents');
2016-05-30 13:46:18 -07:00
2016-08-01 22:55:52 -07:00
dlg.querySelector('.originalSubtitleFileLabel').innerHTML = globalize.translate('sharedcomponents#File');
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
dlg.querySelector('.subtitleSearchForm').addEventListener('submit', onSearchSubmit);
2016-05-30 13:46:18 -07:00
2016-05-31 08:29:00 -07:00
var btnSubmit = dlg.querySelector('.btnSubmit');
if (layoutManager.tv) {
2016-08-06 23:15:03 -07:00
centerFocus(dlg.querySelector('.formDialogContent'), false, true);
2016-05-31 08:29:00 -07:00
dlg.querySelector('.btnSearchSubtitles').classList.add('hide');
} else {
btnSubmit.classList.add('hide');
}
2016-08-06 23:15:03 -07:00
var editorContent = dlg.querySelector('.formDialogContent');
2016-05-31 08:29:00 -07:00
dlg.querySelector('.subtitleList').addEventListener('click', onSubtitleListClick);
dlg.querySelector('.subtitleResults').addEventListener('click', onSubtitleResultsClick);
apiClient.getCultures().then(function (languages) {
fillLanguages(editorContent, apiClient, languages);
});
dlg.querySelector('.btnCancel').addEventListener('click', function () {
dialogHelper.close(dlg);
});
return new Promise(function (resolve, reject) {
dlg.addEventListener('close', function () {
2016-07-26 22:19:56 -07:00
if (layoutManager.tv) {
2016-08-06 23:15:03 -07:00
centerFocus(dlg.querySelector('.formDialogContent'), false, false);
2016-07-26 22:19:56 -07:00
}
2016-05-31 08:29:00 -07:00
if (hasChanges) {
resolve();
} else {
reject();
}
2016-05-30 13:46:18 -07:00
});
2016-05-31 08:29:00 -07:00
dialogHelper.open(dlg);
reload(editorContent, apiClient, item);
2016-05-30 13:46:18 -07:00
});
});
}
2016-05-31 08:29:00 -07:00
function showEditor(itemId, serverId) {
loading.show();
return new Promise(function (resolve, reject) {
require(['text!./subtitleeditor.template.html'], function (template) {
showEditorInternal(itemId, serverId, template).then(resolve, reject);
});
});
}
2016-05-30 13:46:18 -07:00
return {
show: showEditor
};
});