mxGenericChangeCodec.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxGenericChangeCodec
  7. *
  8. * Codec for <mxValueChange>s, <mxStyleChange>s, <mxGeometryChange>s,
  9. * <mxCollapseChange>s and <mxVisibleChange>s. This class is created
  10. * and registered dynamically at load time and used implicitly
  11. * via <mxCodec> and the <mxCodecRegistry>.
  12. *
  13. * Transient Fields:
  14. *
  15. * - model
  16. * - previous
  17. *
  18. * Reference Fields:
  19. *
  20. * - cell
  21. *
  22. * Constructor: mxGenericChangeCodec
  23. *
  24. * Factory function that creates a <mxObjectCodec> for
  25. * the specified change and fieldname.
  26. *
  27. * Parameters:
  28. *
  29. * obj - An instance of the change object.
  30. * variable - The fieldname for the change data.
  31. */
  32. var mxGenericChangeCodec = function(obj, variable)
  33. {
  34. var codec = new mxObjectCodec(obj, ['model', 'previous'], ['cell']);
  35. /**
  36. * Function: afterDecode
  37. *
  38. * Restores the state by assigning the previous value.
  39. */
  40. codec.afterDecode = function(dec, node, obj)
  41. {
  42. // Allows forward references in sessions. This is a workaround
  43. // for the sequence of edits in mxGraph.moveCells and cellsAdded.
  44. if (mxUtils.isNode(obj.cell))
  45. {
  46. obj.cell = dec.decodeCell(obj.cell, false);
  47. }
  48. obj.previous = obj[variable];
  49. return obj;
  50. };
  51. return codec;
  52. };
  53. // Registers the codecs
  54. mxCodecRegistry.register(mxGenericChangeCodec(new mxValueChange(), 'value'));
  55. mxCodecRegistry.register(mxGenericChangeCodec(new mxStyleChange(), 'style'));
  56. mxCodecRegistry.register(mxGenericChangeCodec(new mxGeometryChange(), 'geometry'));
  57. mxCodecRegistry.register(mxGenericChangeCodec(new mxCollapseChange(), 'collapsed'));
  58. mxCodecRegistry.register(mxGenericChangeCodec(new mxVisibleChange(), 'visible'));
  59. mxCodecRegistry.register(mxGenericChangeCodec(new mxCellAttributeChange(), 'value'));
  60. __mxOutput.mxGenericChangeCodec = typeof mxGenericChangeCodec !== 'undefined' ? mxGenericChangeCodec : undefined;