Skip to content

Commit bd67852

Browse files
committed
Move all properties to subprops (for proto change)
1 parent 4702ac9 commit bd67852

1 file changed

Lines changed: 45 additions & 44 deletions

File tree

src/worker.js

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ Worker.convert = function convert(promise, inherit) {
2424
};
2525

2626
Worker.template = {
27-
ready: null,
28-
src: null,
29-
container: null,
30-
overlay: null,
31-
canvas: null,
32-
img: null,
33-
pdf: null,
34-
pageSize: null,
27+
prop: {
28+
src: null,
29+
container: null,
30+
overlay: null,
31+
canvas: null,
32+
img: null,
33+
pdf: null,
34+
pageSize: null
35+
},
3536
progress: {
3637
val: 0,
3738
state: null,
@@ -90,8 +91,8 @@ Worker.prototype.to = function to(target) {
9091
Worker.prototype.toContainer = function toContainer() {
9192
// Set up function prerequisites.
9293
var prereqs = [
93-
function() { return this.src || this.error('Cannot duplicate - no source HTML.'); },
94-
function() { return this.pageSize || this.setPageSize(); }
94+
function() { return this.prop.src || this.error('Cannot duplicate - no source HTML.'); },
95+
function() { return this.prop.pageSize || this.setPageSize(); }
9596
];
9697

9798
return this.thenList(prereqs).then(function() {
@@ -102,7 +103,7 @@ Worker.prototype.toContainer = function toContainer() {
102103
backgroundColor: 'rgba(0,0,0,0.8)'
103104
};
104105
var containerCSS = {
105-
position: 'absolute', width: this.pageSize.inner.width + this.pageSize.unit,
106+
position: 'absolute', width: this.prop.pageSize.inner.width + this.prop.pageSize.unit,
106107
left: 0, right: 0, top: 0, height: 'auto', margin: 'auto',
107108
backgroundColor: 'white'
108109
};
@@ -111,16 +112,16 @@ Worker.prototype.toContainer = function toContainer() {
111112
overlayCSS.opacity = 0;
112113

113114
// Create and attach the elements.
114-
var source = cloneNode(this.src, this.opt.html2canvas.javascriptEnabled);
115-
this.overlay = createElement('div', { className: 'html2pdf__overlay', style: overlayCSS });
116-
this.container = createElement('div', { className: 'html2pdf__container', style: containerCSS });
117-
this.container.appendChild(source);
118-
this.overlay.appendChild(this.container);
119-
document.body.appendChild(this.overlay);
115+
var source = cloneNode(this.prop.src, this.opt.html2canvas.javascriptEnabled);
116+
this.prop.overlay = createElement('div', { className: 'html2pdf__overlay', style: overlayCSS });
117+
this.prop.container = createElement('div', { className: 'html2pdf__container', style: containerCSS });
118+
this.prop.container.appendChild(source);
119+
this.prop.overlay.appendChild(this.prop.container);
120+
document.body.appendChild(this.prop.overlay);
120121

121122
// Enable page-breaks.
122123
var pageBreaks = source.querySelectorAll('.html2pdf__page-break');
123-
var pxPageHeight = this.pageSize.inner.px.height;
124+
var pxPageHeight = this.prop.pageSize.inner.px.height;
124125
Array.prototype.forEach.call(pageBreaks, function(el) {
125126
el.style.display = 'block';
126127
var clientRect = el.getBoundingClientRect();
@@ -132,7 +133,7 @@ Worker.prototype.toContainer = function toContainer() {
132133
Worker.prototype.toCanvas = function toCanvas() {
133134
// Set up function prerequisites.
134135
var prereqs = [
135-
function() { return document.body.contains(this.container) || this.toContainer(); }
136+
function() { return document.body.contains(this.prop.container) || this.toContainer(); }
136137
];
137138

138139
// Fulfill prereqs then create the canvas.
@@ -141,51 +142,51 @@ Worker.prototype.toCanvas = function toCanvas() {
141142
var options = Object.assign({}, this.opt.html2canvas);
142143
delete options.onrendered;
143144

144-
return html2canvas(this.container, options);
145+
return html2canvas(this.prop.container, options);
145146
}).then(function(canvas) {
146147
// Handle old-fashioned 'onrendered' argument.
147148
var onRendered = this.opt.html2canvas.onrendered || function() {};
148149
onRendered(canvas);
149150

150-
this.canvas = canvas;
151-
document.body.removeChild(this.overlay);
151+
this.prop.canvas = canvas;
152+
document.body.removeChild(this.prop.overlay);
152153
});
153154
};
154155

155156
Worker.prototype.toImg = function toImg() {
156157
// Set up function prerequisites.
157158
var prereqs = [
158-
function() { return this.canvas || this.toCanvas(); }
159+
function() { return this.prop.canvas || this.toCanvas(); }
159160
];
160161

161162
// Fulfill prereqs then create the image.
162163
return this.thenList(prereqs).then(function() {
163-
var imgData = this.canvas.toDataURL('image/' + this.opt.image.type, this.opt.image.quality);
164-
this.img = document.createElement('img');
165-
this.img.src = imgData;
164+
var imgData = this.prop.canvas.toDataURL('image/' + this.opt.image.type, this.opt.image.quality);
165+
this.prop.img = document.createElement('img');
166+
this.prop.img.src = imgData;
166167
});
167168
};
168169

169170
Worker.prototype.toPdf = function toPdf() {
170171
// Set up function prerequisites.
171172
var prereqs = [
172-
function() { return this.canvas || this.toCanvas(); }
173+
function() { return this.prop.canvas || this.toCanvas(); }
173174
];
174175

175176
// Fulfill prereqs then create the image.
176177
return this.thenList(prereqs).then(function() {
177178
// Create local copies of frequently used properties.
178-
var canvas = this.canvas;
179+
var canvas = this.prop.canvas;
179180
var opt = this.opt;
180181

181182
// Calculate the number of pages.
182183
var ctx = canvas.getContext('2d');
183184
var pxFullHeight = canvas.height;
184-
var pxPageHeight = Math.floor(canvas.width * this.pageSize.inner.ratio);
185+
var pxPageHeight = Math.floor(canvas.width * this.prop.pageSize.inner.ratio);
185186
var nPages = Math.ceil(pxFullHeight / pxPageHeight);
186187

187188
// Define pageHeight separately so it can be trimmed on the final page.
188-
var pageHeight = this.pageSize.inner.height;
189+
var pageHeight = this.prop.pageSize.inner.height;
189190

190191
// Create a one-page canvas to split up the full image.
191192
var pageCanvas = document.createElement('canvas');
@@ -194,13 +195,13 @@ Worker.prototype.toPdf = function toPdf() {
194195
pageCanvas.height = pxPageHeight;
195196

196197
// Initialize the PDF.
197-
this.pdf = this.pdf || new jsPDF(opt.jsPDF);
198+
this.prop.pdf = this.prop.pdf || new jsPDF(opt.jsPDF);
198199

199200
for (var page=0; page<nPages; page++) {
200201
// Trim the final page to reduce file size.
201202
if (page === nPages-1) {
202203
pageCanvas.height = pxFullHeight % pxPageHeight;
203-
pageHeight = pageCanvas.height * this.pageSize.inner.width / pageCanvas.width;
204+
pageHeight = pageCanvas.height * this.prop.pageSize.inner.width / pageCanvas.width;
204205
}
205206

206207
// Display the page.
@@ -211,10 +212,10 @@ Worker.prototype.toPdf = function toPdf() {
211212
pageCtx.drawImage(canvas, 0, page*pxPageHeight, w, h, 0, 0, w, h);
212213

213214
// Add the page to the PDF.
214-
if (page) this.pdf.addPage();
215+
if (page) this.prop.pdf.addPage();
215216
var imgData = pageCanvas.toDataURL('image/' + opt.image.type, opt.image.quality);
216-
this.pdf.addImage(imgData, opt.image.type, opt.margin[1], opt.margin[0],
217-
this.pageSize.inner.width, pageHeight);
217+
this.prop.pdf.addImage(imgData, opt.image.type, opt.margin[1], opt.margin[0],
218+
this.prop.pageSize.inner.width, pageHeight);
218219
}
219220
});
220221
};
@@ -235,7 +236,7 @@ Worker.prototype.output = function output(type, options, src) {
235236
Worker.prototype.outputPdf = function outputPdf(type, options) {
236237
// Set up function prerequisites.
237238
var prereqs = [
238-
function() { return this.pdf || this.toPdf(); }
239+
function() { return this.prop.pdf || this.toPdf(); }
239240
];
240241

241242
// Fulfill prereqs then perform the appropriate output.
@@ -245,28 +246,28 @@ Worker.prototype.outputPdf = function outputPdf(type, options) {
245246
* save(options), arraybuffer, blob, bloburi/bloburl,
246247
* datauristring/dataurlstring, dataurlnewwindow, datauri/dataurl
247248
*/
248-
return this.pdf.output(type, options);
249+
return this.prop.pdf.output(type, options);
249250
});
250251
};
251252

252253
Worker.prototype.outputImg = function outputImg(type, options) {
253254
// Set up function prerequisites.
254255
var prereqs = [
255-
function() { return this.img || this.toImg(); }
256+
function() { return this.prop.img || this.toImg(); }
256257
];
257258

258259
// Fulfill prereqs then perform the appropriate output.
259260
return this.thenList(prereqs).then(function() {
260261
switch (type) {
261262
case undefined:
262263
case 'img':
263-
return this.img;
264+
return this.prop.img;
264265
case 'datauristring':
265266
case 'dataurlstring':
266-
return this.img.src;
267+
return this.prop.img.src;
267268
case 'datauri':
268269
case 'dataurl':
269-
return document.location.href = this.img.src;
270+
return document.location.href = this.prop.img.src;
270271
default:
271272
throw 'Image output type "' + type + '" is not supported.';
272273
}
@@ -276,14 +277,14 @@ Worker.prototype.outputImg = function outputImg(type, options) {
276277
Worker.prototype.save = function save(filename) {
277278
// Set up function prerequisites.
278279
var prereqs = [
279-
function() { return this.pdf || this.toPdf(); }
280+
function() { return this.prop.pdf || this.toPdf(); }
280281
];
281282

282283
// Fulfill prereqs, update the filename (if provided), and save the PDF.
283284
return this.thenList(prereqs).set(
284285
filename ? { filename: filename } : null
285286
).then(function() {
286-
this.pdf.save(this.opt.filename);
287+
this.prop.pdf.save(this.opt.filename);
287288
});
288289
};
289290

@@ -375,7 +376,7 @@ Worker.prototype.setPageSize = function setPageSize(pageSize) {
375376
}
376377

377378
// Attach pageSize to this.
378-
this.pageSize = pageSize;
379+
this.prop.pageSize = pageSize;
379380

380381
// Return this for command chaining.
381382
return this;

0 commit comments

Comments
 (0)