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

200 lines
5.7 KiB
JavaScript
Raw Normal View History

define(['paperdialoghelper', 'jQuery', 'paper-fab', 'paper-item-body', 'paper-icon-item'], function (paperDialogHelper, $) {
2015-10-15 20:17:42 -07:00
var currentDeferred;
var hasChanges;
var currentOptions;
function addMediaLocation(page, path) {
var virtualFolder = currentOptions.library;
var refreshAfterChange = currentOptions.refresh;
2015-12-14 08:43:03 -07:00
ApiClient.addMediaPath(virtualFolder.Name, path, refreshAfterChange).then(function () {
2015-10-15 20:17:42 -07:00
2015-10-16 11:55:35 -07:00
hasChanges = true;
2015-10-15 20:17:42 -07:00
refreshLibraryFromServer(page);
2015-12-14 08:43:03 -07:00
}, function () {
2015-10-15 20:17:42 -07:00
2016-02-24 23:38:12 -07:00
require(['toast'], function (toast) {
toast(Globalize.translate('ErrorAddingMediaPathToVirtualFolder'));
});
2015-10-16 11:55:35 -07:00
});
2015-10-15 20:17:42 -07:00
}
function onRemoveClick() {
var button = this;
var index = parseInt(button.getAttribute('data-index'));
var virtualFolder = currentOptions.library;
var location = virtualFolder.Locations[index];
2016-02-22 12:12:06 -07:00
require(['confirm'], function (confirm) {
2015-10-15 20:17:42 -07:00
2016-02-22 12:12:06 -07:00
confirm(Globalize.translate('MessageConfirmRemoveMediaLocation'), Globalize.translate('HeaderRemoveMediaLocation')).then(function () {
2015-10-15 20:17:42 -07:00
var refreshAfterChange = currentOptions.refresh;
2015-12-14 08:43:03 -07:00
ApiClient.removeMediaPath(virtualFolder.Name, location, refreshAfterChange).then(function () {
2015-10-15 20:17:42 -07:00
2015-10-16 11:55:35 -07:00
hasChanges = true;
2015-10-15 20:17:42 -07:00
refreshLibraryFromServer($(button).parents('.editorContent')[0]);
2015-12-14 08:43:03 -07:00
}, function () {
2015-10-15 20:17:42 -07:00
2016-02-24 23:38:12 -07:00
require(['toast'], function (toast) {
toast(Globalize.translate('DefaultErrorMessage'));
});
2015-10-15 20:17:42 -07:00
});
2016-02-22 12:12:06 -07:00
});
2015-10-15 20:17:42 -07:00
});
}
function getFolderHtml(path, index) {
var html = '';
html += '<paper-icon-item role="menuitem" class="lnkPath">';
2015-10-26 11:55:46 -07:00
html += '<paper-fab mini style="background:#52B54B;" icon="folder" item-icon></paper-fab>';
2015-10-15 20:17:42 -07:00
html += '<paper-item-body>';
html += path;
html += '</paper-item-body>';
2015-10-16 11:34:05 -07:00
html += '<paper-icon-button icon="remove-circle" class="btnRemovePath" data-index="' + index + '"></paper-icon-button>';
2015-10-15 20:17:42 -07:00
html += '</paper-icon-item>';
return html;
}
function refreshLibraryFromServer(page) {
2015-12-14 08:43:03 -07:00
ApiClient.getVirtualFolders().then(function (result) {
2015-10-15 20:17:42 -07:00
var library = result.filter(function (f) {
return f.Name == currentOptions.library.Name;
})[0];
if (library) {
currentOptions.library = library;
renderLibrary(page, currentOptions);
}
});
}
function renderLibrary(page, options) {
var foldersHtml = options.library.Locations.map(getFolderHtml).join('');
page.querySelector('.folderList').innerHTML = foldersHtml;
$(page.querySelectorAll('.btnRemovePath')).on('click', onRemoveClick);
}
function onAddButtonClick() {
var page = $(this).parents('.editorContent')[0];
require(['directorybrowser'], function (directoryBrowser) {
var picker = new directoryBrowser();
picker.show({
callback: function (path) {
if (path) {
addMediaLocation(page, path);
}
picker.close();
}
});
});
}
function initEditor(page, options) {
renderLibrary(page, options);
$('.btnAddFolder', page).on('click', onAddButtonClick);
}
function onDialogClosed() {
$(this).remove();
Dashboard.hideLoadingMsg();
currentDeferred.resolveWith(null, [hasChanges]);
}
function editor() {
var self = this;
self.show = function (options) {
2016-02-17 19:55:15 -07:00
var deferred = jQuery.Deferred();
2015-10-15 20:17:42 -07:00
currentOptions = options;
currentDeferred = deferred;
hasChanges = false;
2015-12-14 08:43:03 -07:00
var xhr = new XMLHttpRequest();
xhr.open('GET', 'components/medialibraryeditor/medialibraryeditor.template.html', true);
2015-10-15 20:17:42 -07:00
2015-12-14 08:43:03 -07:00
xhr.onload = function (e) {
2015-10-15 20:17:42 -07:00
2015-12-14 08:43:03 -07:00
var template = this.response;
var dlg = paperDialogHelper.createDialog({
size: 'small',
2015-10-15 20:17:42 -07:00
2015-12-14 08:43:03 -07:00
// In (at least) chrome this is causing the text field to not be editable
modal: false
});
2015-10-15 20:17:42 -07:00
2016-01-30 13:59:09 -07:00
dlg.classList.add('ui-body-a');
dlg.classList.add('background-theme-a');
dlg.classList.add('popupEditor');
2015-12-14 08:43:03 -07:00
var html = '';
html += '<h2 class="dialogHeader">';
2016-01-30 13:59:09 -07:00
html += '<paper-fab icon="arrow-back" mini class="btnCloseDialog" tabindex="-1"></paper-fab>';
2015-10-15 20:17:42 -07:00
2015-12-14 08:43:03 -07:00
html += '<div style="display:inline-block;margin-left:.6em;vertical-align:middle;">' + options.library.Name + '</div>';
html += '</h2>';
2015-10-15 20:17:42 -07:00
2015-12-14 08:43:03 -07:00
html += '<div class="editorContent" style="max-width:800px;margin:auto;">';
2016-03-05 12:07:58 -07:00
html += Globalize.translateDocument(template);
2015-12-14 08:43:03 -07:00
html += '</div>';
2015-10-15 20:17:42 -07:00
2015-12-14 08:43:03 -07:00
dlg.innerHTML = html;
document.body.appendChild(dlg);
2015-10-15 20:17:42 -07:00
2015-12-14 08:43:03 -07:00
var editorContent = dlg.querySelector('.editorContent');
initEditor(editorContent, options);
2015-10-15 20:17:42 -07:00
2016-03-22 10:46:57 -07:00
$(dlg).on('close', onDialogClosed);
2015-10-15 20:17:42 -07:00
2015-12-14 08:43:03 -07:00
paperDialogHelper.open(dlg);
2015-10-15 20:17:42 -07:00
2015-12-14 08:43:03 -07:00
$('.btnCloseDialog', dlg).on('click', function () {
2015-10-15 20:17:42 -07:00
2015-12-14 08:43:03 -07:00
paperDialogHelper.close(dlg);
2015-10-15 20:17:42 -07:00
});
2015-12-14 08:43:03 -07:00
refreshLibraryFromServer(editorContent);
}
xhr.send();
2015-10-15 20:17:42 -07:00
return deferred.promise();
};
}
return editor;
});