';
}
html += '';
itemHtml += '';
if (s.Path) {
itemHtml += '';
}
itemHtml += '';
return itemHtml;
}).join('');
html += '
';
}
else {
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 '';
}));
Dashboard.getCurrentUser().done(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 += '
';
lastProvider = provider;
}
html += '';
html += '';
if (result.Comment) {
html += '';
}
else {
html += '';
}
//html += '';
html += '
';
html += '';
html += '';
}
if (results.length) {
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) {
Dashboard.showLoadingMsg();
var url = ApiClient.getUrl('Items/' + currentItem.Id + '/RemoteSearch/Subtitles/' + language);
ApiClient.getJSON(url).done(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).done(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();
ApiClient.ajax({
type: 'GET',
url: 'subtitleeditor/subtitleeditor.template.html'
}).done(function (template) {
ApiClient.getItem(Dashboard.getCurrentUserId(), itemId).done(function (item) {
var dlg = document.createElement('paper-dialog');
dlg.setAttribute('with-backdrop', 'with-backdrop');
dlg.setAttribute('role', 'alertdialog');
dlg.entryAnimation = 'scale-up-animation';
dlg.exitAnimation = 'fade-out-animation';
dlg.classList.add('fullscreen-editor-paper-dialog');
dlg.classList.add('ui-body-b');
var html = '';
html += '
';
html += '';
html += '
' + item.Name + '
';
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);
document.body.classList.add('bodyWithPopupOpen');
dlg.open();
window.location.hash = getHash(itemId);
window.addEventListener('hashchange', onHashChange);
currentDialog = dlg;
var editorContent = dlg.querySelector('.editorContent');
reload(editorContent, item);
ApiClient.getCultures().done(function (languages) {
fillLanguages(editorContent, languages);
});
$('.btnCloseDialog', dlg).on('click', closeDialog);
});
});
}
function getHash(itemId) {
return 'subtitleeditor?id=' + itemId;
}
function onHashChange() {
// In some browsers this will fire immediately after opening the dialog, despite the fact that we bound the event after setting the hash
if (currentItem && window.location.hash == '#' + getHash(currentItem.Id)) {
return;
}
if (currentDialog) {
closeDialog();
}
}
function closeDialog() {
window.removeEventListener('hashchange', onHashChange);
if (currentDialog) {
currentDialog.close();
}
}
function onDialogClosed() {
currentDialog = null;
window.removeEventListener('hashchange', onHashChange);
document.body.classList.remove('bodyWithPopupOpen');
$(this).remove();
Dashboard.hideLoadingMsg();
if ((window.location.hash || '').length > 1) {
history.back();
}
}
window.SubtitleEditor = {
show: showEditor
};
})(jQuery, window, document);