(function (window, document, $) {
function refreshDirectoryBrowser(page, path, fileOptions) {
Dashboard.showLoadingMsg();
if (path) {
$('.networkHeadline').hide();
} else {
$('.networkHeadline').show();
}
var promise;
var parentPathPromise = null;
if (path === "Network") {
promise = ApiClient.getNetworkDevices();
}
else if (path) {
promise = ApiClient.getDirectoryContents(path, fileOptions);
parentPathPromise = ApiClient.getParentPath(path);
} else {
promise = ApiClient.getDrives();
}
if (!parentPathPromise) {
parentPathPromise = $.Deferred();
parentPathPromise.resolveWith(null, []);
parentPathPromise = parentPathPromise.promise();
}
$.when(promise, parentPathPromise).done(function (response1, response2) {
var folders = response1[0];
var parentPath = response2 && response2.length ? response2[0] || '' : '';
$('#txtDirectoryPickerPath', page).val(path || "");
var html = '';
if (path) {
html += '
..';
}
for (var i = 0, length = folders.length; i < length; i++) {
var folder = folders[i];
var cssClass = folder.Type == "File" ? "lnkPath lnkFile" : "lnkPath lnkDirectory";
html += '' + folder.Name + '';
}
if (!path) {
html += '' + Globalize.translate('ButtonNetwork') + '';
}
$('#ulDirectoryPickerList', page).html(html).listview('refresh');
Dashboard.hideLoadingMsg();
}).fail(function () {
$('#txtDirectoryPickerPath', page).val("");
$('#ulDirectoryPickerList', page).html('').listview('refresh');
Dashboard.hideLoadingMsg();
});
}
window.DirectoryBrowser = function (page) {
var self = this;
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;
}
options.header = options.header || Globalize.translate('HeaderSelectPath');
options.instruction = options.instruction || "";
var html = '';
$(page).append(html);
var popup = $('#popupDirectoryPicker').popup().trigger('create').on("popupafteropen", function () {
$('#popupDirectoryPicker input:first', this).focus();
}).popup("open").on("popupafterclose", function () {
$('form', this).off("submit");
$(this).off("click").off("change").off("popupafterclose").remove();
}).on("click", ".lnkPath", function () {
var path = this.getAttribute('data-path');
if ($(this).hasClass('lnkFile')) {
$('#txtDirectoryPickerPath', page).val(path);
} else {
refreshDirectoryBrowser(page, path, fileOptions);
}
}).on("click", ".btnRefreshDirectories", function () {
var path = $('#txtDirectoryPickerPath', page).val();
refreshDirectoryBrowser(page, path, fileOptions);
}).on("change", "#txtDirectoryPickerPath", function () {
refreshDirectoryBrowser(page, this.value, fileOptions);
});
var txtCurrentPath = $('#txtDirectoryPickerPath', popup);
if (options.path) {
txtCurrentPath.val(options.path);
}
$('form', popup).on('submit', function () {
if (options.callback) {
options.callback($('#txtDirectoryPickerPath', this).val());
}
return false;
});
refreshDirectoryBrowser(page, txtCurrentPath.val());
};
self.close = function () {
$('#popupDirectoryPicker', page).popup("close");
};
};
})(window, document, jQuery);