print.js 3.7 KB

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