Refactor iframe comm code out of the read_book module

This commit is contained in:
Kovid Goyal 2018-03-18 11:24:08 +05:30
parent 40110b9977
commit dbcd096fbc
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
6 changed files with 30 additions and 21 deletions

View File

@ -18,7 +18,7 @@ LOADING_DOC = '''
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script type="text/javascript" id="bootstrap">
window.iframe_type = '__IFRAME_TYPE__'; // different in different iframes
window.iframe_entry_point = '__ENTRY_POINT__'; // different in different iframes
window.default_font_family = '__FONT__'; // from the theme
__SCRIPT__
end_script
@ -58,14 +58,14 @@ class Messenger:
class IframeWrapper:
def __init__(self, handlers, iframe, iframe_type, bootstrap_text):
def __init__(self, handlers, iframe, entry_point, bootstrap_text):
self.messenger = Messenger()
self.iframe_id = ensure_id(iframe, 'content-iframe')
self.needs_init = True
self.ready = False
self.encrypted_communications = False
self.srcdoc_created = False
self.iframe_type = iframe_type
self.entry_point = entry_point
self.bootstrap_text = bootstrap_text
self.handlers = {k: handlers[k] for k in handlers}
self.on_ready_handler = self.handlers.ready
@ -82,7 +82,7 @@ class IframeWrapper:
'BS': self.bootstrap_text,
'SCRIPT': iframe_js(),
'FONT': get_font_family(),
'IFRAME_TYPE': self.iframe_type,
'ENTRY_POINT': self.entry_point,
}
self.iframe.srcdoc = LOADING_DOC.replace(r, def(match, field): return data[field];)
self.srcdoc_created = True

View File

@ -6,7 +6,7 @@ from elementmaker import E
from gettext import gettext as _
from dom import add_extra_css, build_rule, clear, svgicon
from read_book.comm import IframeWrapper
from iframe_comm import IframeWrapper
from read_book.resources import load_resources
CLASS_NAME = 'book-content-popup-container'
@ -56,7 +56,7 @@ class ContentPopupOverlay:
'error': self.view.on_iframe_error,
'content_loaded': self.on_content_loaded,
}
self.iframe_wrapper = IframeWrapper(handlers, iframe, 'popup', _('Loading data, please wait...'))
self.iframe_wrapper = IframeWrapper(handlers, iframe, 'read_book.footnotes', _('Loading data, please wait...'))
self.pending_load = None
@property

View File

@ -3,7 +3,7 @@
from __python__ import bound_methods, hash_literals
from dom import clear
from read_book.comm import IframeClient
from iframe_comm import IframeClient
from read_book.resources import finalize_resources, unserialize_html
from read_book.settings import apply_settings, update_settings
@ -220,3 +220,7 @@ class PopupIframeBoss:
def content_loaded(self):
apply_settings()
self.comm.send_message('content_loaded', height=document.documentElement.scrollHeight + 25)
def main():
main.boss = PopupIframeBoss()

View File

@ -5,14 +5,14 @@ from __python__ import bound_methods, hash_literals
import traceback
from gettext import gettext as _
from iframe_comm import IframeClient
from read_book.cfi import at_current, scroll_to as scroll_to_cfi
from read_book.comm import IframeClient
from read_book.flow_mode import (
anchor_funcs as flow_anchor_funcs, flow_onkeydown, flow_onwheel,
flow_to_scroll_fraction, handle_gesture as flow_handle_gesture,
layout as flow_layout, scroll_by_page as flow_scroll_by_page
)
from read_book.footnotes import PopupIframeBoss, is_footnote_link
from read_book.footnotes import is_footnote_link
from read_book.globals import (
current_book, current_layout_mode, current_spine_item, set_boss,
set_current_spine_item, set_layout_mode
@ -382,10 +382,6 @@ class IframeBoss:
else:
self.send_message('find_in_spine', text=data.text, backwards=data.backwards, searched_in_spine=data.searched_in_spine)
def init():
if window.iframe_type is 'main':
script = document.getElementById('bootstrap')
script.parentNode.removeChild(script) # free up some memory
IframeBoss()
elif window.iframe_type is 'popup':
PopupIframeBoss()
def main():
main.boss = IframeBoss()

View File

@ -5,14 +5,15 @@ from __python__ import bound_methods, hash_literals
from elementmaker import E
from gettext import gettext as _
import read_book.iframe # noqa
from ajax import ajax_send
from book_list.book_details import CLASS_NAME as BD_CLASS_NAME, render_metadata
from book_list.globals import get_session_data
from book_list.router import push_state, read_book_mode
from book_list.theme import get_color
from dom import add_extra_css, build_rule, clear, set_css, svgicon, unique_id
from iframe_comm import IframeWrapper
from modals import error_dialog, warning_dialog
from read_book.comm import IframeWrapper
from read_book.content_popup import ContentPopupOverlay
from read_book.globals import current_book, set_current_spine_item
from read_book.goto import get_next_section
@ -22,7 +23,7 @@ from read_book.prefs.font_size import change_font_size_by
from read_book.prefs.head_foot import render_head_foot
from read_book.resources import load_resources
from read_book.search import SearchOverlay, find_in_spine
from read_book.toc import update_visible_toc_nodes, get_current_toc_nodes
from read_book.toc import get_current_toc_nodes, update_visible_toc_nodes
from read_book.touch import set_left_margin_handler, set_right_margin_handler
from session import get_device_uuid, get_interface_data
from utils import html_escape, is_ios, parse_url_params, username_key
@ -163,7 +164,7 @@ class View:
'show_footnote': self.on_show_footnote,
'print': self.on_print,
}
self.iframe_wrapper = IframeWrapper(handlers, document.getElementById(iframe_id), 'main', _('Bootstrapping book reader...'))
self.iframe_wrapper = IframeWrapper(handlers, document.getElementById(iframe_id), 'read_book.iframe', _('Bootstrapping book reader...'))
self.search_overlay = SearchOverlay(self)
self.content_popup_overlay = ContentPopupOverlay(self)
self.overlay = Overlay(self)

View File

@ -1,5 +1,6 @@
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
# globals: ρσ_get_module
from __python__ import hash_literals
from gettext import gettext as _
@ -9,7 +10,6 @@ from ajax import ajax, console_print, set_default_timeout
from autoreload import create_auto_reload_watcher
from book_list.globals import main_js
from book_list.main import main
from read_book.iframe import init
is_running_in_iframe = False # Changed before script is loaded in the iframe
autoreload_enabled = False
@ -17,7 +17,15 @@ asked_appcache_update = False
AUTO_UPDATE_THRESHOLD = 1000 # millisecs
if is_running_in_iframe:
init()
script = document.getElementById('bootstrap')
if script:
script.parentNode.removeChild(script) # free up some memory
m = ρσ_get_module(window.iframe_entry_point)
main = m?.main
if main:
main()
else:
console.log('iframe entry_point ' + window.iframe_entry_point + ' not found')
else:
start_time = window.performance.now()