mxEditorCodec.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. mxCodecRegistry.register(function()
  6. {
  7. /**
  8. * Class: mxEditorCodec
  9. *
  10. * Codec for <mxEditor>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. * - modified
  17. * - lastSnapshot
  18. * - ignoredChanges
  19. * - undoManager
  20. * - graphContainer
  21. * - toolbarContainer
  22. */
  23. var codec = new mxObjectCodec(new mxEditor(),
  24. ['modified', 'lastSnapshot', 'ignoredChanges',
  25. 'undoManager', 'graphContainer', 'toolbarContainer']);
  26. /**
  27. * Function: beforeDecode
  28. *
  29. * Decodes the ui-part of the configuration node by reading
  30. * a sequence of the following child nodes and attributes
  31. * and passes the control to the default decoding mechanism:
  32. *
  33. * Child Nodes:
  34. *
  35. * stylesheet - Adds a CSS stylesheet to the document.
  36. * resource - Adds the basename of a resource bundle.
  37. * add - Creates or configures a known UI element.
  38. *
  39. * These elements may appear in any order given that the
  40. * graph UI element is added before the toolbar element
  41. * (see Known Keys).
  42. *
  43. * Attributes:
  44. *
  45. * as - Key for the UI element (see below).
  46. * element - ID for the element in the document.
  47. * style - CSS style to be used for the element or window.
  48. * x - X coordinate for the new window.
  49. * y - Y coordinate for the new window.
  50. * width - Width for the new window.
  51. * height - Optional height for the new window.
  52. * name - Name of the stylesheet (absolute/relative URL).
  53. * basename - Basename of the resource bundle (see <mxResources>).
  54. *
  55. * The x, y, width and height attributes are used to create a new
  56. * <mxWindow> if the element attribute is not specified in an add
  57. * node. The name and basename are only used in the stylesheet and
  58. * resource nodes, respectively.
  59. *
  60. * Known Keys:
  61. *
  62. * graph - Main graph element (see <mxEditor.setGraphContainer>).
  63. * title - Title element (see <mxEditor.setTitleContainer>).
  64. * toolbar - Toolbar element (see <mxEditor.setToolbarContainer>).
  65. * status - Status bar element (see <mxEditor.setStatusContainer>).
  66. *
  67. * Example:
  68. *
  69. * (code)
  70. * <ui>
  71. * <stylesheet name="css/process.css"/>
  72. * <resource basename="resources/app"/>
  73. * <add as="graph" element="graph"
  74. * style="left:70px;right:20px;top:20px;bottom:40px"/>
  75. * <add as="status" element="status"/>
  76. * <add as="toolbar" x="10" y="20" width="54"/>
  77. * </ui>
  78. * (end)
  79. */
  80. codec.afterDecode = function(dec, node, obj)
  81. {
  82. // Assigns the specified templates for edges
  83. var defaultEdge = node.getAttribute('defaultEdge');
  84. if (defaultEdge != null)
  85. {
  86. node.removeAttribute('defaultEdge');
  87. obj.defaultEdge = obj.templates[defaultEdge];
  88. }
  89. // Assigns the specified templates for groups
  90. var defaultGroup = node.getAttribute('defaultGroup');
  91. if (defaultGroup != null)
  92. {
  93. node.removeAttribute('defaultGroup');
  94. obj.defaultGroup = obj.templates[defaultGroup];
  95. }
  96. return obj;
  97. };
  98. /**
  99. * Function: decodeChild
  100. *
  101. * Overrides decode child to handle special child nodes.
  102. */
  103. codec.decodeChild = function(dec, child, obj)
  104. {
  105. if (child.nodeName == 'Array')
  106. {
  107. var role = child.getAttribute('as');
  108. if (role == 'templates')
  109. {
  110. this.decodeTemplates(dec, child, obj);
  111. return;
  112. }
  113. }
  114. else if (child.nodeName == 'ui')
  115. {
  116. this.decodeUi(dec, child, obj);
  117. return;
  118. }
  119. mxObjectCodec.prototype.decodeChild.apply(this, arguments);
  120. };
  121. /**
  122. * Function: decodeUi
  123. *
  124. * Decodes the ui elements from the given node.
  125. */
  126. codec.decodeUi = function(dec, node, editor)
  127. {
  128. var tmp = node.firstChild;
  129. while (tmp != null)
  130. {
  131. if (tmp.nodeName == 'add')
  132. {
  133. var as = tmp.getAttribute('as');
  134. var elt = tmp.getAttribute('element');
  135. var style = tmp.getAttribute('style');
  136. var element = null;
  137. if (elt != null)
  138. {
  139. element = document.getElementById(elt);
  140. if (element != null && style != null)
  141. {
  142. element.style.cssText += ';' + style;
  143. }
  144. }
  145. else
  146. {
  147. var x = parseInt(tmp.getAttribute('x'));
  148. var y = parseInt(tmp.getAttribute('y'));
  149. var width = tmp.getAttribute('width');
  150. var height = tmp.getAttribute('height');
  151. // Creates a new window around the element
  152. element = document.createElement('div');
  153. element.style.cssText = style;
  154. var wnd = new mxWindow(mxResources.get(as) || as,
  155. element, x, y, width, height, false, true);
  156. wnd.setVisible(true);
  157. }
  158. // TODO: Make more generic
  159. if (as == 'graph')
  160. {
  161. editor.setGraphContainer(element);
  162. }
  163. else if (as == 'toolbar')
  164. {
  165. editor.setToolbarContainer(element);
  166. }
  167. else if (as == 'title')
  168. {
  169. editor.setTitleContainer(element);
  170. }
  171. else if (as == 'status')
  172. {
  173. editor.setStatusContainer(element);
  174. }
  175. else if (as == 'map')
  176. {
  177. editor.setMapContainer(element);
  178. }
  179. }
  180. else if (tmp.nodeName == 'resource')
  181. {
  182. mxResources.add(tmp.getAttribute('basename'));
  183. }
  184. else if (tmp.nodeName == 'stylesheet')
  185. {
  186. mxClient.link('stylesheet', tmp.getAttribute('name'));
  187. }
  188. tmp = tmp.nextSibling;
  189. }
  190. };
  191. /**
  192. * Function: decodeTemplates
  193. *
  194. * Decodes the cells from the given node as templates.
  195. */
  196. codec.decodeTemplates = function(dec, node, editor)
  197. {
  198. if (editor.templates == null)
  199. {
  200. editor.templates = [];
  201. }
  202. var children = mxUtils.getChildNodes(node);
  203. for (var j=0; j<children.length; j++)
  204. {
  205. var name = children[j].getAttribute('as');
  206. var child = children[j].firstChild;
  207. while (child != null && child.nodeType != 1)
  208. {
  209. child = child.nextSibling;
  210. }
  211. if (child != null)
  212. {
  213. // LATER: Only single cells means you need
  214. // to group multiple cells within another
  215. // cell. This should be changed to support
  216. // arrays of cells, or the wrapper must
  217. // be automatically handled in this class.
  218. editor.templates[name] = dec.decodeCell(child);
  219. }
  220. }
  221. };
  222. // Returns the codec into the registry
  223. return codec;
  224. }());
  225. __mxOutput.mxEditorCodec = typeof mxEditorCodec !== 'undefined' ? mxEditorCodec : undefined;