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

214 lines
6.2 KiB
JavaScript
Raw Normal View History

2016-08-13 13:54:29 -07:00
define(['dialogHelper', 'dom', 'components/libraryoptionseditor/libraryoptionseditor', 'emby-button', 'listViewStyle', 'paper-icon-button-light', 'formDialogStyle'], function (dialogHelper, dom, libraryoptionseditor) {
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;
2016-08-13 13:54:29 -07:00
refreshLibraryFromServer(dom.parentWithClass(button, 'dlg-libraryeditor'));
2015-10-15 20:17:42 -07:00
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 = '';
2016-08-06 07:07:44 -07:00
html += '<div class="listItem lnkPath">';
2015-10-15 20:17:42 -07:00
2016-08-06 07:07:44 -07:00
html += '<i class="listItemIcon md-icon">folder</i>';
2015-10-15 20:17:42 -07:00
2016-08-06 07:07:44 -07:00
html += '<div class="listItemBody">';
html += '<h3 class="listItemBodyText">';
2015-10-15 20:17:42 -07:00
html += path;
2016-08-06 07:07:44 -07:00
html += '</h3>';
html += '</div>';
2015-10-15 20:17:42 -07:00
2016-08-13 15:27:14 -07:00
html += '<button is="paper-icon-button-light" class="listItemButton btnRemovePath" data-index="' + index + '"><i class="md-icon">remove_circle</i></button>';
2015-10-15 20:17:42 -07:00
2016-08-06 07:07:44 -07:00
html += '</div>';
2015-10-15 20:17:42 -07:00
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;
2016-08-13 13:54:29 -07:00
var btnRemovePath = page.querySelectorAll('.btnRemovePath');
for (var i = 0, length = btnRemovePath.length; i < length; i++) {
btnRemovePath[i].addEventListener('click', onRemoveClick);
}
2015-10-15 20:17:42 -07:00
}
function onAddButtonClick() {
2016-08-13 13:54:29 -07:00
var page = dom.parentWithClass(this, 'dlg-libraryeditor');
2015-10-15 20:17:42 -07:00
require(['directorybrowser'], function (directoryBrowser) {
var picker = new directoryBrowser();
picker.show({
callback: function (path) {
if (path) {
addMediaLocation(page, path);
}
picker.close();
}
});
});
}
2016-08-13 13:54:29 -07:00
function initEditor(dlg, options) {
renderLibrary(dlg, options);
dlg.querySelector('.btnAddFolder').addEventListener('click', onAddButtonClick);
libraryoptionseditor.embed(dlg.querySelector('.libraryOptions'), options.library.CollectionType, options.library.LibraryOptions);
}
function onDialogClosing() {
2015-10-15 20:17:42 -07:00
2016-08-13 20:12:26 -07:00
var dlg = this;
var libraryOptions = libraryoptionseditor.getLibraryOptions(dlg.querySelector('.libraryOptions'));
ApiClient.updateVirtualFolderOptions(currentOptions.library.ItemId, libraryOptions);
2015-10-15 20:17:42 -07:00
}
function onDialogClosed() {
Dashboard.hideLoadingMsg();
2016-08-29 00:12:24 -07:00
// hardcoding this to true for now until libraryOptions are taken into account
hasChanges = true;
2015-10-15 20:17:42 -07:00
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;
2016-03-23 12:03:17 -07:00
var dlg = dialogHelper.createDialog({
2016-09-07 10:17:26 -07:00
size: 'medium',
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
2016-08-13 13:54:29 -07:00
modal: false,
2016-09-07 10:17:26 -07:00
removeOnClose: true,
scrollY: false
2015-12-14 08:43:03 -07:00
});
2015-10-15 20:17:42 -07:00
2016-08-13 13:54:29 -07:00
dlg.classList.add('dlg-libraryeditor');
2016-01-30 13:59:09 -07:00
dlg.classList.add('ui-body-a');
dlg.classList.add('background-theme-a');
2016-09-07 10:17:26 -07:00
dlg.classList.add('formDialog');
2016-01-30 13:59:09 -07:00
2016-08-13 13:54:29 -07:00
dlg.innerHTML = Globalize.translateDocument(template);
2015-10-15 20:17:42 -07:00
2016-08-13 13:54:29 -07:00
dlg.querySelector('.formDialogHeaderTitle').innerHTML = options.library.Name;
2015-10-15 20:17:42 -07:00
2015-12-14 08:43:03 -07:00
document.body.appendChild(dlg);
2015-10-15 20:17:42 -07:00
2016-08-13 13:54:29 -07:00
initEditor(dlg, options);
2015-10-15 20:17:42 -07:00
2016-08-13 13:54:29 -07:00
dlg.addEventListener('closing', onDialogClosing);
dlg.addEventListener('close', onDialogClosed);
2015-10-15 20:17:42 -07:00
2016-03-23 12:03:17 -07:00
dialogHelper.open(dlg);
2015-10-15 20:17:42 -07:00
2016-08-13 13:54:29 -07:00
dlg.querySelector('.btnCancel').addEventListener('click', function () {
2015-10-15 20:17:42 -07:00
2016-03-23 12:03:17 -07:00
dialogHelper.close(dlg);
2015-10-15 20:17:42 -07:00
});
2016-08-13 13:54:29 -07:00
refreshLibraryFromServer(dlg);
2015-12-14 08:43:03 -07:00
}
xhr.send();
2015-10-15 20:17:42 -07:00
return deferred.promise();
};
}
return editor;
});