print.js 4.1 KB

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