mxCellCodec.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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' && mxUtils.isNode(value));
  74. };
  75. /**
  76. * Function: afterEncode
  77. *
  78. * Encodes an <mxCell> and wraps the XML up inside the
  79. * XML of the user object (inversion).
  80. */
  81. codec.afterEncode = function(enc, obj, node)
  82. {
  83. if (obj.value != null && mxUtils.isNode(obj.value))
  84. {
  85. // Wraps the graphical annotation up in the user object (inversion)
  86. // by putting the result of the default encoding into a clone of the
  87. // user object (node type 1) and returning this cloned user object.
  88. var tmp = node;
  89. node = mxUtils.importNode(enc.document, obj.value, true);
  90. node.appendChild(tmp);
  91. // Moves the id attribute to the outermost XML node, namely the
  92. // node which denotes the object boundaries in the file.
  93. var id = tmp.getAttribute('id');
  94. node.setAttribute('id', id);
  95. tmp.removeAttribute('id');
  96. }
  97. return node;
  98. };
  99. /**
  100. * Function: beforeDecode
  101. *
  102. * Decodes an <mxCell> and uses the enclosing XML node as
  103. * the user object for the cell (inversion).
  104. */
  105. codec.beforeDecode = function(dec, node, obj)
  106. {
  107. var inner = node.cloneNode(true);
  108. var classname = this.getName();
  109. if (node.nodeName != classname)
  110. {
  111. // Passes the inner graphical annotation node to the
  112. // object codec for further processing of the cell.
  113. var tmp = node.getElementsByTagName(classname)[0];
  114. if (tmp != null && tmp.parentNode == node)
  115. {
  116. mxUtils.removeWhitespace(tmp, true);
  117. mxUtils.removeWhitespace(tmp, false);
  118. tmp.parentNode.removeChild(tmp);
  119. inner = tmp;
  120. }
  121. else
  122. {
  123. inner = null;
  124. }
  125. // Creates the user object out of the XML node
  126. obj.value = node.cloneNode(true);
  127. var id = obj.value.getAttribute('id');
  128. if (id != null)
  129. {
  130. obj.setId(id);
  131. obj.value.removeAttribute('id');
  132. }
  133. }
  134. else
  135. {
  136. // Uses ID from XML file as ID for cell in model
  137. obj.setId(node.getAttribute('id'));
  138. }
  139. // Preprocesses and removes all Id-references in order to use the
  140. // correct encoder (this) for the known references to cells (all).
  141. if (inner != null)
  142. {
  143. for (var i = 0; i < this.idrefs.length; i++)
  144. {
  145. var attr = this.idrefs[i];
  146. var ref = inner.getAttribute(attr);
  147. if (ref != null)
  148. {
  149. inner.removeAttribute(attr);
  150. var object = dec.objects[ref] || dec.lookup(ref);
  151. if (object == null)
  152. {
  153. // Needs to decode forward reference
  154. var element = dec.getElementById(ref);
  155. if (element != null)
  156. {
  157. var decoder = mxCodecRegistry.codecs[element.nodeName] || this;
  158. object = decoder.decode(dec, element);
  159. }
  160. else if (window.console != null)
  161. {
  162. console.error('mxCellCodec.beforeDecode: ' +
  163. this.idrefs[i] + ' ' + ref + ' not found' +
  164. ' for cell ' + obj.getId());
  165. }
  166. }
  167. obj[attr] = object;
  168. }
  169. }
  170. }
  171. return inner;
  172. };
  173. // Returns the codec into the registry
  174. return codec;
  175. }());