diff --git a/src/components/directorybrowser/directorybrowser.js b/src/components/directorybrowser/directorybrowser.js index b10c9b8805..1c9615e2f0 100644 --- a/src/components/directorybrowser/directorybrowser.js +++ b/src/components/directorybrowser/directorybrowser.js @@ -10,241 +10,239 @@ import '../formdialog.scss'; import '../../elements/emby-button/emby-button'; import alert from '../alert'; -/* eslint-disable indent */ - - function getSystemInfo() { - return systemInfo ? Promise.resolve(systemInfo) : ApiClient.getPublicSystemInfo().then( - info => { - systemInfo = info; - return info; - } - ); - } - - function onDialogClosed() { - loading.hide(); - } - - function refreshDirectoryBrowser(page, path, fileOptions, updatePathOnError) { - if (path && typeof path !== 'string') { - throw new Error('invalid path'); +function getSystemInfo() { + return systemInfo ? Promise.resolve(systemInfo) : ApiClient.getPublicSystemInfo().then( + info => { + systemInfo = info; + return info; } + ); +} - loading.show(); +function onDialogClosed() { + loading.hide(); +} - const promises = []; +function refreshDirectoryBrowser(page, path, fileOptions, updatePathOnError) { + if (path && typeof path !== 'string') { + throw new Error('invalid path'); + } - if (path === 'Network') { - promises.push(ApiClient.getNetworkDevices()); + loading.show(); + + const promises = []; + + if (path === 'Network') { + promises.push(ApiClient.getNetworkDevices()); + } else { + if (path) { + promises.push(ApiClient.getDirectoryContents(path, fileOptions)); + promises.push(ApiClient.getParentPath(path)); } else { + promises.push(ApiClient.getDrives()); + } + } + + Promise.all(promises).then( + responses => { + const folders = responses[0]; + const parentPath = responses[1] || ''; + let html = ''; + + page.querySelector('.results').scrollTop = 0; + page.querySelector('#txtDirectoryPickerPath').value = path || ''; + if (path) { - promises.push(ApiClient.getDirectoryContents(path, fileOptions)); - promises.push(ApiClient.getParentPath(path)); - } else { - promises.push(ApiClient.getDrives()); + html += getItem('lnkPath lnkDirectory', '', parentPath, '...'); + } + for (let i = 0, length = folders.length; i < length; i++) { + const folder = folders[i]; + const cssClass = folder.Type === 'File' ? '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(); + }, () => { + if (updatePathOnError) { + page.querySelector('#txtDirectoryPickerPath').value = ''; + page.querySelector('.results').innerHTML = ''; + loading.hide(); } } + ); +} - Promise.all(promises).then( - responses => { - const folders = responses[0]; - const parentPath = responses[1] || ''; - let html = ''; +function getItem(cssClass, type, path, name) { + let html = ''; + html += `
`; + html += '
'; + html += '
'; + html += name; + html += '
'; + html += '
'; + html += ''; + html += '
'; + return html; +} - page.querySelector('.results').scrollTop = 0; - page.querySelector('#txtDirectoryPickerPath').value = path || ''; +function getEditorHtml(options, systemInfo) { + let html = ''; + html += '
'; + html += '
'; + if (!options.pathReadOnly) { + const instruction = options.instruction ? `${options.instruction}

` : ''; + html += '
'; + html += instruction; + if (systemInfo.OperatingSystem.toLowerCase() === 'bsd') { + html += '
'; + html += '
'; + html += globalize.translate('MessageDirectoryPickerBSDInstruction'); + html += '
'; + } else if (systemInfo.OperatingSystem.toLowerCase() === 'linux') { + html += '
'; + html += '
'; + html += globalize.translate('MessageDirectoryPickerLinuxInstruction'); + html += '
'; + } + html += '
'; + } + html += '
'; + html += '
'; + html += '
'; + let labelKey; + if (options.includeFiles !== true) { + labelKey = 'LabelFolder'; + } else { + labelKey = 'LabelPath'; + } + const readOnlyAttribute = options.pathReadOnly ? ' readonly' : ''; + html += ``; + html += '
'; + if (!readOnlyAttribute) { + html += ``; + } + html += '
'; + if (!readOnlyAttribute) { + html += '
'; + } + if (options.enableNetworkSharePath) { + html += '
'; + html += ``; + html += '
'; + html += globalize.translate('LabelOptionalNetworkPathHelp', '\\\\server', '\\\\192.168.1.101'); + html += '
'; + html += '
'; + } + html += '
'; + html += ``; + html += '
'; + html += '
'; + html += '
'; + html += '
'; + html += ''; - if (path) { - html += getItem('lnkPath lnkDirectory', '', parentPath, '...'); - } - for (let i = 0, length = folders.length; i < length; i++) { - const folder = folders[i]; - const cssClass = folder.Type === 'File' ? 'lnkPath lnkFile' : 'lnkPath lnkDirectory'; - html += getItem(cssClass, folder.Type, folder.Path, folder.Name); + return html; +} + +function alertText(text) { + alertTextWithOptions({ + text: text + }); +} + +function alertTextWithOptions(options) { + alert(options); +} + +function validatePath(path, validateWriteable, apiClient) { + return apiClient.ajax({ + type: 'POST', + url: apiClient.getUrl('Environment/ValidatePath'), + data: JSON.stringify({ + ValidateWriteable: validateWriteable, + Path: path + }), + contentType: 'application/json' + }).catch(response => { + if (response) { + if (response.status === 404) { + alertText(globalize.translate('PathNotFound')); + return Promise.reject(); + } + if (response.status === 500) { + if (validateWriteable) { + alertText(globalize.translate('WriteAccessRequired')); + } else { + alertText(globalize.translate('PathNotFound')); } + return Promise.reject(); + } + } + return Promise.resolve(); + }); +} - if (!path) { - html += getItem('lnkPath lnkDirectory', '', 'Network', globalize.translate('ButtonNetwork')); - } +function initEditor(content, options, fileOptions) { + content.addEventListener('click', e => { + const lnkPath = dom.parentWithClass(e.target, 'lnkPath'); + if (lnkPath) { + const path = lnkPath.getAttribute('data-path'); + if (lnkPath.classList.contains('lnkFile')) { + content.querySelector('#txtDirectoryPickerPath').value = path; + } else { + refreshDirectoryBrowser(content, path, fileOptions, true); + } + } + }); - page.querySelector('.results').innerHTML = html; - loading.hide(); + content.addEventListener('click', e => { + if (dom.parentWithClass(e.target, 'btnRefreshDirectories')) { + const path = content.querySelector('#txtDirectoryPickerPath').value; + refreshDirectoryBrowser(content, path, fileOptions); + } + }); + + content.addEventListener('change', e => { + const txtDirectoryPickerPath = dom.parentWithTag(e.target, 'INPUT'); + if (txtDirectoryPickerPath && txtDirectoryPickerPath.id === 'txtDirectoryPickerPath') { + refreshDirectoryBrowser(content, txtDirectoryPickerPath.value, fileOptions); + } + }); + + content.querySelector('form').addEventListener('submit', function(e) { + if (options.callback) { + let networkSharePath = this.querySelector('#txtNetworkPath'); + networkSharePath = networkSharePath ? networkSharePath.value : null; + const path = this.querySelector('#txtDirectoryPickerPath').value; + validatePath(path, options.validateWriteable, ApiClient).then(options.callback(path, networkSharePath)); + } + e.preventDefault(); + e.stopPropagation(); + return false; + }); +} + +function getDefaultPath(options) { + if (options.path) { + return Promise.resolve(options.path); + } else { + return ApiClient.getJSON(ApiClient.getUrl('Environment/DefaultDirectoryBrowser')).then( + result => { + return result.Path || ''; }, () => { - if (updatePathOnError) { - page.querySelector('#txtDirectoryPickerPath').value = ''; - page.querySelector('.results').innerHTML = ''; - loading.hide(); - } + return ''; } ); } +} - function getItem(cssClass, type, path, name) { - let html = ''; - html += `
`; - html += '
'; - html += '
'; - html += name; - html += '
'; - html += '
'; - html += ''; - html += '
'; - return html; - } - - function getEditorHtml(options, systemInfo) { - let html = ''; - html += '
'; - html += '
'; - if (!options.pathReadOnly) { - const instruction = options.instruction ? `${options.instruction}

` : ''; - html += '
'; - html += instruction; - if (systemInfo.OperatingSystem.toLowerCase() === 'bsd') { - html += '
'; - html += '
'; - html += globalize.translate('MessageDirectoryPickerBSDInstruction'); - html += '
'; - } else if (systemInfo.OperatingSystem.toLowerCase() === 'linux') { - html += '
'; - html += '
'; - html += globalize.translate('MessageDirectoryPickerLinuxInstruction'); - html += '
'; - } - html += '
'; - } - html += '
'; - html += '
'; - html += '
'; - let labelKey; - if (options.includeFiles !== true) { - labelKey = 'LabelFolder'; - } else { - labelKey = 'LabelPath'; - } - const readOnlyAttribute = options.pathReadOnly ? ' readonly' : ''; - html += ``; - html += '
'; - if (!readOnlyAttribute) { - html += ``; - } - html += '
'; - if (!readOnlyAttribute) { - html += '
'; - } - if (options.enableNetworkSharePath) { - html += '
'; - html += ``; - html += '
'; - html += globalize.translate('LabelOptionalNetworkPathHelp', '\\\\server', '\\\\192.168.1.101'); - html += '
'; - html += '
'; - } - html += '
'; - html += ``; - html += '
'; - html += '
'; - html += '
'; - html += '
'; - html += ''; - - return html; - } - - function alertText(text) { - alertTextWithOptions({ - text: text - }); - } - - function alertTextWithOptions(options) { - alert(options); - } - - function validatePath(path, validateWriteable, apiClient) { - return apiClient.ajax({ - type: 'POST', - url: apiClient.getUrl('Environment/ValidatePath'), - data: JSON.stringify({ - ValidateWriteable: validateWriteable, - Path: path - }), - contentType: 'application/json' - }).catch(response => { - if (response) { - if (response.status === 404) { - alertText(globalize.translate('PathNotFound')); - return Promise.reject(); - } - if (response.status === 500) { - if (validateWriteable) { - alertText(globalize.translate('WriteAccessRequired')); - } else { - alertText(globalize.translate('PathNotFound')); - } - return Promise.reject(); - } - } - return Promise.resolve(); - }); - } - - function initEditor(content, options, fileOptions) { - content.addEventListener('click', e => { - const lnkPath = dom.parentWithClass(e.target, 'lnkPath'); - if (lnkPath) { - const path = lnkPath.getAttribute('data-path'); - if (lnkPath.classList.contains('lnkFile')) { - content.querySelector('#txtDirectoryPickerPath').value = path; - } else { - refreshDirectoryBrowser(content, path, fileOptions, true); - } - } - }); - - content.addEventListener('click', e => { - if (dom.parentWithClass(e.target, 'btnRefreshDirectories')) { - const path = content.querySelector('#txtDirectoryPickerPath').value; - refreshDirectoryBrowser(content, path, fileOptions); - } - }); - - content.addEventListener('change', e => { - const txtDirectoryPickerPath = dom.parentWithTag(e.target, 'INPUT'); - if (txtDirectoryPickerPath && txtDirectoryPickerPath.id === 'txtDirectoryPickerPath') { - refreshDirectoryBrowser(content, txtDirectoryPickerPath.value, fileOptions); - } - }); - - content.querySelector('form').addEventListener('submit', function(e) { - if (options.callback) { - let networkSharePath = this.querySelector('#txtNetworkPath'); - networkSharePath = networkSharePath ? networkSharePath.value : null; - const path = this.querySelector('#txtDirectoryPickerPath').value; - validatePath(path, options.validateWriteable, ApiClient).then(options.callback(path, networkSharePath)); - } - e.preventDefault(); - e.stopPropagation(); - return false; - }); - } - - function getDefaultPath(options) { - if (options.path) { - return Promise.resolve(options.path); - } else { - return ApiClient.getJSON(ApiClient.getUrl('Environment/DefaultDirectoryBrowser')).then( - result => { - return result.Path || ''; - }, () => { - return ''; - } - ); - } - } - - let systemInfo; - class DirectoryBrowser { +let systemInfo; +class DirectoryBrowser { currentDialog; constructor() {} @@ -306,7 +304,6 @@ import alert from '../alert'; dialogHelper.close(this.currentDialog); } }; - } +} -/* eslint-enable indent */ export default DirectoryBrowser;