MediaWiki:Common.js: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Tag: Manual revert
(4 intermediate revisions by the same user not shown)
Line 10:
.replace(/\s+/g, ' ')
.trim();
}
 
 
/*
* Parse a query into OR groups containing AND terms.
*
* Examples:
* raster terrain
* raster AND terrain
* QuickOSM OR HCMGIS
* "repository install" AND raster
*/
function tuflowParseSearchQuery(rawQuery) {
var query = String(rawQuery || '').trim();
 
if (!query) {
return [];
}
 
return query.split(/\s+OR\s+/i).map(function (group) {
var terms = [];
var pattern = /"([^"]+)"|(\S+)/g;
var match;
 
while ((match = pattern.exec(group)) !== null) {
var term = match[1] || match[2];
 
if (!/^AND$/i.test(term)) {
term = tuflowNormaliseSearchText(term);
 
if (term) {
terms.push(term);
}
}
}
 
return terms;
}).filter(function (group) {
return group.length > 0;
});
}
 
function tuflowMatchesSearch(text, rawQuery) {
var normalisedText = tuflowNormaliseSearchText(text);
var groups = tuflowParseSearchQuery(rawQuery);
 
if (!groups.length) {
return true;
}
 
return groups.some(function (terms) {
return terms.every(function (term) {
return normalisedText.includes(term);
});
});
}
 
function tuflowGetSearchTerms(rawQuery) {
var terms = [];
 
tuflowParseSearchQuery(rawQuery).forEach(function (group) {
group.forEach(function (term) {
if (terms.indexOf(term) === -1) {
terms.push(term);
}
});
});
 
return terms.sort(function (a, b) {
return b.length - a.length;
});
}
 
Line 34 ⟶ 105:
}
 
function tuflowHighlightMatches(root, queryrawQuery) {
if (!root || !queryrawQuery) {
return;
}
Line 41 ⟶ 112:
tuflowClearHighlights(root);
 
var terms = tuflowGetSearchTerms(rawQuery);
var testPattern = new RegExp(tuflowEscapeRegExp(query), 'i');
 
var replacePattern = new RegExp(tuflowEscapeRegExp(query), 'ig');
if (!terms.length) {
return;
}
 
/*
* Longer terms are checked first so that phrases such as
* "repository install" are highlighted before "repository".
*/
terms.sort(function (a, b) {
return b.length - a.length;
});
 
var escapedTerms = terms.map(function (term) {
return tuflowEscapeRegExp(term);
});
 
var testPattern = new RegExp(
escapedTerms.join('|'),
'i'
);
 
var replacePattern = new RegExp(
escapedTerms.join('|'),
'ig'
);
 
var walker = document.createTreeWalker(
Line 55 ⟶ 151:
if (
node.parentNode &&
[
['SCRIPT', 'STYLE', 'MARK', 'TEXTAREA', 'INPUT'].indexOf(node.parentNode.nodeName) !== -1
'SCRIPT',
'STYLE',
'MARK',
'TEXTAREA',
'INPUT',
'BUTTON'
].indexOf(node.parentNode.nodeName) !== -1
) {
return NodeFilter.FILTER_REJECT;
Line 79 ⟶ 182:
var lastIndex = 0;
 
text.replace(replacePattern, function (match, offset) {
if (offset > lastIndex) {replacePattern,
function (match, offset) fragment.appendChild({
if (offset > document.createTextNode(text.slice(lastIndex,) offset)){
); fragment.appendChild(
} document.createTextNode(
text.slice(lastIndex, offset)
)
);
}
 
var marknormalisedMatch = document.createElement('mark');
mark.className = 'search-highlight' tuflowNormaliseSearchText(match);
mark.textContent = match;
fragment.appendChild(mark);
 
lastIndex = offset + matchvar termIndex = terms.length;findIndex(function (term) {
return matchterm === normalisedMatch;
});
 
/*
* Cycle through five highlight colours.
*/
var colourNumber =
(Math.max(termIndex, 0) % 5) + 1;
 
var mark = document.createElement('mark');
 
mark.className =
'search-highlight search-highlight-' +
colourNumber;
 
mark.textContent = match;
fragment.appendChild(mark);
 
lastIndex = offset + match.length;
 
return match;
}
);
 
if (lastIndex < text.length) {
fragment.appendChild(
document.createTextNode(text.slice(lastIndex))
text.slice(lastIndex)
)
);
}
Line 106 ⟶ 234:
});
}
 
 
/* -------------------------------------------------------------------------
Line 172 ⟶ 299:
 
function filterRows() {
var queryrawQuery = tuflowNormaliseSearchText(input.value.trim();
var visibleCount = 0;
 
Line 178 ⟶ 305:
tuflowClearHighlights(row);
 
var matches = tuflowMatchesSearch(
query === '' ||row.textContent,
tuflowNormaliseSearchText(row.textContent).includes(query);rawQuery
);
 
row.hidden = !matches;
Line 187 ⟶ 315:
visibleCount += 1;
 
if (queryrawQuery !== '') {
tuflowHighlightMatches(row, queryrawQuery);
}
}
});
 
resultCount.textContent = queryrawQuery
? visibleCount + ' matching result' + (visibleCount === 1 ? '' : 's')
: rows.length + ' benchmark results';
 
clearButton.hidden = queryrawQuery === '';
}
 
Line 568 ⟶ 696:
 
function filterRows() {
var queryrawQuery = input.value.trim();
tuflowNormaliseSearchText(input.value);
var matches;
 
if (!queryrawQuery) {
restoreOriginalRows();
 
Line 583 ⟶ 710:
 
matches = records.filter(function (record) {
return record.searchableText.includestuflowMatchesSearch(query);
record.searchableText,
rawQuery
);
});
 
renderFilteredRows(matches, queryrawQuery);
 
resultCount.textContent =
Line 820 ⟶ 950:
 
function filterRows() {
var queryrawQuery = normaliseText(input.value.trim();
var visibleCount = 0;
 
rows.forEach(function (row) {
clearHighlightstuflowClearHighlights(row);
 
var matches = tuflowMatchesSearch(
query === '' ||row.textContent,
normaliseText(rawQuery
row.textContent);
).includes(query);
 
row.hidden = !matches;
Line 837 ⟶ 966:
visibleCount += 1;
 
if (queryrawQuery) {
highlightMatchestuflowHighlightMatches(row, queryrawQuery);
}
}
});
 
resultCount.textContent = queryrawQuery
? visibleCount +
' matching plugin' +
Line 849 ⟶ 978:
: rows.length + ' QGIS plugins';
 
clearButton.hidden = queryrawQuery === '';
}