Paged display: Implement go to reference

This commit is contained in:
Kovid Goyal 2012-06-24 10:53:31 +05:30
parent 3c4cb3f492
commit 876ddf27b3
3 changed files with 42 additions and 14 deletions

View File

@ -60,8 +60,13 @@ function goto_reference(ref) {
if (num < 0) {alert("Invalid reference: "+ref); return;} if (num < 0) {alert("Invalid reference: "+ref); return;}
var p = $("p"); var p = $("p");
if (num >= p.length) {alert("Reference not found: "+ref); return;} if (num >= p.length) {alert("Reference not found: "+ref); return;}
$.scrollTo($(p[num]), 1000, var dest = $(p[num]);
{onAfter:function(){window.py_bridge.animated_scroll_done()}}); if (window.paged_display.in_paged_mode) {
var xpos = dest.offset().left;
window.paged_display.scroll_to_xpos(xpos, true, true, 1000);
} else
$.scrollTo(dest, 1000,
{onAfter:function(){window.py_bridge.animated_scroll_done()}});
} }

View File

@ -50,14 +50,12 @@ absleft = (elem) -> # {{{
# }}} # }}}
class PagedDisplay class PagedDisplay
### # This class is a namespace to expose functions via the
This class is a namespace to expose functions via the # window.paged_display object. The most important functions are:
window.paged_display object. The most important functions are: #
# set_geometry(): sets the parameters used to layout text in paged mode
set_geometry(): sets the parameters used to layout text in paged mode #
# layout(): causes the currently loaded document to be laid out in columns.
layout(): causes the currently loaded document to be laid out in columns.
###
constructor: () -> constructor: () ->
if not this instanceof arguments.callee if not this instanceof arguments.callee
@ -163,7 +161,7 @@ class PagedDisplay
xpos = Math.floor(document.body.scrollWidth * frac) xpos = Math.floor(document.body.scrollWidth * frac)
this.scroll_to_xpos(xpos) this.scroll_to_xpos(xpos)
scroll_to_xpos: (xpos) -> scroll_to_xpos: (xpos, animated=false, notify=false, duration=1000) ->
# Scroll so that the column containing xpos is the left most column in # Scroll so that the column containing xpos is the left most column in
# the viewport # the viewport
if typeof(xpos) != 'number' if typeof(xpos) != 'number'
@ -177,7 +175,31 @@ class PagedDisplay
pos += this.page_width pos += this.page_width
limit = document.body.scrollWidth - this.screen_width limit = document.body.scrollWidth - this.screen_width
pos = limit if pos > limit pos = limit if pos > limit
window.scrollTo(pos, 0) if animated
this.animated_scroll(pos, duration=1000, notify=notify)
else
window.scrollTo(pos, 0)
animated_scroll: (pos, duration=1000, notify=true) ->
delta = pos - window.pageXOffset
interval = 50
steps = Math.floor(duration/interval)
step_size = Math.floor(delta/steps)
this.current_scroll_animation = {target:pos, step_size:step_size, interval:interval, notify:notify, fn: () =>
a = this.current_scroll_animation
npos = window.pageXOffset + a.step_size
completed = false
if Math.abs(npos - a.target) < Math.abs(a.step_size)
completed = true
npos = a.target
window.scrollTo(npos, 0)
if completed
if notify
window.py_bridge.animated_scroll_done()
else
setTimeout(a.fn, a.interval)
}
this.current_scroll_animation.fn()
current_pos: (frac) -> current_pos: (frac) ->
# The current scroll position as a fraction between 0 and 1 # The current scroll position as a fraction between 0 and 1
@ -329,7 +351,6 @@ if window?
window.paged_display = new PagedDisplay() window.paged_display = new PagedDisplay()
# TODO: # TODO:
# Go to reference positions
# Indexing # Indexing
# Resizing of images # Resizing of images
# Full screen mode # Full screen mode

View File

@ -138,7 +138,9 @@ class Reference(QLineEdit):
self.editingFinished.connect(self.editing_finished) self.editingFinished.connect(self.editing_finished)
def editing_finished(self): def editing_finished(self):
self.goto.emit(unicode(self.text())) text = unicode(self.text())
self.setText('')
self.goto.emit(text)
class RecentAction(QAction): class RecentAction(QAction):