[Pkg-javascript-commits] [pdf.js] 82/116: Refactor PDFAttachmentView to be more class-like and to separate functionality into methods

David Prévot taffit at moszumanska.debian.org
Fri Mar 6 16:20:03 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository pdf.js.

commit ea1d37eb0d2f931b4ab6baf2d7ec585fe72c37e7
Author: Tim van der Meij <timvandermeij at gmail.com>
Date:   Tue Jan 27 22:51:39 2015 +0100

    Refactor PDFAttachmentView to be more class-like and to separate functionality into methods
---
 web/pdf_outline_view.js | 87 +++++++++++++++++++++++++++++--------------------
 web/viewer.js           |  7 ++--
 2 files changed, 55 insertions(+), 39 deletions(-)

diff --git a/web/pdf_outline_view.js b/web/pdf_outline_view.js
index 1b01860..f8fcc04 100644
--- a/web/pdf_outline_view.js
+++ b/web/pdf_outline_view.js
@@ -17,48 +17,63 @@
 
 'use strict';
 
-var PDFOutlineView = function documentOutlineView(options) {
-  var outline = options.outline;
-  var outlineView = options.outlineView;
-  while (outlineView.firstChild) {
-    outlineView.removeChild(outlineView.firstChild);
+var PDFOutlineView = (function PDFOutlineViewClosure() {
+  function PDFOutlineView(options) {
+    this.container = options.container;
+    this.outline = options.outline;
+    this.linkService = options.linkService;
   }
 
-  if (!outline) {
-    return;
-  }
+  PDFOutlineView.prototype = {
+    reset: function PDFOutlineView_reset() {
+      var container = this.container;
+      while (container.firstChild) {
+        container.removeChild(container.firstChild);
+      }
+    },
 
-  var linkService = options.linkService;
+    _bindLink: function PDFOutlineView_bindLink(element, item) {
+      var linkService = this.linkService;
+      element.href = linkService.getDestinationHash(item.dest);
+      element.onclick = function goToDestination(e) {
+        linkService.navigateTo(item.dest);
+        return false;
+      };
+    },
 
-  function bindItemLink(domObj, item) {
-    domObj.href = linkService.getDestinationHash(item.dest);
-    domObj.onclick = function documentOutlineViewOnclick(e) {
-      linkService.navigateTo(item.dest);
-      return false;
-    };
-  }
+    render: function PDFOutlineView_render() {
+      var outline = this.outline;
 
-  var queue = [{parent: outlineView, items: outline}];
-  while (queue.length > 0) {
-    var levelData = queue.shift();
-    var i, n = levelData.items.length;
-    for (i = 0; i < n; i++) {
-      var item = levelData.items[i];
-      var div = document.createElement('div');
-      div.className = 'outlineItem';
-      var a = document.createElement('a');
-      bindItemLink(a, item);
-      a.textContent = item.title;
-      div.appendChild(a);
+      this.reset();
 
-      if (item.items.length > 0) {
-        var itemsDiv = document.createElement('div');
-        itemsDiv.className = 'outlineItems';
-        div.appendChild(itemsDiv);
-        queue.push({parent: itemsDiv, items: item.items});
+      if (!outline) {
+        return;
       }
 
-      levelData.parent.appendChild(div);
+      var queue = [{ parent: this.container, items: this.outline }];
+      while (queue.length > 0) {
+        var levelData = queue.shift();
+        for (var i = 0, len = levelData.items.length; i < len; i++) {
+          var item = levelData.items[i];
+          var div = document.createElement('div');
+          div.className = 'outlineItem';
+          var element = document.createElement('a');
+          this._bindLink(element, item);
+          element.textContent = item.title;
+          div.appendChild(element);
+
+          if (item.items.length > 0) {
+            var itemsDiv = document.createElement('div');
+            itemsDiv.className = 'outlineItems';
+            div.appendChild(itemsDiv);
+            queue.push({ parent: itemsDiv, items: item.items });
+          }
+
+          levelData.parent.appendChild(div);
+        }
+      }
     }
-  }
-};
+  };
+
+  return PDFOutlineView;
+})();
diff --git a/web/viewer.js b/web/viewer.js
index b6c1403..ac3a490 100644
--- a/web/viewer.js
+++ b/web/viewer.js
@@ -966,15 +966,16 @@ var PDFViewerApplication = {
     var promises = [pagesPromise, this.animationStartedPromise];
     Promise.all(promises).then(function() {
       pdfDocument.getOutline().then(function(outline) {
-        var outlineView = document.getElementById('outlineView');
+        var container = document.getElementById('outlineView');
         self.outline = new PDFOutlineView({
+          container: container,
           outline: outline,
-          outlineView: outlineView,
           linkService: self
         });
+        self.outline.render();
         document.getElementById('viewOutline').disabled = !outline;
 
-        if (!outline && !outlineView.classList.contains('hidden')) {
+        if (!outline && !container.classList.contains('hidden')) {
           self.switchSidebarView('thumbs');
         }
         if (outline &&

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/pdf.js.git



More information about the Pkg-javascript-commits mailing list