]> git.parisson.com Git - pdf.js.git/commitdiff
Implemented Settings manager. Now remembering scroll positions
authorSaebekassebil <saebekassebil@gmail.com>
Thu, 22 Dec 2011 21:29:01 +0000 (22:29 +0100)
committerSaebekassebil <saebekassebil@gmail.com>
Thu, 22 Dec 2011 21:29:01 +0000 (22:29 +0100)
src/core.js
src/obj.js
web/viewer.js

index 93cbc72acd4b83da3c418a08444ec8695f2eca75..ac140dafe76afd766e52f8702b46898b4ebfdc77 100644 (file)
@@ -527,6 +527,14 @@ var PDFDocModel = (function PDFDocModelClosure() {
                            this.startXRef,
                            this.mainXRefEntriesOffset);
       this.catalog = new Catalog(this.xref);
+      
+      if(this.xref.trailer && this.xref.trailer.has('ID')) {
+        var fileID = '';
+        this.xref.trailer.get('ID')[0].split('').forEach(function(el) {
+          fileID += Number(el.charCodeAt(0)).toString(16);
+        });
+        this.fileID = fileID;
+      }
     },
     get numPages() {
       var linearization = this.linearization;
@@ -560,7 +568,7 @@ var PDFDoc = (function PDFDocClosure() {
     this.data = data;
     this.stream = stream;
     this.pdf = new PDFDocModel(stream);
-
+    this.fileID = this.pdf.fileID;
     this.catalog = this.pdf.catalog;
     this.objs = new PDFObjects();
 
index 453014a91cbb2f2c29814f6770822e0497a9c216..c0e67efbb08da8b23ac5e04a7fcfd38477a2647c 100644 (file)
@@ -273,7 +273,7 @@ var XRef = (function XRefClosure() {
     this.entries = [];
     this.xrefstms = {};
     var trailerDict = this.readXRef(startXRef);
-
+    this.trailer = trailerDict;
     // prepare the XRef cache
     this.cache = [];
 
index e2ffcef2920d25a12a1472a64363def826717abc..5e8ec773f3aefdb8a1e8d23cf75f918ed23c7d2c 100644 (file)
@@ -25,6 +25,61 @@ var Cache = function cacheCache(size) {
   };
 };
 
+// Settings Manager - This is a utility for saving settings
+// First we see if localStorage is available, which isn't pt. in FF due to bug #495747
+// If not, we use FUEL in FF and fallback to Cookies for other browsers.
+(function(parent) {
+var COOKIE_WORKS = (function() {
+  document.cookie = 'they=work';
+  return document.cookie.length > 0;
+})();
+
+var LOCALSTORAGE_WORKS = (function() {
+  try {
+    if(typeof localStorage != 'undefined') {
+      return true;
+    }
+  } catch(e) {
+    return false;
+  }
+  return true;
+})();
+
+var extPrefix = 'extensions.uriloader@pdf.js';
+
+var Settings = {
+  set: function(name, val) {
+    if(location.protocol == 'chrome:' && !LOCALSTORAGE_WORKS) {
+      Application.prefs.setValue(extPrefix + '.' + name, val); 
+    } else if(LOCALSTORAGE_WORKS) {
+      localStorage.setItem(name, val);
+    } else if(COOKIE_WORKS) {
+      var cookieString = name + '=' + escape(val);
+      var expire = (new Date((new Date().getTime())+1000*60*60*24*365)).toGMTString();
+      cookieString += '; expires='+expire;
+      document.cookie = cookieString; 
+    } 
+  },
+
+  get: function(name, defaultValue) {
+    if(location.protocol == 'chrome:' && !LOCALSTORAGE_WORKS) {
+      return Application.prefs.getValue(extPrefix + '.' + name, defaultValue); 
+    } else if(LOCALSTORAGE_WORKS) {
+      return localStorage.getItem(name) || defaultValue;
+    } else if(COOKIE_WORKS) {
+      var res = document.cookie.match ( '(^|;) ?' + name + '=([^;]*)(;|$)' );
+      if (res) {
+       return unescape(res[2]);
+      } else {
+       return fallback;
+      }
+    }
+  }
+};
+
+parent.Settings = Settings;
+})(this);
+
 var cache = new Cache(kCacheSize);
 var currentPageNumber = 1;
 
@@ -292,6 +347,15 @@ var PDFView = {
       pagesRefMap[pageRef.num + ' ' + pageRef.gen + ' R'] = i;
     }
 
+    var id = pdf.fileID;
+    if (id) {
+      var scroll = Settings.get(id + '.scroll', -1);
+      if (scroll != -1) {
+        setTimeout(function scrollWindow() {
+          window.scrollTo(0, scroll);
+        }, 0);
+      }
+    }
     this.pagesRefMap = pagesRefMap;
     this.destinations = pdf.catalog.destinations;
     this.setScale(scale || kDefaultScale, true);
@@ -831,6 +895,10 @@ function updateViewarea() {
 
 window.addEventListener('scroll', function webViewerScroll(evt) {
   updateViewarea();
+  var fileID;
+  if((fileID = PDFView.pages[0].content.pdf.fileID)) {
+    Settings.set(fileID+'.scroll', window.pageYOffset);
+  }
 }, true);
 
 
@@ -888,7 +956,6 @@ window.addEventListener('change', function webViewerChange(evt) {
   // implemented in Firefox.
   var file = files[0];
   fileReader.readAsBinaryString(file);
-
   document.title = file.name;
 
   // URL does not reflect proper document location - hiding some icons.