dxf-xform.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const BaseXform = require('../base-xform');
  2. const AlignmentXform = require('./alignment-xform');
  3. const BorderXform = require('./border-xform');
  4. const FillXform = require('./fill-xform');
  5. const FontXform = require('./font-xform');
  6. const NumFmtXform = require('./numfmt-xform');
  7. const ProtectionXform = require('./protection-xform');
  8. // <xf numFmtId="[numFmtId]" fontId="[fontId]" fillId="[fillId]" borderId="[xf.borderId]" xfId="[xfId]">
  9. // Optional <alignment>
  10. // Optional <protection>
  11. // </xf>
  12. // Style assists translation from style model to/from xlsx
  13. class DxfXform extends BaseXform {
  14. constructor() {
  15. super();
  16. this.map = {
  17. alignment: new AlignmentXform(),
  18. border: new BorderXform(),
  19. fill: new FillXform(),
  20. font: new FontXform(),
  21. numFmt: new NumFmtXform(),
  22. protection: new ProtectionXform(),
  23. };
  24. }
  25. get tag() {
  26. return 'dxf';
  27. }
  28. // how do we generate dxfid?
  29. render(xmlStream, model) {
  30. xmlStream.openNode(this.tag);
  31. if (model.font) {
  32. this.map.font.render(xmlStream, model.font);
  33. }
  34. if (model.numFmt && model.numFmtId) {
  35. const numFmtModel = {id: model.numFmtId, formatCode: model.numFmt};
  36. this.map.numFmt.render(xmlStream, numFmtModel);
  37. }
  38. if (model.fill) {
  39. this.map.fill.render(xmlStream, model.fill);
  40. }
  41. if (model.alignment) {
  42. this.map.alignment.render(xmlStream, model.alignment);
  43. }
  44. if (model.border) {
  45. this.map.border.render(xmlStream, model.border);
  46. }
  47. if (model.protection) {
  48. this.map.protection.render(xmlStream, model.protection);
  49. }
  50. xmlStream.closeNode();
  51. }
  52. parseOpen(node) {
  53. if (this.parser) {
  54. this.parser.parseOpen(node);
  55. return true;
  56. }
  57. switch (node.name) {
  58. case this.tag:
  59. // this node is often repeated. Need to reset children
  60. this.reset();
  61. return true;
  62. default:
  63. this.parser = this.map[node.name];
  64. if (this.parser) {
  65. this.parser.parseOpen(node);
  66. }
  67. return true;
  68. }
  69. }
  70. parseText(text) {
  71. if (this.parser) {
  72. this.parser.parseText(text);
  73. }
  74. }
  75. parseClose(name) {
  76. if (this.parser) {
  77. if (!this.parser.parseClose(name)) {
  78. this.parser = undefined;
  79. }
  80. return true;
  81. }
  82. if (name === this.tag) {
  83. this.model = {
  84. alignment: this.map.alignment.model,
  85. border: this.map.border.model,
  86. fill: this.map.fill.model,
  87. font: this.map.font.model,
  88. numFmt: this.map.numFmt.model,
  89. protection: this.map.protection.model,
  90. };
  91. return false;
  92. }
  93. return true;
  94. }
  95. }
  96. module.exports = DxfXform;