Refactor coffeescript, putting utils into their own files

This commit is contained in:
Kovid Goyal 2012-07-04 14:04:54 +05:30
parent dda411fcd4
commit 3a686e2427
5 changed files with 82 additions and 44 deletions

Binary file not shown.

View File

@ -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)

View File

@ -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 <kovid@kovidgoyal.net>
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()

View File

@ -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):

View File

@ -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):