table-xform.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. const XmlStream = require('../../../utils/xml-stream');
  2. const BaseXform = require('../base-xform');
  3. const ListXform = require('../list-xform');
  4. const AutoFilterXform = require('./auto-filter-xform');
  5. const TableColumnXform = require('./table-column-xform');
  6. const TableStyleInfoXform = require('./table-style-info-xform');
  7. class TableXform extends BaseXform {
  8. constructor() {
  9. super();
  10. this.map = {
  11. autoFilter: new AutoFilterXform(),
  12. tableColumns: new ListXform({
  13. tag: 'tableColumns',
  14. count: true,
  15. empty: true,
  16. childXform: new TableColumnXform(),
  17. }),
  18. tableStyleInfo: new TableStyleInfoXform(),
  19. };
  20. }
  21. prepare(model, options) {
  22. this.map.autoFilter.prepare(model);
  23. this.map.tableColumns.prepare(model.columns, options);
  24. }
  25. get tag() {
  26. return 'table';
  27. }
  28. render(xmlStream, model) {
  29. xmlStream.openXml(XmlStream.StdDocAttributes);
  30. xmlStream.openNode(this.tag, {
  31. ...TableXform.TABLE_ATTRIBUTES,
  32. id: model.id,
  33. name: model.name,
  34. displayName: model.displayName || model.name,
  35. ref: model.tableRef,
  36. totalsRowCount: model.totalsRow ? '1' : undefined,
  37. totalsRowShown: model.totalsRow ? undefined : '1',
  38. headerRowCount: model.headerRow ? '1' : '0',
  39. });
  40. this.map.autoFilter.render(xmlStream, model);
  41. this.map.tableColumns.render(xmlStream, model.columns);
  42. this.map.tableStyleInfo.render(xmlStream, model.style);
  43. xmlStream.closeNode();
  44. }
  45. parseOpen(node) {
  46. if (this.parser) {
  47. this.parser.parseOpen(node);
  48. return true;
  49. }
  50. const {name, attributes} = node;
  51. switch (name) {
  52. case this.tag:
  53. this.reset();
  54. this.model = {
  55. name: attributes.name,
  56. displayName: attributes.displayName || attributes.name,
  57. tableRef: attributes.ref,
  58. totalsRow: attributes.totalsRowCount === '1',
  59. headerRow: attributes.headerRowCount === '1',
  60. };
  61. break;
  62. default:
  63. this.parser = this.map[node.name];
  64. if (this.parser) {
  65. this.parser.parseOpen(node);
  66. }
  67. break;
  68. }
  69. return true;
  70. }
  71. parseText(text) {
  72. if (this.parser) {
  73. this.parser.parseText(text);
  74. }
  75. }
  76. parseClose(name) {
  77. if (this.parser) {
  78. if (!this.parser.parseClose(name)) {
  79. this.parser = undefined;
  80. }
  81. return true;
  82. }
  83. switch (name) {
  84. case this.tag:
  85. this.model.columns = this.map.tableColumns.model;
  86. if (this.map.autoFilter.model) {
  87. this.model.autoFilterRef = this.map.autoFilter.model.autoFilterRef;
  88. this.map.autoFilter.model.columns.forEach((column, index) => {
  89. this.model.columns[index].filterButton = column.filterButton;
  90. });
  91. }
  92. this.model.style = this.map.tableStyleInfo.model;
  93. return false;
  94. default:
  95. // could be some unrecognised tags
  96. return true;
  97. }
  98. }
  99. reconcile(model, options) {
  100. // fetch the dfxs from styles
  101. model.columns.forEach(column => {
  102. if (column.dxfId !== undefined) {
  103. column.style = options.styles.getDxfStyle(column.dxfId);
  104. }
  105. });
  106. }
  107. }
  108. TableXform.TABLE_ATTRIBUTES = {
  109. xmlns: 'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
  110. 'xmlns:mc': 'http://schemas.openxmlformats.org/markup-compatibility/2006',
  111. 'mc:Ignorable': 'xr xr3',
  112. 'xmlns:xr': 'http://schemas.microsoft.com/office/spreadsheetml/2014/revision',
  113. 'xmlns:xr3': 'http://schemas.microsoft.com/office/spreadsheetml/2016/revision3',
  114. // 'xr:uid': '{00000000-000C-0000-FFFF-FFFF00000000}',
  115. };
  116. module.exports = TableXform;