| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import bound_methods, hash_literals |
| 4 |
|
| 5 |
from elementmaker import E |
| 6 |
from gettext import gettext as _ |
| 7 |
|
| 8 |
from book_list.globals import get_session_data |
| 9 |
from book_list.item_list import create_item, create_item_list |
| 10 |
from dom import clear, unique_id |
| 11 |
from read_book.globals import ui_operations |
| 12 |
from widgets import create_button |
| 13 |
|
| 14 |
current_container_id = None |
| 15 |
current_opened_book_path = None |
| 16 |
|
| 17 |
|
| 18 |
def create_open_book(container, book): |
| 19 |
nonlocal current_container_id, current_opened_book_path |
| 20 |
container.appendChild(E.div(style='margin: 1rem', id=unique_id())) |
| 21 |
container = container.lastChild |
| 22 |
current_container_id = container.id |
| 23 |
current_opened_book_path = None |
| 24 |
if book: |
| 25 |
current_opened_book_path = book.manifest.pathtoebook |
| 26 |
rebuild_open_list(container) |
| 27 |
|
| 28 |
|
| 29 |
def rebuild_open_list(container): |
| 30 |
clear(container) |
| 31 |
container.appendChild(create_button(_('Open a book from your computer'), action=ui_operations.ask_for_open.bind(None, None))) |
| 32 |
sd = get_session_data() |
| 33 |
rl = sd.get('standalone_recently_opened') |
| 34 |
if rl?.length: |
| 35 |
container.appendChild(E.div(id=unique_id())) |
| 36 |
c = container.lastChild |
| 37 |
items = [] |
| 38 |
c.appendChild(E.h2(style='margin-top: 1rem', _('Recently viewed books'))) |
| 39 |
c.appendChild(E.div()) |
| 40 |
for entry in rl: |
| 41 |
if current_opened_book_path and current_opened_book_path is entry.pathtoebook: |
| 42 |
continue |
| 43 |
if jstype(entry.pathtoebook) is not 'string': |
| 44 |
continue |
| 45 |
fname = entry.pathtoebook.replace(/\\/g, '/') |
| 46 |
if '/' in fname: |
| 47 |
fname = fname.rpartition('/')[-1] |
| 48 |
items.push(create_item(entry.title or _('Unknown'), ui_operations.ask_for_open.bind(None, entry.pathtoebook), fname)) |
| 49 |
create_item_list(c.lastChild, items) |
| 50 |
|
| 51 |
c.appendChild(E.div(style='margin: 1rem')) |
| 52 |
c.lastChild.appendChild( |
| 53 |
create_button(_('Clear recent list'), action=clear_recent_list.bind(None, c.id))) |
| 54 |
|
| 55 |
|
| 56 |
def clear_recent_list(container_id): |
| 57 |
sd = get_session_data() |
| 58 |
sd.set('standalone_recently_opened', []) |
| 59 |
document.getElementById(container_id).style.display = 'none' |
| 60 |
|
| 61 |
|
| 62 |
def add_book_to_recently_viewed(book): |
| 63 |
sd = get_session_data() |
| 64 |
rl = sd.get('standalone_recently_opened') |
| 65 |
key = book.manifest.pathtoebook |
| 66 |
new_entry = { |
| 67 |
'key': key, 'pathtoebook': book.manifest.pathtoebook, |
| 68 |
'title': book.metadata.title, 'authors': book.metadata.authors, |
| 69 |
'timestamp': Date().toISOString(), |
| 70 |
} |
| 71 |
ans = [] |
| 72 |
ans.push(new_entry) |
| 73 |
for entry in rl: |
| 74 |
if entry.key is not key: |
| 75 |
ans.push(entry) |
| 76 |
sd.set('standalone_recently_opened', ans.slice(0, 25)) |
| 77 |
return ans |
| 78 |
|
| 79 |
|
| 80 |
def remove_recently_opened(path): |
| 81 |
sd = get_session_data() |
| 82 |
rl = sd.get('standalone_recently_opened') |
| 83 |
newl = [] |
| 84 |
if path: |
| 85 |
for entry in rl: |
| 86 |
if entry.key is not path: |
| 87 |
newl.push(entry) |
| 88 |
sd.set('standalone_recently_opened', newl) |
| 89 |
if current_container_id: |
| 90 |
container = document.getElementById(current_container_id) |
| 91 |
if container: |
| 92 |
rebuild_open_list(container) |