define(['dialogHelper', 'jQuery', 'listViewStyle', 'emby-input', 'emby-button', 'paper-icon-button-light', 'css!./directorybrowser'], function (dialogHelper, $) {
var systemInfo;
function getSystemInfo() {
var deferred = jQuery.Deferred();
if (systemInfo) {
deferred.resolveWith(null, [systemInfo]);
} else {
ApiClient.getPublicSystemInfo().then(function (info) {
systemInfo = info;
deferred.resolveWith(null, [systemInfo]);
});
}
return deferred.promise();
}
function onDialogClosed() {
Dashboard.hideLoadingMsg();
}
function refreshDirectoryBrowser(page, path, fileOptions) {
if (path && typeof (path) !== 'string') {
throw new Error('invalid path');
}
Dashboard.showLoadingMsg();
if (path) {
$('.networkHeadline').hide();
} else {
$('.networkHeadline').show();
}
var 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(function (responses) {
var folders = responses[0];
var parentPath = responses[1] || '';
$('#txtDirectoryPickerPath', page).val(path || "");
var html = '';
if (path) {
html += getItem("lnkPath lnkDirectory", "", parentPath, '...');
}
for (var i = 0, length = folders.length; i < length; i++) {
var folder = folders[i];
var 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'));
}
$('.results', page).html(html);
Dashboard.hideLoadingMsg();
}, function () {
$('#txtDirectoryPickerPath', page).val("");
$('.results', page).html('');
Dashboard.hideLoadingMsg();
});
}
function getItem(cssClass, type, path, name) {
var html = '';
html += '
';
html += '
';
html += '
';
html += name;
html += '
';
html += '
';
html += '
arrow_forward';
html += '
';
return html;
}
function getEditorHtml(options, systemInfo) {
var html = '';
var instruction = options.instruction ? options.instruction + '
' : '';
html += '';
html += instruction;
html += Globalize.translate('MessageDirectoryPickerInstruction')
.replace('{0}', '\\\\server')
.replace('{1}', '\\\\192.168.1.101');
if (systemInfo.OperatingSystem.toLowerCase() == 'bsd') {
html += '
';
html += '
';
html += Globalize.translate('MessageDirectoryPickerBSDInstruction');
html += '
';
html += '' + Globalize.translate('ButtonMoreInformation') + '';
}
else if (systemInfo.OperatingSystem.toLowerCase() == 'linux') {
html += '
';
html += '
';
html += Globalize.translate('MessageDirectoryPickerLinuxInstruction');
html += '
';
}
html += '
';
html += '';
html += '';
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 getDefaultPath(options) {
if (options.path) {
return Promise.resolve(options.path);
}
return ApiClient.getJSON(ApiClient.getUrl("Environment/DefaultDirectoryBrowser")).then(function (result) {
return result.Path || '';
}, function () {
return '';
});
}
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;
}
Promise.all([getSystemInfo(), getDefaultPath(options)]).then(function (responses) {
var systemInfo = responses[0];
var initialPath = responses[1];
var dlg = dialogHelper.createDialog({
size: 'medium',
removeOnClose: true
});
dlg.classList.add('ui-body-a');
dlg.classList.add('background-theme-a');
dlg.classList.add('directoryPicker');
var html = '';
html += '';
html += '';
html += getEditorHtml(options, systemInfo);
html += '
';
dlg.innerHTML = html;
document.body.appendChild(dlg);
var editorContent = dlg.querySelector('.editorContent');
initEditor(editorContent, options, fileOptions);
// Has to be assigned a z-index after the call to .open()
$(dlg).on('iron-overlay-opened', function () {
this.querySelector('#txtDirectoryPickerPath input').focus();
});
$(dlg).on('close', onDialogClosed);
dialogHelper.open(dlg);
$('.btnCloseDialog', dlg).on('click', function () {
dialogHelper.close(dlg);
});
currentDialog = dlg;
var txtCurrentPath = editorContent.querySelector('#txtDirectoryPickerPath');
txtCurrentPath.value = initialPath;
refreshDirectoryBrowser(editorContent, txtCurrentPath.value);
});
};
self.close = function () {
if (currentDialog) {
dialogHelper.close(currentDialog);
}
};
}
return directoryBrowser;
});