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/directorybrowser/directorybrowser.js

302 lines
12 KiB
JavaScript
Raw Normal View History

2020-03-29 20:22:38 +02:00
define(['loading', 'dialogHelper', 'dom', 'globalize', 'listViewStyle', 'emby-input', 'paper-icon-button-light', 'css!./directorybrowser', 'formDialogStyle', 'emby-button'], function(loading, dialogHelper, dom, globalize) {
2019-01-11 09:11:57 -05:00
'use strict';
2018-10-23 01:05:09 +03:00
function getSystemInfo() {
2019-01-11 09:11:57 -05:00
return systemInfo ? Promise.resolve(systemInfo) : ApiClient.getPublicSystemInfo().then(
function(info) {
systemInfo = info;
return info;
}
);
2018-10-23 01:05:09 +03:00
}
function onDialogClosed() {
loading.hide();
2018-10-23 01:05:09 +03:00
}
function refreshDirectoryBrowser(page, path, fileOptions, updatePathOnError) {
2019-01-11 09:11:57 -05:00
if (path && typeof path !== 'string') {
2020-05-04 12:44:12 +02:00
throw new Error('invalid path');
2019-01-11 09:11:57 -05:00
}
2018-10-23 01:05:09 +03:00
loading.show();
2018-10-23 01:05:09 +03:00
var promises = [];
2019-01-11 09:11:57 -05:00
2020-05-04 12:44:12 +02:00
if ('Network' === path) {
promises.push(ApiClient.getNetworkDevices());
2019-01-11 09:11:57 -05:00
} else {
if (path) {
2019-01-11 09:11:57 -05:00
promises.push(ApiClient.getDirectoryContents(path, fileOptions));
promises.push(ApiClient.getParentPath(path));
} else {
2019-01-11 09:11:57 -05:00
promises.push(ApiClient.getDrives());
2018-10-23 01:05:09 +03:00
}
2019-01-11 09:11:57 -05:00
}
Promise.all(promises).then(
function(responses) {
var folders = responses[0];
2020-05-04 12:44:12 +02:00
var parentPath = responses[1] || '';
var html = '';
2019-01-11 09:11:57 -05:00
2020-05-04 12:44:12 +02:00
page.querySelector('.results').scrollTop = 0;
page.querySelector('#txtDirectoryPickerPath').value = path || '';
2019-01-11 09:11:57 -05:00
if (path) {
2020-05-04 12:44:12 +02:00
html += getItem('lnkPath lnkDirectory', '', parentPath, '...');
2019-01-11 09:11:57 -05:00
}
for (var i = 0, length = folders.length; i < length; i++) {
var folder = folders[i];
2020-05-04 12:44:12 +02:00
var cssClass = 'File' === folder.Type ? 'lnkPath lnkFile' : 'lnkPath lnkDirectory';
2019-01-11 09:11:57 -05:00
html += getItem(cssClass, folder.Type, folder.Path, folder.Name);
}
2019-01-11 09:11:57 -05:00
if (!path) {
2020-05-04 12:44:12 +02:00
html += getItem('lnkPath lnkDirectory', '', 'Network', globalize.translate('ButtonNetwork'));
2019-01-11 09:11:57 -05:00
}
2020-05-04 12:44:12 +02:00
page.querySelector('.results').innerHTML = html;
2019-01-11 09:11:57 -05:00
loading.hide();
}, function() {
if (updatePathOnError) {
2020-05-04 12:44:12 +02:00
page.querySelector('#txtDirectoryPickerPath').value = '';
page.querySelector('.results').innerHTML = '';
2019-01-11 09:11:57 -05:00
loading.hide();
}
}
);
2018-10-23 01:05:09 +03:00
}
function getItem(cssClass, type, path, name) {
2020-05-04 12:44:12 +02:00
var html = '';
2019-01-11 09:11:57 -05:00
html += '<div class="listItem listItem-border ' + cssClass + '" data-type="' + type + '" data-path="' + path + '">';
html += '<div class="listItemBody" style="padding-left:0;padding-top:.5em;padding-bottom:.5em;">';
html += '<div class="listItemBodyText">';
html += name;
2020-05-04 12:44:12 +02:00
html += '</div>';
html += '</div>';
2020-04-26 02:37:28 +03:00
html += '<span class="material-icons arrow_forward" style="font-size:inherit;"></span>';
2020-05-04 12:44:12 +02:00
html += '</div>';
2019-01-11 09:11:57 -05:00
return html;
2018-10-23 01:05:09 +03:00
}
function getEditorHtml(options, systemInfo) {
2020-05-04 12:44:12 +02:00
var html = '';
2019-01-11 09:11:57 -05:00
html += '<div class="formDialogContent scrollY">';
html += '<div class="dialogContentInner dialog-content-centered" style="padding-top:2em;">';
if (!options.pathReadOnly) {
2020-05-04 12:44:12 +02:00
var instruction = options.instruction ? options.instruction + '<br/><br/>' : '';
2019-01-11 09:11:57 -05:00
html += '<div class="infoBanner" style="margin-bottom:1.5em;">';
html += instruction;
2020-05-04 12:44:12 +02:00
if ('bsd' === systemInfo.OperatingSystem.toLowerCase()) {
html += '<br/>';
html += '<br/>';
html += globalize.translate('MessageDirectoryPickerBSDInstruction');
html += '<br/>';
} else if ('linux' === systemInfo.OperatingSystem.toLowerCase()) {
html += '<br/>';
html += '<br/>';
html += globalize.translate('MessageDirectoryPickerLinuxInstruction');
html += '<br/>';
2019-01-11 09:11:57 -05:00
}
2020-05-04 12:44:12 +02:00
html += '</div>';
2019-01-11 09:11:57 -05:00
}
html += '<form style="margin:auto;">';
html += '<div class="inputContainer" style="display: flex; align-items: center;">';
html += '<div style="flex-grow:1;">';
var labelKey;
if (options.includeFiles !== true) {
2020-05-04 12:44:12 +02:00
labelKey = 'LabelFolder';
2019-01-11 09:11:57 -05:00
} else {
2020-05-04 12:44:12 +02:00
labelKey = 'LabelPath';
2019-01-11 09:11:57 -05:00
}
2020-05-04 12:44:12 +02:00
var readOnlyAttribute = options.pathReadOnly ? ' readonly' : '';
2020-03-26 14:25:16 +01:00
html += '<input is="emby-input" id="txtDirectoryPickerPath" type="text" required="required" ' + readOnlyAttribute + ' label="' + globalize.translate(labelKey) + '"/>';
2020-05-04 12:44:12 +02:00
html += '</div>';
if (!readOnlyAttribute) {
2020-05-04 12:44:12 +02:00
html += '<button type="button" is="paper-icon-button-light" class="btnRefreshDirectories emby-input-iconbutton" title="' + globalize.translate('ButtonRefresh') + '"><span class="material-icons search"></span></button>';
2019-01-11 09:11:57 -05:00
}
2020-05-04 12:44:12 +02:00
html += '</div>';
if (!readOnlyAttribute) {
2019-01-11 09:11:57 -05:00
html += '<div class="results paperList" style="max-height: 200px; overflow-y: auto;"></div>';
}
2019-03-10 07:06:28 +09:00
if (options.enableNetworkSharePath) {
html += '<div class="inputContainer" style="margin-top:2em;">';
2020-05-04 12:44:12 +02:00
html += '<input is="emby-input" id="txtNetworkPath" type="text" label="' + globalize.translate('LabelOptionalNetworkPath') + '"/>';
2019-03-10 07:06:28 +09:00
html += '<div class="fieldDescription">';
2020-05-04 12:44:12 +02:00
html += globalize.translate('LabelOptionalNetworkPathHelp');
html += globalize.translate('MessageDirectoryPickerInstruction', '<b>\\\\server</b>', '<b>\\\\192.168.1.101</b>');
2020-05-04 12:44:12 +02:00
html += '</div>';
html += '</div>';
2019-03-10 07:06:28 +09:00
}
2019-01-11 09:11:57 -05:00
html += '<div class="formDialogFooter">';
2020-05-04 12:44:12 +02:00
html += '<button is="emby-button" type="submit" class="raised button-submit block formDialogFooterItem">' + globalize.translate('ButtonOk') + '</button>';
html += '</div>';
html += '</form>';
html += '</div>';
html += '</div>';
html += '</div>';
2019-01-11 09:11:57 -05:00
return html;
2018-10-23 01:05:09 +03:00
}
function alertText(text) {
alertTextWithOptions({
text: text
});
2018-10-23 01:05:09 +03:00
}
function alertTextWithOptions(options) {
2020-05-04 12:44:12 +02:00
require(['alert'], function(alert) {
alert(options);
});
2018-10-23 01:05:09 +03:00
}
function validatePath(path, validateWriteable, apiClient) {
return apiClient.ajax({
2020-05-04 12:44:12 +02:00
type: 'POST',
url: apiClient.getUrl('Environment/ValidatePath'),
2018-10-23 01:05:09 +03:00
data: {
ValidateWriteable: validateWriteable,
Path: path
}
}).catch(function(response) {
if (response) {
2019-01-11 09:11:57 -05:00
if (response.status === 404) {
2020-05-04 12:44:12 +02:00
alertText(globalize.translate('PathNotFound'));
2019-01-11 09:11:57 -05:00
return Promise.reject();
}
if (response.status === 500) {
if (validateWriteable) {
2020-05-04 12:44:12 +02:00
alertText(globalize.translate('WriteAccessRequired'));
2019-01-11 09:11:57 -05:00
} else {
2020-05-04 12:44:12 +02:00
alertText(globalize.translate('PathNotFound'));
2019-01-11 09:11:57 -05:00
}
return Promise.reject();
2019-01-11 09:11:57 -05:00
}
2018-10-23 01:05:09 +03:00
}
return Promise.resolve();
2019-01-11 09:11:57 -05:00
});
2018-10-23 01:05:09 +03:00
}
function initEditor(content, options, fileOptions) {
2020-05-04 12:44:12 +02:00
content.addEventListener('click', function(e) {
var lnkPath = dom.parentWithClass(e.target, 'lnkPath');
2018-10-23 01:05:09 +03:00
if (lnkPath) {
2020-05-04 12:44:12 +02:00
var path = lnkPath.getAttribute('data-path');
if (lnkPath.classList.contains('lnkFile')) {
content.querySelector('#txtDirectoryPickerPath').value = path;
} else {
refreshDirectoryBrowser(content, path, fileOptions, true);
}
2018-10-23 01:05:09 +03:00
}
2019-01-11 09:11:57 -05:00
});
2020-05-04 12:44:12 +02:00
content.addEventListener('click', function(e) {
if (dom.parentWithClass(e.target, 'btnRefreshDirectories')) {
var path = content.querySelector('#txtDirectoryPickerPath').value;
2019-01-11 09:11:57 -05:00
refreshDirectoryBrowser(content, path, fileOptions);
2018-10-23 01:05:09 +03:00
}
2019-01-11 09:11:57 -05:00
});
2020-05-04 12:44:12 +02:00
content.addEventListener('change', function(e) {
var txtDirectoryPickerPath = dom.parentWithTag(e.target, 'INPUT');
if (txtDirectoryPickerPath && 'txtDirectoryPickerPath' === txtDirectoryPickerPath.id) {
2019-01-11 09:11:57 -05:00
refreshDirectoryBrowser(content, txtDirectoryPickerPath.value, fileOptions);
}
});
2020-05-04 12:44:12 +02:00
content.querySelector('form').addEventListener('submit', function(e) {
2018-10-23 01:05:09 +03:00
if (options.callback) {
2020-05-04 12:44:12 +02:00
var networkSharePath = this.querySelector('#txtNetworkPath');
2019-03-10 07:06:28 +09:00
networkSharePath = networkSharePath ? networkSharePath.value : null;
2020-05-04 12:44:12 +02:00
var path = this.querySelector('#txtDirectoryPickerPath').value;
2019-03-10 07:06:28 +09:00
validatePath(path, options.validateWriteable, ApiClient).then(options.callback(path, networkSharePath));
2018-10-23 01:05:09 +03:00
}
2019-01-11 09:11:57 -05:00
e.preventDefault();
e.stopPropagation();
return false;
});
2018-10-23 01:05:09 +03:00
}
function getDefaultPath(options) {
2019-01-11 09:11:57 -05:00
if (options.path) {
return Promise.resolve(options.path);
2019-01-11 09:11:57 -05:00
} else {
2020-05-04 12:44:12 +02:00
return ApiClient.getJSON(ApiClient.getUrl('Environment/DefaultDirectoryBrowser')).then(
2019-01-11 09:11:57 -05:00
function(result) {
2020-05-04 12:44:12 +02:00
return result.Path || '';
2019-01-11 09:11:57 -05:00
}, function() {
2020-05-04 12:44:12 +02:00
return '';
2019-01-11 09:11:57 -05:00
}
);
}
2018-10-23 01:05:09 +03:00
}
function directoryBrowser() {
2019-01-11 09:11:57 -05:00
var currentDialog;
var self = this;
2018-10-23 01:05:09 +03:00
self.show = function(options) {
options = options || {};
var fileOptions = {
2019-01-11 09:11:57 -05:00
includeDirectories: true
2018-10-23 01:05:09 +03:00
};
2019-01-11 09:11:57 -05:00
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({
2020-05-04 12:44:12 +02:00
size: 'medium-tall',
removeOnClose: true,
scrollY: false
});
2020-05-04 12:44:12 +02:00
dlg.classList.add('ui-body-a');
dlg.classList.add('background-theme-a');
dlg.classList.add('directoryPicker');
dlg.classList.add('formDialog');
2019-01-11 09:11:57 -05:00
2020-05-04 12:44:12 +02:00
var html = '';
2019-01-11 09:11:57 -05:00
html += '<div class="formDialogHeader">';
2020-04-26 02:37:28 +03:00
html += '<button is="paper-icon-button-light" class="btnCloseDialog autoSize" tabindex="-1"><span class="material-icons arrow_back"></span></button>';
2019-01-11 09:11:57 -05:00
html += '<h3 class="formDialogHeaderTitle">';
2020-05-04 12:44:12 +02:00
html += options.header || globalize.translate('HeaderSelectPath');
html += '</h3>';
html += '</div>';
2019-01-11 09:11:57 -05:00
html += getEditorHtml(options, systemInfo);
dlg.innerHTML = html;
initEditor(dlg, options, fileOptions);
2020-05-04 12:44:12 +02:00
dlg.addEventListener('close', onDialogClosed);
2019-01-11 09:11:57 -05:00
dialogHelper.open(dlg);
2020-05-04 12:44:12 +02:00
dlg.querySelector('.btnCloseDialog').addEventListener('click', function() {
dialogHelper.close(dlg);
2018-10-23 01:05:09 +03:00
});
2019-01-11 09:11:57 -05:00
currentDialog = dlg;
2020-05-04 12:44:12 +02:00
dlg.querySelector('#txtDirectoryPickerPath').value = initialPath;
var txtNetworkPath = dlg.querySelector('#txtNetworkPath');
2019-03-10 07:06:28 +09:00
if (txtNetworkPath) {
2020-05-04 12:44:12 +02:00
txtNetworkPath.value = options.networkSharePath || '';
2019-03-10 07:06:28 +09:00
}
2019-01-11 09:11:57 -05:00
if (!options.pathReadOnly) {
refreshDirectoryBrowser(dlg, initialPath, fileOptions, true);
}
}
);
};
self.close = function() {
if (currentDialog) {
dialogHelper.close(currentDialog);
}
};
2018-10-23 01:05:09 +03:00
}
2019-01-11 09:11:57 -05:00
2018-10-23 01:05:09 +03:00
var systemInfo;
return directoryBrowser;
});