From b3fe328a54a101178e9b1c890e124eee97494530 Mon Sep 17 00:00:00 2001 From: Navdeep Singh Date: Thu, 13 Sep 2018 07:52:18 -0700 Subject: [PATCH] Fix 73 consolidate search (#313) * Promisify all search methods and perform all searches inside local banidb module * Update viewer.js to use local banidb module ES6-ify functions for scope consistency * fix #73 --- www/js/{ => banidb}/constants.js | 0 .../{search-database.js => banidb/index.js} | 124 ++++++++------- www/js/{ => banidb}/realm-db.js | 0 www/js/desktop_scripts.js | 3 - www/js/menu.js | 8 +- www/js/search.js | 28 ++-- www/js/viewer.js | 145 +++++++++--------- 7 files changed, 163 insertions(+), 145 deletions(-) rename www/js/{ => banidb}/constants.js (100%) rename www/js/{search-database.js => banidb/index.js} (67%) rename www/js/{ => banidb}/realm-db.js (100%) diff --git a/www/js/constants.js b/www/js/banidb/constants.js similarity index 100% rename from www/js/constants.js rename to www/js/banidb/constants.js diff --git a/www/js/search-database.js b/www/js/banidb/index.js similarity index 67% rename from www/js/search-database.js rename to www/js/banidb/index.js index fc28fd203..2da4ec632 100644 --- a/www/js/search-database.js +++ b/www/js/banidb/index.js @@ -1,10 +1,9 @@ const Realm = require('realm'); - const realmDB = require('./realm-db'); const CONSTS = require('./constants'); -module.exports = { - search(searchQuery, searchType, searchSource) { +const query = (searchQuery, searchType, searchSource) => ( + new Promise((resolve, reject) => { let dbQuery = ''; let searchCol = ''; let condition = ''; @@ -13,6 +12,7 @@ module.exports = { // default source for ang search to GURU_GRANTH_SAHIB let angSearchSourceId = CONSTS.SOURCE_TYPES.GURU_GRANTH_SAHIB; const order = []; + let howManyRows = 20; switch (searchType) { case CONSTS.SEARCH_TYPES.FIRST_LETTERS: // First letter start case CONSTS.SEARCH_TYPES.FIRST_LETTERS_ANYWHERE: { // First letter anywhere @@ -55,6 +55,7 @@ module.exports = { } case CONSTS.SEARCH_TYPES.ANG: // Ang searchCol = 'PageNo'; + howManyRows = 1000; dbQuery = parseInt(saniQuery, 10); condition = `${searchCol} = ${dbQuery}`; @@ -75,33 +76,38 @@ module.exports = { Realm.open(realmDB.realmVerseSchema) .then((realm) => { const rows = realm.objects('Verse').filtered(condition); - global.core.search.printResults(rows.slice(0, 20)); - }); - }, + resolve(rows.slice(0, howManyRows)); + }) + .catch(reject); + }) +); - loadShabad(ShabadID, LineID) { +const loadShabad = ShabadID => ( + new Promise((resolve, reject) => { Realm.open(realmDB.realmVerseSchema) .then((realm) => { const rows = realm.objects('Verse').filtered('ANY Shabads.ShabadID == $0', ShabadID); if (rows.length > 0) { - global.core.search.printShabad(rows, ShabadID, LineID || rows[0].ID); + resolve(rows); } - }); - }, + }) + .catch(reject); + }) +); - getAng(ShabadID) { - return new Promise((resolve) => { - Realm.open(realmDB.realmVerseSchema) - .then((realm) => { - const row = realm.objects('Verse').filtered('ANY Shabads.ShabadID == $0', ShabadID)[0]; - const { PageNo, SourceID } = row; - resolve({ - PageNo, - SourceID, - }); +const getAng = ShabadID => ( + new Promise((resolve) => { + Realm.open(realmDB.realmVerseSchema) + .then((realm) => { + const row = realm.objects('Verse').filtered('ANY Shabads.ShabadID == $0', ShabadID)[0]; + const { PageNo, SourceID } = row; + resolve({ + PageNo, + SourceID, }); - }); - }, + }); + }) +); /** * Retrieve all lines from a page @@ -115,19 +121,19 @@ module.exports = { * loadAng(1); * // => [{ Gurmukhi: 'jo gurisK guru syvdy sy puMn prwxI ]', ID: 31057 },...] */ - loadAng(PageNo, SourceID = 'G') { - return new Promise((resolve, reject) => { - Realm.open(realmDB.realmVerseSchema) - .then((realm) => { - const rows = realm.objects('Verse').filtered('PageNo = $0 AND Source.SourceID = $1', PageNo, SourceID); - if (rows.length > 0) { - resolve(rows); - } else { - reject(); - } - }); - }); - }, +const loadAng = (PageNo, SourceID = 'G') => ( + new Promise((resolve, reject) => { + Realm.open(realmDB.realmVerseSchema) + .then((realm) => { + const rows = realm.objects('Verse').filtered('PageNo = $0 AND Source.SourceID = $1', PageNo, SourceID); + if (rows.length > 0) { + resolve(rows); + } else { + reject(); + } + }); + }) +); /** * Retrieve Shabad for Verse @@ -140,15 +146,15 @@ module.exports = { * getShabad(1); * // => 1 */ - getShabad(VerseID) { - return new Promise((resolve) => { - Realm.open(realmDB.realmVerseSchema) - .then((realm) => { - const shabad = realm.objects('Verse').filtered('ID = $0', VerseID)[0]; - resolve(shabad.Shabads[0].ShabadID); - }); - }); - }, +const getShabad = VerseID => ( + new Promise((resolve) => { + Realm.open(realmDB.realmVerseSchema) + .then((realm) => { + const shabad = realm.objects('Verse').filtered('ID = $0', VerseID)[0]; + resolve(shabad.Shabads[0].ShabadID); + }); + }) +); /** * Retrieve a random Shabad from a source @@ -161,14 +167,24 @@ module.exports = { * randomShabad(); * // => 13 */ - randomShabad(SourceID = 'G') { - return new Promise((resolve) => { - Realm.open(realmDB.realmVerseSchema) - .then((realm) => { - const rows = realm.objects('Verse').filtered('Source.SourceID = $0', SourceID); - const row = rows[Math.floor(Math.random() * rows.length)]; - resolve(row.Shabads[0].ShabadID); - }); - }); - }, +const randomShabad = (SourceID = 'G') => ( + new Promise((resolve) => { + Realm.open(realmDB.realmVerseSchema) + .then((realm) => { + const rows = realm.objects('Verse').filtered('Source.SourceID = $0', SourceID); + const row = rows[Math.floor(Math.random() * rows.length)]; + resolve(row.Shabads[0].ShabadID); + }); + }) +); + +// Re-export CONSTS for use in other areas +module.exports = { + CONSTS, + query, + loadShabad, + getAng, + loadAng, + getShabad, + randomShabad, }; diff --git a/www/js/realm-db.js b/www/js/banidb/realm-db.js similarity index 100% rename from www/js/realm-db.js rename to www/js/banidb/realm-db.js diff --git a/www/js/desktop_scripts.js b/www/js/desktop_scripts.js index d7ee6570a..0ef35e344 100644 --- a/www/js/desktop_scripts.js +++ b/www/js/desktop_scripts.js @@ -7,8 +7,6 @@ const path = require('path'); const request = require('request'); const progress = require('request-progress'); -const search = require('./search-database'); - const { remote } = electron; const ipc = electron.ipcRenderer; const userDataPath = remote.app.getPath('userData'); @@ -60,7 +58,6 @@ function checkForNotifcations() { module.exports = { ipc, - search, store, init() { diff --git a/www/js/menu.js b/www/js/menu.js index 231e5b993..4dbce1ef8 100644 --- a/www/js/menu.js +++ b/www/js/menu.js @@ -1,3 +1,5 @@ +const { randomShabad } = require('./banidb'); + const h = require('hyperscript'); const settings = require('./settings'); const getJSON = require('get-json'); @@ -133,7 +135,7 @@ const randomShabadButton = h( 'a.random-shabad-button', { onclick: () => { - global.platform.search.randomShabad() + randomShabad() .then(goToShabadPage); } }, h('i.fa.fa-random.list-icon'), @@ -163,7 +165,9 @@ const hukamnamaButton = h( { onclick: () => { getJSON('https://api.banidb.com/hukamnama/today', (error, response) => { - goToShabadPage(response.shabadinfo.id); + if (!error) { + goToShabadPage(response.shabadinfo.id); + } }); }, }, diff --git a/www/js/search.js b/www/js/search.js index 34d9efceb..2d19e4739 100644 --- a/www/js/search.js +++ b/www/js/search.js @@ -1,4 +1,5 @@ /* eslint-disable arrow-parens */ +const { CONSTS, query, getAng, loadAng, getShabad, loadShabad } = require('./banidb'); // Gurmukhi keyboard layout file const keyboardLayout = require('./keyboard.json'); @@ -9,8 +10,6 @@ const h = require('hyperscript'); const { store } = require('electron').remote.require('./app'); -const CONSTS = require('./constants.js'); - // the non-character keys that will register as a keypress when searching const allowedKeys = [ 8, // Backspace @@ -472,7 +471,8 @@ module.exports = { searchQuery = this.$search.value; } if (searchQuery.length >= 1) { - global.platform.search.search(searchQuery, searchType, this.searchSource); + query(searchQuery, searchType, this.searchSource) + .then(rows => this.printResults(rows)); } else { this.$results.innerHTML = ''; } @@ -570,21 +570,20 @@ module.exports = { $shabadList.innerHTML = ''; currentShabad.splice(0, currentShabad.length); if (apv) { - global.platform.search - .getAng(ShabadID) + getAng(ShabadID) .then(ang => { currentMeta = ang; - return global.platform.search.loadAng(ang.PageNo, ang.SourceID); + return loadAng(ang.PageNo, ang.SourceID); }) .then(rows => this.printShabad(rows, ShabadID, LineID)); } else { - global.platform.search.loadShabad(ShabadID, LineID); + loadShabad(ShabadID, LineID) + .then(rows => this.printShabad(rows, ShabadID, LineID)); } }, loadAng(PageNo, SourceID) { - global.platform.search - .loadAng(PageNo, SourceID) + loadAng(PageNo, SourceID) .then(rows => this.printShabad(rows)); }, @@ -597,8 +596,15 @@ module.exports = { const PreviousVerseID = FirstLine === 1 ? FirstLine : FirstLine - 1; const NextVerseID = LastLine === 60403 ? LastLine : LastLine + 1; const adjacentVerseID = Forward ? NextVerseID : PreviousVerseID; - global.platform.search.getShabad(adjacentVerseID) - .then(global.platform.search.loadShabad); + let adjacentShabadID; + getShabad(adjacentVerseID) + .then(ShabadID => { + adjacentShabadID = ShabadID; + return loadShabad(ShabadID); + }) + .then((rows) => { + this.printShabad(rows, adjacentShabadID); + }); }, printShabad(rows, ShabadID, LineID) { diff --git a/www/js/viewer.js b/www/js/viewer.js index added0006..0ba43d073 100644 --- a/www/js/viewer.js +++ b/www/js/viewer.js @@ -5,10 +5,10 @@ no-use-before-define: 0, no-undef: 0 */ +const { loadAng, getAng, loadShabad } = require('./js/banidb'); + global.platform = require('./js/desktop_scripts'); const h = require('hyperscript'); -const Realm = require('realm'); -const realmDB = require('./js/realm-db'); const scroll = require('scroll'); const core = require('./js/index'); const { store } = require('electron').remote.require('./app'); @@ -46,18 +46,18 @@ $scroll.addEventListener('wheel', () => { passive: true, }); -function hideDecks() { +const hideDecks = () => { Array.from(document.querySelectorAll('.deck')).forEach((el) => { el.classList.remove('active'); }); -} +}; -function castToReceiver() { +const castToReceiver = () => { castCur.prefs = store.get('userPrefs'); sendMessage(JSON.stringify(castCur)); -} +}; -function castShabadLine(lineID) { +const castShabadLine = (lineID) => { document.querySelector('.viewer-controls').innerHTML = ''; castCur = decks[currentShabad][lineID]; let nextLine = ''; @@ -72,15 +72,15 @@ function castShabadLine(lineID) { const icons = iconsetHtml(`icons-${element.classList[0]}`, element.innerHTML); if (icons) document.querySelector('.viewer-controls').appendChild(icons); })); -} +}; -function castText(text, isGurmukhi) { +const castText = (text, isGurmukhi) => { castCur = {}; castCur.showInEnglish = isGurmukhi !== true; castCur.gurmukhi = text; castCur.larivaar = text; castToReceiver(); -} +}; // IPC global.platform.ipc.on('search-cast', (event, pos) => { @@ -144,14 +144,14 @@ global.platform.ipc.on('update-settings', () => { castToReceiver(); }); -function nextAng() { +const nextAng = () => { const next = apvCur.PageNo + 1; $apvObserver.unobserve($apvObserving); showAng(next, apvCur.SourceID); global.platform.ipc.send('next-ang', { PageNo: next, SourceID: apvCur.SourceID }); -} +}; -function createAPVContainer() { +const createAPVContainer = () => { if (!$apv) { $apv = document.createElement('div'); $apv.id = 'apv'; @@ -165,7 +165,7 @@ function createAPVContainer() { hideDecks(); $apv.classList.add('active'); } -} +}; const iconsetHtml = (classname, content) => { let icons; @@ -188,50 +188,48 @@ const iconsetHtml = (classname, content) => { return icons; }; -function createCards(rows, LineID) { - return new Promise((resolve) => { - if (rows.length > 0) { - const cards = []; - const lines = []; - const shabad = {}; - rows.forEach((row) => { - lines.push(row.ID); - const gurmukhiShabads = row.Gurmukhi.split(' '); - const taggedGurmukhi = []; - gurmukhiShabads.forEach((val, index) => { - if (val.indexOf(']') !== -1) { - taggedGurmukhi[index - 1] = `${taggedGurmukhi[index - 1]} ${val}`; - } else { - taggedGurmukhi[index] = val; - } - }); - const gurmukhiContainer = document.createElement('div'); - - gurmukhiContainer.innerHTML = `${taggedGurmukhi.join(' ')} - ${taggedGurmukhi.join('')}`; - cards.push( - h( - `div#slide${row.ID}.slide${row.ID === LineID ? '.active' : ''}`, - [ - h('h1.gurbani.gurmukhi', gurmukhiContainer), - h('h2.translation', row.English), - h('h2.teeka', row.PunjabiUni), - h('h2.transliteration', row.Transliteration), - ])); - shabad[row.ID] = { - gurmukhi: row.Gurmukhi, - larivaar: taggedGurmukhi.join(''), - translation: row.English, - teeka: row.Punjabi, - transliteration: row.Transliteration, - }; +const createCards = (rows, LineID) => new Promise((resolve) => { + if (rows.length > 0) { + const cards = []; + const lines = []; + const shabad = {}; + rows.forEach((row) => { + lines.push(row.ID); + const gurmukhiShabads = row.Gurmukhi.split(' '); + const taggedGurmukhi = []; + gurmukhiShabads.forEach((val, index) => { + if (val.indexOf(']') !== -1) { + taggedGurmukhi[index - 1] = `${taggedGurmukhi[index - 1]} ${val}`; + } else { + taggedGurmukhi[index] = val; + } }); - resolve({ cards, lines, shabad }); - } - }); -} + const gurmukhiContainer = document.createElement('div'); + + gurmukhiContainer.innerHTML = `${taggedGurmukhi.join(' ')} + ${taggedGurmukhi.join('')}`; + cards.push( + h( + `div#slide${row.ID}.slide${row.ID === LineID ? '.active' : ''}`, + [ + h('h1.gurbani.gurmukhi', gurmukhiContainer), + h('h2.translation', row.English), + h('h2.teeka', row.PunjabiUni), + h('h2.transliteration', row.Transliteration), + ])); + shabad[row.ID] = { + gurmukhi: row.Gurmukhi, + larivaar: taggedGurmukhi.join(''), + translation: row.English, + teeka: row.Punjabi, + transliteration: row.Transliteration, + }; + }); + resolve({ cards, lines, shabad }); + } +}); -function createDeck(cards, curSlide, shabad, ShabadID) { +const createDeck = (cards, curSlide, shabad, ShabadID) => { document.querySelector('.vc-toggle-icon').style.left = '0'; hideDecks(); if (document.querySelector('.vc-open')) { @@ -243,10 +241,10 @@ function createDeck(cards, curSlide, shabad, ShabadID) { currentShabad = parseInt(ShabadID, 10); decks[ShabadID] = shabad; castShabadLine(curSlide); -} +}; -function showAng(PageNo, SourceID, LineID) { - global.platform.search.loadAng(PageNo, SourceID) +const showAng = (PageNo, SourceID, LineID) => { + loadAng(PageNo, SourceID) .then(res => createCards(res, LineID)) .then(({ cards, lines }) => { apvCur.PageNo = PageNo; @@ -263,9 +261,9 @@ function showAng(PageNo, SourceID, LineID) { setTimeout(() => smoothScroll(`#apv #slide${LineID}`), 100); } }); -} +}; -function smoothScroll(pos = 0) { +const smoothScroll = (pos = 0) => { let newScrollPos; switch (typeof pos) { case 'object': @@ -279,9 +277,9 @@ function smoothScroll(pos = 0) { break; } scroll.top($body, newScrollPos); -} +}; -function showLine(ShabadID, LineID) { +const showLine = (ShabadID, LineID) => { if (!global.platform.db) { global.platform.initDB(); } @@ -289,7 +287,7 @@ function showLine(ShabadID, LineID) { if (apv) { createAPVContainer(); if (!apvCur.ShabadID || apvCur.ShabadID !== ShabadID) { - global.platform.search.getAng(ShabadID) + getAng(ShabadID) .then(ang => showAng(ang.PageNo, ang.SourceID, LineID)); apvCur.ShabadID = ShabadID; } else { @@ -309,16 +307,13 @@ function showLine(ShabadID, LineID) { smoothScroll(line); castShabadLine(LineID); } else { - Realm.open(realmDB.realmVerseSchema) - .then((realm) => { - const rows = realm.objects('Verse').filtered('ANY Shabads.ShabadID = $0', newShabadID); - createCards(rows, LineID) - .then(({ cards, shabad }) => createDeck(cards, LineID, shabad, newShabadID)); - }); + loadShabad(newShabadID) + .then(rows => createCards(rows, LineID)) + .then(({ cards, shabad }) => createDeck(cards, LineID, shabad, newShabadID)); } -} +}; -function showText(text, isGurmukhi = false) { +const showText = (text, isGurmukhi = false) => { hideDecks(); $message.classList.add('active'); while ($message.firstChild) { @@ -327,9 +322,9 @@ function showText(text, isGurmukhi = false) { const textNode = isGurmukhi ? h('h1.gurmukhi.gurbani', text) : h('h1.gurbani', text); $message.appendChild(h('div.slide.active', textNode)); castText(text, isGurmukhi); -} +}; -function toggleSideMenu() { +const toggleSideMenu = () => { Array.from(document.querySelectorAll('.vc-toggle-icon i')).forEach((el) => { el.classList.toggle('vc-icon-hidden'); }); @@ -337,6 +332,6 @@ function toggleSideMenu() { el.classList.toggle('vc-open'); }); document.querySelector('.viewer-controls').classList.toggle('viewer-controls-open'); -} +}; document.querySelector('.vc-toggle-icon').onclick = toggleSideMenu;