otsdaq_utilities  v2_05_02_indev
print.js
1 var Print = function(table){
2  this.table = table; //hold Tabulator object
3  this.element = false;
4  this.manualBlock = false;
5 };
6 
7 Print.prototype.initialize = function(){
8  window.addEventListener("beforeprint", this.replaceTable.bind(this));
9  window.addEventListener("afterprint", this.cleanup.bind(this));
10 };
11 
12 Print.prototype.replaceTable = function(){
13  if(!this.manualBlock){
14  this.element = document.createElement("div");
15  this.element.classList.add("tabulator-print-table");
16 
17  this.element.appendChild(this.table.modules.htmlTableExport.genereateTable(this.table.options.printConfig, this.table.options.printCopyStyle, this.table.options.printVisibleRows, "print"));
18 
19  this.table.element.style.display = "none";
20 
21  this.table.element.parentNode.insertBefore(this.element, this.table.element);
22  }
23 };
24 
25 Print.prototype.cleanup = function(){
26  document.body.classList.remove("tabulator-print-fullscreen-hide");
27 
28  if(this.element && this.element.parentNode){
29  this.element.parentNode.removeChild(this.element);
30  this.table.element.style.display = "";
31  }
32 };
33 
34 Print.prototype.printFullscreen = function(visible, style, config){
35  var scrollX = window.scrollX,
36  scrollY = window.scrollY,
37  headerEl = document.createElement("div"),
38  footerEl = document.createElement("div"),
39  tableEl = this.table.modules.htmlTableExport.genereateTable(typeof config != "undefined" ? config : this.table.options.printConfig, typeof style != "undefined" ? style : this.table.options.printCopyStyle, visible, "print"),
40  headerContent, footerContent;
41 
42  this.manualBlock = true;
43 
44  this.element = document.createElement("div");
45  this.element.classList.add("tabulator-print-fullscreen");
46 
47  if(this.table.options.printHeader){
48  headerEl.classList.add("tabulator-print-header");
49 
50  headerContent = typeof this.table.options.printHeader == "function" ? this.table.options.printHeader.call(this.table) : this.table.options.printHeader;
51 
52  if(typeof headerContent == "string"){
53  headerEl.innerHTML = headerContent;
54  }else{
55  headerEl.appendChild(headerContent);
56  }
57 
58  this.element.appendChild(headerEl);
59  }
60 
61  this.element.appendChild(tableEl);
62 
63  if(this.table.options.printFooter){
64  footerEl.classList.add("tabulator-print-footer");
65 
66  footerContent = typeof this.table.options.printFooter == "function" ? this.table.options.printFooter.call(this.table) : this.table.options.printFooter;
67 
68 
69  if(typeof footerContent == "string"){
70  footerEl.innerHTML = footerContent;
71  }else{
72  footerEl.appendChild(footerContent);
73  }
74 
75  this.element.appendChild(footerEl);
76  }
77 
78  document.body.classList.add("tabulator-print-fullscreen-hide");
79  document.body.appendChild(this.element);
80 
81  if(this.table.options.printFormatter){
82  this.table.options.printFormatter(this.element, tableEl);
83  }
84 
85  window.print();
86 
87  this.cleanup();
88 
89  window.scrollTo(scrollX, scrollY);
90 
91  this.manualBlock = false;
92 };
93 
94 Tabulator.prototype.registerModule("print", Print);