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,
if (!root || !
return;
}
Line 41 ⟶ 112:
tuflowClearHighlights(root);
var terms = tuflowGetSearchTerms(rawQuery);
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',
'BUTTON'
].indexOf(node.parentNode.nodeName) !== -1
) {
return NodeFilter.FILTER_REJECT;
Line 79 ⟶ 182:
var lastIndex = 0;
text.replace(
function (match, offset)
if (offset >
text.slice(lastIndex, offset)
)
);
}
var
return
});
/*
* 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)
)
);
}
Line 106 ⟶ 234:
});
}
/* -------------------------------------------------------------------------
Line 172 ⟶ 299:
function filterRows() {
var
var visibleCount = 0;
Line 178 ⟶ 305:
tuflowClearHighlights(row);
var matches = tuflowMatchesSearch(
);
row.hidden = !matches;
Line 187 ⟶ 315:
visibleCount += 1;
if (
tuflowHighlightMatches(row,
}
}
});
resultCount.textContent =
? visibleCount + ' matching result' + (visibleCount === 1 ? '' : 's')
: rows.length + ' benchmark results';
clearButton.hidden =
}
Line 568 ⟶ 696:
function filterRows() {
var
var matches;
if (!
restoreOriginalRows();
Line 583 ⟶ 710:
matches = records.filter(function (record) {
return
record.searchableText,
rawQuery
);
});
renderFilteredRows(matches,
resultCount.textContent =
Line 820 ⟶ 950:
function filterRows() {
var
var visibleCount = 0;
rows.forEach(function (row) {
var matches = tuflowMatchesSearch(
row.hidden = !matches;
Line 837 ⟶ 966:
visibleCount += 1;
if (
}
}
});
resultCount.textContent =
? visibleCount +
' matching plugin' +
Line 849 ⟶ 978:
: rows.length + ' QGIS plugins';
clearButton.hidden =
}
| |||