]> git.parisson.com Git - pdf.js.git/commitdiff
Implemented getPdf(). Closes #516
authorArtur Adib <arturadib@gmail.com>
Tue, 27 Sep 2011 17:30:47 +0000 (13:30 -0400)
committerArtur Adib <arturadib@gmail.com>
Tue, 27 Sep 2011 17:30:47 +0000 (13:30 -0400)
Conflicts:

examples/helloworld/hello.js

examples/helloworld/hello.js
pdf.js
test/driver.js
web/viewer.js

index 953700ae86275a9d963610a9bc8ea115775881a0..3b0f6dca7c2edb385bed545ce8b0f42416966f0d 100644 (file)
@@ -7,30 +7,7 @@
 
 'use strict';
 
-//
-// Ajax GET request, for binary files
-// (like jQuery's $.get(), but supports the binary type ArrayBuffer)
-//
-var ajaxGet = function(url, callback) {
-  var xhr = new XMLHttpRequest();
-  xhr.open('GET', url);
-  xhr.mozResponseType = xhr.responseType = 'arraybuffer';
-  xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;
-  xhr.onreadystatechange = function() {
-    if (xhr.readyState === 4 && xhr.status === xhr.expected) {
-      var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
-                  xhr.responseArrayBuffer || xhr.response);
-
-      callback(data);
-    }
-  };
-  xhr.send(null);
-};
-
-//
-// This is where the fun happens
-//
-ajaxGet('helloworld.pdf', function ajaxGetHelloWorld(data) {
+getPdf('helloworld.pdf', function(data){  
   //
   // Instantiate PDFDoc with PDF data
   //
diff --git a/pdf.js b/pdf.js
index 449fd9c16f858e29b69517d51efadb10c4eb8aa0..197c6e6d8cb1f741a796d900c302a4412d7fcd3e 100644 (file)
--- a/pdf.js
+++ b/pdf.js
@@ -112,6 +112,35 @@ function stringToPDFString(str) {
   return str2;
 }
 
+//
+// getPdf()
+// Convenience function to perform binary Ajax GET
+// Usage: getPdf('http://...', callback)
+//        getPdf({url:String [,progress:Function]}, callback)
+//
+function getPdf(arg, callback) {
+  var params = arg;
+  if (typeof arg === 'string') {
+    params = {url: arg};
+  }
+
+  var xhr = new XMLHttpRequest();
+  xhr.open('GET', params.url);
+  xhr.mozResponseType = xhr.responseType = 'arraybuffer';
+  xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;
+  xhr.onprogress = params.progress || undefined;
+  
+  xhr.onreadystatechange = function() {
+    var data;
+    if (xhr.readyState === 4 && xhr.status === xhr.expected) {
+      data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
+                  xhr.responseArrayBuffer || xhr.response);
+      callback(data);
+    }
+  };
+  xhr.send(null);  
+}
+
 var Stream = (function streamStream() {
   function constructor(arrayBuffer, start, length, dict) {
     this.bytes = new Uint8Array(arrayBuffer);
index 144b97589eeb199563fb081cd0b033a934d2c94a..e948835dd2cf334c3567c3051ecc94127a5d5059 100644 (file)
@@ -73,26 +73,16 @@ function nextTask() {
 
   log('Loading file "' + task.file + '"\n');
 
-  var r = new XMLHttpRequest();
-  r.open('GET', task.file);
-  r.mozResponseType = r.responseType = 'arraybuffer';
-  r.onreadystatechange = function nextTaskOnreadystatechange() {
+  getPdf(task.file, function(data){
     var failure;
-    if (r.readyState == 4) {
-      var data = r.mozResponseArrayBuffer || r.mozResponse ||
-        r.responseArrayBuffer || r.response;
-
-      try {
-        task.pdfDoc = new PDFDoc(data);
-      } catch (e) {
-        failure = 'load PDF doc : ' + e.toString();
-      }
-
-      task.pageNum = 1;
-      nextPage(task, failure);
+    try {
+      task.pdfDoc = new PDFDoc(data);
+    } catch (e) {
+      failure = 'load PDF doc : ' + e.toString();
     }
-  };
-  r.send(null);
+    task.pageNum = 1;
+    nextPage(task, failure);
+  });
 }
 
 function isLastPage(task) {
index 482ac95e09c11007b8dca4dd8e92f4c2c9e6d31b..2cc3aaa4f24b92304fc2fc4cfcf5cd1d94ac1be6 100644 (file)
@@ -107,23 +107,10 @@ var PDFView = {
 
     document.title = url;
 
-    var xhr = new XMLHttpRequest();
-    xhr.open('GET', url);
-    xhr.mozResponseType = xhr.responseType = 'arraybuffer';
-    xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;
-    xhr.onprogress = PDFView.progressLevel;
-
-    xhr.onreadystatechange = function() {
-      if (xhr.readyState === 4 && xhr.status === xhr.expected) {
-        var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
-                    xhr.responseArrayBuffer || xhr.response);
-
-        document.getElementById('loading').style.display = 'none';
-        PDFView.load(data, scale);
-      }
-    };
-
-    xhr.send(null);
+    getPdf({url:url, progress:PDFView.progressLevel}, function(data) {
+      document.getElementById('loading').style.display = 'none';
+      PDFView.load(data, scale);
+    });
   },
 
   progressLevel: function(evt) {