mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Refactor coffeescript, putting utils into their own files
This commit is contained in:
parent
dda411fcd4
commit
3a686e2427
Binary file not shown.
@ -6,48 +6,9 @@
|
|||||||
Released under the GPLv3 License
|
Released under the GPLv3 License
|
||||||
###
|
###
|
||||||
|
|
||||||
log = (args...) -> # {{{
|
log = window.calibre_utils.log
|
||||||
if args
|
viewport_to_document = window.calibre_utils.viewport_to_document
|
||||||
msg = args.join(' ')
|
absleft = window.calibre_utils.absleft
|
||||||
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]
|
|
||||||
# }}}
|
|
||||||
|
|
||||||
class PagedDisplay
|
class PagedDisplay
|
||||||
# This class is a namespace to expose functions via the
|
# This class is a namespace to expose functions via the
|
||||||
@ -75,6 +36,7 @@ class PagedDisplay
|
|||||||
this.cols_per_screen = cols_per_screen
|
this.cols_per_screen = cols_per_screen
|
||||||
|
|
||||||
layout: () ->
|
layout: () ->
|
||||||
|
# start_time = new Date().getTime()
|
||||||
body_style = window.getComputedStyle(document.body)
|
body_style = window.getComputedStyle(document.body)
|
||||||
# When laying body out in columns, webkit bleeds the top margin of the
|
# 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
|
# first block element out above the columns, leading to an extra top
|
||||||
@ -160,8 +122,12 @@ class PagedDisplay
|
|||||||
|
|
||||||
this.in_paged_mode = true
|
this.in_paged_mode = true
|
||||||
this.current_margin_side = sm
|
this.current_margin_side = sm
|
||||||
|
# log('Time to layout:', new Date().getTime() - start_time)
|
||||||
return sm
|
return sm
|
||||||
|
|
||||||
|
fit_images: () ->
|
||||||
|
null
|
||||||
|
|
||||||
scroll_to_pos: (frac) ->
|
scroll_to_pos: (frac) ->
|
||||||
# Scroll to the position represented by frac (number between 0 and 1)
|
# Scroll to the position represented by frac (number between 0 and 1)
|
||||||
xpos = Math.floor(document.body.scrollWidth * frac)
|
xpos = Math.floor(document.body.scrollWidth * frac)
|
||||||
|
70
src/calibre/ebooks/oeb/display/utils.coffee
Normal file
70
src/calibre/ebooks/oeb/display/utils.coffee
Normal 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()
|
||||||
|
|
@ -136,7 +136,7 @@ class Document(QWebPage): # {{{
|
|||||||
self.max_fs_width = min(opts.max_fs_width, screen_width-50)
|
self.max_fs_width = min(opts.max_fs_width, screen_width-50)
|
||||||
|
|
||||||
def fit_images(self):
|
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()')
|
self.javascript('setup_image_scaling_handlers()')
|
||||||
|
|
||||||
def add_window_objects(self):
|
def add_window_objects(self):
|
||||||
@ -219,6 +219,7 @@ class Document(QWebPage): # {{{
|
|||||||
if scroll_width > self.window_width:
|
if scroll_width > self.window_width:
|
||||||
sz.setWidth(scroll_width+side_margin)
|
sz.setWidth(scroll_width+side_margin)
|
||||||
self.setPreferredContentsSize(sz)
|
self.setPreferredContentsSize(sz)
|
||||||
|
self.javascript('window.paged_display.fit_images()')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def column_boundaries(self):
|
def column_boundaries(self):
|
||||||
|
@ -31,10 +31,11 @@ class JavaScriptLoader(object):
|
|||||||
'cfi':'ebooks.oeb.display.cfi',
|
'cfi':'ebooks.oeb.display.cfi',
|
||||||
'indexing':'ebooks.oeb.display.indexing',
|
'indexing':'ebooks.oeb.display.indexing',
|
||||||
'paged':'ebooks.oeb.display.paged',
|
'paged':'ebooks.oeb.display.paged',
|
||||||
|
'utils':'ebooks.oeb.display.utils',
|
||||||
}
|
}
|
||||||
|
|
||||||
ORDER = ('jquery', 'jquery_scrollTo', 'bookmarks', 'referencing', 'images',
|
ORDER = ('jquery', 'jquery_scrollTo', 'bookmarks', 'referencing', 'images',
|
||||||
'hyphenation', 'hyphenator', 'cfi', 'indexing', 'paged')
|
'hyphenation', 'hyphenator', 'utils', 'cfi', 'indexing', 'paged')
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, dynamic_coffeescript=False):
|
def __init__(self, dynamic_coffeescript=False):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user