Token.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. REPLAY_TOKEN_WIDTH = 10;
  2. /**
  3. * @description the current token.
  4. * @author <a href="mailto:lingosurf168@gmail.com">Lingo Surf168</a>
  5. *
  6. * @class
  7. * @param {Node} node node
  8. * @param {Replay} replay replay
  9. */
  10. Token = function(node, replay) {
  11. this.replay = replay;
  12. this.src = node;
  13. this.status = 'prepare';
  14. this.future = 0;
  15. this.forkIndex = 0;
  16. this.step = 10;
  17. };
  18. Token.prototype = {
  19. /**
  20. * @description initialize.
  21. */
  22. init: function() {
  23. this.x = this.src.x + this.src.w / 2 - REPLAY_TOKEN_WIDTH;
  24. this.y = this.src.y + this.src.h / 2 - REPLAY_TOKEN_WIDTH;
  25. if (this.status === 'prepare') {
  26. this.status = 'waiting';
  27. this.createImage();
  28. }
  29. },
  30. /**
  31. * @description create image.
  32. */
  33. createImage: function() {
  34. var dom = document.createElement('img');
  35. document.getElementById('processGraphMask').appendChild(dom);
  36. dom.style.position = 'absolute';
  37. dom.src = REPLAY_TOKEN_IMAGE;
  38. dom.style.left = this.x + 'px';
  39. dom.style.top = this.y + 'px';
  40. this.dom = dom;
  41. },
  42. /**
  43. * @description find next nodes.
  44. * @return {Array} children
  45. */
  46. findNext: function() {
  47. return this.src.children;
  48. },
  49. /**
  50. * @description find previous nodes.
  51. * @return {Array} parent
  52. */
  53. findPrev: function() {
  54. return this.src.parent;
  55. },
  56. /**
  57. * @description start move.
  58. * @param {Number} future step
  59. * @return {Boolean} need to notify jobExecutor
  60. */
  61. startMove: function(future) {
  62. if (future === 0) {
  63. return false;
  64. }
  65. if (this.status === 'waiting' || this.status === 'prepare') {
  66. var nodes = future > 0 ? this.findNext() : this.findPrev();
  67. if (nodes.length === 0) {
  68. this.future = 0;
  69. return false;
  70. }
  71. for (var i = 0; i < nodes.length; i++) {
  72. var dest = nodes[i];
  73. var token = this;
  74. if (i !== 0) {
  75. token = new Token(this.src, replay);
  76. this.replay.tokens.push(token);
  77. }
  78. token.forkIndex = this.forkIndex + i;
  79. token.prepare(dest, future);
  80. }
  81. return true;
  82. } else {
  83. this.future += future;
  84. return false;
  85. }
  86. },
  87. /**
  88. * @description prepare token.
  89. * @param {Node} dest dest node
  90. * @parma {Number} future step
  91. */
  92. prepare: function(dest, future) {
  93. this.init();
  94. this.dest = dest;
  95. this.future = future;
  96. this.status = 'running';
  97. this.step = 0;
  98. this.calculatePoints();
  99. },
  100. /**
  101. * @description calculate points for polyline.
  102. */
  103. calculatePoints: function() {
  104. var x1 = this.src.x + this.src.w / 2 - REPLAY_TOKEN_WIDTH;
  105. var y1 = this.src.y + this.src.h / 2 - REPLAY_TOKEN_WIDTH;
  106. var x2 = this.dest.x + this.dest.w / 2 - REPLAY_TOKEN_WIDTH;
  107. var y2 = this.dest.y + this.dest.h / 2 - REPLAY_TOKEN_WIDTH;
  108. this.points = [
  109. [x1, y1]
  110. ];
  111. var innerPoints = this.findTransition();
  112. if (innerPoints.length == 0) {
  113. var dx = (x2 - x1) / 10;
  114. var dy = (y2 - y1) / 10;
  115. for (var i = 0; i < 10; i++) {
  116. this.points.push([
  117. x1 + dx * (i + 1),
  118. y1 + dy * (i + 1)
  119. ]);
  120. }
  121. } else if (innerPoints.length == 1) {
  122. var x3 = innerPoints[0][0] - 10;
  123. var y3 = innerPoints[0][1] - 10;
  124. var dx = (x3 - x1) / 5;
  125. var dy = (y3 - y1) / 5;
  126. for (var i = 0; i < 5; i++) {
  127. this.points.push([
  128. x1 + dx * (i + 1),
  129. y1 + dy * (i + 1)
  130. ]);
  131. }
  132. dx = (x2 - x3) / 5;
  133. dy = (y2 - y3) / 5;
  134. for (var i = 0; i < 5; i++) {
  135. this.points.push([
  136. x3 + dx * (i + 1),
  137. y3 + dy * (i + 1)
  138. ]);
  139. }
  140. }
  141. },
  142. /**
  143. * @description find transition.
  144. * @return {Array} inner points
  145. */
  146. findTransition: function() {
  147. var innerPoints = null;
  148. if (this.future > 0) {
  149. innerPoints = this.findTransitionByParent();
  150. } else if (this.future < 0) {
  151. innerPoints = this.findTransitionByChild();
  152. }
  153. if (!innerPoints) {
  154. innerPoints = [];
  155. }
  156. return innerPoints;
  157. },
  158. /**
  159. * @description find transition by parent.
  160. * @return {Array} inner points
  161. */
  162. findTransitionByParent: function() {
  163. for (var i = 0; i < this.dest.parent.length; i++) {
  164. var parentNode = this.dest.parent[i];
  165. if (this.src == parentNode) {
  166. for (var j = 0; j < parentNode.activity.ts.length; j++) {
  167. var t = parentNode.activity.ts[j];
  168. if (t.to == this.dest.activity.name) {
  169. return t.g;
  170. }
  171. }
  172. }
  173. }
  174. return null;
  175. },
  176. /**
  177. * @description find transition by children.
  178. * @return {Array} inner points
  179. */
  180. findTransitionByChild: function() {
  181. for (var i = 0; i < this.dest.children.length; i++) {
  182. var childNode = this.dest.children[i];
  183. if (this.src == childNode) {
  184. for (var j = 0; j < this.dest.activity.ts.length; j++) {
  185. var t = this.dest.activity.ts[j];
  186. if (t.to == childNode.activity.name) {
  187. if (!t.g) {
  188. return null;
  189. }
  190. var g = [];
  191. for (var i = t.g.length - 1; i >= 0; i--) {
  192. g.push(t.g[i]);
  193. }
  194. return g;
  195. }
  196. }
  197. }
  198. }
  199. return null;
  200. },
  201. /**
  202. * @description token move.
  203. */
  204. move: function() {
  205. this.step++;
  206. if (this.step > 10) {
  207. if (this.future !== 0) {
  208. if (this.future > 0) {
  209. this.future--;
  210. } else {
  211. this.future++;
  212. }
  213. }
  214. var node = this.dest;
  215. if (this.forkIndex > 0) {
  216. if (node.type == 'fork' || node.type == 'join') {
  217. this.destroy();
  218. return;
  219. }
  220. }
  221. this.src = node;
  222. this.init();
  223. this.status = 'waiting';
  224. this.startMove(this.future);
  225. } else {
  226. this.dom.style.left = this.points[this.step][0] + 'px';
  227. this.dom.style.top = this.points[this.step][1] + 'px';
  228. }
  229. },
  230. /**
  231. * @description destory token.
  232. */
  233. destroy: function() {
  234. if (typeof this.dom !== 'undefined') {
  235. document.getElementById('processGraphMask').removeChild(this.dom);
  236. delete this.dom;
  237. }
  238. this.status = 'removed';
  239. }
  240. };