print2.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* @Print.js
  2. * DH (http://denghao.me)
  3. * 2017-7-14
  4. */
  5. (function (window, document) {
  6. let Print = function (dom, options) {
  7. if (!(this instanceof Print)) return new Print(dom, options);
  8. this.options = this.extend({
  9. noPrint: '.no-print',
  10. onStart: function () {
  11. },
  12. onEnd: function () {
  13. }
  14. }, options);
  15. if ((typeof dom) === "string") {
  16. this.dom = document.querySelector(dom);
  17. } else {
  18. this.dom = dom;
  19. }
  20. this.init();
  21. };
  22. Print.prototype = {
  23. init: function () {
  24. let content = this.getStyle() + this.getHtml();
  25. this.writeIframe(content);
  26. },
  27. extend: function (obj, obj2) {
  28. for (let k in obj2) {
  29. obj[k] = obj2[k];
  30. }
  31. return obj;
  32. },
  33. getStyle: function () {
  34. let str = "",
  35. styles = document.querySelectorAll('style,link');
  36. for (let i = 0; i < styles.length; i++) {
  37. str += styles[i].outerHTML;
  38. }
  39. str += "<style>" + (this.options.noPrint ? this.options.noPrint : '.no-print') + "{display:none;}</style>";
  40. return str;
  41. },
  42. getHtml: function () {
  43. let inputs = document.querySelectorAll('input');
  44. let textareas = document.querySelectorAll('textarea');
  45. let selects = document.querySelectorAll('select');
  46. for (let k in inputs) {
  47. if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
  48. if (inputs[k].checked == true) {
  49. inputs[k].setAttribute('checked', "checked")
  50. } else {
  51. inputs[k].removeAttribute('checked')
  52. }
  53. } else if (inputs[k].type == "text") {
  54. inputs[k].setAttribute('value', inputs[k].value)
  55. }
  56. }
  57. for (let k2 in textareas) {
  58. if (textareas[k2].type == 'textarea') {
  59. textareas[k2].innerHTML = textareas[k2].value
  60. }
  61. }
  62. for (let k3 in selects) {
  63. if (selects[k3].type == 'select-one') {
  64. let child = selects[k3].children;
  65. for (let i in child) {
  66. if (child[i].tagName == 'OPTION') {
  67. if (child[i].selected == true) {
  68. child[i].setAttribute('selected', "selected")
  69. } else {
  70. child[i].removeAttribute('selected')
  71. }
  72. }
  73. }
  74. }
  75. }
  76. return this.dom.outerHTML;
  77. },
  78. writeIframe: function (content) {
  79. let w, doc, iframe = document.createElement('iframe'),
  80. f = document.body.appendChild(iframe);
  81. iframe.id = "myIframe";
  82. iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
  83. w = f.contentWindow || f.contentDocument;
  84. doc = f.contentDocument || f.contentWindow.document;
  85. doc.open();
  86. doc.write(content);
  87. doc.close();
  88. this.toPrint(w, function () {
  89. document.body.removeChild(iframe)
  90. });
  91. },
  92. toPrint: function (w, cb) {
  93. let _this = this;
  94. w.onload = function () {
  95. try {
  96. setTimeout(function () {
  97. w.focus();
  98. typeof _this.options.onStart === 'function' && _this.options.onStart();
  99. if (!w.document.execCommand('print', false, null)) {
  100. w.print();
  101. }
  102. typeof _this.options.onEnd === 'function' && _this.options.onEnd();
  103. w.close();
  104. cb && cb()
  105. });
  106. } catch (err) {
  107. console.log('err', err);
  108. }
  109. }
  110. }
  111. };
  112. window.Print = Print;
  113. }(window, document));