mxCodecRegistry.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. var mxCodecRegistry =
  6. {
  7. /**
  8. * Class: mxCodecRegistry
  9. *
  10. * Singleton class that acts as a global registry for codecs.
  11. *
  12. * Adding an <mxCodec>:
  13. *
  14. * 1. Define a default codec with a new instance of the
  15. * object to be handled.
  16. *
  17. * (code)
  18. * var codec = new mxObjectCodec(new mxGraphModel());
  19. * (end)
  20. *
  21. * 2. Define the functions required for encoding and decoding
  22. * objects.
  23. *
  24. * (code)
  25. * codec.encode = function(enc, obj) { ... }
  26. * codec.decode = function(dec, node, into) { ... }
  27. * (end)
  28. *
  29. * 3. Register the codec in the <mxCodecRegistry>.
  30. *
  31. * (code)
  32. * mxCodecRegistry.register(codec);
  33. * (end)
  34. *
  35. * <mxObjectCodec.decode> may be used to either create a new
  36. * instance of an object or to configure an existing instance,
  37. * in which case the into argument points to the existing
  38. * object. In this case, we say the codec "configures" the
  39. * object.
  40. *
  41. * Variable: codecs
  42. *
  43. * Maps from constructor names to codecs.
  44. */
  45. codecs: [],
  46. /**
  47. * Variable: aliases
  48. *
  49. * Maps from classnames to codecnames.
  50. */
  51. aliases: [],
  52. /**
  53. * Function: register
  54. *
  55. * Registers a new codec and associates the name of the template
  56. * constructor in the codec with the codec object.
  57. *
  58. * Parameters:
  59. *
  60. * codec - <mxObjectCodec> to be registered.
  61. */
  62. register: function(codec)
  63. {
  64. if (codec != null)
  65. {
  66. var name = codec.getName();
  67. mxCodecRegistry.codecs[name] = codec;
  68. var classname = mxUtils.getFunctionName(codec.template.constructor);
  69. if (classname != name)
  70. {
  71. mxCodecRegistry.addAlias(classname, name);
  72. }
  73. }
  74. return codec;
  75. },
  76. /**
  77. * Function: addAlias
  78. *
  79. * Adds an alias for mapping a classname to a codecname.
  80. */
  81. addAlias: function(classname, codecname)
  82. {
  83. mxCodecRegistry.aliases[classname] = codecname;
  84. },
  85. /**
  86. * Function: getCodec
  87. *
  88. * Returns a codec that handles objects that are constructed
  89. * using the given constructor.
  90. *
  91. * Parameters:
  92. *
  93. * ctor - JavaScript constructor function.
  94. */
  95. getCodec: function(ctor)
  96. {
  97. var codec = null;
  98. if (ctor != null)
  99. {
  100. var name = mxUtils.getFunctionName(ctor);
  101. var tmp = mxCodecRegistry.aliases[name];
  102. if (tmp != null)
  103. {
  104. name = tmp;
  105. }
  106. codec = mxCodecRegistry.codecs[name];
  107. // Registers a new default codec for the given constructor
  108. // if no codec has been previously defined.
  109. if (codec == null)
  110. {
  111. try
  112. {
  113. codec = new mxObjectCodec(new ctor());
  114. mxCodecRegistry.register(codec);
  115. }
  116. catch (e)
  117. {
  118. // ignore
  119. }
  120. }
  121. }
  122. return codec;
  123. }
  124. };
  125. __mxOutput.mxCodecRegistry = typeof mxCodecRegistry !== 'undefined' ? mxCodecRegistry : undefined;