define(['components/paperdialoghelper', 'paper-fab'], function () {
var currentItem;
function showLocalSubtitles(page, index) {
Dashboard.showLoadingMsg();
var popup = $('.popupSubtitleViewer', page).popup('open');
$('.subtitleContent', page).html('');
var url = 'Videos/' + currentItem.Id + '/Subtitles/' + index;
ApiClient.ajax({
type: 'GET',
url: url
}).then(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('');
var url = 'Providers/Subtitles/Subtitles/' + id;
ApiClient.get(ApiClient.getUrl(url)).then(function (result) {
$('.subtitleContent', page).html(result);
Dashboard.hideLoadingMsg();
popup.popup('reposition', {});
});
}
function downloadRemoteSubtitles(page, id) {
var url = 'Items/' + currentItem.Id + '/RemoteSearch/Subtitles/' + id;
ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl(url)
}).then(function () {
Dashboard.alert(Globalize.translate('MessageDownloadQueued'));
});
}
function deleteLocalSubtitle(page, index) {
var msg = Globalize.translate('MessageAreYouSureDeleteSubtitles');
Dashboard.confirm(msg, Globalize.translate('HeaderConfirmDeletion'), function (result) {
if (result) {
Dashboard.showLoadingMsg();
var itemId = currentItem.Id;
var url = 'Videos/' + itemId + '/Subtitles/' + index;
ApiClient.ajax({
type: "DELETE",
url: ApiClient.getUrl(url)
}).then(function () {
reload(page, itemId);
});
}
});
}
function fillSubtitleList(page, item) {
var streams = item.MediaStreams || [];
var subs = streams.filter(function (s) {
return s.Type == 'Subtitle';
});
var html = '';
if (subs.length) {
html += '
' + Globalize.translate('HeaderCurrentSubtitles') + ' ';
html += '';
html += subs.map(function (s) {
var itemHtml = '';
itemHtml += '
';
itemHtml += ' ';
var atts = [];
atts.push(s.Codec);
if (s.IsDefault) {
atts.push('Default');
}
if (s.IsForced) {
atts.push('Forced');
}
if (atts.length == 3) {
itemHtml += '';
}
else {
itemHtml += '';
}
itemHtml += '';
itemHtml += (s.Language || Globalize.translate('LabelUnknownLanaguage'));
itemHtml += '
';
itemHtml += '' + atts.join(' - ') + '
';
if (s.Path) {
itemHtml += '' + (s.Path) + '
';
}
html += '';
itemHtml += ' ';
if (s.Path) {
itemHtml += ' ';
}
itemHtml += ' ';
return itemHtml;
}).join('');
html += '
';
}
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 '' + l.DisplayName + ' ';
}));
var lastLanguage = appStorage.getItem('subtitleeditor-language');
if (lastLanguage) {
$('#selectLanguage', page).val(lastLanguage);
}
else {
Dashboard.getCurrentUser().then(function (user) {
var lang = user.Configuration.SubtitleLanguagePreference;
if (lang) {
$('#selectLanguage', page).val(lang);
}
});
}
}
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) {
if (i > 0) {
html += '';
}
html += '' + provider + ' ';
html += '';
}
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) {
appStorage.setItem('subtitleeditor-language', language);
Dashboard.showLoadingMsg();
var url = ApiClient.getUrl('Items/' + currentItem.Id + '/RemoteSearch/Subtitles/' + language);
ApiClient.getJSON(url).then(function (results) {
renderSearchResults(page, results);
});
}
function reload(page, itemId) {
$('.noSearchResults', page).hide();
function onGetItem(item) {
currentItem = item;
fillSubtitleList(page, item);
Dashboard.hideLoadingMsg();
}
if (typeof itemId == 'string') {
ApiClient.getItem(Dashboard.getCurrentUserId(), itemId).then(onGetItem);
}
else {
onGetItem(itemId);
}
}
function onSearchSubmit() {
var form = this;
var lang = $('#selectLanguage', form).val();
searchForSubtitles($(form).parents('.editorContent'), lang);
return false;
}
function showEditor(itemId) {
Dashboard.showLoadingMsg();
var xhr = new XMLHttpRequest();
xhr.open('GET', 'components/subtitleeditor/subtitleeditor.template.html', true);
xhr.onload = function (e) {
var template = this.response;
ApiClient.getItem(Dashboard.getCurrentUserId(), itemId).then(function (item) {
var dlg = PaperDialogHelper.createDialog();
var html = '';
html += '';
html += '';
html += Globalize.translateDocument(template);
html += '
';
dlg.innerHTML = html;
document.body.appendChild(dlg);
$('.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);
PaperDialogHelper.openWithHash(dlg, 'subtitleeditor');
var editorContent = dlg.querySelector('.editorContent');
reload(editorContent, item);
ApiClient.getCultures().then(function (languages) {
fillLanguages(editorContent, languages);
});
$('.btnCloseDialog', dlg).on('click', function () {
PaperDialogHelper.close(dlg);
});
});
}
xhr.send();
}
function onDialogClosed() {
$(this).remove();
Dashboard.hideLoadingMsg();
}
return {
show: showEditor
};
});