mxModelCodec.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. mxCodecRegistry.register(function()
  6. {
  7. /**
  8. * Class: mxModelCodec
  9. *
  10. * Codec for <mxGraphModel>s. This class is created and registered
  11. * dynamically at load time and used implicitly via <mxCodec>
  12. * and the <mxCodecRegistry>.
  13. */
  14. var codec = new mxObjectCodec(new mxGraphModel());
  15. /**
  16. * Function: encodeObject
  17. *
  18. * Encodes the given <mxGraphModel> by writing a (flat) XML sequence of
  19. * cell nodes as produced by the <mxCellCodec>. The sequence is
  20. * wrapped-up in a node with the name root.
  21. */
  22. codec.encodeObject = function(enc, obj, node)
  23. {
  24. var rootNode = enc.document.createElement('root');
  25. enc.encodeCell(obj.getRoot(), rootNode);
  26. node.appendChild(rootNode);
  27. };
  28. /**
  29. * Function: decodeChild
  30. *
  31. * Overrides decode child to handle special child nodes.
  32. */
  33. codec.decodeChild = function(dec, child, obj)
  34. {
  35. if (child.nodeName == 'root')
  36. {
  37. this.decodeRoot(dec, child, obj);
  38. }
  39. else
  40. {
  41. mxObjectCodec.prototype.decodeChild.apply(this, arguments);
  42. }
  43. };
  44. /**
  45. * Function: decodeRoot
  46. *
  47. * Reads the cells into the graph model. All cells
  48. * are children of the root element in the node.
  49. */
  50. codec.decodeRoot = function(dec, root, model)
  51. {
  52. var rootCell = null;
  53. var tmp = root.firstChild;
  54. while (tmp != null)
  55. {
  56. var cell = dec.decodeCell(tmp);
  57. if (cell != null && cell.getParent() == null)
  58. {
  59. rootCell = cell;
  60. }
  61. tmp = tmp.nextSibling;
  62. }
  63. // Sets the root on the model if one has been decoded
  64. if (rootCell != null)
  65. {
  66. model.setRoot(rootCell);
  67. }
  68. };
  69. // Returns the codec into the registry
  70. return codec;
  71. }());
  72. __mxOutput.mxModelCodec = typeof mxModelCodec !== 'undefined' ? mxModelCodec : undefined;