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

Search function #2315

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions images/icons/search-gray.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions images/icons/search-white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ <h2 class="groundstation-telemetry__header" data-i18n="gsTelemetry"></h2>
<li class="tab_cli">
<a href="#" data-i18n="tabCLI" class="tabicon ic_cli" title="CLI"></a>
</li>
<li class="tab_search">
<a href="#" data-i18n="search" class="tabicon ic_search" title="Search"></a>
</li>


<!-- <li class=""><a href="#" class="tabicon ic_advanced">Advanced (spare icon)</a></li> -->
</ul>
Expand Down
4 changes: 4 additions & 0 deletions js/configurator_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ $(function() {
require('./../tabs/ez_tune');
TABS.ez_tune.initialize(content_ready);
break;
case 'search':
require('./../tabs/search');
TABS.search.initialize(content_ready);
break;
default:
console.log('Tab not found:' + tab);
}
Expand Down
3 changes: 2 additions & 1 deletion js/gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ var GUI_control = function () {
'mission_control',
'mixer',
'programming',
'ez_tune'
'ez_tune',
'search'
];
this.allowedTabs = this.defaultAllowedTabsWhenDisconnected;

Expand Down
3 changes: 3 additions & 0 deletions locale/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -6178,5 +6178,8 @@
},
"currentLanguage": {
"message": "en"
},
"search": {
"message": "Search"
}
}
10 changes: 10 additions & 0 deletions src/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,16 @@ li.active .ic_mag {
background-image: url("./../../images/icons/cf_icon_mag_white.svg");
}

.ic_search {
background-image: url("./../../images/icons/search-gray.svg");
}

.ic_search:hover {
background-image: url("./../../images/icons/search-white.svg");
}



#content {
margin-top: 0;
padding: 0;
Expand Down
19 changes: 19 additions & 0 deletions tabs/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div class="tab-search">
<div class="content_wrapper">

<div class="options-section gui_box grey">
<div class="gui_box_titlebar">
<div class="spacer_box_title" data-i18n="search"></div>
</div>

<div class="spacer_box settings">
<div>
<input type="text" id="search-keyword" style="border: solid 1px black" />
<label for="search-keyword" id="search-label">🔎</label>
</div>
<div id="search-results"></div>
</div>
</div>

</div>
</div>
204 changes: 204 additions & 0 deletions tabs/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
const { GUI, TABS } = require('./../js/gui');
const path = require('path');
const i18n = require('./../js/localization');



TABS.search = { };


tabNames = [
"adjustments",
"advanced_tuning",
"auxiliary",
"calibration",
"cli",
"configuration",
"debug_trace",
"failsafe",
"firmware_flasher",
"gps",
"landing",
"led_strip",
"logging",
"magnetometer",
"mission_control",
"mixer",
"onboard_logging",
"options",
"osd",
"outputs",
"pid_tuning",
"ports",
"programming",
"receiver",
"receiver_msp",
"sensors",
"setup",
"sitl"
];


TABS.search.searchMessages = function (keyword) {
var resultsDiv = document.getElementById('search-results');
keyword = keyword.toLowerCase();
resultsDiv.innerHTML = '';

simClick = function (evt) {
tabName = evt.currentTarget.getAttribute("tabName");
tabLink = document.getElementsByClassName("tab_".concat(tabName))[0].getElementsByTagName("a")[0];
tabLink.click();
};


for (const [key, value] of Object.entries(this.messages)) {
// Get plain text of message (with tags)
var message = value.message.toLowerCase().replace(new RegExp('<[^>]*>'), '');

if ( message.includes(keyword) ) {
if (this.key2page.get(key) ) {
var pages = this.key2page.get(key);
var kwEscaped = keyword.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
var context = message.match( new RegExp( `([a-z]{0,10}.{0,14}${kwEscaped}.{0,14}[a-z]{0,10})`, 'i') )[1];
for (const page of pages) {
var rHTML = "<li><button class=\"searchResult\" tabName=\"{0}\">{1} tab</button>: {2}</li>".format(page, page, context);
resultsDiv.innerHTML= resultsDiv.innerHTML.concat(rHTML);
}
}
}
}

for (const [key, value] of this.setting2page) {
// for (const [key, value] of Object.entries(this.setting2page)) {
if ( key.toLowerCase().includes(keyword) ) {
for (const page of value) {
var rHTML = "<li><button class=\"searchResult\" tabName=\"{0}\">{1} tab</button>: {2}</li>".format(page, page, key);
resultsDiv.innerHTML= resultsDiv.innerHTML.concat(rHTML);
}
}
}

for ( result of document.getElementsByClassName("searchResult") ) {
result.addEventListener('click', simClick, false);
}
}


TABS.search.getMessages = function () {
const res_messages = fetch('locale/en/messages.json');
res_messages
.then (data => data.json())
.then (data => {
this.messages = data;
})
.catch((error) => {
console.error(error)
});
}

TABS.search.geti18nHTML = function (filename, filecontents) {

const parser = new DOMParser();
const htmlDoc = parser.parseFromString(filecontents, 'text/html');
var hasDataI18n = htmlDoc.querySelectorAll('[data-i18n]:not([data-i18n=""])');
for (const element of hasDataI18n) {
key = element.getAttribute('data-i18n');
if (! this.key2page.has(key) ) {
this.key2page.set( key, new Set() );
}
this.key2page.get(key).add(filename);
}
hasDataI18n = htmlDoc.querySelectorAll('[i18n]:not([i18n=""])');
for (const element of hasDataI18n) {
key = element.getAttribute('i18n');
if (! this.key2page.has(key) ) {
this.key2page.set( key, new Set() );
}
this.key2page.get(key).add(filename);
}

settings = htmlDoc.querySelectorAll('[data-setting]:not([data-setting=""])');
for (const element of settings) {
key = element.getAttribute('data-setting');
if (! this.setting2page.has(key) ) {
this.setting2page.set( key, new Set() );
}
this.setting2page.get(key).add(filename);
}

}


TABS.search.geti18nJs = function (filename, filecontents) {
var re = /(?:data-i18n=|i18n.getMessage\()["']([^"']*)['"]/g

while (match = re.exec(filecontents)) {
key = match[1];
if (! this.key2page.has(key) ) {
this.key2page.set( key, new Set() );
}
this.key2page.get(key).add(filename);
}
}


TABS.search.indexTab = async function indexTab(tabName) {
var response = fetch(`tabs/${tabName}.js`);
response
.then (data => data.text())
.then (data => {
this.geti18nJs(tabName, data);
})
.catch((error) => {
console.error(error)
});


response = fetch(`tabs/${tabName}.html`);
response
.then (data => data.text())
.then (data => {
this.geti18nHTML(tabName, data);
})
.catch((error) => {
console.error(error)
});
};


TABS.search.initialize = function (callback) {
var self = this;
this.key2page = new Map();
this.setting2page = new Map();
this.messages;

if (GUI.active_tab != 'search') {
GUI.active_tab = 'search';
}

function searchKeyword() {
TABS.search.searchMessages(document.getElementById('search-keyword').value);
}

function searchKeywordTyping() {
if (document.getElementById('search-keyword').value.length > 2) {
TABS.search.searchMessages(document.getElementById('search-keyword').value);
}
}
GUI.load(path.join(__dirname, "search.html"), function () {
i18n.localize();
document.getElementById('search-label').addEventListener('click', searchKeyword, false);
document.getElementById('search-keyword').addEventListener('keyup', searchKeywordTyping, false);
GUI.content_ready(callback);
} );
self.getMessages();
for (let tab of tabNames) {
self.indexTab(tab);
}
}


TABS.search.cleanup = function (callback) {
if (callback) callback();
};

Loading