2018-09-18 13:14:39 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
(function(){
|
|
|
|
"use strict";
|
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
let scope;
|
2018-09-18 13:14:39 +02:00
|
|
|
if ((typeof exports) !== "undefined"){
|
|
|
|
scope = exports;
|
|
|
|
}
|
|
|
|
else {
|
2019-03-12 22:24:23 +01:00
|
|
|
scope = require.register("./search", {});
|
2018-09-18 13:14:39 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 08:29:52 +02:00
|
|
|
const extension = require("./extension");
|
2018-09-18 13:14:39 +02:00
|
|
|
const texts = [];
|
|
|
|
|
|
|
|
scope.register = function(text, content){
|
|
|
|
texts.push({text: text.toLowerCase(), content});
|
|
|
|
};
|
|
|
|
scope.search = function(search){
|
2024-04-08 00:09:37 +02:00
|
|
|
const resultSets = search.split(/\s+/).filter(function(term){
|
2018-09-19 09:15:19 +02:00
|
|
|
return term.trim();
|
|
|
|
}).map(function(term){
|
|
|
|
const matching = new Set();
|
2024-04-08 00:09:37 +02:00
|
|
|
if (term.match(/^:[a-z]+$/i)){
|
|
|
|
const tag = term.substring(1);
|
|
|
|
texts.forEach(function(text){
|
|
|
|
if (text.content.querySelector(`.${tag}`)){
|
|
|
|
matching.add(text.content);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
term = new RegExp(term.toLowerCase());
|
|
|
|
texts.forEach(function(text){
|
|
|
|
if (term.test(text.text)){
|
|
|
|
matching.add(text.content);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-09-19 09:15:19 +02:00
|
|
|
return matching;
|
2018-09-18 13:14:39 +02:00
|
|
|
});
|
2018-09-19 09:15:19 +02:00
|
|
|
if (resultSets.length){
|
|
|
|
return Array.from(
|
|
|
|
resultSets.reduce(function(previousSet, set){
|
2019-11-28 01:26:35 +01:00
|
|
|
const andSet = new Set();
|
2018-09-19 09:15:19 +02:00
|
|
|
set.forEach(function(entry){
|
|
|
|
if (previousSet.has(entry)){
|
|
|
|
andSet.add(entry);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return andSet;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return [];
|
|
|
|
}
|
2018-09-18 13:14:39 +02:00
|
|
|
};
|
|
|
|
const searchListeners = [];
|
|
|
|
scope.init = function(){
|
|
|
|
const node = document.createElement("input");
|
|
|
|
node.id = "search";
|
2019-04-09 08:29:52 +02:00
|
|
|
node.placeholder = extension.getTranslation("search");
|
2018-09-18 13:14:39 +02:00
|
|
|
window.setTimeout(() => node.focus(), 1);
|
|
|
|
let lastResults = [];
|
|
|
|
node.addEventListener("input", function(){
|
2018-10-09 12:59:53 +02:00
|
|
|
this.search();
|
|
|
|
});
|
|
|
|
node.search = function(){
|
2018-09-18 13:14:39 +02:00
|
|
|
const search = this.value;
|
|
|
|
const results = search? scope.search(search): [];
|
|
|
|
searchListeners.forEach(function(callback){
|
|
|
|
callback({search, results, lastResults});
|
|
|
|
});
|
|
|
|
lastResults = results;
|
2018-10-09 12:59:53 +02:00
|
|
|
};
|
2018-09-18 13:14:39 +02:00
|
|
|
return node;
|
|
|
|
};
|
|
|
|
scope.on = function(callback){
|
|
|
|
searchListeners.push(callback);
|
|
|
|
};
|
|
|
|
}());
|