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

273 lines
8.7 KiB
JavaScript
Raw Normal View History

2016-06-13 12:02:48 -07:00
define(['dialogHelper', 'jQuery', 'paper-item', 'emby-input', 'emby-button', 'paper-item-body', 'paper-icon-button-light'], function (dialogHelper, $) {
2015-10-13 12:22:45 -07:00
var systemInfo;
function getSystemInfo() {
2016-02-17 19:55:15 -07:00
var deferred = jQuery.Deferred();
2015-10-13 12:22:45 -07:00
if (systemInfo) {
deferred.resolveWith(null, [systemInfo]);
} else {
2015-12-14 08:43:03 -07:00
ApiClient.getPublicSystemInfo().then(function (info) {
2015-10-13 12:22:45 -07:00
systemInfo = info;
deferred.resolveWith(null, [systemInfo]);
});
}
return deferred.promise();
}
function onDialogClosed() {
$(this).remove();
Dashboard.hideLoadingMsg();
}
function refreshDirectoryBrowser(page, path, fileOptions) {
2016-01-29 21:55:05 -07:00
if (path && typeof(path) !== 'string') {
throw new Error('invalid path');
}
2015-10-13 12:22:45 -07:00
Dashboard.showLoadingMsg();
if (path) {
$('.networkHeadline').hide();
} else {
$('.networkHeadline').show();
}
2016-01-29 21:55:05 -07:00
var promises = [];
2015-10-13 12:22:45 -07:00
if (path === "Network") {
2016-01-29 21:55:05 -07:00
promises.push(ApiClient.getNetworkDevices());
2015-10-13 12:22:45 -07:00
}
else if (path) {
2016-01-29 21:55:05 -07:00
promises.push(ApiClient.getDirectoryContents(path, fileOptions));
promises.push(ApiClient.getParentPath(path));
} else {
promises.push(ApiClient.getDrives());
2015-10-13 12:22:45 -07:00
}
2016-01-29 21:55:05 -07:00
Promise.all(promises).then(function (responses) {
2015-10-13 12:22:45 -07:00
2015-12-14 08:43:03 -07:00
var folders = responses[0];
var parentPath = responses[1] || '';
2015-10-13 12:22:45 -07:00
$('#txtDirectoryPickerPath', page).val(path || "");
var html = '';
if (path) {
html += getItem("lnkPath lnkDirectory", "", parentPath, '...');
2015-10-13 12:22:45 -07:00
}
for (var i = 0, length = folders.length; i < length; i++) {
var folder = folders[i];
var cssClass = folder.Type == "File" ? "lnkPath lnkFile" : "lnkPath lnkDirectory";
2015-10-17 18:18:29 -07:00
html += getItem(cssClass, folder.Type, folder.Path, folder.Name);
2015-10-13 12:22:45 -07:00
}
if (!path) {
2015-10-17 18:18:29 -07:00
html += getItem("lnkPath lnkDirectory", "", "Network", Globalize.translate('ButtonNetwork'));
2015-10-13 12:22:45 -07:00
}
$('.results', page).html(html);
Dashboard.hideLoadingMsg();
2015-12-14 08:43:03 -07:00
}, function () {
2015-10-13 12:22:45 -07:00
$('#txtDirectoryPickerPath', page).val("");
$('.results', page).html('');
Dashboard.hideLoadingMsg();
});
}
2015-10-17 18:18:29 -07:00
function getItem(cssClass, type, path, name) {
var html = '';
html += '<paper-item role="menuitem" class="' + cssClass + '" data-type="' + type + '" data-path="' + path + '">';
html += '<paper-item-body>';
html += name;
html += '</paper-item-body>';
html += '<iron-icon icon="arrow-forward"></iron-icon>';
html += '</paper-item>';
return html;
}
2015-10-13 12:22:45 -07:00
function getEditorHtml(options, systemInfo) {
var html = '';
var instruction = options.instruction ? options.instruction + '<br/><br/>' : '';
html += '<p class="directoryPickerHeadline">';
html += instruction;
html += Globalize.translate('MessageDirectoryPickerInstruction')
.replace('{0}', '<b>\\\\server</b>')
.replace('{1}', '<b>\\\\192.168.1.101</b>');
if (systemInfo.OperatingSystem.toLowerCase() == 'bsd') {
html += '<br/>';
html += '<br/>';
html += Globalize.translate('MessageDirectoryPickerBSDInstruction');
html += '<br/>';
html += '<a href="http://doc.freenas.org/9.3/freenas_jails.html#add-storage" target="_blank">' + Globalize.translate('ButtonMoreInformation') + '</a>';
}
2015-10-13 19:41:46 -07:00
else if (systemInfo.OperatingSystem.toLowerCase() == 'linux') {
html += '<br/>';
html += '<br/>';
html += Globalize.translate('MessageDirectoryPickerLinuxInstruction');
html += '<br/>';
2015-10-16 15:21:03 -07:00
//html += '<a href="http://doc.freenas.org/9.3/freenas_jails.html#add-storage" target="_blank">' + Globalize.translate('ButtonMoreInformation') + '</a>';
2015-10-13 19:41:46 -07:00
}
2015-10-13 12:22:45 -07:00
html += '</p>';
html += '<form style="max-width:100%;">';
2016-06-13 12:02:48 -07:00
html += '<div class="inputContainer" style="display: flex; align-items: center;">';
html += '<div style="flex-grow:1;">';
html += '<input is="emby-input" id="txtDirectoryPickerPath" type="text" required="required" label="' + Globalize.translate('LabelCurrentPath') + '"/>';
html += '</div>';
2016-05-11 10:12:09 -07:00
html += '<button type="button" is="paper-icon-button-light" class="btnRefreshDirectories" title="' + Globalize.translate('ButtonRefresh') + '"><iron-icon icon="refresh"></iron-icon></button>';
2015-10-13 12:22:45 -07:00
html += '</div>';
html += '<div class="results paperList" style="height: 180px; overflow-y: auto;"></div>';
html += '<div>';
2016-06-04 20:50:07 -07:00
html += '<button is="emby-button" type="submit" class="raised submit block">' + Globalize.translate('ButtonOk') + '</button>';
2015-10-13 12:22:45 -07:00
html += '</div>';
html += '</form>';
html += '</div>';
return html;
}
function initEditor(content, options, fileOptions) {
$(content).on("click", ".lnkPath", function () {
var path = this.getAttribute('data-path');
if ($(this).hasClass('lnkFile')) {
$('#txtDirectoryPickerPath', content).val(path);
} else {
refreshDirectoryBrowser(content, path, fileOptions);
}
}).on("click", ".btnRefreshDirectories", function () {
var path = $('#txtDirectoryPickerPath', content).val();
refreshDirectoryBrowser(content, path, fileOptions);
}).on("change", "#txtDirectoryPickerPath", function () {
refreshDirectoryBrowser(content, this.value, fileOptions);
});
$('form', content).on('submit', function () {
if (options.callback) {
options.callback(this.querySelector('#txtDirectoryPickerPath').value);
}
return false;
});
}
function directoryBrowser() {
var self = this;
var currentDialog;
self.show = function (options) {
options = options || {};
var fileOptions = {
includeDirectories: true
};
if (options.includeDirectories != null) {
fileOptions.includeDirectories = options.includeDirectories;
}
if (options.includeFiles != null) {
fileOptions.includeFiles = options.includeFiles;
}
2015-12-14 08:43:03 -07:00
getSystemInfo().then(function (systemInfo) {
2015-10-13 12:22:45 -07:00
2016-03-23 12:03:17 -07:00
var dlg = dialogHelper.createDialog({
2015-12-14 08:43:03 -07:00
size: 'medium'
});
2015-10-13 12:22:45 -07:00
2016-01-30 13:59:09 -07:00
dlg.classList.add('ui-body-a');
dlg.classList.add('background-theme-a');
dlg.classList.add('popupEditor');
2015-12-14 08:43:03 -07:00
dlg.classList.add('directoryPicker');
2015-10-13 12:22:45 -07:00
2015-12-14 08:43:03 -07:00
var html = '';
html += '<h2 class="dialogHeader">';
2016-06-05 11:51:18 -07:00
html += '<button type="button" is="emby-button" icon="arrow-back" class="fab mini btnCloseDialog" tabindex="-1"><iron-icon icon="arrow-back"></iron-icon></button>';
2015-12-14 08:43:03 -07:00
html += '<div style="display:inline-block;margin-left:.6em;vertical-align:middle;">' + (options.header || Globalize.translate('HeaderSelectPath')) + '</div>';
html += '</h2>';
2015-10-13 12:22:45 -07:00
2015-12-14 08:43:03 -07:00
html += '<div class="editorContent" style="max-width:800px;margin:auto;">';
html += getEditorHtml(options, systemInfo);
html += '</div>';
2015-10-13 12:22:45 -07:00
2015-12-14 08:43:03 -07:00
dlg.innerHTML = html;
document.body.appendChild(dlg);
2015-10-13 12:22:45 -07:00
2015-12-14 08:43:03 -07:00
var editorContent = dlg.querySelector('.editorContent');
initEditor(editorContent, options, fileOptions);
2015-10-13 12:22:45 -07:00
2015-12-14 08:43:03 -07:00
// Has to be assigned a z-index after the call to .open()
$(dlg).on('iron-overlay-opened', function () {
this.querySelector('#txtDirectoryPickerPath input').focus();
});
2016-03-22 10:46:57 -07:00
$(dlg).on('close', onDialogClosed);
2015-10-13 12:22:45 -07:00
2016-03-23 12:03:17 -07:00
dialogHelper.open(dlg);
2015-10-13 12:22:45 -07:00
2015-12-14 08:43:03 -07:00
$('.btnCloseDialog', dlg).on('click', function () {
2015-10-13 12:22:45 -07:00
2016-03-23 12:03:17 -07:00
dialogHelper.close(dlg);
2015-12-14 08:43:03 -07:00
});
2015-10-13 12:22:45 -07:00
2015-12-14 08:43:03 -07:00
currentDialog = dlg;
2015-10-13 12:22:45 -07:00
2015-12-14 08:43:03 -07:00
var txtCurrentPath = $('#txtDirectoryPickerPath', editorContent);
2015-10-13 12:22:45 -07:00
2015-12-14 08:43:03 -07:00
if (options.path) {
txtCurrentPath.val(options.path);
}
2015-10-13 12:22:45 -07:00
2015-12-14 08:43:03 -07:00
refreshDirectoryBrowser(editorContent, txtCurrentPath.val());
2015-10-13 12:22:45 -07:00
});
};
self.close = function () {
if (currentDialog) {
2016-03-23 12:03:17 -07:00
dialogHelper.close(currentDialog);
2015-10-13 12:22:45 -07:00
}
};
}
return directoryBrowser;
});