1
0
Fork 0
mirror of https://github.com/Yetangitu/owncloud-apps.git synced 2025-10-02 14:49:17 +02:00

files_reader: more PDF changes, working outline,progress report, starting experiment with preloading next page(s)

This commit is contained in:
frankdelange 2017-03-28 10:46:00 +02:00
parent c62859820d
commit 15a382edbf
17 changed files with 890 additions and 177 deletions

Binary file not shown.

Binary file not shown.

View file

@ -23,7 +23,8 @@ PDFJS.reader.ControlsController = function(book) {
$rotate_option = $(".rotate_option"),
$rotate_left = $("#rotate_left"),
$rotate_right = $("#rotate_right"),
$page_num = $("#page_num");
$page_num = $("#page_num"),
$total_pages = $("#total_pages");
if (reader.isMobile() === true) {
$titlebar.addClass("background_visible");
@ -136,6 +137,19 @@ PDFJS.reader.ControlsController = function(book) {
$zoom_options.css("opacity", "");
});
var setZoomIcon = function(zoom) {
$zoom_icon[0].className="";
var $current_zoom_option = $zoom_options.find("[data-value='" + zoom + "']");
if ($current_zoom_option.data("class")) {
$zoom_icon.addClass($current_zoom_option.data("class"));
} else {
$zoom_icon[0].textContent = $current_zoom_option.data("text");
}
};
setZoomIcon(settings.zoomLevel);
/*
$zoom_icon[0].className="";
var $current_zoom_option = $zoom_options.find("[data-value='" + settings.zoomLevel + "']");
if ($current_zoom_option.data("class")) {
@ -143,6 +157,7 @@ PDFJS.reader.ControlsController = function(book) {
} else {
$zoom_icon[0].textContent = $current_zoom_option.data("text");
}
*/
$zoom_option.on("click", function () {
var $this = $(this);
@ -158,8 +173,12 @@ PDFJS.reader.ControlsController = function(book) {
});
// rotate
$rotate_icon[0].className = "";
$rotate_icon[0].className = "icon-rotate_" + settings.rotation;
var setRotateIcon = function (rotation) {
$rotate_icon[0].className = "";
$rotate_icon[0].className = "icon-rotate_" + rotation;
};
setRotateIcon(settings.rotation);
$rotate_icon.on("click", function () {
var offset = $(this).offset();
@ -238,6 +257,42 @@ PDFJS.reader.ControlsController = function(book) {
$page_num[0].addEventListener("keydown", enterPageNum, false);
});
var setPageCount = function (_numPages) {
var numPages = _numPages || reader.settings.numPages;
$total_pages[0].textContent = parseInt(numPages).toString();
};
var setCurrentPage = function (_page) {
var page = _page || reader.settings.currentPage,
zoom = reader.settings.zoomLevel,
oddPageRight = reader.settings.oddPageRight,
total_pages = reader.settings.numPages,
text;
if (zoom === "spread") {
if (oddPageRight === true) {
page -= page % 2;
} else {
page -= (page + 1) % 2;
}
}
if (page >= 0 && page <= total_pages) {
if (page === total_pages) {
text = reader.getPageLabel(page);
} else if (page === 0) {
text = reader.getPageLabel(page + 1);
} else {
text = reader.getPageLabel(page) + "-" + reader.getPageLabel(page + 1);
}
}
$page_num[0].textContent = text;
};
/*
book.on('renderer:locationChanged', function(cfi){
@ -273,6 +328,10 @@ PDFJS.reader.ControlsController = function(book) {
return {
"show": show,
"hide": hide,
"toggle": toggle
"toggle": toggle,
"setZoomIcon": setZoomIcon,
"setRotateIcon": setRotateIcon,
"setCurrentPage": setCurrentPage,
"setPageCount": setPageCount
};
};

View file

@ -0,0 +1,164 @@
PDFJS.reader.OutlineController = function(_outline) {
var reader = this,
book = this.book,
outline = _outline || [];
var outlineView = document.getElementById("outlineView"),
$list = $("#outlineView"),
baseUrl = location.href.split('#')[0],
lastToggleIsShow;
var DEFAULT_TITLE = '\u2013';
var bindLink = function (element, item) {
var destination = item.dest;
if (item.url) {
PDFJS.addLinkAttributes (element, {
url: item.url,
target: (item.newWindow
? PDFJS.LinkTarget.BLANK
: undefined),
});
return;
} else {
element.href = getDestinationHash(destination);
element.onclick = function () {
if (destination) {
reader.navigateTo(destination);
}
return false;
};
}
};
var setStyles = function (element, item) {
var styleStr = "";
if (item.bold) {
styleStr += 'font-weight: bold;';
}
if (item.italic) {
styleStr += 'font-style: italic;';
}
if (styleStr) {
element.setAttribute('style', styleStr);
}
};
var getDestinationHash = function (destination) {
var url = baseUrl || "",
str;
if (typeof destination === 'string') {
url += "#"
+ (parseInt(destination) === destination)
? "nameddest="
: ""
+ escape(destination);
} else if (destination instanceof Array) {
url += "#"
+ escape(JSON.stringify(destination));
}
return url;
};
var generateOutlineItems = function (outline, level) {
var container = document.createElement("ul");
if(!level) level = 1;
outline.forEach(function (chapter) {
var listitem = document.createElement("li"),
link = document.createElement("a"),
toggle = document.createElement("a"),
subitems;
listitem.id = "outline-"+chapter.dest;
listitem.classList.add('list_item');
link.textContent = PDFJS.removeNullCharacters(chapter.title) || DEFAULT_TITLE;
bindLink(link, chapter);
setStyles(link, chapter);
link.classList.add('outline_link');
listitem.appendChild(link);
if(chapter.items.length > 0) {
level++;
subitems = generateOutlineItems(chapter.items, level);
toggle.classList.add('outline_toggle');
listitem.insertBefore(toggle, link);
listitem.appendChild(subitems);
}
container.appendChild(listitem);
});
return container;
}
console.log(outline);
var onShow = function() {
outlineView.classList.add('open');
};
var onHide = function() {
outlineView.classList.remove('open');
};
$list.append(generateOutlineItems(outline));
/*
$list.find(".outline_link").on("click", function(event){
var url = this.getAttribute('href');
event.preventDefault();
//-- Provide the Book with the url to show
// The Url must be found in the books manifest
book.goto(url);
$list.find(".currentChapter")
.addClass("openChapter")
.removeClass("currentChapter");
$(this).parent('li').addClass("currentChapter");
});
*/
$list.find(".outline_toggle").on("click", function(event){
var $el = $(this).parent('li'),
open = $el.hasClass("openChapter");
event.preventDefault();
if(open){
$el.removeClass("openChapter");
} else {
$el.addClass("openChapter");
}
});
return {
"show" : onShow,
"hide" : onHide
};
};

View file

@ -0,0 +1,57 @@
PDFJS.reader.ProgressController = function() {
var reader = this,
settings = reader.settings,
percentage = 0;
var $progress = $("#progress"),
$bar = $(".bar"),
$download_icon = $("#download_icon"),
$message = $(".message-text");
var show = function () {
$progress.removeClass("hide");
};
var hide = function () {
$progress.addClass("hide");
};
var setSize = function (size) {
settings.fileSize = size;
};
var setProgress = function (progress) {
if (percentage < 1)
$download_icon.addClass("active");
percentage = Math.floor((progress.loaded / progress.total) * 100);
$bar.css("width", percentage + "%");
if (percentage === 100)
$download_icon.removeClass("active").addClass("ok");
};
var reset = function() {
$bar.css("width", 0);
};
var setMessage = function (text, category, state) {
var $category_icon = $("#" + category + "_icon");
$message.text(text);
$category_icon[0].classList.remove("ok", "active", "error");
$category_icon.addClass(state);
};
return {
"show": show,
"hide": hide,
"setSize": setSize,
"setProgress": setProgress,
"setMessage": setMessage
};
};

View file

@ -119,8 +119,8 @@ PDFJS.reader.ReaderController = function(book) {
case 'rotateRight':
$rotate_right.click();
break;
case 'toggleZoom':
// TODO
case 'cycleZoom':
reader.cycleZoom();
break;
default:
console.log("unsupported keyCode: " + e.keyCode);

View file

@ -6,7 +6,7 @@ PDFJS.reader.SidebarController = function(book) {
$panels = $("#panels"),
$views = $("#views"),
$close = $("#hide-Sidebar");
$slider = $("#slider");
$slider = $("#slider");
var activePanel = "Toc";
@ -62,6 +62,8 @@ PDFJS.reader.SidebarController = function(book) {
e.preventDefault();
});
$sidebar.css("width", "calc(" + parseInt(settings.thumbnailWidth) + "px + 2em)");
return {
'show' : show,
'hide' : hide,

View file

@ -1,115 +1,192 @@
PDFJS.reader.TocController = function(toc) {
var book = this.book;
PDFJS.reader.TocController = function() {
var $list = $("#tocView"),
docfrag = document.createDocumentFragment();
var reader = this,
book = this.book,
settings = reader.settings,
toc = document.getElementById("toc"),
tovView = document.getElementById("tocView"),
$toc_populate = $("#toc_populate"),
timeout;
var currentChapter = false;
var isVisible = function (element) {
var generateTocItems = function(toc, level) {
var container = document.createElement("ul");
var viewport = element.getBoundingClientRect(),
visible,
offset = settings.preloadOffset;
if(!level) level = 1;
visible = (
viewport.top >= (0 - offset)
&& viewport.left >= (0 - offset)
&& viewport.right < (window.innerWidth + offset)
&& viewport.bottom < (window.innerHeight + offset)
);
toc.forEach(function(chapter) {
var listitem = document.createElement("li"),
link = document.createElement("a");
toggle = document.createElement("a");
var subitems;
listitem.id = "toc-"+chapter.id;
listitem.classList.add('list_item');
link.textContent = chapter.label;
link.href = chapter.href;
link.classList.add('toc_link');
listitem.appendChild(link);
if(chapter.subitems.length > 0) {
level++;
subitems = generateTocItems(chapter.subitems, level);
toggle.classList.add('toc_toggle');
listitem.insertBefore(toggle, link);
listitem.appendChild(subitems);
}
container.appendChild(listitem);
});
return container;
return visible;
};
var lazyLoad = function () {
var elements = toc.querySelectorAll('img[data-pagenum]'),
pagenum,
count;
for (var i = 0; i < elements.length; i++) {
if (isVisible(elements[i])) {
pagenum = elements[i].getAttribute("data-pagenum");
elements[i].removeAttribute("data-pagenum");
reader.getThumb(pagenum, true);
count++;
}
}
if (!elements.length || count === elements.length ) {
removeLazyLoader(tocView);
}
};
var lazyLoader = function () {
timeout = setTimeout( function () {
lazyLoad();
}, settings.lazyDelay);
};
var addLazyLoader = function (_element) {
var element = _element || window;
element.addEventListener("scroll", lazyLoader, false);
element.addEventListener("load", lazyLoader, false);
};
var removeLazyLoader = function (_element) {
var element = _element || window;
element.removeEventListener("scroll", lazyLoader);
element.removeEventListener("load", lazyLoader);
};
var tocCreate = function (no_pages, width, height, populate) {
var canvas = document.createElement("canvas"),
ctx = canvas.getContext("2d"),
aspect,
imgsrc,
preloadcount,
scale,
timeout;
aspect = parseFloat(width / height);
// create small placeholder image
canvas.width = 10;
canvas.height = parseInt(canvas.width / aspect);
placeholder_width = reader.settings.thumbnailWidth;
placeholder_height = parseInt(reader.settings.thumbnailWidth / aspect);
// fill with transparent black, style in CSS
ctx.fillStyle = "rgba(0, 0, 0, 0)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
imgsrc = canvas.toDataURL();
for(var i = 0; i < no_pages; i++) {
var item = document.createElement('li'),
placeholder = new Image(),
label = document.createElement('span'),
page_label = reader.pageLabels[i + 1];
item.setAttribute("id", "page_" + parseInt(i + 1));
placeholder.src = imgsrc;
placeholder.style.width = reader.settings.thumbnailWidth;
placeholder.style.height = parseInt(reader.settings.thumbnailWidth / aspect);
placeholder.classList.add("placeholder");
label.innerHTML = page_label || (i + 1).toString();
label.classList.add("page_label");
//label.style.left = width;
item.appendChild(placeholder);
item.appendChild(label);
toc.appendChild(item);
if (populate) {
reader.getThumb(i + 1, true);
} else {
placeholder.setAttribute("data-pagenum", parseInt(i + 1));
}
}
if (!populate) {
// preload first screenfull of thumbnails
scale = parseFloat(settings.thumbnailWidth / width);
preloadcount = parseInt(window.innerHeight / placeholder_height) + 2;
if (preloadcount > settings.numPages)
preloadcount = numPages;
var _timeout = setTimeout(function () {
for (var i = 1; i <= preloadcount; i++) {
reader.getThumb(i, true);
}
}, settings.initialLazyDelay);
}
};
var tocInsert = function (image, page, replace) {
var placeholder = toc.children[page - 1].firstChild;
if (replace === true) {
placeholder.parentNode.replaceChild(image, placeholder);
}
toc.children[page - 1].addEventListener('click', function (e) {
reader.queuePage(page);
});
};
var tocPopulate = function () {
var i = 0;
while (i < reader.settings.numPages) {
reader.getThumb(i, true);
i++;
}
reader.thumbnails = true;
$toc_populate.addClass("hide");
remove_lazy_loader();
};
if (!settings.thumbnails) {
addLazyLoader(tocView);
}
reader.book.getPage(1).then(function(page) {
var width,
height,
viewport,
page_rotation,
rotation;
page_rotation = page.rotate;
rotation = (page_rotation + reader.settings.rotation) % 360;
viewport = page.getViewport(1, rotation);
width = viewport.width;
height = viewport.height;
tocCreate(settings.numPages, width, height, settings.thumbnails);
});
var onShow = function() {
$list.addClass('open');
tocView.classList.add('open');
};
var onHide = function() {
$list.removeClass('open');
tocView.classList.remove('open');
};
var chapterChange = function(e) {
var id = e.id,
$item = $list.find("#toc-"+id),
$current = $list.find(".currentChapter"),
$open = $list.find('.openChapter');
if($item.length){
if($item != $current && $item.has(currentChapter).length > 0) {
$current.removeClass("currentChapter");
}
$item.addClass("currentChapter");
// $open.removeClass("openChapter");
$item.parents('li').addClass("openChapter");
}
};
book.on('renderer:chapterDisplayed', chapterChange);
var tocitems = generateTocItems(toc);
docfrag.appendChild(tocitems);
$list.append(docfrag);
$list.find(".toc_link").on("click", function(event){
var url = this.getAttribute('href');
event.preventDefault();
//-- Provide the Book with the url to show
// The Url must be found in the books manifest
book.goto(url);
$list.find(".currentChapter")
.addClass("openChapter")
.removeClass("currentChapter");
$(this).parent('li').addClass("currentChapter");
});
$list.find(".toc_toggle").on("click", function(event){
var $el = $(this).parent('li'),
open = $el.hasClass("openChapter");
event.preventDefault();
if(open){
$el.removeClass("openChapter");
} else {
$el.addClass("openChapter");
}
});
return {
"show" : onShow,
"hide" : onHide
"hide" : onHide,
"tocInsert": tocInsert,
"totPopulate": tocPopulate
};
};