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,7 +60,12 @@ function goto_reference(ref) {
if (num < 0) {alert("Invalid reference: "+ref); return;}
var p = $("p");
if (num >= p.length) {alert("Reference not found: "+ref); return;}
$.scrollTo($(p[num]), 1000,
var dest = $(p[num]);
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
###
This class is a namespace to expose functions via the
window.paged_display object. The most important functions are:
set_geometry(): sets the parameters used to layout text in paged mode
layout(): causes the currently loaded document to be laid out in columns.
###
# This class is a namespace to expose functions via the
# window.paged_display object. The most important functions are:
#
# set_geometry(): sets the parameters used to layout text in paged mode
#
# layout(): causes the currently loaded document to be laid out in columns.
constructor: () ->
if not this instanceof arguments.callee
@ -163,7 +161,7 @@ class PagedDisplay
xpos = Math.floor(document.body.scrollWidth * frac)
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
# the viewport
if typeof(xpos) != 'number'
@ -177,8 +175,32 @@ class PagedDisplay
pos += this.page_width
limit = document.body.scrollWidth - this.screen_width
pos = limit if pos > limit
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) ->
# The current scroll position as a fraction between 0 and 1
limit = document.body.scrollWidth - window.innerWidth
@ -329,7 +351,6 @@ if window?
window.paged_display = new PagedDisplay()
# TODO:
# Go to reference positions
# Indexing
# Resizing of images
# Full screen mode

View File

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