Merge pull request #47 from justindarc/master

Added local file support to the multi-page viewer
This commit is contained in:
Andreas Gal 2011-06-21 21:51:12 -07:00
commit 669d7f6efb
5 changed files with 144 additions and 19 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

View File

@ -136,10 +136,61 @@ span {
background: url('images/buttons.png') no-repeat -28px 0px; background: url('images/buttons.png') no-repeat -28px 0px;
} }
#openFileButton {
background: url('images/buttons.png') no-repeat -56px -23px;
cursor: default;
display: inline-block;
float: left;
margin: 0px 0px 0px 3px;
width: 29px;
height: 23px;
}
#openFileButton.down {
background: url('images/buttons.png') no-repeat -56px -46px;
}
#openFileButton.disabled {
background: url('images/buttons.png') no-repeat -56px 0px;
}
#fileInput {
display: none;
}
#pageNumber { #pageNumber {
text-align: right; text-align: right;
} }
#sidebar {
background-color: rgba(0, 0, 0, 0.8);
position: fixed;
width: 150px;
top: 62px;
bottom: 18px;
border-top-right-radius: 8px;
border-bottom-right-radius: 8px;
-moz-border-radius-topright: 8px;
-moz-border-radius-bottomright: 8px;
-webkit-border-top-right-radius: 8px;
-webkit-border-bottom-right-radius: 8px;
}
#sidebarScrollView {
position: absolute;
overflow: hidden;
overflow-y: auto;
top: 40px;
right: 10px;
bottom: 10px;
left: 10px;
}
#sidebarContentView {
height: auto;
width: 100px;
}
#viewer { #viewer {
margin: 44px 0px 0px; margin: 44px 0px 0px;
padding: 8px 0px; padding: 8px 0px;

View File

@ -33,7 +33,19 @@
</select> </select>
<span class="label">Zoom</span> <span class="label">Zoom</span>
</span> </span>
<span class="control">
<span id="openFileButton"></span>
<input type="file" id="fileInput"/>
<span class="label">Open File</span>
</span>
</div> </div>
<!--<div id="sidebar">
<div id="sidebarScrollView">
<div id="sidebarContentView">
</div>
</div>
</div>-->
<div id="viewer"></div> <div id="viewer"></div>
</body> </body>
</html> </html>

View File

@ -12,6 +12,7 @@ var PDFViewer = {
nextPageButton: null, nextPageButton: null,
pageNumberInput: null, pageNumberInput: null,
scaleSelect: null, scaleSelect: null,
fileInput: null,
willJumpToPage: false, willJumpToPage: false,
@ -215,7 +216,7 @@ var PDFViewer = {
} }
}, },
open: function(url) { openURL: function(url) {
PDFViewer.url = url; PDFViewer.url = url;
document.title = url; document.title = url;
@ -231,6 +232,18 @@ var PDFViewer = {
req.responseArrayBuffer || req.responseArrayBuffer ||
req.response; req.response;
PDFViewer.readPDF(data);
}
};
req.send(null);
},
readPDF: function(data) {
while (PDFViewer.element.hasChildNodes()) {
PDFViewer.element.removeChild(PDFViewer.element.firstChild);
}
PDFViewer.pdf = new PDFDoc(new Stream(data)); PDFViewer.pdf = new PDFDoc(new Stream(data));
PDFViewer.numberOfPages = PDFViewer.pdf.numPages; PDFViewer.numberOfPages = PDFViewer.pdf.numPages;
document.getElementById('numPages').innerHTML = PDFViewer.numberOfPages.toString(); document.getElementById('numPages').innerHTML = PDFViewer.numberOfPages.toString();
@ -241,6 +254,7 @@ var PDFViewer = {
if (PDFViewer.numberOfPages > 0) { if (PDFViewer.numberOfPages > 0) {
PDFViewer.drawPage(1); PDFViewer.drawPage(1);
document.location.hash = 1;
} }
PDFViewer.previousPageButton.className = (PDFViewer.pageNumber === 1) ? PDFViewer.previousPageButton.className = (PDFViewer.pageNumber === 1) ?
@ -250,10 +264,6 @@ var PDFViewer = {
} }
}; };
req.send(null);
}
};
window.onload = function() { window.onload = function() {
// Parse the URL query parameters into a cached object. // Parse the URL query parameters into a cached object.
@ -355,10 +365,62 @@ window.onload = function() {
PDFViewer.changeScale(parseInt(this.value)); PDFViewer.changeScale(parseInt(this.value));
}; };
if (window.File && window.FileReader && window.FileList && window.Blob) {
var openFileButton = document.getElementById('openFileButton');
openFileButton.onclick = function(evt) {
if (this.className.indexOf('disabled') === -1) {
PDFViewer.fileInput.click();
}
};
openFileButton.onmousedown = function(evt) {
if (this.className.indexOf('disabled') === -1) {
this.className = 'down';
}
};
openFileButton.onmouseup = function(evt) {
this.className = (this.className.indexOf('disabled') !== -1) ? 'disabled' : '';
};
openFileButton.onmouseout = function(evt) {
this.className = (this.className.indexOf('disabled') !== -1) ? 'disabled' : '';
};
PDFViewer.fileInput = document.getElementById('fileInput');
PDFViewer.fileInput.onchange = function(evt) {
var files = evt.target.files;
if (files.length > 0) {
var file = files[0];
var fileReader = new FileReader();
document.title = file.name;
// Read the local file into a Uint8Array.
fileReader.onload = function(evt) {
var data = evt.target.result;
var buffer = new ArrayBuffer(data.length);
var uint8Array = new Uint8Array(buffer);
for (var i = 0; i < data.length; i++) {
uint8Array[i] = data.charCodeAt(i);
}
PDFViewer.readPDF(uint8Array);
};
// Read as a binary string since "readAsArrayBuffer" is not yet
// implemented in Firefox.
fileReader.readAsBinaryString(file);
}
};
PDFViewer.fileInput.value = null;
} else {
document.getElementById('fileWrapper').style.display = 'none';
}
PDFViewer.pageNumber = parseInt(PDFViewer.queryParams.page) || PDFViewer.pageNumber; PDFViewer.pageNumber = parseInt(PDFViewer.queryParams.page) || PDFViewer.pageNumber;
PDFViewer.scale = parseInt(PDFViewer.scaleSelect.value) / 100 || 1.0; PDFViewer.scale = parseInt(PDFViewer.scaleSelect.value) / 100 || 1.0;
PDFViewer.open(PDFViewer.queryParams.file || PDFViewer.url); PDFViewer.openURL(PDFViewer.queryParams.file || PDFViewer.url);
window.onscroll = function(evt) { window.onscroll = function(evt) {
var lastPagesDrawn = PDFViewer.lastPagesDrawn; var lastPagesDrawn = PDFViewer.lastPagesDrawn;