]> git.parisson.com Git - pdf.js.git/commitdiff
Firefox extension now use the default loading indicator
authorVivien Nicolas <21@vingtetun.org>
Wed, 28 Sep 2011 10:53:53 +0000 (12:53 +0200)
committerVivien Nicolas <21@vingtetun.org>
Wed, 28 Sep 2011 10:53:53 +0000 (12:53 +0200)
Makefile
extensions/firefox/components/pdfContentHandler.js
web/viewer.js

index 56b597e5f0eccc18622b6b38e795519b28704ba8..4499a57e7f5a3092fe4f9b09aa8868814c5406a2 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -165,9 +165,9 @@ PDF_WEB_FILES = \
 extension:
        # Copy a standalone version of pdf.js inside the content directory
        @rm -Rf $(EXTENSION_SRC)/$(CONTENT_DIR)/
-       @mkdir $(EXTENSION_SRC)/$(CONTENT_DIR)/
+       @mkdir -p $(EXTENSION_SRC)/$(CONTENT_DIR)/web
        @cp $(PDF_JS_FILES) $(EXTENSION_SRC)/$(CONTENT_DIR)/ 
-       @cp -r $(PDF_WEB_FILES) $(EXTENSION_SRC)/$(CONTENT_DIR)/ 
+       @cp -r $(PDF_WEB_FILES) $(EXTENSION_SRC)/$(CONTENT_DIR)/web/
 
        # Create the xpi
        @cd $(EXTENSION_SRC); zip -r $(EXTENSION_NAME) *
index bafb83b12830f2063b3280ccb86b4b98e2c10dd6..ab5ec9654e642da05ed8a4ceba80eb5ad903d077 100644 (file)
@@ -8,9 +8,6 @@ const PDF_CONTENT_TYPE = "application/pdf";
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 
-// TODO
-// Add some download progress event
-
 function log(aMsg) {
   let msg = "pdfContentHandler.js: " + (aMsg.join ? aMsg.join("") : aMsg);
   Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService)
@@ -18,33 +15,45 @@ function log(aMsg) {
   dump(msg + "\n");
 };
 
+function fireEventTo(aName, aData, aWindow) {
+  let window = aWindow.wrappedJSObject;
+  let evt = window.document.createEvent("CustomEvent");
+  evt.initCustomEvent("pdf" + aName, false, false, aData);
+  window.document.dispatchEvent(evt);
+};
+
 function loadDocument(aWindow, aDocumentUrl) {
   let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
               .createInstance(Ci.nsIXMLHttpRequest);
-  xhr.open("GET", aDocumentUrl);
-  xhr.mozResponseType = xhr.responseType = "arraybuffer";
-  xhr.onreadystatechange = function() {
-    if (xhr.readyState == 4 && xhr.status == 200) {
-      let data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
-                  xhr.responseArrayBuffer || xhr.response);
-      try {
-        var view = new Uint8Array(data);
-
-        // I think accessing aWindow.wrappedJSObject returns a 
-        // XPCSafeJSObjectWrapper and so it is safe but mrbkap can confirm that
-        let window = aWindow.wrappedJSObject;
-        var arrayBuffer = new window.ArrayBuffer(data.byteLength);
-        var view2 = new window.Uint8Array(arrayBuffer);
-        view2.set(view);
-
-        let evt = window.document.createEvent("CustomEvent");
-        evt.initCustomEvent("pdfloaded", false, false, arrayBuffer);
-        window.document.dispatchEvent(evt);
-      } catch(e) {
-        log("Error - " + e);
-      }
+
+  xhr.onprogress = function updateProgress(evt) {
+    if (evt.lengthComputable)
+      fireEventTo(evt.type, evt.loaded / evt.total, aWindow);
+  };
+
+  xhr.onerror = function error(evt) {
+    fireEventTo(evt.type, false, aWindow);
+  };
+
+  xhr.onload = function load(evt) {
+    let data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
+                xhr.responseArrayBuffer || xhr.response);
+    try {
+      let view = new Uint8Array(data);
+
+      let window = aWindow.wrappedJSObject;
+      let arrayBuffer = new window.ArrayBuffer(data.byteLength);
+      let view2 = new window.Uint8Array(arrayBuffer);
+      view2.set(view);
+
+      fireEventTo(evt.type, arrayBuffer, aWindow);
+    } catch(e) {
+      log("Error - " + e);
     }
   };
+
+  xhr.open("GET", aDocumentUrl);
+  xhr.responseType = "arraybuffer";
   xhr.send(null);
 };
 
@@ -131,7 +140,7 @@ pdfContentHandler.prototype = {
       url = url.replace("%s", uri.spec);
       window.location = url;
     } catch(e) {
-      log("Error - " + e);
+      log("Error retrieving the pdf.js base url - " + e);
     }
   },
 
index 482ac95e09c11007b8dca4dd8e92f4c2c9e6d31b..68071103b27457d1c1fe43faec8f09ed964dd94f 100644 (file)
@@ -111,14 +111,17 @@ var PDFView = {
     xhr.open('GET', url);
     xhr.mozResponseType = xhr.responseType = 'arraybuffer';
     xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;
-    xhr.onprogress = PDFView.progressLevel;
+    xhr.onprogress = function updateProgress(evt) {
+      if (evt.lengthComputable)
+        PDFView.progress(evt.loaded / evt.total);
+    };
+
+    xhr.onerror = PDFView.error;
 
     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);
       }
     };
@@ -126,11 +129,6 @@ var PDFView = {
     xhr.send(null);
   },
 
-  progressLevel: function(evt) {
-    var p = Math.round((evt.loaded / evt.total) * 100);
-    document.getElementById('loading').innerHTML = 'Loading... ' + p + '%';
-  },
-
   navigateTo: function(dest) {
     if (typeof dest === 'string')
       dest = this.destinations[dest];
@@ -147,7 +145,21 @@ var PDFView = {
     }
   },
 
+  error: function() {
+    var loadingIndicator = document.getElementById('loading');
+    loadingIndicator.innerHTML = 'Error';
+  },
+
+  progress: function(level) {
+    var percent = Math.round(level * 100);
+    var loadingIndicator = document.getElementById('loading');
+    loadingIndicator.innerHTML = 'Loading... ' + percent + '%';
+  },
+
   load: function(data, scale) {
+    var loadingIndicator = document.getElementById('loading');
+    loadingIndicator.style.display = 'none';
+
     var sidebar = document.getElementById('sidebarView');
     sidebar.parentNode.scrollTop = 0;
 
@@ -495,10 +507,18 @@ window.addEventListener('load', function(evt) {
     document.getElementById('fileInput').value = null;
 }, true);
 
-window.addEventListener('pdfloaded', function(evt) {
+window.addEventListener('pdfload', function(evt) {
   PDFView.load(evt.detail);
 }, true);
 
+window.addEventListener('pdfprogress', function(evt) {
+  PDFView.progress(evt.detail);
+}, true);
+
+window.addEventListener('pdferror', function(evt) {
+  PDFView.error();
+}, true);
+
 function updateViewarea() {
   var visiblePages = PDFView.getVisiblePages();
   for (var i = 0; i < visiblePages.length; i++) {