diff options
Diffstat (limited to 'src/js/io/mxModelCodec.js')
-rw-r--r-- | src/js/io/mxModelCodec.js | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/js/io/mxModelCodec.js b/src/js/io/mxModelCodec.js new file mode 100644 index 0000000..760a2b1 --- /dev/null +++ b/src/js/io/mxModelCodec.js @@ -0,0 +1,80 @@ +/** + * $Id: mxModelCodec.js,v 1.11 2010-11-23 08:46:41 gaudenz Exp $ + * Copyright (c) 2006-2010, JGraph Ltd + */ +mxCodecRegistry.register(function() +{ + /** + * Class: mxModelCodec + * + * Codec for <mxGraphModel>s. This class is created and registered + * dynamically at load time and used implicitely via <mxCodec> + * and the <mxCodecRegistry>. + */ + var codec = new mxObjectCodec(new mxGraphModel()); + + /** + * Function: encodeObject + * + * Encodes the given <mxGraphModel> by writing a (flat) XML sequence of + * cell nodes as produced by the <mxCellCodec>. The sequence is + * wrapped-up in a node with the name root. + */ + codec.encodeObject = function(enc, obj, node) + { + var rootNode = enc.document.createElement('root'); + enc.encodeCell(obj.getRoot(), rootNode); + node.appendChild(rootNode); + }; + + /** + * Function: decodeChild + * + * Overrides decode child to handle special child nodes. + */ + codec.decodeChild = function(dec, child, obj) + { + if (child.nodeName == 'root') + { + this.decodeRoot(dec, child, obj); + } + else + { + mxObjectCodec.prototype.decodeChild.apply(this, arguments); + } + }; + + /** + * Function: decodeRoot + * + * Reads the cells into the graph model. All cells + * are children of the root element in the node. + */ + codec.decodeRoot = function(dec, root, model) + { + var rootCell = null; + var tmp = root.firstChild; + + while (tmp != null) + { + var cell = dec.decodeCell(tmp); + + if (cell != null && cell.getParent() == null) + { + rootCell = cell; + } + + tmp = tmp.nextSibling; + } + + // Sets the root on the model if one has been decoded + if (rootCell != null) + { + model.setRoot(rootCell); + } + }; + + // Returns the codec into the registry + return codec; + +}()); |