mxObjectIdentity.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. var mxObjectIdentity =
  6. {
  7. /**
  8. * Class: mxObjectIdentity
  9. *
  10. * Identity for JavaScript objects and functions. This is implemented using
  11. * a simple incrementing counter which is stored in each object under
  12. * <FIELD_NAME>.
  13. *
  14. * The identity for an object does not change during its lifecycle.
  15. *
  16. * Variable: FIELD_NAME
  17. *
  18. * Name of the field to be used to store the object ID. Default is
  19. * <code>mxObjectId</code>.
  20. */
  21. FIELD_NAME: 'mxObjectId',
  22. /**
  23. * Variable: counter
  24. *
  25. * Current counter.
  26. */
  27. counter: 0,
  28. /**
  29. * Function: get
  30. *
  31. * Returns the ID for the given object or function or null if no object
  32. * is specified.
  33. */
  34. get: function(obj)
  35. {
  36. if (obj != null)
  37. {
  38. if (obj[mxObjectIdentity.FIELD_NAME] == null)
  39. {
  40. if (typeof obj === 'object')
  41. {
  42. var ctor = mxUtils.getFunctionName(obj.constructor);
  43. obj[mxObjectIdentity.FIELD_NAME] = ctor + '#' + mxObjectIdentity.counter++;
  44. }
  45. else if (typeof obj === 'function')
  46. {
  47. obj[mxObjectIdentity.FIELD_NAME] = 'Function#' + mxObjectIdentity.counter++;
  48. }
  49. }
  50. return obj[mxObjectIdentity.FIELD_NAME];
  51. }
  52. return null;
  53. },
  54. /**
  55. * Function: clear
  56. *
  57. * Deletes the ID from the given object or function.
  58. */
  59. clear: function(obj)
  60. {
  61. if (typeof(obj) === 'object' || typeof obj === 'function')
  62. {
  63. delete obj[mxObjectIdentity.FIELD_NAME];
  64. }
  65. }
  66. };
  67. __mxOutput.mxObjectIdentity = typeof mxObjectIdentity !== 'undefined' ? mxObjectIdentity : undefined;