Node.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @description the node in ProcessDefinition.
  3. * @author <a href="mailto:lingosurf168@gmail.com">Lingo Surf168</a>
  4. *
  5. * @class
  6. * @param {Map} activity origin information from ProcessDefinition
  7. * @parma {Replay} replay Replay
  8. */
  9. Node = function(activity, replay) {
  10. this.name = activity.name;
  11. this.type = activity.type;
  12. this.x = activity.x;
  13. this.y = activity.y;
  14. if (this.type === 'start' || this.type === 'end' || this.type === 'end-error'
  15. || this.type === 'end-cancel' || this.type === 'decision'
  16. || this.type === 'fork' || this.type === 'join') {
  17. this.w = 48;
  18. this.h = 48;
  19. } else {
  20. this.w = activity.w;
  21. this.h = activity.h;
  22. }
  23. this.activity = activity;
  24. this.replay = replay;
  25. this.parent = [];
  26. this.children = [];
  27. var duplicatedNode = this.replay.map[this.name];
  28. if (typeof duplicatedNode !== 'undefined') {
  29. if (duplicatedNode !== this) {
  30. throw new Error('node duplicated, name: ' + this.name);
  31. }
  32. } else {
  33. this.replay.map[this.name] = this;
  34. }
  35. if (!this.isCurrentActivity(this.name)) {
  36. this.init();
  37. }
  38. };
  39. Node.prototype = {
  40. /**
  41. * @description initialize.
  42. */
  43. init: function() {
  44. if (!this.hasHistory()) {
  45. this.findTransitions();
  46. }
  47. },
  48. /**
  49. * @description create child node by activity.
  50. * @param {Map} activity origin information
  51. */
  52. createChildNode: function(activity) {
  53. var name = activity.name;
  54. var alreadyCreatedNode = this.replay.map[activity.name];
  55. var childNode = null;
  56. if (typeof alreadyCreatedNode !== 'undefined') {
  57. childNode = alreadyCreatedNode;
  58. } else {
  59. childNode = new Node(activity, this.replay);
  60. }
  61. this.children.push(childNode);
  62. childNode.parent.push(this);
  63. },
  64. /**
  65. * @description compare history and acitivity.
  66. * return {Boolean} if has history
  67. */
  68. hasHistory: function() {
  69. var activities = this.replay.historyActivities;
  70. for (var i = 0; i < activities.length; i++) {
  71. var ha = activities[i];
  72. if (ha.name === this.activity.name) {
  73. var ht = ha.t;
  74. var ts = this.activity.ts;
  75. for (var j = 0; j < ts.length; j++) {
  76. var t = ts[j];
  77. if (t.name === ht) {
  78. var activity = this.findActivity(t.to);
  79. this.createChildNode(activity);
  80. return true;
  81. }
  82. }
  83. }
  84. }
  85. return false;
  86. },
  87. /**
  88. * @description find transitions.
  89. */
  90. findTransitions: function() {
  91. var ts = this.activity.ts;
  92. for (var i = 0; i < ts.length; i++) {
  93. var t = ts[i];
  94. var activityName = t.to;
  95. var activity = this.findActivity(activityName);
  96. this.createChildNode(activity);
  97. }
  98. },
  99. /**
  100. * @description find activity by name.
  101. * @parma {String} activityName activity name
  102. */
  103. findActivity: function(activityName) {
  104. var pd = this.replay.processDefinition;
  105. for (var i = 0; i < pd.length; i++) {
  106. var activity = pd[i];
  107. if (activity.name === activityName) {
  108. return activity;
  109. }
  110. }
  111. },
  112. /**
  113. * @description if it is the current activity.
  114. * @param {String} activity activity name
  115. * @return {Boolean} if is current
  116. */
  117. isCurrentActivity: function(activityName) {
  118. var activities = this.replay.currentActivities;
  119. for (var i = 0; i < activities.length; i++) {
  120. var currentActivityName = activities[i];
  121. if (currentActivityName === activityName) {
  122. return true;
  123. }
  124. }
  125. return false;
  126. }
  127. };