Private GIT

Skip to content
Snippets Groups Projects
Commit 67e896ed authored by Jonathon Saine's avatar Jonathon Saine
Browse files

Updated the autocomplete to have a bit more intelligence.

 -- Filter out search term results to only include things that start with the search term.
 -- Highlight the matching term from the item
 -- Ran code through jslint and fixed numerous mistakes.
Modified the browser backend so it does not throw an assertion error for invalid paths anymore. As this serves no purpose for the pathbrowser except an ugly ajax error in the console.
parent 911f5d1b
No related branches found
No related tags found
No related merge requests found
...@@ -8,19 +8,19 @@ ...@@ -8,19 +8,19 @@
} }
}; };
var fileBrowserDialog = null; var fileBrowserDialog, currentBrowserPath, currentRequest = null;
var currentBrowserPath = null;
var currentRequest = null;
function browse(path, endpoint) { function browse(path, endpoint) {
if(currentBrowserPath == path) if (currentBrowserPath === path) {
return; return;
}
currentBrowserPath = path; currentBrowserPath = path;
if(currentRequest) if (currentRequest) {
currentRequest.abort(); currentRequest.abort();
}
fileBrowserDialog.dialog('option', 'dialogClass', 'browserDialog busy'); fileBrowserDialog.dialog('option', 'dialogClass', 'browserDialog busy');
...@@ -28,7 +28,8 @@ ...@@ -28,7 +28,8 @@
fileBrowserDialog.empty(); fileBrowserDialog.empty();
var first_val = data[0]; var first_val = data[0];
var i = 0; var i = 0;
data = jQuery.grep(data, function(value) { var list, link = null;
data = $.grep(data, function (value) {
return i++ != 0; return i++ != 0;
}); });
$('<h1>').text(first_val.current_path).appendTo(fileBrowserDialog); $('<h1>').text(first_val.current_path).appendTo(fileBrowserDialog);
...@@ -37,8 +38,8 @@ ...@@ -37,8 +38,8 @@
link = $("<a href='javascript:void(0)' />").click(function () { browse(entry.path, endpoint); }).text(entry.name); link = $("<a href='javascript:void(0)' />").click(function () { browse(entry.path, endpoint); }).text(entry.name);
$('<span class="ui-icon ui-icon-folder-collapsed"></span>').prependTo(link); $('<span class="ui-icon ui-icon-folder-collapsed"></span>').prependTo(link);
link.hover( link.hover(
function(){jQuery("span", this).addClass("ui-icon-folder-open"); }, function () {$("span", this).addClass("ui-icon-folder-open"); },
function(){jQuery("span", this).removeClass("ui-icon-folder-open"); } function () {$("span", this).removeClass("ui-icon-folder-open"); }
); );
link.appendTo(list); link.appendTo(list);
}); });
...@@ -68,30 +69,25 @@ ...@@ -68,30 +69,25 @@
} }
// add the OK/Close buttons to the dialog // add the OK/Close buttons to the dialog
fileBrowserDialog.dialog('option', 'buttons', fileBrowserDialog.dialog('option', 'buttons', {
{
"Ok": function () { "Ok": function () {
// store the browsed path to the associated text field // store the browsed path to the associated text field
//options.field.val(currentBrowserPath); //options.field.val(currentBrowserPath);
//alert(currentBrowserPath); //alert(currentBrowserPath);
callback(currentBrowserPath, options); callback(currentBrowserPath, options);
fileBrowserDialog.dialog("close"); fileBrowserDialog.dialog("close");
}, },
"Cancel": function () { "Cancel": function () {
fileBrowserDialog.dialog("close"); fileBrowserDialog.dialog("close");
} }
}); });
// set up the browser and launch the dialog // set up the browser and launch the dialog
if (options.initialDir) var initialDir = '';
if (options.initialDir) {
initialDir = options.initialDir; initialDir = options.initialDir;
else }
initialDir = ''; browse(initialDir, options.url);
browse(initialDir, options.url)
fileBrowserDialog.dialog('open'); fileBrowserDialog.dialog('open');
return false; return false;
...@@ -99,43 +95,71 @@ ...@@ -99,43 +95,71 @@
$.fn.fileBrowser = function (options) { $.fn.fileBrowser = function (options) {
options = $.extend({}, $.Browser.defaults, options); options = $.extend({}, $.Browser.defaults, options);
// text field used for the result // text field used for the result
options.field = $(this); options.field = $(this);
if (options.field.autocomplete && options.autocompleteURL) { if (options.field.autocomplete && options.autocompleteURL) {
var query = '';
options.field.autocomplete({ options.field.autocomplete({
source: options.autocompleteURL, source: function (request, response) {
//keep track of user submitted search term
query = $.ui.autocomplete.escapeRegex(request.term);
$.ajax({
url: options.autocompleteURL,
data: request,
dataType: "json",
success: function (data, item) {
//implement a startsWith filter for the results
var matcher = new RegExp("^" + query, "i");
var a = $.grep(data, function (item, index) {
return matcher.test(item);
});
response(a);
}
});
},
open: function (event, ui) { open: function (event, ui) {
$(".ui-autocomplete li.ui-menu-item a").removeClass("ui-corner-all"); $(".ui-autocomplete li.ui-menu-item a").removeClass("ui-corner-all");
$(".ui-autocomplete li.ui-menu-item:odd a").addClass("ui-menu-item-alternate"); $(".ui-autocomplete li.ui-menu-item:odd a").addClass("ui-menu-item-alternate");
} }
})
.data("autocomplete")._renderItem = function (ul, item) {
//highlight the matched search term from the item -- note that this is global and will match anywhere
var result_item = item.label;
var x = new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + query + ")(?![^<>]*>)(?![^&;]+;)", "gi");
result_item = result_item.replace(x, function (FullMatch, n) {
return '<b>' + FullMatch + '</b>';
}); });
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a class='nowrap'>" + result_item + "</a>")
.appendTo(ul);
};
} }
var initialDir, path, callback = null;
// if the text field is empty and we're given a key then populate it with the last browsed value from a cookie // if the text field is empty and we're given a key then populate it with the last browsed value from a cookie
if(options.key && options.field.val().length == 0 && (path = $.cookie('fileBrowser-' + options.key))) if (options.key && options.field.val().length === 0 && (path = $.cookie('fileBrowser-' + options.key))) {
options.field.val(path); options.field.val(path);
}
callback = function (path, options) { callback = function (path, options) {
// store the browsed path to the associated text field // store the browsed path to the associated text field
options.field.val(path); options.field.val(path);
// use a cookie to remember for next time // use a cookie to remember for next time
if(options.key) if (options.key) {
$.cookie('fileBrowser-' + options.key, path); $.cookie('fileBrowser-' + options.key, path);
} }
};
initialDir = options.field.val() || (options.key && $.cookie('fileBrowser-' + options.key)) || ''; initialDir = options.field.val() || (options.key && $.cookie('fileBrowser-' + options.key)) || '';
options = $.extend(options, {initialDir: initialDir}) options = $.extend(options, {initialDir: initialDir});
// append the browse button and give it a click behavior // append the browse button and give it a click behavior
return options.field.addClass('fileBrowserField').after($('<input type="button" value="Browse&hellip;" class="fileBrowser" />').click(function () { return options.field.addClass('fileBrowserField').after($('<input type="button" value="Browse&hellip;" class="fileBrowser" />').click(function () {
$(this).nFileBrowser(callback, options); $(this).nFileBrowser(callback, options);
return false; return false;
})); }));
}; };
......
...@@ -52,7 +52,6 @@ def foldersAtPath(path, includeParent = False): ...@@ -52,7 +52,6 @@ def foldersAtPath(path, includeParent = False):
Give the empty string as the path to list the contents of the root path Give the empty string as the path to list the contents of the root path
under Unix this means "/", on Windows this will be a list of drive letters) under Unix this means "/", on Windows this will be a list of drive letters)
""" """
assert os.path.isabs(path) or path == ""
# walk up the tree until we find a valid path # walk up the tree until we find a valid path
while path and not os.path.isdir(path): while path and not os.path.isdir(path):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment