Fix related to ensure page indices are properly restored on page change requests
MODIFY
js/views/cfi_navigation_logic.js
js/views/cfi_navigation_logic.js
@@ -372,13 +372,13 @@ var CfiNavigationLogic = function (options) {
if (isVwm) {
var topOffset = firstRectangle.top;
- pageIndex = Math.floor(topOffset / frameDimensions.height);
+ pageIndex = Math.round(topOffset / frameDimensions.height);
} else {
var leftOffset = firstRectangle.left;
if (isRtl) {
leftOffset = (columnFullWidth * (options.paginationInfo ? options.paginationInfo.visibleColumnCount : 1)) - leftOffset;
}
- pageIndex = Math.floor(leftOffset / columnFullWidth);
+ pageIndex = Math.round(leftOffset / columnFullWidth);
}
return pageIndex;

MODIFY
js/views/reflowable_view.js
js/views/reflowable_view.js
@@ -91,6 +91,7 @@ var ReflowableView = function(options, reader){
columnMinWidth: 400,
spreadCount : 0,
currentSpreadIndex : 0,
+ currentPageIndex: 0,
columnWidth : undefined,
pageOffset : 0,
columnCount: 0
@@ -243,6 +244,7 @@ var ReflowableView = function(options, reader){
_paginationInfo.pageOffset = 0;
_paginationInfo.currentSpreadIndex = 0;
+ _paginationInfo.currentPageIndex = 0;
_currentSpineItem = spineItem;
// TODO: this is a dirty hack!!
@@ -434,18 +436,18 @@ var ReflowableView = function(options, reader){
}
- this.openPageInternal = function(pageRequest) {
+ function _openPageInternal(pageRequest) {
if(_isWaitingFrameRender) {
_deferredPageRequest = pageRequest;
- return;
+ return false;
}
// if no spine item specified we are talking about current spine item
if(pageRequest.spineItem && pageRequest.spineItem != _currentSpineItem) {
_deferredPageRequest = pageRequest;
loadSpineItem(pageRequest.spineItem);
- return;
+ return true;
}
var pageIndex = undefined;
@@ -455,9 +457,7 @@ var ReflowableView = function(options, reader){
pageIndex = pageRequest.spineItemPageIndex;
}
else if(pageRequest.elementId) {
- pageIndex = _navigationLogic.getPageIndexForElementId(pageRequest.elementId);
-
- if (pageIndex < 0) pageIndex = 0;
+ pageIndex = _paginationInfo.currentPageIndex + _navigationLogic.getPageIndexForElementId(pageRequest.elementId);
}
else if(pageRequest.firstVisibleCfi && pageRequest.lastVisibleCfi) {
var firstPageIndex;
@@ -468,8 +468,6 @@ var ReflowableView = function(options, reader){
_cfiClassBlacklist,
_cfiElementBlacklist,
_cfiIdBlacklist);
-
- if (firstPageIndex < 0) firstPageIndex = 0;
}
catch (e)
{
@@ -482,8 +480,6 @@ var ReflowableView = function(options, reader){
_cfiClassBlacklist,
_cfiElementBlacklist,
_cfiIdBlacklist);
-
- if (lastPageIndex < 0) lastPageIndex = 0;
}
catch (e)
{
@@ -491,17 +487,15 @@ var ReflowableView = function(options, reader){
console.error(e);
}
// Go to the page in the middle of the two elements
- pageIndex = Math.round((firstPageIndex + lastPageIndex) / 2);
+ pageIndex = _paginationInfo.currentPageIndex + Math.round((firstPageIndex + lastPageIndex) / 2);
}
else if(pageRequest.elementCfi) {
try
{
- pageIndex = _navigationLogic.getPageIndexForCfi(pageRequest.elementCfi,
+ pageIndex = _paginationInfo.currentPageIndex + _navigationLogic.getPageIndexForCfi(pageRequest.elementCfi,
_cfiClassBlacklist,
_cfiElementBlacklist,
_cfiIdBlacklist);
-
- if (pageIndex < 0) pageIndex = 0;
}
catch (e)
{
@@ -520,18 +514,20 @@ var ReflowableView = function(options, reader){
pageIndex = 0;
}
- if(pageIndex >= 0 && pageIndex < _paginationInfo.columnCount) {
+ if (pageIndex < 0 || pageIndex > _paginationInfo.columnCount) {
+ console.log('Illegal pageIndex value: ', pageIndex, 'column count is ', _paginationInfo.columnCount);
+ pageIndex = pageIndex < 0 ? 0 : _paginationInfo.columnCount;
+ }
+
+ _paginationInfo.currentPageIndex = pageIndex;
_paginationInfo.currentSpreadIndex = Math.floor(pageIndex / _paginationInfo.visibleColumnCount) ;
onPaginationChanged(pageRequest.initiator, pageRequest.spineItem, pageRequest.elementId);
+ return true;
}
- else {
- console.log('Illegal pageIndex value: ', pageIndex, 'column count is ', _paginationInfo.columnCount);
- }
- };
this.openPage = function(pageRequest) {
// Go to request page, it will save the new position in onPaginationChanged
- this.openPageInternal(pageRequest);
+ _openPageInternal(pageRequest);
// Save it for when pagination is updated
_lastPageRequest = pageRequest;
};
@@ -555,7 +551,7 @@ var ReflowableView = function(options, reader){
this.restoreCurrentPosition = function() {
if (_lastPageRequest) {
- this.openPageInternal(_lastPageRequest);
+ _openPageInternal(_lastPageRequest);
}
};
@@ -600,7 +596,7 @@ var ReflowableView = function(options, reader){
}
function onPaginationChanged_(initiator, paginationRequest_spineItem, paginationRequest_elementId) {
-
+ _paginationInfo.currentPageIndex = _paginationInfo.currentSpreadIndex * _paginationInfo.visibleColumnCount;
_paginationInfo.pageOffset = (_paginationInfo.columnWidth + _paginationInfo.columnGap) * _paginationInfo.visibleColumnCount * _paginationInfo.currentSpreadIndex;
redraw();
@@ -861,6 +857,7 @@ var ReflowableView = function(options, reader){
if (_lastPageRequest) {
// Make sure we stay on the same page after the content or the viewport
// has been resized
+ _paginationInfo.currentPageIndex = 0; // current page index is not stable, reset it
self.restoreCurrentPosition();
} else {
onPaginationChanged(self); // => redraw() => showBook(), so the trick below is not needed

Fix related to #384, ensure page indices are properly restored on page change requests