| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2017, 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 dom import add_extra_css, build_rule, clear, svgicon |
| 9 |
from iframe_comm import create_wrapped_iframe |
| 10 |
from read_book.globals import runtime, ui_operations, is_dark_theme |
| 11 |
from read_book.resources import load_resources |
| 12 |
|
| 13 |
CLASS_NAME = 'book-content-popup-container' |
| 14 |
TOP_LEVEL_DISPLAY = 'flex' |
| 15 |
|
| 16 |
add_extra_css(def (): |
| 17 |
sel = '.' + CLASS_NAME |
| 18 |
style = '' |
| 19 |
style += build_rule(sel, justify_content='center', align_items='center', height='100%') |
| 20 |
sel += ' > div' |
| 21 |
style += build_rule(sel, border_radius='8px', border='solid currentColor 2px', margin='1rem', padding='0.5rem', box_shadow='2px 2px 4px currentColor') |
| 22 |
sel += ' > div' |
| 23 |
style += build_rule(sel, display='flex', justify_content='space-between', align_items='center') |
| 24 |
sel += ' > div' # button container |
| 25 |
style += build_rule(sel, display='flex', justify_content='space-between', align_items='center') |
| 26 |
sel += ' > a' # buttons |
| 27 |
style += build_rule(sel, margin_left='1ex', cursor='pointer', display='inline-block') |
| 28 |
style += build_rule(sel + ':hover', transform='scale(1.5)') |
| 29 |
style += build_rule(sel + ':active', transform='scale(2)') |
| 30 |
return style |
| 31 |
) |
| 32 |
|
| 33 |
|
| 34 |
class ContentPopupOverlay: |
| 35 |
def __init__(self, view): |
| 36 |
self.view = view |
| 37 |
self.loaded_resources = {} |
| 38 |
c = self.container |
| 39 |
c.classList.add(CLASS_NAME) |
| 40 |
c.appendChild(E.div(E.div())) |
| 41 |
c.addEventListener('click', self.hide) |
| 42 |
c.firstChild.addEventListener('click', def (ev): |
| 43 |
ev.stopPropagation(), ev.preventDefault() |
| 44 |
) |
| 45 |
self.pending_load = None |
| 46 |
|
| 47 |
def reset(self): |
| 48 |
if self.iframe_wrapper: |
| 49 |
self.iframe_wrapper.reset() |
| 50 |
|
| 51 |
def create_iframe(self): |
| 52 |
handlers = { |
| 53 |
'ready': self.on_iframe_ready, |
| 54 |
'error': self.view.on_iframe_error, |
| 55 |
'content_loaded': self.on_content_loaded, |
| 56 |
'print': self.on_print, |
| 57 |
'link_activated': self.on_link_activated, |
| 58 |
'close_requested': self.on_close_requested, |
| 59 |
} |
| 60 |
iframe_kw = { |
| 61 |
'seamless': True, 'sandbox': 'allow-scripts', 'style': 'width: 100%; max-height: 70vh' |
| 62 |
} |
| 63 |
if runtime.is_standalone_viewer: |
| 64 |
entry_point = f'{runtime.FAKE_PROTOCOL}://{runtime.SANDBOX_HOST}/book/__popup__' |
| 65 |
else: |
| 66 |
entry_point = 'read_book.footnotes' |
| 67 |
iframe, self.iframe_wrapper = create_wrapped_iframe( |
| 68 |
handlers, _('Loading data, please wait...'), entry_point, iframe_kw |
| 69 |
) |
| 70 |
iframe.style.colorScheme = 'dark' if is_dark_theme() else 'light' |
| 71 |
c = self.container |
| 72 |
c.firstChild.appendChild(iframe) |
| 73 |
|
| 74 |
def on_print(self, data): |
| 75 |
print(data.string) |
| 76 |
|
| 77 |
def on_link_activated(self, data): |
| 78 |
self.view.link_in_content_popup_activated(data.name, data.frag, data.is_popup, data.title) |
| 79 |
|
| 80 |
def on_close_requested(self, data): |
| 81 |
self.hide() |
| 82 |
|
| 83 |
@property |
| 84 |
def container(self): |
| 85 |
return document.getElementById('book-content-popup-overlay') |
| 86 |
|
| 87 |
@property |
| 88 |
def iframe(self): |
| 89 |
return self.iframe_wrapper.iframe |
| 90 |
|
| 91 |
@property |
| 92 |
def is_visible(self): |
| 93 |
return self.container.style.display is not 'none' |
| 94 |
|
| 95 |
def hide(self): |
| 96 |
self.container.style.display = 'none' |
| 97 |
ui_operations.focus_iframe() |
| 98 |
|
| 99 |
def show(self): |
| 100 |
c = self.container |
| 101 |
c.style.display = TOP_LEVEL_DISPLAY |
| 102 |
|
| 103 |
def on_iframe_ready(self, msg): |
| 104 |
return self.do_pending_load() |
| 105 |
|
| 106 |
def apply_color_scheme(self, bg, fg): |
| 107 |
c = self.container.firstChild |
| 108 |
c.style.backgroundColor = bg |
| 109 |
c.style.color = fg |
| 110 |
try: |
| 111 |
self.iframe.style.colorScheme = 'dark' if is_dark_theme() else 'light' |
| 112 |
except: |
| 113 |
pass |
| 114 |
|
| 115 |
def create_footnote_header(self, header): |
| 116 |
clear(header) |
| 117 |
header.appendChild( |
| 118 |
E.h3(self.current_footnote_data.title or _('Footnote')), |
| 119 |
) |
| 120 |
bc = E.div( |
| 121 |
E.a(svgicon('arrow-right'), title=_('Go to this footnote in the main view'), href='javascript:void(0)'), |
| 122 |
E.a(svgicon('close'), title=_('Close the footnotes window'), href='javascript:void(0)') |
| 123 |
) |
| 124 |
bc.firstChild.addEventListener('click', def (): |
| 125 |
self.hide() |
| 126 |
self.view.goto_named_destination(self.current_footnote_data.name, self.current_footnote_data.frag) |
| 127 |
) |
| 128 |
bc.lastChild.addEventListener('click', self.hide) |
| 129 |
header.appendChild(bc) |
| 130 |
|
| 131 |
def load_doc(self, name, done_callback): |
| 132 |
def cb(resource_data): |
| 133 |
self.loaded_resources = resource_data |
| 134 |
done_callback(resource_data) |
| 135 |
load_resources(self.view.book, name, self.loaded_resources, cb) |
| 136 |
|
| 137 |
def show_footnote(self, data): |
| 138 |
if not self.iframe_wrapper: |
| 139 |
self.create_iframe() |
| 140 |
self.current_footnote_data = data |
| 141 |
c = self.container.firstChild |
| 142 |
header = c.firstChild |
| 143 |
s = header.style |
| 144 |
s.paddingLeft = s.paddingRight = s.paddingBottom = s.paddingTop = '0' |
| 145 |
s.marginLeft = s.marginRight = s.marginBottom = s.marginTop = '0' |
| 146 |
s.borderBottom = s.borderTop = s.borderLeft = s.borderRight = 'solid currentColor 0' |
| 147 |
bs = 'solid currentColor 2px' |
| 148 |
if self.current_footnote_data.vertical_writing_mode: |
| 149 |
c.style.width = str(50 // data.cols_per_screen) + 'vw' |
| 150 |
self.iframe.style.height = '80vh' |
| 151 |
c.style.writingMode = 'vertical-rl' if self.current_footnote_data.rtl else 'vertical-lr' |
| 152 |
if self.current_footnote_data.rtl: |
| 153 |
s.paddingLeft = s.marginLeft = '1ex' |
| 154 |
s.borderLeft = bs |
| 155 |
else: |
| 156 |
s.paddingRight = s.marginRight = '1ex' |
| 157 |
s.borderRight = bs |
| 158 |
else: |
| 159 |
c.style.width = str(100 // data.cols_per_screen) + 'vw' |
| 160 |
self.iframe.style.height = '12ex' |
| 161 |
c.style.writingMode = 'horizontal-rl' if self.current_footnote_data.rtl else 'horizontal-lr' |
| 162 |
s.paddingBottom = s.marginBottom = '1ex' |
| 163 |
s.borderBottom = bs |
| 164 |
self.create_footnote_header(header) |
| 165 |
self.load_doc(data.name, self.show_footnote_item) |
| 166 |
self.iframe_wrapper.send_message('clear', text=_('Loading note, please wait...')) |
| 167 |
|
| 168 |
def show_footnote_item(self, resource_data): |
| 169 |
self.pending_load = resource_data, self.show_footnote_item_stage2 |
| 170 |
if self.iframe_wrapper.ready: |
| 171 |
self.do_pending_load() |
| 172 |
else: |
| 173 |
self.iframe_wrapper.init() |
| 174 |
|
| 175 |
def do_pending_load(self): |
| 176 |
if self.pending_load: |
| 177 |
data, func = self.pending_load |
| 178 |
self.pending_load = None |
| 179 |
func(data) |
| 180 |
|
| 181 |
def show_footnote_item_stage2(self, resource_data): |
| 182 |
self.iframe_wrapper.send_unencrypted_message('display', |
| 183 |
resource_data=resource_data, book=self.view.book, name=self.current_footnote_data.name, |
| 184 |
frag=self.current_footnote_data.frag, settings=self.view.currently_showing.settings) |
| 185 |
|
| 186 |
def on_content_loaded(self, data): |
| 187 |
self.iframe.style.height = f'{data.height}px' |