From 5a70de65d4e689d7e246b565c306b809d417f6b7 Mon Sep 17 00:00:00 2001 From: Kumar Priyansh Date: Sat, 18 Jul 2020 01:28:03 +0530 Subject: Add/Update Chapters Function --- src/views/templates/blank/template.html | 2 +- src/views/templates/blank/template.js | 46 +++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/views/templates/blank/template.html b/src/views/templates/blank/template.html index a1d8477..b41acac 100644 --- a/src/views/templates/blank/template.html +++ b/src/views/templates/blank/template.html @@ -15,7 +15,7 @@

Blank Template

- diff --git a/src/views/templates/blank/template.js b/src/views/templates/blank/template.js index d20f8ea..d45edc3 100644 --- a/src/views/templates/blank/template.js +++ b/src/views/templates/blank/template.js @@ -4,24 +4,49 @@ const MenuItem = remote.MenuItem; const { ipcRenderer } = require('electron'); const chapterList = ['Chapterfl1']; -const chapterCount = chapterList.length; +const currentActiveChapter = 0; +let chapterNameCount = 1; $(document).ready(function () { updateChapters(); + setActiveChapter(currentActiveChapter); }); function updateChapters() { + + if (!chapterList.length) { + addChapter(); + } + $('#chapters').empty(); chapterList.forEach(chapter => { const chapterName = chapter.replace(/fl/g, ' '); - $('#chapters').append(`${chapterName}`) + $('#chapters').append(`${chapterName}`) }); + + setActiveChapter(currentActiveChapter); +} + +function setActiveChapter(currentActiveChapter) { + const chapter = chapterList[currentActiveChapter]; + $(`#${chapter}`).addClass('active'); +} + +function addChapter() { + const chapterCount = chapterList.length; + chapterList[chapterCount] = `Chapterfl${chapterNameCount+1}`; + chapterNameCount++; + updateChapters(); } +$('#addChapter').click(() => { + addChapter(); +}) + let currentSelectedChapterForContextAction = null; let rightClickPosition = null; const chapterMenu = new Menu() -const chapterContextItems = new MenuItem({ +const renameChapterMenuItem = new MenuItem({ label: 'Rename Chapter', click: (e) => { const currentChapter = currentSelectedChapterForContextAction.replace(/fl/g, ' '); @@ -54,8 +79,19 @@ const chapterContextItems = new MenuItem({ updateChapters(); }); } -}) -chapterMenu.append(chapterContextItems); +}); + +const deleteChapterMenuItem = new MenuItem({ + label: 'Delete Chapter', + click: (e) => { + const index = chapterList.findIndex(chapter => chapter === currentSelectedChapterForContextAction); + chapterList.splice(index, 1); + updateChapters(); + } +}); + +chapterMenu.append(renameChapterMenuItem); +chapterMenu.append(deleteChapterMenuItem); $(document).on('contextmenu', '.chapter_navigation', (e) => { e.preventDefault(); -- cgit v1.2.3