pdfview: state of a pdf-viewer should be on instance and not on prototype

so that we can have 2 pdf-viewer tags on the document rendering different pdfs

see. https://github.com/rahmathm1/pdf-viewer/pull/2

authorSamuel Trégouët <samuel.tregouet@logilab.fr>
changeset66cf5d7a04a2
branchdefault
phasepublic
hiddenno
parent revision#46d371948774 pdfview: add basic pdfview (closes #289639)
child revision#a6c47feea690 add a basic plaintextview (related to #289632)
files modified by this revision
components/pdfview/pdf-viewer/pdf-viewer.js
# HG changeset patch
# User Samuel Trégouët <samuel.tregouet@logilab.fr>
# Date 1427978595 -7200
# Thu Apr 02 14:43:15 2015 +0200
# Node ID 66cf5d7a04a25bdd05ed33171962f2961022591d
# Parent 46d371948774405e28a5e3285c188eb276a2e7a1
pdfview: state of a pdf-viewer should be on instance and not on prototype

so that we can have 2 pdf-viewer tags on the document rendering different pdfs

see. https://github.com/rahmathm1/pdf-viewer/pull/2

diff --git a/components/pdfview/pdf-viewer/pdf-viewer.js b/components/pdfview/pdf-viewer/pdf-viewer.js
@@ -24,82 +24,82 @@
1          // Adds a template clone into shadow root
2          var clone = thatDoc.importNode(template, true);
3          shadowRoot.appendChild(clone);
4 
5          // Caches <canvas> DOM query
6 -        pdfProto.canvas = shadowRoot.getElementById('pdf-canvas');
7 +        this.canvas = shadowRoot.getElementById('pdf-canvas');
8 
9          if (this.hasAttribute('path')) {
10              var path = this.getAttribute('path');
11              this.setPath(path);
12          }
13      };
14 
15      pdfProto.renderBook = function() {
16 -        pdfProto.pageNumPending = null;
17 -        pdfProto.pageRendering = false;
18 -        pdfProto.pageNumPending = null;
19 -        pdfProto.ctx = pdfProto.canvas.getContext('2d');
20 -        pdfProto.scale = 2;
21 -        pdfProto.pageNum = 1;
22 -        pdfProto.isReady = false;
23 +        this.pageNumPending = null;
24 +        this.pageRendering = false;
25 +        this.pageNumPending = null;
26 +        this.ctx = this.canvas.getContext('2d');
27 +        this.scale = 2;
28 +        this.pageNum = 1;
29 +        this.isReady = false;
30 
31          //PDFJS.disableWorker = true;
32          PDFJS.workerSrc = '/components/pdfview/pdf-viewer/js/pdf/pdf.worker.js';
33          PDFJS.getDocument(this.path).then(function(pdfDoc) {
34 -            pdfProto.pdf = pdfDoc;
35 -            pdfProto.renderPage(pdfProto.pageNum);
36 -        });
37 +            this.pdf = pdfDoc;
38 +            this.renderPage(this.pageNum);
39 +        }.bind(this));
40      };
41 
42      pdfProto.renderPage = function(num) {
43 -        pdfProto.pageRendering = true;
44 -        pdfProto.pdf.getPage(num).then(function(page) {
45 -            var viewport = page.getViewport(pdfProto.scale);
46 -            pdfProto.canvas.height = viewport.height;
47 -            pdfProto.canvas.width = viewport.width;
48 +        this.pageRendering = true;
49 +        this.pdf.getPage(num).then(function(page) {
50 +            var viewport = page.getViewport(this.scale);
51 +            this.canvas.height = viewport.height;
52 +            this.canvas.width = viewport.width;
53 
54              var renderContext = {
55 -                canvasContext: pdfProto.ctx,
56 +                canvasContext: this.ctx,
57                  viewport: viewport
58              };
59              var renderTask = page.render(renderContext);
60 -            pdfProto.isReady = true;
61 +            this.isReady = true;
62 
63              renderTask.promise.then(function() {
64 -                pdfProto.pageRendering = false;
65 -                if (pdfProto.pageNumPending !== null) {
66 -                    pdfProto.renderPage(pdfProto.pageNumPending);
67 -                    pdfProto.pageNumPending = null;
68 +                this.pageRendering = false;
69 +                if (this.pageNumPending !== null) {
70 +                    this.renderPage(this.pageNumPending);
71 +                    this.pageNumPending = null;
72                  }
73 -            });
74 -        });
75 +            }.bind(this));
76 +        }.bind(this));
77      };
78 
79      pdfProto.nextPage = function() {
80 -        if (pdfProto.pageNum >= pdfProto.pdf.numPages) {
81 +        if (this.pageNum >= this.pdf.numPages) {
82              return false;
83          }
84 -        pdfProto.pageNum++;
85 -        pdfProto.queueRenderPage(pdfProto.pageNum);
86 +        this.pageNum++;
87 +        this.queueRenderPage(this.pageNum);
88          return true;
89      };
90 
91      pdfProto.prevPage = function() {
92 -        if (pdfProto.pageNum <= 1) {
93 +        if (this.pageNum <= 1) {
94              return false;
95          }
96 -        pdfProto.pageNum--;
97 -        pdfProto.queueRenderPage(pdfProto.pageNum);
98 +        this.pageNum--;
99 +        this.queueRenderPage(this.pageNum);
100          return true;
101      };
102 
103      pdfProto.queueRenderPage = function(num) {
104 -        if (pdfProto.pageRendering) {
105 -            pdfProto.pageNumPending = num;
106 +        if (this.pageRendering) {
107 +            this.pageNumPending = num;
108          } else {
109 -            pdfProto.renderPage(num);
110 +            this.renderPage(num);
111          }
112      };
113 
114      // Fires when an attribute was added, removed, or updated
115      pdfProto.attributeChangedCallback = function(attr, oldVal, newVal) {