col-xform.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const utils = require('../../../utils/utils');
  2. const BaseXform = require('../base-xform');
  3. class ColXform extends BaseXform {
  4. get tag() {
  5. return 'col';
  6. }
  7. prepare(model, options) {
  8. const styleId = options.styles.addStyleModel(model.style || {});
  9. if (styleId) {
  10. model.styleId = styleId;
  11. }
  12. }
  13. render(xmlStream, model) {
  14. xmlStream.openNode('col');
  15. xmlStream.addAttribute('min', model.min);
  16. xmlStream.addAttribute('max', model.max);
  17. if (model.width) {
  18. xmlStream.addAttribute('width', model.width);
  19. }
  20. if (model.styleId) {
  21. xmlStream.addAttribute('style', model.styleId);
  22. }
  23. if (model.hidden) {
  24. xmlStream.addAttribute('hidden', '1');
  25. }
  26. if (model.bestFit) {
  27. xmlStream.addAttribute('bestFit', '1');
  28. }
  29. if (model.outlineLevel) {
  30. xmlStream.addAttribute('outlineLevel', model.outlineLevel);
  31. }
  32. if (model.collapsed) {
  33. xmlStream.addAttribute('collapsed', '1');
  34. }
  35. xmlStream.addAttribute('customWidth', '1');
  36. xmlStream.closeNode();
  37. }
  38. parseOpen(node) {
  39. if (node.name === 'col') {
  40. const model = (this.model = {
  41. min: parseInt(node.attributes.min || '0', 10),
  42. max: parseInt(node.attributes.max || '0', 10),
  43. width:
  44. node.attributes.width === undefined
  45. ? undefined
  46. : parseFloat(node.attributes.width || '0'),
  47. });
  48. if (node.attributes.style) {
  49. model.styleId = parseInt(node.attributes.style, 10);
  50. }
  51. if (utils.parseBoolean(node.attributes.hidden)) {
  52. model.hidden = true;
  53. }
  54. if (utils.parseBoolean(node.attributes.bestFit)) {
  55. model.bestFit = true;
  56. }
  57. if (node.attributes.outlineLevel) {
  58. model.outlineLevel = parseInt(node.attributes.outlineLevel, 10);
  59. }
  60. if (utils.parseBoolean(node.attributes.collapsed)) {
  61. model.collapsed = true;
  62. }
  63. return true;
  64. }
  65. return false;
  66. }
  67. parseText() {}
  68. parseClose() {
  69. return false;
  70. }
  71. reconcile(model, options) {
  72. // reconcile column styles
  73. if (model.styleId) {
  74. model.style = options.styles.getStyleModel(model.styleId);
  75. }
  76. }
  77. }
  78. module.exports = ColXform;