From 3a686e242756ee0d4e386b38db0c4598677c243e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 4 Jul 2012 14:04:54 +0530 Subject: [PATCH] Refactor coffeescript, putting utils into their own files --- resources/compiled_coffeescript.zip | Bin 43332 -> 44408 bytes src/calibre/ebooks/oeb/display/paged.coffee | 50 +++----------- src/calibre/ebooks/oeb/display/utils.coffee | 70 ++++++++++++++++++++ src/calibre/gui2/viewer/documentview.py | 3 +- src/calibre/gui2/viewer/javascript.py | 3 +- 5 files changed, 82 insertions(+), 44 deletions(-) create mode 100644 src/calibre/ebooks/oeb/display/utils.coffee diff --git a/resources/compiled_coffeescript.zip b/resources/compiled_coffeescript.zip index d7fad735ba651cfe4b99775b7f301581150e1182..c026c894ded0cb354244edef135a793e5f4ad608 100644 GIT binary patch delta 976 zcmb7DO-vI}5Z*4OEsHiH6vR@Ps!hw^nyLio+Nd$9Bm(|Ky%2s1Y;jF#o82vi7(`4o z@uI=cgYjTI*66MAW&$Q&ylA2ajqzf_$wa*xPQJG-VTpKfli9rYX6Bpkn>X*x2l4k8 z(SQDIm9s(M-*RsKx6AV=+|&MQp>$&^>GYLyTY}A>nD>g)UX{S=Ygp z8uAu8()|%H(Ogoe`7T;eIN+210Q9N`gbQAL>hFda3m_2lBY&tKXJS`y$r-`xSP1K} zo0wFN;DHiF@lJF4{-@xiMH(nhN;-O2%8ndJE zeGuJE7>fs842{!Tj<+$USR+lUoK3vU~MfaoQbZQc|Ak_ zL+-7+t+w0~-D=1$cL%Fn0V-DxCVc4egaTViD{i}BjP+iiDf>HM%rk0X+f0<+_ukKW zw?8bTiwgZUyVeWb{0&BNZopR=e~ D4dylW delta 267 zcmexyi|NQECg}igW)=|!5cpg8$f0HO-}?3D3=AMFJ^7ur*yOr391|@UPp%FSR?y*6 z0D}1V;+)LnR0Ugw=vX~4)0zt?k&~Z3IZ#AovV*b0beXtJLu*XD&HTV*!iwazKvLv{A#@HN(xZ?BPK qdSO2Ky|uz*>9tx+#vVYX=wyE%Hn9M2RyL4dnSt;QkPcb_;sF3tjac;n diff --git a/src/calibre/ebooks/oeb/display/paged.coffee b/src/calibre/ebooks/oeb/display/paged.coffee index 695ca3af7f..3dd930d92f 100644 --- a/src/calibre/ebooks/oeb/display/paged.coffee +++ b/src/calibre/ebooks/oeb/display/paged.coffee @@ -6,48 +6,9 @@ Released under the GPLv3 License ### -log = (args...) -> # {{{ - if args - msg = args.join(' ') - if window?.console?.log - window.console.log(msg) - else if process?.stdout?.write - process.stdout.write(msg + '\n') -# }}} - -window_scroll_pos = (win=window) -> # {{{ - if typeof(win.pageXOffset) == 'number' - x = win.pageXOffset - y = win.pageYOffset - else # IE < 9 - if document.body and ( document.body.scrollLeft or document.body.scrollTop ) - x = document.body.scrollLeft - y = document.body.scrollTop - else if document.documentElement and ( document.documentElement.scrollLeft or document.documentElement.scrollTop) - y = document.documentElement.scrollTop - x = document.documentElement.scrollLeft - return [x, y] -# }}} - -viewport_to_document = (x, y, doc=window?.document) -> # {{{ - until doc == window.document - # We are in a frame - frame = doc.defaultView.frameElement - rect = frame.getBoundingClientRect() - x += rect.left - y += rect.top - doc = frame.ownerDocument - win = doc.defaultView - [wx, wy] = window_scroll_pos(win) - x += wx - y += wy - return [x, y] -# }}} - -absleft = (elem) -> # {{{ - r = elem.getBoundingClientRect() - return viewport_to_document(r.left, 0, elem.ownerDocument)[0] -# }}} +log = window.calibre_utils.log +viewport_to_document = window.calibre_utils.viewport_to_document +absleft = window.calibre_utils.absleft class PagedDisplay # This class is a namespace to expose functions via the @@ -75,6 +36,7 @@ class PagedDisplay this.cols_per_screen = cols_per_screen layout: () -> + # start_time = new Date().getTime() body_style = window.getComputedStyle(document.body) # When laying body out in columns, webkit bleeds the top margin of the # first block element out above the columns, leading to an extra top @@ -160,8 +122,12 @@ class PagedDisplay this.in_paged_mode = true this.current_margin_side = sm + # log('Time to layout:', new Date().getTime() - start_time) return sm + fit_images: () -> + null + scroll_to_pos: (frac) -> # Scroll to the position represented by frac (number between 0 and 1) xpos = Math.floor(document.body.scrollWidth * frac) diff --git a/src/calibre/ebooks/oeb/display/utils.coffee b/src/calibre/ebooks/oeb/display/utils.coffee new file mode 100644 index 0000000000..b35021ae51 --- /dev/null +++ b/src/calibre/ebooks/oeb/display/utils.coffee @@ -0,0 +1,70 @@ +#!/usr/bin/env coffee +# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai + +### + Copyright 2012, Kovid Goyal + Released under the GPLv3 License +### + +class CalibreUtils + # This class is a namespace to expose functions via the + # window.calibre_utils object. + + constructor: () -> + if not this instanceof arguments.callee + throw new Error('CalibreUtils constructor called as function') + + log: (args...) -> # {{{ + # Output args to the window.console object. args are automatically + # coerced to strings + if args + msg = args.join(' ') + if window?.console?.log + window.console.log(msg) + else if process?.stdout?.write + process.stdout.write(msg + '\n') + # }}} + + window_scroll_pos: (win=window) -> # {{{ + # The current scroll position of the browser window + if typeof(win.pageXOffset) == 'number' + x = win.pageXOffset + y = win.pageYOffset + else # IE < 9 + if document.body and ( document.body.scrollLeft or document.body.scrollTop ) + x = document.body.scrollLeft + y = document.body.scrollTop + else if document.documentElement and ( document.documentElement.scrollLeft or document.documentElement.scrollTop) + y = document.documentElement.scrollTop + x = document.documentElement.scrollLeft + return [x, y] + # }}} + + viewport_to_document: (x, y, doc=window?.document) -> # {{{ + # Convert x, y from the viewport (window) co-ordinate system to the + # document (body) co-ordinate system + until doc == window.document + # We are in a frame + frame = doc.defaultView.frameElement + rect = frame.getBoundingClientRect() + x += rect.left + y += rect.top + doc = frame.ownerDocument + win = doc.defaultView + [wx, wy] = this.window_scroll_pos(win) + x += wx + y += wy + return [x, y] + # }}} + + absleft: (elem) -> # {{{ + # The left edge of elem in document co-ords. Works in all + # circumstances, including column layout. Note that this will cause + # a relayout if the render tree is dirty. + r = elem.getBoundingClientRect() + return this.viewport_to_document(r.left, 0, elem.ownerDocument)[0] + # }}} + +if window? + window.calibre_utils = new CalibreUtils() + diff --git a/src/calibre/gui2/viewer/documentview.py b/src/calibre/gui2/viewer/documentview.py index 10d10b7155..8beea63df4 100644 --- a/src/calibre/gui2/viewer/documentview.py +++ b/src/calibre/gui2/viewer/documentview.py @@ -136,7 +136,7 @@ class Document(QWebPage): # {{{ self.max_fs_width = min(opts.max_fs_width, screen_width-50) def fit_images(self): - if self.do_fit_images: + if self.do_fit_images and not self.in_paged_mode: self.javascript('setup_image_scaling_handlers()') def add_window_objects(self): @@ -219,6 +219,7 @@ class Document(QWebPage): # {{{ if scroll_width > self.window_width: sz.setWidth(scroll_width+side_margin) self.setPreferredContentsSize(sz) + self.javascript('window.paged_display.fit_images()') @property def column_boundaries(self): diff --git a/src/calibre/gui2/viewer/javascript.py b/src/calibre/gui2/viewer/javascript.py index 1594a1d5db..2aedaf9e13 100644 --- a/src/calibre/gui2/viewer/javascript.py +++ b/src/calibre/gui2/viewer/javascript.py @@ -31,10 +31,11 @@ class JavaScriptLoader(object): 'cfi':'ebooks.oeb.display.cfi', 'indexing':'ebooks.oeb.display.indexing', 'paged':'ebooks.oeb.display.paged', + 'utils':'ebooks.oeb.display.utils', } ORDER = ('jquery', 'jquery_scrollTo', 'bookmarks', 'referencing', 'images', - 'hyphenation', 'hyphenator', 'cfi', 'indexing', 'paged') + 'hyphenation', 'hyphenator', 'utils', 'cfi', 'indexing', 'paged') def __init__(self, dynamic_coffeescript=False):