Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dropdown fuzzy matches (non-ajax ones) #5149

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions inc/html.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4317,23 +4317,32 @@ static function jsAdaptDropdown($id, $params = []) {
quietMillis: 100,
minimumResultsForSearch: ".$CFG_GLPI['ajax_limit_count'].",
matcher: function(params, data) {
// store last search in the global var
query = params;

// If there are no search terms, return all of the data
if ($.trim(params.term) === '') {
return data;
}

var searched_term = getTextWithoutDiacriticalMarks(params.term.toUpperCase());

var searched_term = getTextWithoutDiacriticalMarks(params.term);
var data_text = typeof(data.text) === 'string'
? getTextWithoutDiacriticalMarks(data.text.toUpperCase())
? getTextWithoutDiacriticalMarks(data.text)
: '';
var select2_fuzzy_opts = {
pre: '<span class=\"select2-rendered__match\">',
post: '</span>',
};

// Skip if there is no 'children' property
if (typeof data.children === 'undefined') {
if (data_text.indexOf(searched_term) != -1) {
return data;
var match = fuzzy.match(searched_term, data_text, select2_fuzzy_opts);
if (match == null) {
return false;
}
return null;
data.rendered_text = match.rendered_text;
data.score = match.score;
return data;
}

// `data.children` contains the actual options that we are matching against
Expand All @@ -4342,11 +4351,21 @@ static function jsAdaptDropdown($id, $params = []) {

$.each(data.children, function (idx, child) {
var child_text = typeof(child.text) === 'string'
? getTextWithoutDiacriticalMarks(child.text.toUpperCase())
? getTextWithoutDiacriticalMarks(child.text)
: '';
if (child_text.indexOf(searched_term) != -1
|| data_text.indexOf(searched_term) != -1
) {

var match_child = fuzzy.match(searched_term, child_text, select2_fuzzy_opts);
var match_text = fuzzy.match(searched_term, data_text, select2_fuzzy_opts);
if (match_child !== null || match_text !== null) {
if (match_text !== null) {
data.score = match_text.score;
data.rendered_text = match_text.rendered;
}

if (match_child !== null) {
child.score = match_child.score;
child.rendered_text = match_child.rendered;
}
filteredChildren.push(child);
}
});
Expand All @@ -4366,7 +4385,7 @@ static function jsAdaptDropdown($id, $params = []) {
return null;
},
templateResult: templateResult,
templateSelection: templateSelection
templateSelection: templateSelection,
})
.bind('setValue', function(e, value) {
$('#$id').val(value).trigger('change');
Expand Down
37 changes: 21 additions & 16 deletions js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -917,28 +917,33 @@ function markMatch (text, term) {
* Function that renders select2 results.
*/
var templateResult = function(result) {
if (!result.id) {
return result.text;
}

var _elt = $('<span></span>');
_elt.attr('title', result.title);

var markup=[result.text];
if (typeof query.term !== 'undefined' &&
typeof result.rendered_text !== 'undefined') {
_elt.html(result.rendered_text);
} else {
if (!result.id) {
return result.text;
}

var markup=[result.text];

var _term = query.term || '';
var markup = markMatch(result.text, _term);
var _term = query.term || '';
var markup = markMatch(result.text, _term);

if (result.level) {
var a='';
var i=result.level;
while (i>1) {
a = a+'&nbsp;&nbsp;&nbsp;';
i=i-1;
if (result.level) {
var a='';
var i=result.level;
while (i>1) {
a = a+'&nbsp;&nbsp;&nbsp;';
i=i-1;
}
_elt.html(a+'&raquo;'+markup);
} else {
_elt.html(markup);
}
_elt.html(a+'&raquo;'+markup);
} else {
_elt.html(markup);
}

return _elt;
Expand Down