(function (window, document, $) {
function refreshDirectoryBrowser(page, path) {
Dashboard.showLoadingMsg();
var promise;
if (path === "Network") {
promise = ApiClient.getNetworkDevices();
}
else if (path) {
promise = ApiClient.getDirectoryContents(path, { includeDirectories: true });
} else {
promise = ApiClient.getDrives();
}
promise.done(function (folders) {
$('#txtDirectoryPickerPath', page).val(path || "");
var html = '';
if (path) {
var parentPath = path;
if (parentPath.endsWith('\\')) {
parentPath = parentPath.substring(0, parentPath.length - 1);
}
var lastIndex = parentPath.lastIndexOf('\\');
parentPath = lastIndex == -1 ? "" : parentPath.substring(0, lastIndex);
if (parentPath.endsWith(':')) {
parentPath += "\\";
}
if (parentPath == '\\') {
parentPath = "Network";
}
html += '
..';
}
for (var i = 0, length = folders.length; i < length; i++) {
var folder = folders[i];
html += '' + folder.Name + '';
}
if (!path) {
html += 'Network';
}
$('#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 || {};
options.header = options.header || "Select Media Path";
options.instruction = options.instruction || "Any path will do, but for optimal playback of bluray, dvd folders, and games, network paths (UNC) are recommended.";
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", ".lnkDirectory", function () {
var path = this.getAttribute('data-path');
refreshDirectoryBrowser(page, path);
}).on("change", "#txtDirectoryPickerPath", function () {
refreshDirectoryBrowser(page, this.value);
});
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);