Skip to content

Commit

Permalink
[cmd:search] fix Studio-42#2867 support incremental search other than…
Browse files Browse the repository at this point in the history
… filename
  • Loading branch information
nao-pon committed Mar 7, 2019
1 parent a58d8ef commit 01ca195
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 24 deletions.
15 changes: 14 additions & 1 deletion js/elFinder.options.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,20 @@ elFinder.prototype._options = {
// "SearchMime" is implemented in default
SearchMime : { // The key is search type that send to the connector
name : 'btnMime', // Button text to be processed in i18n()
title : 'searchMime' // Button title to be processed in i18n()
title : 'searchMime',// Button title to be processed in i18n()
incsearch : 'mime' // Incremental search target filed name of the file object
// Or Callable function
/* incsearch function example
function(queryObject, cwdHashes, elFinderInstance) {
var q = queryObject.val;
var regex = queryObject.regex;
var matchedHashes = $.grep(cwdHashes, function(hash) {
var file = elFinderInstance.file(hash);
return (file && file.mime && file.mime.match(regex))? true : false;
});
return matchedHashes;
}
*/
}
}
},
Expand Down
68 changes: 46 additions & 22 deletions js/ui/cwd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2583,29 +2583,53 @@ $.fn.elfindercwd = function(fm, options) {
query = e.data.query;
})
.bind('incsearchstart', function(e) {
selectedFiles = {};
fm.lazy(function() {
// incremental search
var regex, q, fst = '';
q = query = e.data.query || '';
if (q) {
if (q.substr(0,1) === '/') {
q = q.substr(1);
fst = '^';
var q = e.data.query || '',
type = e.data.type || 'SearchName',
searchTypes = fm.options.commandsOptions.search.searchTypes || {};

if ((searchTypes[type] && searchTypes[type].incsearch) || type === 'SearchName') {
selectedFiles = {};
fm.lazy(function() {
// incremental search
var regex, incSearch, fst = '';
query = q;
if (q) {
if (q.substr(0,1) === '/') {
q = q.substr(1);
fst = '^';
}
regex = new RegExp(fst + q.replace(/([\\*\;\.\?\[\]\{\}\(\)\^\$\-\|])/g, '\\$1'), 'i');
if (type === 'SearchName') {
incHashes = $.grep(cwdHashes, function(hash) {
var file = fm.file(hash);
return (file && (file.name.match(regex) || (file.i18 && file.i18.match(regex))))? true : false;
});
} else {
incSearch = searchTypes[type].incsearch;
if (typeof incSearch === 'string') {
incHashes = $.grep(cwdHashes, function(hash) {
var file = fm.file(hash);
return (file && file[incSearch] && (file[incSearch] + '').match(regex))? true : false;
});
} else if (typeof incSearch === 'function') {
try {
incHashes = $.grep(incSearch({val: q, regex: regex}, cwdHashes, fm), function(hash) {
return fm.file(hash)? true : false;
});
} catch(e) {
incHashes = [];
}
}
}
fm.trigger('incsearch', { hashes: incHashes, query: q })
.searchStatus.ininc = true;
content();
fm.autoSync('stop');
} else {
fm.trigger('incsearchend');
}
regex = new RegExp(fst + q.replace(/([\\*\;\.\?\[\]\{\}\(\)\^\$\-\|])/g, '\\$1'), 'i');
incHashes = $.grep(cwdHashes, function(hash) {
var file = fm.file(hash);
return (file && (file.name.match(regex) || (file.i18 && file.i18.match(regex))))? true : false;
});
fm.trigger('incsearch', { hashes: incHashes, query: q })
.searchStatus.ininc = true;
content();
fm.autoSync('stop');
} else {
fm.trigger('incsearchend');
}
});
});
}
})
.bind('incsearchend', function(e) {
query = '';
Expand Down
5 changes: 4 additions & 1 deletion js/ui/searchbutton.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ $.fn.elfindersearchbutton = function(cmd) {
input.data('inctm', setTimeout(function() {
var val = input.val();
if (val.length === 0 || val.length >= isopts.minlen) {
(incVal !== val) && fm.trigger('incsearchstart', { query: val });
(incVal !== val) && fm.trigger('incsearchstart', {
query: val,
type: typeSet? typeSet.children('input:checked').val() : 'searchName'
});
incVal = val;
if (val === '' && fm.searchStatus.state > 1 && fm.searchStatus.query) {
input.val(fm.searchStatus.query).trigger('select');
Expand Down

0 comments on commit 01ca195

Please sign in to comment.