yh-hiprint 2.2.6 → 2.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,110 +1,110 @@
1
- import $ from "jquery";
2
- $.fn.hiwprint = function (options) {
3
- var usedFrame = document.getElementById("hiwprint_iframe");
4
- if (usedFrame) usedFrame.parentNode.removeChild(usedFrame);
5
- var opt = $.extend({}, $.fn.hiwprint.defaults, options);
6
- var $element = this;
7
- var $iframe = $(
8
- '<iframe id="hiwprint_iframe" style="visibility: hidden; height: 0; width: 0; position: absolute;"></iframe>'
9
- );
10
- var css = "";
11
- if (opt.importCss) {
12
- if (opt.styleHandler) {
13
- css += opt.styleHandler();
14
- }
15
- if ($("link[media=print]").length > 0) {
16
- $("link[media=print]").each(function () {
17
- if ($(this).attr("href").indexOf("print-lock.css") >= 0) {
18
- css += '<link rel="stylesheet" type="text/css" media="print" href="' + $(this).attr("href") + '">';
19
- // ↑若加上media="print",仅对浏览器打印时有效 所以查看iframe页面时样式无效
20
- css += '<link rel="stylesheet" type="text/css" href="' + $(this).attr("href") + '">';
21
- }
22
- });
23
- }
24
- }
25
- $iframe[0].srcdoc =
26
- '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8">' + css + "</head><body></body></html>";
27
-
28
- $iframe[0].onload = function () {
29
- var printDocument = $iframe[0].contentWindow || $iframe[0].contentDocument;
30
- if (printDocument.document) printDocument = printDocument.document;
31
- if (!$iframe.attr("srcdoc")) {
32
- printDocument.write(
33
- '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8">' + css + "</head><body></body></html>"
34
- );
35
- }
36
- if (opt.printContainer) {
37
- printDocument.body.innerHTML = $element[0].outerHTML;
38
- } else {
39
- printDocument.body.innerHTML = $element.html();
40
- }
41
- loadAllImages(printDocument, function () {
42
- performPrint($iframe[0], opt);
43
- });
44
- };
45
-
46
- $iframe.appendTo("body");
47
- };
48
-
49
- $.fn.hiwprint.defaults = {
50
- importCss: true,
51
- printContainer: true,
52
- callback: null,
53
- styleHandler: null,
54
- };
55
-
56
- function performPrint(iframeElement, opt) {
57
- try {
58
- iframeElement.focus();
59
- if (isEdge() || isIE()) {
60
- try {
61
- iframeElement.contentWindow.document.execCommand("print", false, null);
62
- } catch (e) {
63
- iframeElement.contentWindow.print();
64
- }
65
- } else {
66
- // Other browsers
67
- iframeElement.contentWindow.print();
68
- }
69
- if (opt.callback) {
70
- opt.callback();
71
- }
72
- } catch (error) {
73
- console.log(error);
74
- }
75
- }
76
-
77
- function isIE() {
78
- return navigator.userAgent.indexOf("MSIE") !== -1 || !!document.documentMode;
79
- }
80
-
81
- // Edge 20+
82
- function isEdge() {
83
- return !isIE() && !!window.StyleMedia;
84
- }
85
-
86
- function loadAllImages(printDocument, callback, time) {
87
- if (time === undefined) {
88
- time = 0;
89
- }
90
- var images = printDocument.getElementsByTagName("img");
91
- var allLoaded = true;
92
- for (var i = 0; i < images.length; i++) {
93
- var image = images[i];
94
- if (image.src && image.src !== window.location.href && image.src.indexOf("base64") == -1) {
95
- if (!image || typeof image.naturalWidth === "undefined" || image.naturalWidth === 0 || !image.complete) {
96
- if (!image.complete) {
97
- allLoaded = false;
98
- }
99
- }
100
- }
101
- }
102
- time++;
103
- if (!allLoaded && time < 10) {
104
- setTimeout(function () {
105
- loadAllImages(printDocument, callback, time);
106
- }, 500);
107
- } else {
108
- callback();
109
- }
110
- }
1
+ import $ from "jquery";
2
+ $.fn.hiwprint = function (options) {
3
+ var usedFrame = document.getElementById("hiwprint_iframe");
4
+ if (usedFrame) usedFrame.parentNode.removeChild(usedFrame);
5
+ var opt = $.extend({}, $.fn.hiwprint.defaults, options);
6
+ var $element = this;
7
+ var $iframe = $(
8
+ '<iframe id="hiwprint_iframe" style="visibility: hidden; height: 0; width: 0; position: absolute;"></iframe>'
9
+ );
10
+ var css = "";
11
+ if (opt.importCss) {
12
+ if (opt.styleHandler) {
13
+ css += opt.styleHandler();
14
+ }
15
+ if ($("link[media=print]").length > 0) {
16
+ $("link[media=print]").each(function () {
17
+ if ($(this).attr("href").indexOf("print-lock.css") >= 0) {
18
+ css += '<link rel="stylesheet" type="text/css" media="print" href="' + $(this).attr("href") + '">';
19
+ // ↑若加上media="print",仅对浏览器打印时有效 所以查看iframe页面时样式无效
20
+ css += '<link rel="stylesheet" type="text/css" href="' + $(this).attr("href") + '">';
21
+ }
22
+ });
23
+ }
24
+ }
25
+ $iframe[0].srcdoc =
26
+ '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8">' + css + "</head><body></body></html>";
27
+
28
+ $iframe[0].onload = function () {
29
+ var printDocument = $iframe[0].contentWindow || $iframe[0].contentDocument;
30
+ if (printDocument.document) printDocument = printDocument.document;
31
+ if (!$iframe.attr("srcdoc")) {
32
+ printDocument.write(
33
+ '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8">' + css + "</head><body></body></html>"
34
+ );
35
+ }
36
+ if (opt.printContainer) {
37
+ printDocument.body.innerHTML = $element[0].outerHTML;
38
+ } else {
39
+ printDocument.body.innerHTML = $element.html();
40
+ }
41
+ loadAllImages(printDocument, function () {
42
+ performPrint($iframe[0], opt);
43
+ });
44
+ };
45
+
46
+ $iframe.appendTo("body");
47
+ };
48
+
49
+ $.fn.hiwprint.defaults = {
50
+ importCss: true,
51
+ printContainer: true,
52
+ callback: null,
53
+ styleHandler: null,
54
+ };
55
+
56
+ function performPrint(iframeElement, opt) {
57
+ try {
58
+ iframeElement.focus();
59
+ if (isEdge() || isIE()) {
60
+ try {
61
+ iframeElement.contentWindow.document.execCommand("print", false, null);
62
+ } catch (e) {
63
+ iframeElement.contentWindow.print();
64
+ }
65
+ } else {
66
+ // Other browsers
67
+ iframeElement.contentWindow.print();
68
+ }
69
+ if (opt.callback) {
70
+ opt.callback();
71
+ }
72
+ } catch (error) {
73
+ console.log(error);
74
+ }
75
+ }
76
+
77
+ function isIE() {
78
+ return navigator.userAgent.indexOf("MSIE") !== -1 || !!document.documentMode;
79
+ }
80
+
81
+ // Edge 20+
82
+ function isEdge() {
83
+ return !isIE() && !!window.StyleMedia;
84
+ }
85
+
86
+ function loadAllImages(printDocument, callback, time) {
87
+ if (time === undefined) {
88
+ time = 0;
89
+ }
90
+ var images = printDocument.getElementsByTagName("img");
91
+ var allLoaded = true;
92
+ for (var i = 0; i < images.length; i++) {
93
+ var image = images[i];
94
+ if (image.src && image.src !== window.location.href && image.src.indexOf("base64") == -1) {
95
+ if (!image || typeof image.naturalWidth === "undefined" || image.naturalWidth === 0 || !image.complete) {
96
+ if (!image.complete) {
97
+ allLoaded = false;
98
+ }
99
+ }
100
+ }
101
+ }
102
+ time++;
103
+ if (!allLoaded && time < 10) {
104
+ setTimeout(function () {
105
+ loadAllImages(printDocument, callback, time);
106
+ }, 500);
107
+ } else {
108
+ callback();
109
+ }
110
+ }