mxCellCodec.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. mxCodecRegistry.register(function()
  6. {
  7. /**
  8. * Class: mxCellCodec
  9. *
  10. * Codec for <mxCell>s. This class is created and registered
  11. * dynamically at load time and used implicitly via <mxCodec>
  12. * and the <mxCodecRegistry>.
  13. *
  14. * Transient Fields:
  15. *
  16. * - children
  17. * - edges
  18. * - overlays
  19. * - mxTransient
  20. *
  21. * Reference Fields:
  22. *
  23. * - parent
  24. * - source
  25. * - target
  26. *
  27. * Transient fields can be added using the following code:
  28. *
  29. * mxCodecRegistry.getCodec(mxCell).exclude.push('name_of_field');
  30. *
  31. * To subclass <mxCell>, replace the template and add an alias as
  32. * follows.
  33. *
  34. * (code)
  35. * function CustomCell(value, geometry, style)
  36. * {
  37. * mxCell.apply(this, arguments);
  38. * }
  39. *
  40. * mxUtils.extend(CustomCell, mxCell);
  41. *
  42. * mxCodecRegistry.getCodec(mxCell).template = new CustomCell();
  43. * mxCodecRegistry.addAlias('CustomCell', 'mxCell');
  44. * (end)
  45. */
  46. var codec = new mxObjectCodec(new mxCell(),
  47. ['children', 'edges', 'overlays', 'mxTransient'],
  48. ['parent', 'source', 'target']);
  49. /**
  50. * Function: isCellCodec
  51. *
  52. * Returns true since this is a cell codec.
  53. */
  54. codec.isCellCodec = function()
  55. {
  56. return true;
  57. };
  58. /**
  59. * Overidden to disable conversion of value to number.
  60. */
  61. codec.isNumericAttribute = function(dec, attr, obj)
  62. {
  63. return attr.nodeName !== 'value' && mxObjectCodec.prototype.isNumericAttribute.apply(this, arguments);
  64. };
  65. /**
  66. * Function: isExcluded
  67. *
  68. * Excludes user objects that are XML nodes.
  69. */
  70. codec.isExcluded = function(obj, attr, value, isWrite)
  71. {
  72. return mxObjectCodec.prototype.isExcluded.apply(this, arguments) ||
  73. (isWrite && attr == 'value' &&
  74. value.nodeType == mxConstants.NODETYPE_ELEMENT);
  75. };
  76. /**
  77. * Function: afterEncode
  78. *
  79. * Encodes an <mxCell> and wraps the XML up inside the
  80. * XML of the user object (inversion).
  81. */
  82. codec.afterEncode = function(enc, obj, node)
  83. {
  84. if (obj.value != null && obj.value.nodeType == mxConstants.NODETYPE_ELEMENT)
  85. {
  86. // Wraps the graphical annotation up in the user object (inversion)
  87. // by putting the result of the default encoding into a clone of the
  88. // user object (node type 1) and returning this cloned user object.
  89. var tmp = node;
  90. node = mxUtils.importNode(enc.document, obj.value, true);
  91. node.appendChild(tmp);
  92. // Moves the id attribute to the outermost XML node, namely the
  93. // node which denotes the object boundaries in the file.
  94. var id = tmp.getAttribute('id');
  95. node.setAttribute('id', id);
  96. tmp.removeAttribute('id');
  97. }
  98. return node;
  99. };
  100. /**
  101. * Function: beforeDecode
  102. *
  103. * Decodes an <mxCell> and uses the enclosing XML node as
  104. * the user object for the cell (inversion).
  105. */
  106. codec.beforeDecode = function(dec, node, obj)
  107. {
  108. var inner = node.cloneNode(true);
  109. var classname = this.getName();
  110. if (node.nodeName != classname)
  111. {
  112. // Passes the inner graphical annotation node to the
  113. // object codec for further processing of the cell.
  114. var tmp = node.getElementsByTagName(classname)[0];
  115. if (tmp != null && tmp.parentNode == node)
  116. {
  117. mxUtils.removeWhitespace(tmp, true);
  118. mxUtils.removeWhitespace(tmp, false);
  119. tmp.parentNode.removeChild(tmp);
  120. inner = tmp;
  121. }
  122. else
  123. {
  124. inner = null;
  125. }
  126. // Creates the user object out of the XML node
  127. obj.value = node.cloneNode(true);
  128. var id = obj.value.getAttribute('id');
  129. if (id != null)
  130. {
  131. obj.setId(id);
  132. obj.value.removeAttribute('id');
  133. }
  134. }
  135. else
  136. {
  137. // Uses ID from XML file as ID for cell in model
  138. obj.setId(node.getAttribute('id'));
  139. }
  140. // Preprocesses and removes all Id-references in order to use the
  141. // correct encoder (this) for the known references to cells (all).
  142. if (inner != null)
  143. {
  144. for (var i = 0; i < this.idrefs.length; i++)
  145. {
  146. var attr = this.idrefs[i];
  147. var ref = inner.getAttribute(attr);
  148. if (ref != null)
  149. {
  150. inner.removeAttribute(attr);
  151. var object = dec.objects[ref] || dec.lookup(ref);
  152. if (object == null)
  153. {
  154. // Needs to decode forward reference
  155. var element = dec.getElementById(ref);
  156. if (element != null)
  157. {
  158. var decoder = mxCodecRegistry.codecs[element.nodeName] || this;
  159. object = decoder.decode(dec, element);
  160. }
  161. }
  162. obj[attr] = object;
  163. }
  164. }
  165. }
  166. return inner;
  167. };
  168. // Returns the codec into the registry
  169. return codec;
  170. }());
  171. __mxOutput.mxCellCodec = typeof mxCellCodec !== 'undefined' ? mxCellCodec : undefined;