-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintEditorPrint.js
557 lines (484 loc) · 23.9 KB
/
printEditorPrint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
(function () {
let printMenuId = EditorUiSdk.createAction({
label: 'Print',
});
EditorUiSdk.createSubAction(printMenuId, {
label: 'Print article',
icon: '',
click: function () {
ContentStationSdk.showNotification({
content: 'The print preview is being generated, one moment please...'
});
printArticle(true);
}
});
EditorUiSdk.createSubAction(printMenuId, {
label: 'Print compare',
icon: '',
click: function () {
ContentStationSdk.showNotification({
content: 'The print preview is being generated, one moment please...'
});
printArticle(false);
}
});
loadJQuery()
async function addScript(src, integrity, crossOrigin) {
return new Promise((resolve, reject) => {
// Use pure JS as we cannot assume jQuery is available.
const script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
script.integrity = integrity;
script.crossOrigin = crossOrigin;
script.onload = (event) => {
resolve(event);
};
script.onerror = (event) => {
reject(event);
};
document.head.appendChild(script);
});
}
async function loadJQuery() {
// Source: https://code.jquery.com/
await addScript(
'https://code.jquery.com/jquery-3.7.1.min.js',
'sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=',
'anonymous',
);
// Ensure that our jQuery dependency is not conflicting with jQuery in the main application.
this.$ = jQuery.noConflict();
initPrintThis();
}
/**
* Post request
*
* @param {*} url
* @param {*} request
* @param {*} callback
*/
function callAjax(url, request, callback) {
$.ajaxSetup({
headers: {
'X-WoodWing-Application': 'Content Station'
}
});
$.post(url, JSON.stringify(request),
function (data) {
callback(data);
}
);
}
/**
* Get the article metadata and print the article
*/
function printArticle(articleView) {
var info = ContentStationSdk.getInfo();
var articleId = window.location.href.substr(window.location.href.lastIndexOf('/') + 1);
var getObjectsRequest = {
"method": "GetObjects",
"id": "1",
"params": [
{
"IDs": [],
"Lock": false,
"Rendition": "native",
"RequestInfo": [
"Relations",
"Targets"
],
"Areas": [
"Workflow"
],
"EditionId": null,
"SupportedContentSources": null
}
],
"jsonrpc": "2.0"
};
getObjectsRequest.params[0].IDs.push(articleId);
//Set ticket for older servers
if (info.Ticket) {
getObjectsRequest.params[0].Ticket = info.Ticket;
}
callAjax(info.ServerInfo.URL + "?protocol=JSON&method=GetObjects", getObjectsRequest, function (result) {
var header = createHeader(result.result.Objects[0]);
var contentSelector;
if (articleView) {
contentSelector = $("article-editor article");
} else {
contentSelector = $("article-compare-content article");
}
contentSelector.printThis({
debug: false, // show the iframe for debugging
importCSS: true, // import parent page css
importStyle: true, // import style tags
printContainer: true, // print outer container/$.selector
loadCSS: "", // path to additional css file - use an array [] for multiple
pageTitle: "", // add title to print page
removeInline: false, // remove inline styles from print elements
removeInlineSelector: "*", // custom selectors to filter inline styles. removeInline must be true
printDelay: 1000, // variable print delay
header: "<style>img {max-height: 15px;max-width: 15px;}</style> " + header, // prefix to html
footer: "", // postfix to html
base: true, // preserve the BASE tag or accept a string for the URL
formValues: true, // preserve input/form values
canvas: false, // copy canvas content
doctypeString: '<!DOCTYPE html>', // enter a different doctype for older markup
removeScripts: false, // remove script tags from print content
copyTagClasses: true, // copy classes from the html & body tag
beforePrintEvent: null, // callback function for printEvent in iframe
beforePrint: null, // function called before iframe is filled
afterPrint: null // function called before iframe is removed
});
});
}
/**
* Create the header HTML
*
* @param {} article
*/
function createHeader(article) {
var info = ContentStationSdk.getInfo();
var html = "";
html += "<style>\n";
html += ".print-header table {border-collapse:collapse;}\n";
html += ".print-header td {\n";
html += " border-style:solid;\n";
html += " border-width:1px;\n";
html += " border-color:#333333;\n";
html += " padding:10px;\n";
html += " }\n";
html += "</style>\n";
html += "<div class='print-header'>\n";
html += " <h1>" + article.MetaData.BasicMetaData.Name + "</h1>\n";
html += " <br>";
html += " <br>";
html += " <table width='100%'>\n";
html += " <tbody>\n";
html += " <tr><td>Printed by</td><td>" + info.CurrentUser.FullName + " </td></tr>\n";
html += " <tr><td>Printed on</td><td>" + new Date().toLocaleString() + " </td></tr>\n";
html += " <tr><td>Brand</td><td>" + article.MetaData.BasicMetaData.Publication.Name + " </td></tr>\n";
html += " <tr><td>Issues</td><td>" + getIssues(article) + " </td></tr>\n";
html += " <tr><td>Category</td><td>" + article.MetaData.BasicMetaData.Category.Name + " </td></tr>\n";
html += " <tr><td>Status</td><td>" + article.MetaData.WorkflowMetaData.State.Name + " </td></tr>\n";
html += " <tr><td>Route To</td><td>" + article.MetaData.WorkflowMetaData.RouteTo + " </td></tr>\n";
html += " <tr><td>Comment</td><td>" + article.MetaData.WorkflowMetaData.Comment + " </td></tr>\n";
html += " </tbody>\n";
html += " </table>\n";
html += " <br>";
html += " <br>";
html += "</div>\n";
return html;
}
/**
* Returns a comma separated list of issues the article is related to
*/
function getIssues(article) {
var issues = [];
if (article.Relations != null) {
article.Relations.forEach(function (relation) {
if (relation.Targets != null) {
relation.Targets.forEach(function (target) {
if (!issues.includes(target.Issue.Name)) {
issues.push(target.Issue.Name);
}
});
}
});
}
return issues.join(', ');
}
function initPrintThis() {
/*
* printThis v1.14.0
* @desc Printing plug-in for jQuery
* @author Jason Day
*
* Resources (based on):
* - jPrintArea: http://plugins.jquery.com/project/jPrintArea
* - jqPrint: https://github.com/permanenttourist/jquery.jqprint
* - Ben Nadal: http://www.bennadel.com/blog/1591-Ask-Ben-Print-Part-Of-A-Web-Page-With-jQuery.htm
*
* Licensed under the MIT licence:
* http://www.opensource.org/licenses/mit-license.php
*
* (c) Jason Day 2015-2018
*
* Usage:
*
* $("#mySelector").printThis({
* debug: false, // show the iframe for debugging
* importCSS: true, // import parent page css
* importStyle: false, // import style tags
* printContainer: true, // grab outer container as well as the contents of the selector
* loadCSS: "path/to/my.css", // path to additional css file - use an array [] for multiple
* pageTitle: "", // add title to print page
* removeInline: false, // remove all inline styles from print elements
* removeInlineSelector: "body *", // custom selectors to filter inline styles. removeInline must be true
* printDelay: 333, // variable print delay
* header: null, // prefix to html
* footer: null, // postfix to html
* base: false, // preserve the BASE tag, or accept a string for the URL
* formValues: true, // preserve input/form values
* canvas: false, // copy canvas elements
* doctypeString: '...', // enter a different doctype for older markup
* removeScripts: false, // remove script tags from print content
* copyTagClasses: false // copy classes from the html & body tag
* beforePrintEvent: null, // callback function for printEvent in iframe
* beforePrint: null, // function called before iframe is filled
* afterPrint: null // function called before iframe is removed
* });
*
* Notes:
* - the loadCSS will load additional CSS (with or without @media print) into the iframe, adjusting layout
*/
;
(function ($) {
function appendContent($el, content) {
if (!content) return;
// Simple test for a jQuery element
$el.append(content.jquery ? content.clone() : content);
}
function appendBody($body, $element, opt) {
// Clone for safety and convenience
// Calls clone(withDataAndEvents = true) to copy form values.
var $content = $element.clone(opt.formValues);
if (opt.formValues) {
// Copy original select and textarea values to their cloned counterpart
// Makes up for inability to clone select and textarea values with clone(true)
copyValues($element, $content, 'select, textarea');
}
if (opt.removeScripts) {
$content.find('script').remove();
}
if (opt.printContainer) {
// grab $.selector as container
$content.appendTo($body);
} else {
// otherwise just print interior elements of container
$content.each(function () {
$(this).children().appendTo($body)
});
}
}
// Copies values from origin to clone for passed in elementSelector
function copyValues(origin, clone, elementSelector) {
var $originalElements = origin.find(elementSelector);
clone.find(elementSelector).each(function (index, item) {
$(item).val($originalElements.eq(index).val());
});
}
var opt;
$.fn.printThis = function (options) {
opt = $.extend({}, $.fn.printThis.defaults, options);
var $element = this instanceof jQuery ? this : $(this);
var strFrameName = "printThis-" + (new Date()).getTime();
if (window.location.hostname !== document.domain && navigator.userAgent.match(/msie/i)) {
// Ugly IE hacks due to IE not inheriting document.domain from parent
// checks if document.domain is set by comparing the host name against document.domain
var iframeSrc = "javascript:document.write(\"<head><script>document.domain=\\\"" + document.domain + "\\\";</s" + "cript></head><body></body>\")";
var printI = document.createElement('iframe');
printI.name = "printIframe";
printI.id = strFrameName;
printI.className = "MSIE";
document.body.appendChild(printI);
printI.src = iframeSrc;
} else {
// other browsers inherit document.domain, and IE works if document.domain is not explicitly set
var $frame = $("<iframe id='" + strFrameName + "' name='printIframe' />");
$frame.appendTo("body");
}
var $iframe = $("#" + strFrameName);
// show frame if in debug mode
if (!opt.debug) $iframe.css({
position: "absolute",
width: "0px",
height: "0px",
left: "-600px",
top: "-600px"
});
// before print callback
if (typeof opt.beforePrint === "function") {
opt.beforePrint($iframe);
}
// $iframe.ready() and $iframe.load were inconsistent between browsers
setTimeout(function () {
// Add doctype to fix the style difference between printing and render
function setDocType($iframe, doctype) {
var win, doc;
win = $iframe.get(0);
win = win.contentWindow || win.contentDocument || win;
doc = win.document || win.contentDocument || win;
doc.open();
doc.write(doctype);
doc.close();
}
if (opt.doctypeString) {
setDocType($iframe, opt.doctypeString);
}
var $doc = $iframe.contents(),
$head = $doc.find("head"),
$body = $doc.find("body"),
$base = $('base'),
baseURL;
// add base tag to ensure elements use the parent domain
if (opt.base === true && $base.length > 0) {
// take the base tag from the original page
baseURL = $base.attr('href');
} else if (typeof opt.base === 'string') {
// An exact base string is provided
baseURL = opt.base;
} else {
// Use the page URL as the base
baseURL = document.location.protocol + '//' + document.location.host;
}
$head.append('<base href="' + baseURL + '">');
// import page stylesheets
if (opt.importCSS) $("link[rel=stylesheet]").each(function () {
var href = $(this).attr("href");
if (href) {
var media = $(this).attr("media") || "all";
$head.append("<link type='text/css' rel='stylesheet' href='" + href + "' media='" + media + "'>");
}
});
// import style tags
if (opt.importStyle) $("style").each(function () {
$head.append(this.outerHTML);
});
// add title of the page
if (opt.pageTitle) $head.append("<title>" + opt.pageTitle + "</title>");
// import additional stylesheet(s)
if (opt.loadCSS) {
if ($.isArray(opt.loadCSS)) {
jQuery.each(opt.loadCSS, function (index, value) {
$head.append("<link type='text/css' rel='stylesheet' href='" + this + "'>");
});
} else {
$head.append("<link type='text/css' rel='stylesheet' href='" + opt.loadCSS + "'>");
}
}
var pageHtml = $('html')[0];
// CSS VAR in html tag when dynamic apply e.g. document.documentElement.style.setProperty("--foo", bar);
$doc.find('html').prop('style', pageHtml.style.cssText);
// copy 'root' tag classes
var tag = opt.copyTagClasses;
if (tag) {
tag = tag === true ? 'bh' : tag;
if (tag.indexOf('b') !== -1) {
$body.addClass($('body')[0].className);
}
if (tag.indexOf('h') !== -1) {
$doc.find('html').addClass(pageHtml.className);
}
}
// print header
//WoodWing 20190830: Add header to print editor article instead of body
//appendContent($body, opt.header);
if (opt.canvas) {
// add canvas data-ids for easy access after cloning.
var canvasId = 0;
// .addBack('canvas') adds the top-level element if it is a canvas.
$element.find('canvas').addBack('canvas').each(function () {
$(this).attr('data-printthis', canvasId++);
});
}
appendBody($body, $element, opt);
if (opt.canvas) {
// Re-draw new canvases by referencing the originals
$body.find('canvas').each(function () {
var cid = $(this).data('printthis'),
$src = $('[data-printthis="' + cid + '"]');
this.getContext('2d').drawImage($src[0], 0, 0);
// Remove the markup from the original
$src.removeData('printthis');
});
}
// remove inline styles
if (opt.removeInline) {
// Ensure there is a selector, even if it's been mistakenly removed
var selector = opt.removeInlineSelector || '*';
// $.removeAttr available jQuery 1.7+
if ($.isFunction($.removeAttr)) {
$body.find(selector).removeAttr("style");
} else {
$body.find(selector).attr("style", "");
}
}
//WoodWing 20190830: Add header to print editor article instead of body
//WoodWing 20200108: Overwrite overflow and position to prevent that only the first page is printed
$body.find('article').prepend(opt.header);
$body.find('article').css("overflow", "unset");
$body.find('article').css("position", "absolute");
// print "footer"
appendContent($body, opt.footer);
// attach event handler function to beforePrint event
function attachOnBeforePrintEvent($iframe, beforePrintHandler) {
var win = $iframe.get(0);
win = win.contentWindow || win.contentDocument || win;
if (typeof beforePrintHandler === "function") {
if ('matchMedia' in win) {
win.matchMedia('print').addListener(function (mql) {
if (mql.matches) beforePrintHandler();
});
} else {
win.onbeforeprint = beforePrintHandler;
}
}
}
attachOnBeforePrintEvent($iframe, opt.beforePrint);
setTimeout(function () {
if ($iframe.hasClass("MSIE")) {
// check if the iframe was created with the ugly hack
// and perform another ugly hack out of neccessity
window.frames["printIframe"].focus();
$head.append("<script> window.print(); </s" + "cript>");
} else {
// proper method
if (document.queryCommandSupported("print")) {
$iframe[0].contentWindow.document.execCommand("print", false, null);
} else {
$iframe[0].contentWindow.focus();
$iframe[0].contentWindow.print();
}
}
// remove iframe after print
if (!opt.debug) {
setTimeout(function () {
$iframe.remove();
}, 1000);
}
// after print callback
if (typeof opt.afterPrint === "function") {
opt.afterPrint();
}
}, opt.printDelay);
}, 333);
};
// defaults
$.fn.printThis.defaults = {
debug: false, // show the iframe for debugging
importCSS: true, // import parent page css
importStyle: false, // import style tags
printContainer: true, // print outer container/$.selector
loadCSS: "", // path to additional css file - use an array [] for multiple
pageTitle: "", // add title to print page
removeInline: false, // remove inline styles from print elements
removeInlineSelector: "*", // custom selectors to filter inline styles. removeInline must be true
printDelay: 333, // variable print delay
header: null, // prefix to html
footer: null, // postfix to html
base: false, // preserve the BASE tag or accept a string for the URL
formValues: true, // preserve input/form values
canvas: false, // copy canvas content
doctypeString: '<!DOCTYPE html>', // enter a different doctype for older markup
removeScripts: false, // remove script tags from print content
copyTagClasses: false, // copy classes from the html & body tag
beforePrintEvent: null, // callback function for printEvent in iframe
beforePrint: null, // function called before iframe is filled
afterPrint: null // function called before iframe is removed
};
})(jQuery);
}
})();