mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-22 11:37:56 -04:00
Merge pull request #135 from kiwix/delete-button
Delete books in a custom contextmenu
This commit is contained in:
commit
399a6cb4d5
@ -74,10 +74,31 @@ function init() {
|
|||||||
downloadUpdaters[book.id] = setInterval(function() { getDownloadInfo(book.id); }, 1000);
|
downloadUpdaters[book.id] = setInterval(function() { getDownloadInfo(book.id); }, 1000);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
eraseBook : function(book) {
|
||||||
|
contentManager.eraseBook(book.id);
|
||||||
|
},
|
||||||
displayedBooks : function(books, nb) {
|
displayedBooks : function(books, nb) {
|
||||||
var a = books.slice(0, nb);
|
var a = books.slice(0, nb);
|
||||||
return a;
|
return a;
|
||||||
},
|
},
|
||||||
|
getBookFromMousePosition : function() {
|
||||||
|
var elements = document.elementsFromPoint(mouseX, mouseY);
|
||||||
|
var bookId = null;
|
||||||
|
for(var i = 0; i < elements.length; i++) {
|
||||||
|
if (elements[i].localName == "summary" && elements[i].classList.contains("book-summary")) {
|
||||||
|
bookId = elements[i].id;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var book = null;
|
||||||
|
for(var i = 0; i < app.books.length; i++) {
|
||||||
|
if (app.books[i]["id"] == bookId) {
|
||||||
|
book = app.books[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return book;
|
||||||
|
},
|
||||||
niceBytes : niceBytes
|
niceBytes : niceBytes
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -97,6 +118,39 @@ function scrolled(e) {
|
|||||||
app.displayedBooksNb = Math.min(app.displayedBooksNb+20, app.books.length);
|
app.displayedBooksNb = Math.min(app.displayedBooksNb+20, app.books.length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
window.addEventListener("click", e => {
|
||||||
|
if (menuVisible)
|
||||||
|
displayMenu(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
var mouseX, mouseY = 0;
|
||||||
|
window.addEventListener("contextmenu", e => {
|
||||||
|
e.preventDefault();
|
||||||
|
mouseX = e.pageX;
|
||||||
|
mouseY = e.pageY;
|
||||||
|
setContextMenuPosition();
|
||||||
|
var book = app.getBookFromMousePosition();
|
||||||
|
displayMenu(book);
|
||||||
|
});
|
||||||
|
|
||||||
|
var menuVisible = false;
|
||||||
|
function displayMenu(book) {
|
||||||
|
var menu = document.getElementById("menu");
|
||||||
|
menu.style.display = (book) ? "block" : "none";
|
||||||
|
menuVisible = (book) ? true : false;
|
||||||
|
if (!book)
|
||||||
|
return;
|
||||||
|
var localElement = document.getElementsByClassName("local-option")[0];
|
||||||
|
localElement.style.display = (book.path) ? "block" : "none";
|
||||||
|
};
|
||||||
|
|
||||||
|
function setContextMenuPosition() {
|
||||||
|
var menu = document.getElementById("menu");
|
||||||
|
menu.style.left = `${mouseX}px`;
|
||||||
|
menu.style.top = `${mouseY}px`;
|
||||||
|
};
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
<style>
|
<style>
|
||||||
html, body {
|
html, body {
|
||||||
@ -204,6 +258,33 @@ details:hover {
|
|||||||
background-color: #d9e9ff;
|
background-color: #d9e9ff;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.menu {
|
||||||
|
/* width: 120px; */
|
||||||
|
box-shadow: 0 4px 5px 3px rgba(0, 0, 0, 0.2);
|
||||||
|
position: fixed;
|
||||||
|
display: none;
|
||||||
|
z-index: 99999999999999;
|
||||||
|
background-color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-options {
|
||||||
|
list-style: none;
|
||||||
|
padding: 0 0 0 0;
|
||||||
|
margin: 2px 0px 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-option {
|
||||||
|
font-weight: 500;
|
||||||
|
font-size: 14px;
|
||||||
|
padding: 10px 40px 10px 20px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-option:hover {
|
||||||
|
background: rgba(0, 0, 0, 0.2);
|
||||||
|
}
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body onload="init()">
|
<body onload="init()">
|
||||||
@ -223,8 +304,13 @@ details:hover {
|
|||||||
<span class="tablecell cell5"></span>
|
<span class="tablecell cell5"></span>
|
||||||
</div>
|
</div>
|
||||||
<div id="bookList" onscroll=scrolled(this)>
|
<div id="bookList" onscroll=scrolled(this)>
|
||||||
|
<div id="menu" class="menu">
|
||||||
|
<ul class="menu-options local-option">
|
||||||
|
<li v-on:click="eraseBook(getBookFromMousePosition())" class="menu-option">Delete</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
<details v-for="book in displayedBooks(books, displayedBooksNb)" class="book">
|
<details v-for="book in displayedBooks(books, displayedBooksNb)" class="book">
|
||||||
<summary class="tablerow">
|
<summary v-bind:id="book.id" class="tablerow book-summary">
|
||||||
<span class="tablecell cell0">
|
<span class="tablecell cell0">
|
||||||
<img v-if="book.faviconUrl" v-bind:src="'http://' + book.faviconUrl" />
|
<img v-if="book.faviconUrl" v-bind:src="'http://' + book.faviconUrl" />
|
||||||
<img v-else-if="book.faviconMimeType" v-bind:src="'zim://' + book.id + '.favicon.meta'" />
|
<img v-else-if="book.faviconMimeType" v-bind:src="'zim://' + book.id + '.favicon.meta'" />
|
||||||
|
@ -25,7 +25,6 @@ ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader,
|
|||||||
connect(this, &ContentManager::filterParamsChanged, this, &ContentManager::updateLibrary);
|
connect(this, &ContentManager::filterParamsChanged, this, &ContentManager::updateLibrary);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ContentManager::setLocal(bool local) {
|
void ContentManager::setLocal(bool local) {
|
||||||
if (local == m_local) {
|
if (local == m_local) {
|
||||||
return;
|
return;
|
||||||
@ -185,10 +184,23 @@ QString ContentManager::downloadBook(const QString &id)
|
|||||||
mp_library->addBookToLibrary(book);
|
mp_library->addBookToLibrary(book);
|
||||||
mp_library->save();
|
mp_library->save();
|
||||||
emit(mp_library->booksChanged());
|
emit(mp_library->booksChanged());
|
||||||
emit(booksChanged());
|
|
||||||
return QString::fromStdString(download->getDid());
|
return QString::fromStdString(download->getDid());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ContentManager::eraseBook(const QString& id)
|
||||||
|
{
|
||||||
|
kiwix::Book book = mp_library->getBookById(id);
|
||||||
|
QString dirName = QString::fromUtf8(getDataDirectory().c_str());
|
||||||
|
QString fileSelection = QString::fromUtf8(getLastPathElement(book.getPath()).c_str()) + "*";
|
||||||
|
QDir dir(dirName, fileSelection);
|
||||||
|
for(const QString& filename: dir.entryList()) {
|
||||||
|
dir.remove(filename);
|
||||||
|
}
|
||||||
|
mp_library->removeBookFromLibraryById(id);
|
||||||
|
mp_library->save();
|
||||||
|
emit(mp_library->booksChanged());
|
||||||
|
}
|
||||||
|
|
||||||
QStringList ContentManager::getDownloadIds()
|
QStringList ContentManager::getDownloadIds()
|
||||||
{
|
{
|
||||||
QStringList list;
|
QStringList list;
|
||||||
|
@ -51,6 +51,7 @@ public slots:
|
|||||||
QString downloadBook(const QString& id);
|
QString downloadBook(const QString& id);
|
||||||
void updateLibrary();
|
void updateLibrary();
|
||||||
void setSearch(const QString& search);
|
void setSearch(const QString& search);
|
||||||
|
void eraseBook(const QString& id);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CONTENTMANAGER_H
|
#endif // CONTENTMANAGER_H
|
||||||
|
@ -10,6 +10,7 @@ ContentManagerView::ContentManagerView(QWidget *parent)
|
|||||||
auto profile = page()->profile();
|
auto profile = page()->profile();
|
||||||
auto app = KiwixApp::instance();
|
auto app = KiwixApp::instance();
|
||||||
profile->installUrlSchemeHandler("zim", app->getSchemeHandler());
|
profile->installUrlSchemeHandler("zim", app->getSchemeHandler());
|
||||||
|
setContextMenuPolicy( Qt::NoContextMenu );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -113,6 +113,10 @@ void Library::addBookToLibrary(kiwix::Book &book)
|
|||||||
m_library.addBook(book);
|
m_library.addBook(book);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Library::removeBookFromLibraryById(const QString& id) {
|
||||||
|
m_library.removeBookById(id.toStdString());
|
||||||
|
}
|
||||||
|
|
||||||
void Library::addBookmark(kiwix::Bookmark &bookmark)
|
void Library::addBookmark(kiwix::Bookmark &bookmark)
|
||||||
{
|
{
|
||||||
m_library.addBookmark(bookmark);
|
m_library.addBookmark(bookmark);
|
||||||
|
@ -32,6 +32,7 @@ public:
|
|||||||
QStringList listBookIds(const QString& query, const QString &categoryFilter);
|
QStringList listBookIds(const QString& query, const QString &categoryFilter);
|
||||||
const std::vector<kiwix::Bookmark>& getBookmarks() { return m_library.getBookmarks(); }
|
const std::vector<kiwix::Bookmark>& getBookmarks() { return m_library.getBookmarks(); }
|
||||||
void addBookToLibrary(kiwix::Book& book);
|
void addBookToLibrary(kiwix::Book& book);
|
||||||
|
void removeBookFromLibraryById(const QString& id);
|
||||||
void addBookmark(kiwix::Bookmark& bookmark);
|
void addBookmark(kiwix::Bookmark& bookmark);
|
||||||
void removeBookmark(const QString& zimId, const QString& url);
|
void removeBookmark(const QString& zimId, const QString& url);
|
||||||
void save();
|
void save();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user