protection-xform.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const BaseXform = require('../base-xform');
  2. const validation = {
  3. boolean(value, dflt) {
  4. if (value === undefined) {
  5. return dflt;
  6. }
  7. return value;
  8. },
  9. };
  10. // Protection encapsulates translation from style.protection model to/from xlsx
  11. class ProtectionXform extends BaseXform {
  12. get tag() {
  13. return 'protection';
  14. }
  15. render(xmlStream, model) {
  16. xmlStream.addRollback();
  17. xmlStream.openNode('protection');
  18. let isValid = false;
  19. function add(name, value) {
  20. if (value !== undefined) {
  21. xmlStream.addAttribute(name, value);
  22. isValid = true;
  23. }
  24. }
  25. add('locked', validation.boolean(model.locked, true) ? undefined : '0');
  26. add('hidden', validation.boolean(model.hidden, false) ? '1' : undefined);
  27. xmlStream.closeNode();
  28. if (isValid) {
  29. xmlStream.commit();
  30. } else {
  31. xmlStream.rollback();
  32. }
  33. }
  34. parseOpen(node) {
  35. const model = {
  36. locked: !(node.attributes.locked === '0'),
  37. hidden: node.attributes.hidden === '1',
  38. };
  39. // only want to record models that differ from defaults
  40. const isSignificant = !model.locked || model.hidden;
  41. this.model = isSignificant ? model : null;
  42. }
  43. parseText() {}
  44. parseClose() {
  45. return false;
  46. }
  47. }
  48. module.exports = ProtectionXform;