Make the download button a direct download link

This commit is contained in:
Kovid Goyal 2017-04-12 11:26:57 +05:30
parent 8ee6f735d2
commit b0e2cb8452
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 10 additions and 4 deletions

View File

@ -351,8 +351,9 @@ def render_book(container_id, book_id):
))
container = c.lastChild.firstChild
read_button = create_button(_('Read'), 'book', read_book.bind(None, book_id), _('Read this book'))
download_button = create_button(_('Download'), 'cloud-download', download_book.bind(None, book_id),
_('Download this book in the {} format').format(preferred_format(book_id)))
fmt = preferred_format(book_id)
download_button = create_button(_('Download'), 'cloud-download', download_url(book_id, fmt),
_('Download this book in the {} format').format(fmt), download_filename=f'{metadata.title}.{fmt.toLowerCase()}')
row = E.div(read_button, '\xa0\xa0\xa0', download_button, style='margin-bottom: 1ex')
if not metadata.formats or not metadata.formats.length:
row.style.display = 'none'

View File

@ -9,14 +9,19 @@ from book_list.theme import get_color
# Button {{{
def create_button(text, icon=None, action=None, tooltip=None, highlight=False):
def create_button(text, icon=None, action=None, tooltip=None, highlight=False, download_filename=None):
ic = ''
if icon:
ic = svgicon(icon)
text = '\xa0' + text
ans = E.a(ic, E.span(text), class_='calibre-push-button', href='javascript: void(0)', title=tooltip or '')
if download_filename and v'"download" in ans':
ans.setAttribute('download', download_filename)
if action is not None:
ans.addEventListener('click', def(event): event.preventDefault(), action(event);)
if jstype(action) is 'string':
ans.setAttribute('href', action)
else:
ans.addEventListener('click', def(event): event.preventDefault(), action(event);)
if highlight:
set_css(ans, font_size='larger', font_weight='bold')
return ans