You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.3 KiB

  1. var UID = {
  2. _current: 0,
  3. getNew: function(){
  4. this._current++;
  5. return this._current;
  6. }
  7. };
  8. HTMLElement.prototype.pseudoStyle = function(element,prop,value){
  9. var _this = this;
  10. var _sheetId = "pseudoStyles";
  11. var _head = document.head || document.getElementsByTagName('head')[0];
  12. var _sheet = document.getElementById(_sheetId) || document.createElement('style');
  13. _sheet.id = _sheetId;
  14. var className = "pseudoStyle" + UID.getNew();
  15. _this.className += " "+className;
  16. _sheet.innerHTML += " ."+className+":"+element+"{"+prop+":"+value+"}";
  17. _head.appendChild(_sheet);
  18. return this;
  19. };
  20. class Sandpoints extends Paged.Handler {
  21. constructor(chunker, polisher, caller) {
  22. super(chunker, polisher, caller);
  23. }
  24. beforeParsed(content) {
  25. var d = {};
  26. content.querySelectorAll('*').forEach((n, i)=> {
  27. if (n.hasAttribute("printhref")) {
  28. n.setAttribute("href", n.getAttribute("printhref"))
  29. }
  30. if (n.classList.length > 0 ) {
  31. n.classList.forEach((c, i)=>{
  32. if (c.startsWith("css-")) {
  33. (c in d) ? d[c]++ : d[c] = 1
  34. let s = ""
  35. Object.keys(d).filter(ks => ks.startsWith(c.slice(0, -1))).forEach((t, i)=> {if (c >= t) {s += `${d[t]}.`}})
  36. n.pseudoStyle('before','content', `"${s} "`)
  37. }
  38. })
  39. }
  40. })
  41. }
  42. afterPageLayout(pageElement, page, breakToken, chunker) {
  43. // Find all split table elements
  44. let tables = pageElement.querySelectorAll("table[data-split-from]");
  45. tables.forEach((table) => {
  46. // There is an edge case where the previous page table
  47. // has zero height (isn't visible).
  48. // To avoid double header we will only add header if there is none.
  49. let tableHeader = table.querySelector("thead");
  50. if (tableHeader) {
  51. return;
  52. }
  53. // Get the reference UUID of the node
  54. let ref = table.dataset.ref;
  55. // Find the node in the original source
  56. let sourceTable = chunker.source.querySelector("[data-ref='" + ref + "']");
  57. // Find if there is a header
  58. let sourceHeader = sourceTable.querySelector("thead");
  59. if (sourceHeader) {
  60. // Clone the header element
  61. let clonedHeader = sourceHeader.cloneNode(true);
  62. // Insert the header at the start of the split table
  63. table.insertBefore(clonedHeader, table.firstChild);
  64. }
  65. });
  66. // Find all tables
  67. tables = pageElement.querySelectorAll("table");
  68. // special case which might not fit for everyone
  69. tables.forEach((table) => {
  70. // if the table has no rows in body, hide it.
  71. // This happens because my render engine creates empty tables.
  72. let sourceBody = table.querySelector("tbody > tr");
  73. if (!sourceBody) {
  74. console.log("Table was hidden, because it has no rows in tbody.");
  75. table.style.visibility = "hidden";
  76. table.style.position = "absolute";
  77. var lineSpacer = table.nextSibling;
  78. if (lineSpacer) {
  79. lineSpacer.style.visibility = "hidden";
  80. lineSpacer.style.position = "absolute";
  81. }
  82. }
  83. });
  84. }
  85. }
  86. Paged.registerHandlers(Sandpoints);