mxRootChangeCodec.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. mxCodecRegistry.register(function()
  6. {
  7. /**
  8. * Class: mxRootChangeCodec
  9. *
  10. * Codec for <mxRootChange>s. This class is created and registered
  11. * dynamically at load time and used implicitly via <mxCodec> and
  12. * the <mxCodecRegistry>.
  13. *
  14. * Transient Fields:
  15. *
  16. * - model
  17. * - previous
  18. * - root
  19. */
  20. var codec = new mxObjectCodec(new mxRootChange(),
  21. ['model', 'previous', 'root']);
  22. /**
  23. * Function: onEncode
  24. *
  25. * Encodes the child recursively.
  26. */
  27. codec.afterEncode = function(enc, obj, node)
  28. {
  29. enc.encodeCell(obj.root, node);
  30. return node;
  31. };
  32. /**
  33. * Function: beforeDecode
  34. *
  35. * Decodes the optional children as cells
  36. * using the respective decoder.
  37. */
  38. codec.beforeDecode = function(dec, node, obj)
  39. {
  40. if (node.firstChild != null &&
  41. node.firstChild.nodeType == mxConstants.NODETYPE_ELEMENT)
  42. {
  43. // Makes sure the original node isn't modified
  44. node = node.cloneNode(true);
  45. var tmp = node.firstChild;
  46. obj.root = dec.decodeCell(tmp, false);
  47. var tmp2 = tmp.nextSibling;
  48. tmp.parentNode.removeChild(tmp);
  49. tmp = tmp2;
  50. while (tmp != null)
  51. {
  52. tmp2 = tmp.nextSibling;
  53. dec.decodeCell(tmp);
  54. tmp.parentNode.removeChild(tmp);
  55. tmp = tmp2;
  56. }
  57. }
  58. return node;
  59. };
  60. /**
  61. * Function: afterDecode
  62. *
  63. * Restores the state by assigning the previous value.
  64. */
  65. codec.afterDecode = function(dec, node, obj)
  66. {
  67. obj.previous = obj.root;
  68. return obj;
  69. };
  70. // Returns the codec into the registry
  71. return codec;
  72. }());
  73. __mxOutput.mxRootChangeCodec = typeof mxRootChangeCodec !== 'undefined' ? mxRootChangeCodec : undefined;