From 9a7306b2656e0fe10c4cca1e107d07d763fb2eac Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Fri, 6 Nov 2020 11:59:23 +0100
Subject: [PATCH] Partial results (limit of 15 for the moment) and using ejs in
 instance side

---
 common/db.js                    | 31 +++++++++++++++++++------------
 instance/main.js                | 26 ++++++++++++--------------
 instance/views/karaListItem.ejs | 14 ++++++++++++++
 main.js                         | 20 +++++++++-----------
 4 files changed, 54 insertions(+), 37 deletions(-)
 create mode 100644 instance/views/karaListItem.ejs

diff --git a/common/db.js b/common/db.js
index 03a4026..6d8b1a3 100644
--- a/common/db.js
+++ b/common/db.js
@@ -57,17 +57,21 @@ class KaraDatabase {
 
     /* Search from the kara table, a string. Can be anything (query, name,
      * language, source, etc).
-     * - queryString: String => a promize, callback with [(id: Int, string,
-     *   cat, type, language, author, title, source)] */
-    search(queryString) {
+     *  - 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 = [];
-        let __sqlQuery = `SELECT id, string,
+        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
                           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 => {
@@ -92,16 +96,19 @@ class KaraDatabase {
     }
 
     /* List all the kara in the db.
-     * => a promize, callback with [(id: Int, string, cat, type, language,
-     *    author, title, source)] */
-    all() {
+     *  - 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)] */
+    all(offset = null, limit = null) {
         var __ret = [];
-        let __sqlQuery = `SELECT id, string,
+        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
-                          FROM kara;`;
+                          FROM kara`;
+        if (offset != null && limit != null)
+            __sqlQuery += ` LIMIT ${offset}, ${limit}`;
 
         function __getRecords(db) {
             return new Promise(resolv => {
@@ -122,9 +129,9 @@ class KaraDatabase {
     }
 
     /* List all the next karas in the queue.
-     * - first: Integer => the first kara to consider being in the queue (the
-     *   current one, starts ad 0)
-     * - count: Integer => the number of karas of the queue to load.
+     * -  first: Integer => the first kara to consider being in the queue (the
+     *    current one, starts ad 0)
+     * -  count: Integer => the number of karas of the queue to load.
      * => a promize, callback with [(id: Int, string, cat, type, language,
      *    author, title, source, position: Int)] */
     queue(first, count) {
diff --git a/instance/main.js b/instance/main.js
index 38d8b0c..e809080 100644
--- a/instance/main.js
+++ b/instance/main.js
@@ -1,4 +1,5 @@
 const { ipcRenderer } = require('electron');
+const ejs = require('ejs');
 const logger = require('../common/logger.js');
 
 /* prettier-ignore */
@@ -103,23 +104,20 @@ function createButtonList(list) {
 
 /* Create the left panel */
 ipcRenderer.on('reload-db-responce', (event, arg) => {
-    var karaList = '';
     logger.debug('instance', `Web page got reload-db`);
+    document.getElementById('panelLeft').innerHTML = '';
     arg.forEach(kara => {
-        karaList += `<li class="card p-2 bd-highlight shadow-none d-flex flex-row bd-highlight mb-3 karaCard">
-            <span class="karaElement text-uppercase p-2 bd-highlight badge badge-light"><b>${kara.language}</b></span>
-            <span class="karaElement text-uppercase p-2 bd-highlight badge badge-light"><b>${kara.cat}</b></span>
-            <span class="karaElement text-uppercase p-2 bd-highlight badge badge-light"><b>${kara.type}</b></span>
-            <span class="karaElement p-2 bd-highlight mr-auto"><b>${kara.source}<br> ${kara.title}</b> <i>[${kara.author}]</i></span>
-            <div class="karaElement p-2 bd-highlight">
-                <div class="d-flex flex-row bd-highlight mb-3 btn-group karaActionBtnGroup" role="group">
-                    <button class="btn btn-outline-light karaActionBtn"><i class="fas fa-plus"></i></button>
-                    <button class="btn btn-outline-light karaActionBtn"><i class="fas fa-level-up-alt"></i></button>
-                </div>
-            </div>
-            </li>`;
+        ejs.renderFile(
+            __dirname + '/views/karaListItem.ejs',
+            { kara: kara },
+            (err, data) => {
+                if (err) logger.error('instance', err);
+                document.getElementById('panelLeft').innerHTML = `
+                ${document.getElementById('panelLeft').innerHTML}
+                ${data}`;
+            }
+        );
     });
-    document.getElementById('panelLeft').innerHTML = karaList;
 });
 
 /* Create the right panel: the queue */
diff --git a/instance/views/karaListItem.ejs b/instance/views/karaListItem.ejs
new file mode 100644
index 0000000..8ec6e77
--- /dev/null
+++ b/instance/views/karaListItem.ejs
@@ -0,0 +1,14 @@
+<!-- vim: ts=4 syntax=html
+     The template for the kara card in lists --!>
+<li class="card p-2 bd-highlight shadow-none d-flex flex-row bd-highlight mb-3 karaCard">
+    <span class="karaElement text-uppercase p-2 bd-highlight badge badge-light"><b><%= kara.language %></b></span>
+    <span class="karaElement text-uppercase p-2 bd-highlight badge badge-light"><b><%= kara.cat      %></b></span>
+    <span class="karaElement text-uppercase p-2 bd-highlight badge badge-light"><b><%= kara.type     %></b></span>
+    <span class="karaElement p-2 bd-highlight mr-auto"><b><%= kara.source %><br> <%= kara.title %></b> <i>[<%= kara.author %>]</i></span>
+    <div class="karaElement p-2 bd-highlight">
+        <div class="d-flex flex-row bd-highlight mb-3 btn-group karaActionBtnGroup" role="group">
+            <button class="btn btn-outline-light karaActionBtn"><i class="fas fa-plus"></i></button>
+            <button class="btn btn-outline-light karaActionBtn"><i class="fas fa-level-up-alt"></i></button>
+        </div>
+    </div>
+</li>
diff --git a/main.js b/main.js
index 8443950..9fdaefb 100644
--- a/main.js
+++ b/main.js
@@ -1,12 +1,10 @@
-const logger = require.main.require('./common/logger.js');
-const electron = require('electron');
-const { app, BrowserWindow } = require('electron');
-const { globalShortcut } = require('electron');
-const { ipcMain } = require('electron');
-const { fork, spawn } = require('child_process');
-const fs = require('fs');
-const db = require.main.require('./common/db.js');
-const lkt = require.main.require('./common/lkt.js');
+const logger = require.main.require('./common/logger.js'),
+    electron = require('electron'),
+    { app, BrowserWindow, globalShortcut, ipcMain } = require('electron'),
+    { fork, spawn } = require('child_process'),
+    fs = require('fs'),
+    db = require.main.require('./common/db.js'),
+    lkt = require.main.require('./common/lkt.js');
 var tail = require('tail').Tail;
 var client; /* Sub process for the express server */
 var lektor; /* Sub process, the lektord player */
@@ -163,9 +161,9 @@ ipcMain.on('reload-db-request', (event, arg) => {
     };
     if (arg) {
         logger.debug('main', `Reload DB with search '${arg}'`);
-        myDb.search(arg).then(callback);
+        myDb.search(arg, 0, 15).then(callback);
     } else {
-        myDb.all().then(callback);
+        myDb.all(0, 15).then(callback);
     }
 });
 
-- 
GitLab