print.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // 打印类属性、方法定义
  2. /* eslint-disable */
  3. const Print = function(dom, options) {
  4. if (!(this instanceof Print)) return new Print(dom, options);
  5. this.options = this.extend({
  6. 'noPrint': '.no-print'
  7. }, options);
  8. if ((typeof dom) === "string") {
  9. try{
  10. this.dom = document.querySelector(dom);
  11. }catch{
  12. var createDom = document.createElement("div")
  13. createDom.innerHTML = dom
  14. this.dom = createDom;
  15. };
  16. } else {
  17. this.isDOM(dom)
  18. this.dom = this.isDOM(dom) ? dom : dom.$el;
  19. }
  20. this.init();
  21. };
  22. Print.prototype = {
  23. init: function() {
  24. var content = this.getStyle() + this.getHtml();
  25. this.writeIframe(content);
  26. },
  27. extend: function(obj, obj2) {
  28. for (var k in obj2) {
  29. obj[k] = obj2[k];
  30. }
  31. return obj;
  32. },
  33. getStyle: function() {
  34. var str = "", styles = document.querySelectorAll('style,link');
  35. for (var i = 0; i < styles.length; i++) {
  36. str += styles[i].outerHTML;
  37. }
  38. str += "<style>" + (this.options.noPrint ? this.options.noPrint : '.no-print') + "{display:none;}</style>";
  39. str += "<style>html,body{background-color:#fff;}</style>";
  40. return str;
  41. },
  42. getHtml: function() {
  43. var inputs = document.querySelectorAll('input');
  44. var textareas = document.querySelectorAll('textarea');
  45. var selects = document.querySelectorAll('select');
  46. for (var k = 0; k < inputs.length; k++) {
  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. } else {
  56. inputs[k].setAttribute('value', inputs[k].value)
  57. }
  58. }
  59. for (var k2 = 0; k2 < textareas.length; k2++) {
  60. if (textareas[k2].type == 'textarea') {
  61. textareas[k2].innerHTML = textareas[k2].value
  62. }
  63. }
  64. for (var k3 = 0; k3 < selects.length; k3++) {
  65. if (selects[k3].type == 'select-one') {
  66. var child = selects[k3].children;
  67. for (var i in child) {
  68. if (child[i].tagName == 'OPTION') {
  69. if (child[i].selected == true) {
  70. child[i].setAttribute('selected', "selected")
  71. } else {
  72. child[i].removeAttribute('selected')
  73. }
  74. }
  75. }
  76. }
  77. }
  78. return this.dom.outerHTML;
  79. },
  80. writeIframe: function(content) {
  81. var w, doc, iframe = document.createElement('iframe'), f = document.body.appendChild(iframe);
  82. iframe.id = "myIframe";
  83. //iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
  84. iframe.setAttribute('style', 'position:absolute;width:0;height:0;top:-10px;left:-10px;');
  85. w = f.contentWindow || f.contentDocument;
  86. doc = f.contentDocument || f.contentWindow.document;
  87. doc.open();
  88. doc.write(content);
  89. doc.close();
  90. var _this = this
  91. iframe.onload = function() {
  92. _this.toPrint(w);
  93. setTimeout(function() {
  94. document.body.removeChild(iframe)
  95. }, 100)
  96. }
  97. },
  98. toPrint: function(frameWindow) {
  99. try {
  100. setTimeout(function() {
  101. frameWindow.focus();
  102. try {
  103. if (!frameWindow.document.execCommand('print', false, null)) {
  104. frameWindow.print();
  105. }
  106. } catch (e) {
  107. frameWindow.print();
  108. }
  109. frameWindow.close();
  110. }, 10);
  111. } catch (err) {
  112. console.log('err', err);
  113. }
  114. },
  115. isDOM: (typeof HTMLElement === 'object') ?
  116. function(obj) {
  117. return obj instanceof HTMLElement;
  118. } : function(obj) {
  119. return obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string';
  120. }
  121. };
  122. export default Print