1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/components/medialibraryeditor/medialibraryeditor.js

238 lines
9.3 KiB
JavaScript
Raw Normal View History

2020-01-26 19:02:37 +03:00
define(["jQuery", "loading", "dialogHelper", "dom", "components/libraryoptionseditor/libraryoptionseditor", "emby-button", "listViewStyle", "paper-icon-button-light", "formDialogStyle", "emby-toggle", "flexStyles"], function (jQuery, loading, dialogHelper, dom, libraryoptionseditor) {
2018-10-23 01:05:09 +03:00
"use strict";
function onEditLibrary() {
2020-01-26 19:02:37 +03:00
if (isCreating) {
return false;
}
isCreating = true;
loading.show();
var dlg = dom.parentWithClass(this, "dlg-libraryeditor");
var libraryOptions = libraryoptionseditor.getLibraryOptions(dlg.querySelector(".libraryOptions"));
libraryOptions = Object.assign(currentOptions.library.LibraryOptions || {}, libraryOptions);
2020-01-26 19:02:37 +03:00
ApiClient.updateVirtualFolderOptions(currentOptions.library.ItemId, libraryOptions).then(function () {
hasChanges = true;
isCreating = false;
loading.hide();
dialogHelper.close(dlg);
2020-01-26 19:02:37 +03:00
}, function () {
isCreating = false;
loading.hide();
});
return false;
}
2018-10-23 01:05:09 +03:00
function addMediaLocation(page, path, networkSharePath) {
2019-05-06 17:26:18 -07:00
var virtualFolder = currentOptions.library;
var refreshAfterChange = currentOptions.refresh;
2020-01-26 19:02:37 +03:00
ApiClient.addMediaPath(virtualFolder.Name, path, networkSharePath, refreshAfterChange).then(function () {
2019-05-06 17:26:18 -07:00
hasChanges = true;
refreshLibraryFromServer(page);
2020-01-26 19:02:37 +03:00
}, function () {
require(["toast"], function (toast) {
2019-05-06 17:26:18 -07:00
toast(Globalize.translate("ErrorAddingMediaPathToVirtualFolder"));
});
});
2018-10-23 01:05:09 +03:00
}
function updateMediaLocation(page, path, networkSharePath) {
var virtualFolder = currentOptions.library;
ApiClient.updateMediaPath(virtualFolder.Name, {
Path: path,
NetworkPath: networkSharePath
2020-01-26 19:02:37 +03:00
}).then(function () {
2019-05-06 17:26:18 -07:00
hasChanges = true;
refreshLibraryFromServer(page);
2020-01-26 19:02:37 +03:00
}, function () {
require(["toast"], function (toast) {
2019-05-06 17:26:18 -07:00
toast(Globalize.translate("ErrorAddingMediaPathToVirtualFolder"));
});
});
2018-10-23 01:05:09 +03:00
}
function onRemoveClick(btnRemovePath, location) {
2019-05-06 17:26:18 -07:00
var button = btnRemovePath;
var virtualFolder = currentOptions.library;
2020-01-26 19:02:37 +03:00
require(["confirm"], function (confirm) {
2018-10-23 01:05:09 +03:00
confirm({
title: Globalize.translate("HeaderRemoveMediaLocation"),
text: Globalize.translate("MessageConfirmRemoveMediaLocation"),
confirmText: Globalize.translate("ButtonDelete"),
primary: "delete"
2020-01-26 19:02:37 +03:00
}).then(function () {
2018-10-23 01:05:09 +03:00
var refreshAfterChange = currentOptions.refresh;
2020-01-26 19:02:37 +03:00
ApiClient.removeMediaPath(virtualFolder.Name, location, refreshAfterChange).then(function () {
2019-05-06 17:26:18 -07:00
hasChanges = true;
refreshLibraryFromServer(dom.parentWithClass(button, "dlg-libraryeditor"));
2020-01-26 19:02:37 +03:00
}, function () {
require(["toast"], function (toast) {
2019-05-06 17:26:18 -07:00
toast(Globalize.translate("DefaultErrorMessage"));
});
});
});
});
2018-10-23 01:05:09 +03:00
}
function onListItemClick(e) {
var listItem = dom.parentWithClass(e.target, "listItem");
2020-01-26 19:02:37 +03:00
2018-10-23 01:05:09 +03:00
if (listItem) {
2019-05-06 17:26:18 -07:00
var index = parseInt(listItem.getAttribute("data-index"));
var pathInfos = (currentOptions.library.LibraryOptions || {}).PathInfos || [];
var pathInfo = null == index ? {} : pathInfos[index] || {};
var originalPath = pathInfo.Path || (null == index ? null : currentOptions.library.Locations[index]);
var btnRemovePath = dom.parentWithClass(e.target, "btnRemovePath");
2020-01-26 19:02:37 +03:00
if (btnRemovePath) {
onRemoveClick(btnRemovePath, originalPath);
return;
}
2019-05-06 17:26:18 -07:00
showDirectoryBrowser(dom.parentWithClass(listItem, "dlg-libraryeditor"), originalPath, pathInfo.NetworkPath);
2018-10-23 01:05:09 +03:00
}
}
function getFolderHtml(pathInfo, index) {
var html = "";
2019-05-06 17:26:18 -07:00
html += '<div class="listItem listItem-border lnkPath" data-index="' + index + '" style="padding-left:.5em;">';
html += '<div class="' + (pathInfo.NetworkPath ? "listItemBody two-line" : "listItemBody") + '">';
html += '<h3 class="listItemBodyText">';
html += pathInfo.Path;
html += "</h3>";
2020-01-26 19:02:37 +03:00
2019-05-06 17:26:18 -07:00
if (pathInfo.NetworkPath) {
html += '<div class="listItemBodyText secondary">' + pathInfo.NetworkPath + "</div>";
}
2020-01-26 19:02:37 +03:00
2019-05-06 17:26:18 -07:00
html += "</div>";
2020-04-25 10:00:20 +03:00
html += '<button type="button" is="paper-icon-button-light" class="listItemButton btnRemovePath" data-index="' + index + '"><i class="material-icons remove_circle"></i></button>';
2019-05-06 17:26:18 -07:00
html += "</div>";
return html;
2018-10-23 01:05:09 +03:00
}
function refreshLibraryFromServer(page) {
2020-01-26 19:02:37 +03:00
ApiClient.getVirtualFolders().then(function (result) {
var library = result.filter(function (f) {
return f.Name === currentOptions.library.Name;
2018-10-23 01:05:09 +03:00
})[0];
2020-01-26 19:02:37 +03:00
2019-05-06 17:26:18 -07:00
if (library) {
currentOptions.library = library;
renderLibrary(page, currentOptions);
}
});
2018-10-23 01:05:09 +03:00
}
function renderLibrary(page, options) {
var pathInfos = (options.library.LibraryOptions || {}).PathInfos || [];
2020-01-26 19:02:37 +03:00
if (!pathInfos.length) {
pathInfos = options.library.Locations.map(function (p) {
return {
Path: p
};
});
}
2019-05-06 17:26:18 -07:00
if (options.library.CollectionType === 'boxsets') {
page.querySelector(".folders").classList.add("hide");
} else {
page.querySelector(".folders").classList.remove("hide");
}
2020-01-26 19:02:37 +03:00
2019-05-06 17:26:18 -07:00
page.querySelector(".folderList").innerHTML = pathInfos.map(getFolderHtml).join("");
2018-10-23 01:05:09 +03:00
}
function onAddButtonClick() {
2019-05-06 17:26:18 -07:00
showDirectoryBrowser(dom.parentWithClass(this, "dlg-libraryeditor"));
2018-10-23 01:05:09 +03:00
}
function showDirectoryBrowser(context, originalPath, networkPath) {
2020-01-26 19:02:37 +03:00
require(["directorybrowser"], function (directoryBrowser) {
var picker = new directoryBrowser();
2018-10-23 01:05:09 +03:00
picker.show({
2020-01-26 19:02:37 +03:00
enableNetworkSharePath: true,
2018-10-23 01:05:09 +03:00
pathReadOnly: null != originalPath,
path: originalPath,
networkSharePath: networkPath,
2020-01-26 19:02:37 +03:00
callback: function (path, networkSharePath) {
if (path) {
if (originalPath) {
updateMediaLocation(context, originalPath, networkSharePath);
} else {
addMediaLocation(context, path, networkSharePath);
}
}
2019-05-06 17:26:18 -07:00
picker.close();
2018-10-23 01:05:09 +03:00
}
2020-01-26 19:02:37 +03:00
});
});
2018-10-23 01:05:09 +03:00
}
function onToggleAdvancedChange() {
var dlg = dom.parentWithClass(this, "dlg-libraryeditor");
2020-01-26 19:02:37 +03:00
libraryoptionseditor.setAdvancedVisible(dlg.querySelector(".libraryOptions"), this.checked);
2018-10-23 01:05:09 +03:00
}
function initEditor(dlg, options) {
2019-05-06 17:26:18 -07:00
renderLibrary(dlg, options);
dlg.querySelector(".btnAddFolder").addEventListener("click", onAddButtonClick);
dlg.querySelector(".folderList").addEventListener("click", onListItemClick);
dlg.querySelector(".chkAdvanced").addEventListener("change", onToggleAdvancedChange);
dlg.querySelector(".btnSubmit").addEventListener("click", onEditLibrary);
2020-01-26 19:02:37 +03:00
libraryoptionseditor.embed(dlg.querySelector(".libraryOptions"), options.library.CollectionType, options.library.LibraryOptions).then(function () {
2019-05-06 17:26:18 -07:00
onToggleAdvancedChange.call(dlg.querySelector(".chkAdvanced"));
});
2018-10-23 01:05:09 +03:00
}
function onDialogClosed() {
2019-05-06 17:26:18 -07:00
currentDeferred.resolveWith(null, [hasChanges]);
2018-10-23 01:05:09 +03:00
}
function editor() {
2020-01-26 19:02:37 +03:00
this.show = function (options) {
2018-10-23 01:05:09 +03:00
var deferred = jQuery.Deferred();
2019-05-06 17:26:18 -07:00
currentOptions = options;
currentDeferred = deferred;
hasChanges = false;
2020-01-26 19:02:37 +03:00
var xhr = new XMLHttpRequest();
2019-05-06 17:26:18 -07:00
xhr.open("GET", "components/medialibraryeditor/medialibraryeditor.template.html", true);
2020-01-26 19:02:37 +03:00
xhr.onload = function (e) {
2019-05-06 17:26:18 -07:00
var template = this.response;
var dlg = dialogHelper.createDialog({
size: "medium-tall",
modal: false,
removeOnClose: true,
scrollY: false
});
dlg.classList.add("dlg-libraryeditor");
dlg.classList.add("ui-body-a");
dlg.classList.add("background-theme-a");
dlg.classList.add("formDialog");
dlg.innerHTML = Globalize.translateDocument(template);
dlg.querySelector(".formDialogHeaderTitle").innerHTML = options.library.Name;
initEditor(dlg, options);
dlg.addEventListener("close", onDialogClosed);
dialogHelper.open(dlg);
2020-01-26 19:02:37 +03:00
dlg.querySelector(".btnCancel").addEventListener("click", function () {
2019-05-06 17:26:18 -07:00
dialogHelper.close(dlg);
});
refreshLibraryFromServer(dlg);
};
2020-01-26 19:02:37 +03:00
2019-05-06 17:26:18 -07:00
xhr.send();
return deferred.promise();
2020-01-26 19:02:37 +03:00
};
2018-10-23 01:05:09 +03:00
}
2019-05-06 17:26:18 -07:00
var currentDeferred;
var currentOptions;
var hasChanges = false;
var isCreating = false;
2019-05-06 17:26:18 -07:00
return editor;
});