diff --git a/common/db.js b/common/db.js index 21ae66ef180501f60da3a0893adbae94c6c44291..3a95a97142de12093341a099a06bc2438ad81bcd 100644 --- a/common/db.js +++ b/common/db.js @@ -78,49 +78,20 @@ class KaraDatabase { logger.info('db', `Close database ${this.m_karaPath}`); } - /* Search from the kara table, a string. Can be anything (query, name, - * language, source, etc). + /* Search for karas with the search engine * - queryString: String - * - limit: Int | null -> maximal number of results - * - offset: Int | null -> offset in the search - * => a promize, callback with [(id: Int, string, cat, type, language, author, title, source)] */ - search(queryString, offset = null, limit = null) { - /*var __ret = []; - var __sqlQuery = `SELECT id, string, - category AS cat, - (song_type || song_number) AS type, - language, author_name AS author, - song_name AS title, source_name AS source, available - FROM kara - WHERE string LIKE ? OR author_name COLLATE nocase = ?`; - if (offset != null && limit != null) __sqlQuery += ` LIMIT ${offset}, ${limit}`; - - function __getRecords(db) { - return new Promise(resolv => { - db.all(__sqlQuery, [`%${queryString}%`, queryString], (err, rows) => { - if (err) { - logger.error('db', err); - throw err; - } - rows.forEach(row => { - __ret.push(row); - }); - resolv([{id:0, string:"a", cat:"cat", type:"type", language:"language", author:"author", title:"title", source:"source"}]); - }); - }); - }*/ + * => A list of search results [(id: Int, string, cat, type, language, author, title, source)] */ + search(queryString) { let searchResult = this.m_search_engine.search(queryString, { - prefix: term => term.length > 3, + prefix: term => term.length, fuzzy: term => term.length > 3 ? 0.2 : null, combineWith: 'AND' }) return searchResult; } - /* List all the kara in the db. - * - limit: Int | null -> maximal number of results - * - offset: Int | null -> offset in the search - * => a promize, callback with [(id: Int, string, cat, type, language, author, title, source)] */ + /* Initiate the search engine with the db + * => a promize */ setup_search_engine() { var __ret = []; var __sqlQuery = `SELECT id, diff --git a/instance/index.js b/instance/index.js index 460b4a4bfa70f1c654ed5b6acca496af826db687..ac61b9e40314eb9b7619b8a9b8fbebd2b4369b26 100644 --- a/instance/index.js +++ b/instance/index.js @@ -119,15 +119,13 @@ window.onload = () => { ipcRenderer.send('verify-lektord'); logger.debug('instance', 'Window loaded'); - - $('#filterInput').on('keypress', e => { + document.getElementById('filterInput').addEventListener('input', e => { /* On 'Return'. */ - if (e.which != 13) return; logger.debug('instance', `Send filter for: ${$('#filterInput').val()}`); ipcRenderer.send('reload-db-request', { search: `${$('#filterInput').val()}`, }); - }); + }) document.addEventListener( 'keydown', diff --git a/main.js b/main.js index c347745272d87cf1ee217a5f89053295c8eca1e5..fa227ff66e267c3b635c72bea2a7327d7cdeae9a 100644 --- a/main.js +++ b/main.js @@ -243,19 +243,18 @@ ipcMain.on('dry-update-database', () => lkt.commandDryUpdateDatabase()); /* Fill the pannel with the content of the DB. * The `arg` is the HTML object of the pannel */ ipcMain.on('reload-db-request', (event, arg) => { - logger.info('main', 'Reloading the DB content'); const callback = karas => event.reply('reload-db-responce', karas); if (arg && arg.search && arg.search !== '') { __lastFilter = arg.search; logger.debug('main', `Reload DB with search '${arg}'`); - karas = db.search(__lastFilter, 0, 150); + karas = db.search(__lastFilter); callback(karas); } else if ((arg && arg.search === '') || __lastFilter === '') { __lastFilter = ''; callback([]); } else { logger.debug('main', `Reload DB with last filter '${__lastFilter}'`); - karas = db.search(__lastFilter, 0, 150); + karas = db.search(__lastFilter); callback(karas); } });