diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 8ce9ec3c3c..1d556cfbc1 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -15,6 +15,7 @@ - [cvium](https://github.com/cvium) - [grafixeyehero](https://github.com/grafixeyehero) - [Drago96](https://github.com/drago-96) + - [ViXXoR](https://github.com/ViXXoR) # Emby Contributors diff --git a/src/components/directorybrowser/directorybrowser.js b/src/components/directorybrowser/directorybrowser.js index 9a3aa7c236..7eb91fffa0 100644 --- a/src/components/directorybrowser/directorybrowser.js +++ b/src/components/directorybrowser/directorybrowser.js @@ -1,10 +1,13 @@ -define(["loading", "dialogHelper", "dom", "listViewStyle", "emby-input", "emby-button", "paper-icon-button-light", "css!./directorybrowser", "formDialogStyle", "emby-linkbutton"], function(loading, dialogHelper, dom) { - "use strict"; +define(['loading', 'dialogHelper', 'dom', 'listViewStyle', 'emby-input', 'emby-button', 'paper-icon-button-light', 'css!./directorybrowser', 'formDialogStyle', 'emby-linkbutton'], function(loading, dialogHelper, dom) { + 'use strict'; function getSystemInfo() { - return systemInfo ? Promise.resolve(systemInfo) : ApiClient.getPublicSystemInfo().then(function(info) { - return systemInfo = info, info - }) + return systemInfo ? Promise.resolve(systemInfo) : ApiClient.getPublicSystemInfo().then( + function(info) { + systemInfo = info; + return info; + } + ) } function onDialogClosed() { @@ -12,40 +15,139 @@ define(["loading", "dialogHelper", "dom", "listViewStyle", "emby-input", "emby-b } function refreshDirectoryBrowser(page, path, fileOptions, updatePathOnError) { - if (path && "string" != typeof path) throw new Error("invalid path"); + if (path && typeof path !== 'string') { + throw new Error("invalid path"); + } + loading.show(); + var promises = []; - "Network" === path ? promises.push(ApiClient.getNetworkDevices()) : path ? (promises.push(ApiClient.getDirectoryContents(path, fileOptions)), promises.push(ApiClient.getParentPath(path))) : promises.push(ApiClient.getDrives()), Promise.all(promises).then(function(responses) { - var folders = responses[0], - parentPath = responses[1] || ""; - page.querySelector("#txtDirectoryPickerPath").value = path || ""; - var html = ""; - path && (html += getItem("lnkPath lnkDirectory", "", parentPath, "...")); - for (var i = 0, length = folders.length; i < length; i++) { - var folder = folders[i]; - html += getItem("File" === folder.Type ? "lnkPath lnkFile" : "lnkPath lnkDirectory", folder.Type, folder.Path, folder.Name) + + if ("Network" === path) { + promises.push(ApiClient.getNetworkDevices()) + } else { + if (path) { + promises.push(ApiClient.getDirectoryContents(path, fileOptions)); + promises.push(ApiClient.getParentPath(path)); + } else { + promises.push(ApiClient.getDrives()); } - path || (html += getItem("lnkPath lnkDirectory", "", "Network", Globalize.translate("ButtonNetwork"))), page.querySelector(".results").innerHTML = html, loading.hide() - }, function() { - updatePathOnError && (page.querySelector("#txtDirectoryPickerPath").value = ""), page.querySelector(".results").innerHTML = "", loading.hide() - }) + } + + Promise.all(promises).then( + function(responses) { + var folders = responses[0]; + var parentPath = responses[1] || ""; + var html = ""; + + page.querySelector(".results").scrollTop = 0; + page.querySelector("#txtDirectoryPickerPath").value = path || ""; + + if (path) { + html += getItem("lnkPath lnkDirectory", "", parentPath, "..."); + } + for (var i = 0, length = folders.length; i < length; i++) { + var folder = folders[i]; + var cssClass = "File" === folder.Type ? "lnkPath lnkFile" : "lnkPath lnkDirectory"; + html += getItem(cssClass, folder.Type, folder.Path, folder.Name); + } + + if (!path) { + html += getItem("lnkPath lnkDirectory", "", "Network", Globalize.translate("ButtonNetwork")); + } + + page.querySelector(".results").innerHTML = html; + loading.hide(); + }, function() { + if (updatePathOnError) { + page.querySelector("#txtDirectoryPickerPath").value = ""; + page.querySelector(".results").innerHTML = ""; + loading.hide(); + } + } + ); } function getItem(cssClass, type, path, name) { var html = ""; - return html += '
', html += '
', html += '
', html += name, html += "
", html += "
", html += 'arrow_forward', html += "
" + html += '
'; + html += '
'; + html += '
'; + html += name; + html += "
"; + html += "
"; + html += 'arrow_forward'; + html += "
"; + return html; } function getEditorHtml(options, systemInfo) { var html = ""; - if (html += '
', html += '
', !options.pathReadOnly) { + html += '
'; + html += '
'; + if (!options.pathReadOnly) { var instruction = options.instruction ? options.instruction + "

" : ""; - html += '
', html += instruction, html += Globalize.translate("MessageDirectoryPickerInstruction").replace("{0}", "\\\\server").replace("{1}", "\\\\192.168.1.101"), "synology" === (systemInfo.PackageName || "").toLowerCase() ? (html += "
", html += "
", html += '' + Globalize.translate("LearnHowToCreateSynologyShares") + "") : "bsd" === systemInfo.OperatingSystem.toLowerCase() ? (html += "
", html += "
", html += Globalize.translate("MessageDirectoryPickerBSDInstruction"), html += "
", html += '' + Globalize.translate("ButtonMoreInformation") + "") : "linux" === systemInfo.OperatingSystem.toLowerCase() && (html += "
", html += "
", html += Globalize.translate("MessageDirectoryPickerLinuxInstruction"), html += "
"), html += "
" + html += '
'; + html += instruction; + html += Globalize.translate("MessageDirectoryPickerInstruction").replace("{0}", "\\\\server").replace("{1}", "\\\\192.168.1.101"); + if ("synology" === (systemInfo.PackageName || "").toLowerCase()) { + html += "
"; + html += "
"; + html += ''; + html += Globalize.translate("LearnHowToCreateSynologyShares"); + html += ""; + } else if ("bsd" === systemInfo.OperatingSystem.toLowerCase()) { + html += "
"; + html += "
"; + html += Globalize.translate("MessageDirectoryPickerBSDInstruction"); + html += "
"; + html += ''; + html += Globalize.translate("ButtonMoreInformation"); + html += ""; + } else if ("linux" === systemInfo.OperatingSystem.toLowerCase()) { + html += "
"; + html += "
"; + html += Globalize.translate("MessageDirectoryPickerLinuxInstruction"); + html += "
"; + } + html += "
" } - html += '
', html += '
', html += '
'; - var labelKey = !0 !== options.includeFiles ? "LabelFolder" : "LabelPath", - readOnlyAttribute = options.pathReadOnly ? " readonly" : ""; - return html += '', html += "
", readOnlyAttribute || (html += ''), html += "
", readOnlyAttribute || (html += '
'), options.enableNetworkSharePath && (html += '
', html += '', html += '
', html += Globalize.translate("LabelOptionalNetworkPathHelp"), html += "
", html += "
"), html += '
', html += '", html += "
", html += "
", html += "
", html += "
", html += "
" + html += '
'; + html += '
'; + html += '
'; + var labelKey; + if (options.includeFiles !== true) { + labelKey = "LabelFolder"; + } else { + labelKey = "LabelPath"; + } + var readOnlyAttribute = options.pathReadOnly ? " readonly" : ""; + html += ''; + html += "
"; + if (!readOnlyAttribute) { + html += ''; + } + html += "
"; + if (!readOnlyAttribute) { + html += '
'; + } + if (options.enableNetworkSharePath) { + html += '
'; + html += ''; + html += '
'; + html += Globalize.translate("LabelOptionalNetworkPathHelp"); + html += "
"; + html += "
"; + } + html += '
'; + html += '"; + html += "
"; + html += "
"; + html += "
"; + html += ""; + html += ""; + + return html; } function alertText(text) { @@ -70,11 +172,22 @@ define(["loading", "dialogHelper", "dom", "listViewStyle", "emby-input", "emby-b } }).catch(function(response) { if (response) { - if (404 === response.status) return alertText("The path could not be found. Please ensure the path is valid and try again."), Promise.reject(); - if (500 === response.status) return alertText(validateWriteable ? "Jellyfin Server requires write access to this folder. Please ensure write access and try again." : "The path could not be found. Please ensure the path is valid and try again."), Promise.reject() + // TODO All alerts (across the project), should use Globalize.translate() + if (response.status === 404) { + alertText("The path could not be found. Please ensure the path is valid and try again."); + return Promise.reject(); + } + if (response.status === 500) { + if (validateWriteable) { + alertText("Jellyfin Server requires write access to this folder. Please ensure write access and try again."); + } else { + alertText("The path could not be found. Please ensure the path is valid and try again.") + } + return Promise.reject() + } } return Promise.resolve() - }) + }); } function initEditor(content, options, fileOptions) { @@ -82,64 +195,121 @@ define(["loading", "dialogHelper", "dom", "listViewStyle", "emby-input", "emby-b var lnkPath = dom.parentWithClass(e.target, "lnkPath"); if (lnkPath) { var path = lnkPath.getAttribute("data-path"); - lnkPath.classList.contains("lnkFile") ? content.querySelector("#txtDirectoryPickerPath").value = path : refreshDirectoryBrowser(content, path, fileOptions, !0) + if (lnkPath.classList.contains("lnkFile")) { + content.querySelector("#txtDirectoryPickerPath").value = path; + } else { + refreshDirectoryBrowser(content, path, fileOptions, true) + }; } - }), content.addEventListener("click", function(e) { + }); + + content.addEventListener("click", function(e) { if (dom.parentWithClass(e.target, "btnRefreshDirectories")) { var path = content.querySelector("#txtDirectoryPickerPath").value; - refreshDirectoryBrowser(content, path, fileOptions) + refreshDirectoryBrowser(content, path, fileOptions); } - }), content.addEventListener("change", function(e) { + }); + + content.addEventListener("change", function(e) { var txtDirectoryPickerPath = dom.parentWithTag(e.target, "INPUT"); - txtDirectoryPickerPath && "txtDirectoryPickerPath" === txtDirectoryPickerPath.id && refreshDirectoryBrowser(content, txtDirectoryPickerPath.value, fileOptions) - }), content.querySelector("form").addEventListener("submit", function(e) { + if (txtDirectoryPickerPath && "txtDirectoryPickerPath" === txtDirectoryPickerPath.id) { + refreshDirectoryBrowser(content, txtDirectoryPickerPath.value, fileOptions); + } + }); + + content.querySelector("form").addEventListener("submit", function(e) { if (options.callback) { var networkSharePath = this.querySelector("#txtNetworkPath"); networkSharePath = networkSharePath ? networkSharePath.value : null; var path = this.querySelector("#txtDirectoryPickerPath").value; - validatePath(path, options.validateWriteable, ApiClient).then(function() { - options.callback(path, networkSharePath) - }) + validatePath(path, options.validateWriteable, ApiClient).then( + function() { + options.callback(path, networkSharePath); + } + ); } - return e.preventDefault(), e.stopPropagation(), !1 - }) + e.preventDefault(); + e.stopPropagation(); + return false; + }); } function getDefaultPath(options) { - return options.path ? Promise.resolve(options.path) : ApiClient.getJSON(ApiClient.getUrl("Environment/DefaultDirectoryBrowser")).then(function(result) { - return result.Path || "" - }, function() { - return "" - }) + if (options.path) { + Promise.resolve(options.path); + } else { + ApiClient.getJSON(ApiClient.getUrl("Environment/DefaultDirectoryBrowser")).then( + function(result) { + return result.Path || ""; + }, function() { + return ""; + } + ); + } } function directoryBrowser() { - var currentDialog, self = this; + var currentDialog; + var self = this; self.show = function(options) { options = options || {}; var fileOptions = { - includeDirectories: !0 + includeDirectories: true }; - null != options.includeDirectories && (fileOptions.includeDirectories = options.includeDirectories), null != options.includeFiles && (fileOptions.includeFiles = options.includeFiles), Promise.all([getSystemInfo(), getDefaultPath(options)]).then(function(responses) { - var systemInfo = responses[0], - initialPath = responses[1], - dlg = dialogHelper.createDialog({ - size: "medium-tall", - removeOnClose: !0, - scrollY: !1 + if (options.includeDirectories != null) { + fileOptions.includeDirectories = options.includeDirectories; + } + if (options.includeFiles != null) { + fileOptions.includeFiles = options.includeFiles; + } + Promise.all([getSystemInfo(), getDefaultPath(options)]).then( + function(responses) { + var systemInfo = responses[0]; + var initialPath = responses[1]; + var dlg = dialogHelper.createDialog({ + size: "medium-tall", + removeOnClose: true, + scrollY: false + }); + dlg.classList.add("ui-body-a"); + dlg.classList.add("background-theme-a"); + dlg.classList.add("directoryPicker"); + dlg.classList.add("formDialog"); + + var html = ""; + html += '
'; + html += ''; + html += '

'; + html += options.header || Globalize.translate("HeaderSelectPath"); + html += "

"; + html += "
"; + html += getEditorHtml(options, systemInfo); + dlg.innerHTML = html; + initEditor(dlg, options, fileOptions); + dlg.addEventListener("close", onDialogClosed); + dialogHelper.open(dlg); + dlg.querySelector(".btnCloseDialog").addEventListener("click", function() { + dialogHelper.close(dlg) }); - dlg.classList.add("ui-body-a"), dlg.classList.add("background-theme-a"), dlg.classList.add("directoryPicker"), dlg.classList.add("formDialog"); - var html = ""; - html += '
', html += '', html += '

', html += options.header || Globalize.translate("HeaderSelectPath"), html += "

", html += "
", html += getEditorHtml(options, systemInfo), dlg.innerHTML = html, initEditor(dlg, options, fileOptions), dlg.addEventListener("close", onDialogClosed), dialogHelper.open(dlg), dlg.querySelector(".btnCloseDialog").addEventListener("click", function() { - dialogHelper.close(dlg) - }), currentDialog = dlg, dlg.querySelector("#txtDirectoryPickerPath").value = initialPath; - var txtNetworkPath = dlg.querySelector("#txtNetworkPath"); - txtNetworkPath && (txtNetworkPath.value = options.networkSharePath || ""), options.pathReadOnly || refreshDirectoryBrowser(dlg, initialPath, fileOptions, !0) - }) - }, self.close = function() { - currentDialog && dialogHelper.close(currentDialog) + currentDialog = dlg; + dlg.querySelector("#txtDirectoryPickerPath").value = initialPath; + var txtNetworkPath = dlg.querySelector("#txtNetworkPath"); + if (txtNetworkPath) { + txtNetworkPath.value = options.networkSharePath || ""; + } + if (!options.pathReadOnly) { + refreshDirectoryBrowser(dlg, initialPath, fileOptions, true); + } + } + ); + }; + self.close = function() { + if (currentDialog) { + dialogHelper.close(currentDialog); + } } } + var systemInfo; return directoryBrowser });