Monitor.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. MWF.xApplication.process.Xform.widget = MWF.xApplication.process.Xform.widget || {};
  2. //MWF.xDesktop.requireApp("process.Xform", "widget.RestActions", null, false);
  3. MWF.require("MWF.widget.Common", null, false);
  4. MWF.require("MWF.widget.MWFRaphael", null, false);
  5. MWF.xApplication.process.Xform.widget.Monitor = new Class({
  6. Implements: [Options, Events],
  7. Extends: MWF.widget.Common,
  8. options: {
  9. "style": "default"
  10. },
  11. initialize: function(container, worklog, processid, options){
  12. this.setOptions(options);
  13. this.path = "/x_component_process_Xform/widget/$Monitor/";
  14. this.cssPath = "/x_component_process_Xform/widget/$Monitor/"+this.options.style+"/css.wcss";
  15. this._loadCss();
  16. this.container = $(container);
  17. this.worklog = worklog;
  18. this.processid = processid;
  19. this.load();
  20. },
  21. load: function(){
  22. this.logProcessChartNode = new Element("div", {"styles": this.css.logProcessChartNode}).inject(this.container);
  23. this.logPathChartNode = new Element("div", {"styles": this.css.logPathChartNode}).inject(this.container);
  24. this.checkMonitorOpen();
  25. },
  26. checkMonitorOpen: function(){
  27. var moduleNode = this.container;
  28. var module = moduleNode.retrieve("module");
  29. var isDisplayNode = false;
  30. var isTabContent = false;
  31. while (true){
  32. if (moduleNode.getStyle("display")==="none"){
  33. isDisplayNode = true;
  34. }
  35. if (module && module.json.type==="Tab$Content"){
  36. isTabContent = true;
  37. }
  38. if (isDisplayNode && isTabContent) break;
  39. moduleNode = moduleNode.getParent();
  40. if (!moduleNode) break;
  41. if (!isTabContent) module = moduleNode.retrieve("module");
  42. }
  43. if (isDisplayNode){
  44. if (isTabContent){
  45. for (var i=0; i<module.tab.tab.pages.length; i++){
  46. if (module.tab.tab.pages[i].contentNode === module.node) break;
  47. }
  48. module.tab.tab.pages[i].setOptions({
  49. "onPostShow": function(){
  50. this.openProcess();
  51. }.bind(this)
  52. });
  53. }else{
  54. this.openProcessChartAction = new Element("div", {"styles": this.css.openProcessChartAction, "text": "Show Process"}).inject(this.logProcessChartNode);
  55. this.openProcessChartAction.addEvent("click", function(e){
  56. this.openProcess(e);
  57. }.bind(this));
  58. }
  59. }else{
  60. this.openProcess();
  61. }
  62. },
  63. openProcess: function(){
  64. if (!this.process){
  65. this.logProcessChartNode.empty();
  66. this.loadToolbar();
  67. this.paperNode = new Element("div", {"styles": this.css.paperNode}).inject(this.logProcessChartNode);
  68. //this.paperNode.addEvent("scroll", function(){
  69. // this.setCountNodePosition();
  70. //}.bind(this));
  71. this.getProcess(function(json){
  72. this.processData = json.data;
  73. this.loadPaper();
  74. }.bind(this));
  75. }
  76. },
  77. loadToolbar: function(){
  78. MWF.require("MWF.widget.Toolbar", function(){
  79. this.toolbarNode = new Element("div").inject(this.logProcessChartNode);
  80. this.createToolbarNode("play.png", "logPlay", "");
  81. //this.createToolbarNode("pause.png", "logPause", "true");
  82. //this.createToolbarNode("stop.png", "playStop", "true");
  83. //this.createToolbarNode("prev.png", "logPrev", "true");
  84. //this.createToolbarNode("next.png", "logNext", "");
  85. this.toolbar = new MWF.widget.Toolbar(this.toolbarNode, {"style": this.options.style}, this);
  86. this.toolbar.load();
  87. }.bind(this));
  88. },
  89. createToolbarNode: function(img, action, disabled){
  90. new Element("div", {
  91. "MWFnodetype": "MWFToolBarButton",
  92. "MWFButtonImage": this.path+""+this.options.style+"/tools/"+img,
  93. "title": "",
  94. "MWFButtonAction": action,
  95. "MWFButtonText": "",
  96. "MWFButtonDisable": disabled
  97. }).inject(this.toolbarNode);
  98. },
  99. logPlay: function(){
  100. if (this.process){
  101. this.isPlaying = true;
  102. this.toolbar.childrenButton[0].setDisable(true);
  103. //this.toolbar.childrenButton[1].setDisable(false);
  104. //this.toolbar.childrenButton[2].setDisable(false);
  105. this.processReturnStyle();
  106. this.playBegin();
  107. this.playToNextActivity();
  108. }
  109. },
  110. playBegin: function(){
  111. var src = "/x_component_process_Xform/widget/$Monitor/"+this.options.style+"/fly.png";
  112. this.playIcon = this.paper.image(src, this.process.begin.center.x-16, this.process.begin.center.y-16, 32, 32);
  113. this.playLogNode = null;
  114. this.playsStatus = {
  115. "index": 0
  116. };
  117. this.isPlaying = true;
  118. },
  119. playGetNextActivity: function(){
  120. var log = this.worklog[this.playsStatus.index];
  121. var activityType = log.fromActivityType;
  122. var activity = (activityType.toLowerCase()=="begin") ? this.process.begin : this.process[activityType+"s"][log.fromActivity];
  123. return {"log": log, "activity": activity};
  124. },
  125. playToNextActivity: function(route){
  126. var activity = this.playGetNextActivity();
  127. if (this.playsStatus.index == 0){
  128. this.playToActivityStop(activity.activity, activity.log);
  129. }else{
  130. this.playMoveToActivity(activity.activity, activity.log, route);
  131. }
  132. },
  133. playMoveToActivity: function(activity, log, route){
  134. if (route){
  135. var points = [route.beginPoint];
  136. points = points.concat(route.positionPoints, [route.endPoint], [{"x": activity.center.x, "y": activity.center.y}]);
  137. this.playMoveByRoutePoint(points, function(){
  138. this.playToActivityStop(activity, log, route);
  139. }.bind(this));
  140. }else{
  141. this.playToActivityStop(activity, log, route);
  142. }
  143. },
  144. playMoveByRoutePoint: function(points, callback){
  145. var p = {"x": this.playIcon.attr("x").toFloat(), "y": this.playIcon.attr("y").toFloat()};
  146. var toP = points.shift();
  147. var d = MWFRaphael.getPointDistance(p, toP);
  148. var ms = d/0.2;
  149. this.playIcon.animate({"x": toP.x-16, "y": toP.y-16}, ms, "linear", function() {
  150. if (points.length) {
  151. this.playMoveByRoutePoint(points, callback);
  152. } else {
  153. if (callback) callback();
  154. }
  155. }.bind(this));
  156. },
  157. playToActivityStop: function(activity, log, prevRoute){
  158. this.playIcon.attr({
  159. "x": activity.center.x-16,
  160. "y": activity.center.y-16
  161. });
  162. if (log.connected){
  163. activity.shap.attr(this.css.passedActivityShap);
  164. }else{
  165. activity.shap.attr(this.css.currentActivityShap);
  166. }
  167. var route = this.process.routes[log.route];
  168. if (prevRoute){
  169. prevRoute.line.attr(this.css.passedRouteShap);
  170. prevRoute.point.attr(this.css.passedRouteFillShap);
  171. prevRoute.arrow.attr(this.css.passedRouteFillShap);
  172. }
  173. this.showPlayLog(activity,log);
  174. this.playsStatus.index++;
  175. window.setTimeout(function(){
  176. if (this.worklog.length<=this.playsStatus.index){
  177. this.playStop();
  178. }else{
  179. this.playLogNode.destroy();
  180. this.playLogNode = null;
  181. this.playToNextActivity(route);
  182. }
  183. }.bind(this), 2000);
  184. },
  185. showPlayLog: function(activity,log){
  186. var offset = this.paperNode.getPosition(this.paperNode.getOffsetParent());
  187. var size = this.paperNode.getSize();
  188. this.playLogNode = this.createWorkLogNode([log]);
  189. this.playLogNode.setStyle("display", "block");
  190. var p = this.getlogNodePosition(activity, this.playLogNode, offset, size);
  191. this.playLogNode.setPosition({"x": p.x, "y": p.y});
  192. },
  193. playStop: function(){
  194. this.playIcon.remove();
  195. if (this.playLogNode) this.playLogNode.destroy();
  196. this.playLogNode = null;
  197. this.playsStatus = {
  198. "index": 0
  199. };
  200. this.isPlaying = false;
  201. this.toolbar.childrenButton[0].setDisable(false);
  202. //this.toolbar.childrenButton[1].setDisable(true);
  203. //this.toolbar.childrenButton[2].setDisable(true);
  204. this.loadWorkLog();
  205. },
  206. processReturnStyle: function(){
  207. this.worklog.each(function(log){
  208. var activityType = log.fromActivityType;
  209. var activity = (activityType.toLowerCase()=="begin") ? this.process.begin : this.process[activityType+"s"][log.fromActivity];
  210. activity.shap.attr(activity.style.shap);
  211. activity.passedCount = 0;
  212. activity.worklogs = [];
  213. var route = this.process.routes[log.route];
  214. if (route){
  215. route.line.attr(this.process.css.route.line.normal);
  216. route.point.attr(this.process.css.route.decision.normal);
  217. route.arrow.attr(this.process.css.route.arrow.normal);
  218. }
  219. if (activity.countSet) activity.countSet.remove();
  220. }.bind(this));
  221. },
  222. loadPaper: function(){
  223. MWFRaphael.load(function(){
  224. this.paperInNode = new Element("div", {"styles": this.css.paperInNode}).inject(this.paperNode);
  225. this.paper = Raphael(this.paperInNode, "98%", "99%");
  226. this.paper.container = this.paperNode;
  227. MWF.xDesktop.requireApp("process.ProcessDesigner", "Process", function(){
  228. this.process = new MWF.APPPD.Process(this.paper, this.processData, this, {"style":"flat", "isView": true,
  229. "onPostLoad": function(){
  230. this.loadWorkLog();
  231. }.bind(this)
  232. });
  233. this.process.load();
  234. }.bind(this));
  235. }.bind(this));
  236. },
  237. getProcess: function(callback){
  238. this.action = MWF.Actions.get("x_processplatform_assemble_surface");
  239. //this.action = new MWF.xApplication.process.Xform.widget.RestActions("x_processplatform_assemble_surface");
  240. //this.action = new MWF.xApplication.process.Xform.widget.RestActions("x_processplatform_assemble_designer");
  241. this.action.getProcess(function(json){
  242. if (callback) callback(json);
  243. }, null, this.processid)
  244. },
  245. loadWorkLog: function(){
  246. this.countNodes = [];
  247. var activitys = {};
  248. this.worklogToken = {};
  249. this.worklog.each(function(log){
  250. this.worklogToken[log.fromActivityToken] = log;
  251. var activityType = log.fromActivityType;
  252. var activity = (activityType.toLowerCase()=="begin") ? this.process.begin : this.process[activityType+"s"][log.fromActivity];
  253. if (log.connected){
  254. activity.shap.attr(this.css.passedActivityShap);
  255. }else{
  256. activity.shap.attr(this.css.currentActivityShap);
  257. }
  258. var route = this.process.routes[log.route];
  259. if (route){
  260. route.line.attr(this.css.passedRouteShap);
  261. route.point.attr(this.css.passedRouteFillShap);
  262. route.arrow.attr(this.css.passedRouteFillShap);
  263. }
  264. var passedCount = log.taskCompletedList.length;
  265. //var passedCount = log.taskCompletedList.length || 1;
  266. if (passedCount) activity.passedCount = (activity.passedCount) ? activity.passedCount+passedCount : passedCount;
  267. if (!activity.worklogs) activity.worklogs = [];
  268. activity.worklogs.push(log);
  269. if (!activitys[log.fromActivity]) activitys[log.fromActivity] = activity
  270. }.bind(this));
  271. var offset = this.paperNode.getPosition(this.paperNode.getOffsetParent());
  272. var size = this.paperNode.getSize();
  273. Object.each(activitys, function(activity){
  274. this.writePassCount(activity);
  275. this.writeWorkLog(activity, offset, size);
  276. }.bind(this));
  277. },
  278. writePassCount: function(activity){
  279. if (activity.passedCount){
  280. var x = activity.point.x+activity.width;
  281. var y = activity.point.y;
  282. var shap = this.paper.circle(x, y, 9);
  283. shap.attr(this.css.activityPassedCount);
  284. text = this.paper.text(x, y, activity.passedCount);
  285. text.attr(this.css.activityPassedCountText);
  286. activity.countSet = this.paper.set();
  287. activity.countSet.push(shap, text);
  288. }
  289. },
  290. writeWorkLog: function(activity, offset, size){
  291. var _self = this;
  292. activity.set.click(function(e){
  293. if (!_self.isPlaying){
  294. if (this.process.selectedActivitys.length){
  295. if (!this.noselected){
  296. this.selected();
  297. _self.showWorklog(this, offset, size);
  298. }
  299. this.noselected = false;
  300. }
  301. if (this.countSet) this.countSet.toFront();
  302. }
  303. e.stopPropagation();
  304. }.bind(activity));
  305. activity.set.mousedown(function(e){
  306. if (!_self.isPlaying) {
  307. if (!this.process.selectedActivitys.length) {
  308. this.selected();
  309. _self.showWorklog(this, offset, size);
  310. }
  311. if (this.countSet) this.countSet.toFront();
  312. }
  313. e.stopPropagation();
  314. }.bind(activity));
  315. this.paper.canvas.addEvent("click", function(e){
  316. if (!_self.isPlaying) {
  317. if (this.unSelectedEvent) {
  318. if (this.currentSelected || this.selectedActivitys.length) {
  319. this.unSelected(e);
  320. _self.hideCurrentWorklog();
  321. }
  322. } else {
  323. this.unSelectedEvent = true;
  324. }
  325. }
  326. }.bind(this.process));
  327. },
  328. getlogNodePosition: function(activity, node, offset, psize){
  329. var size = node.getSize();
  330. var y = 0;
  331. var x = activity.point.x+activity.width+15+offset.x;
  332. tmpX = x + size.x;
  333. if (tmpX>offset.x+psize.x){
  334. x = activity.point.x - size.x - 15 + offset.x;
  335. if (x<offset.x){
  336. y = activity.point.y-size.y-15+offset.y;
  337. x = activity.center.x - (size.x/2) + offset.x;
  338. }else{
  339. y = activity.center.y - (size.y/2) + offset.y;
  340. if (y<offset.y){
  341. y = offset.y
  342. }
  343. }
  344. }else{
  345. y = activity.center.y - (size.y/2) + offset.y;
  346. if (y<offset.y){
  347. y = offset.y
  348. }
  349. }
  350. var p = this.paperNode.getScroll();
  351. var scrollY = 0;
  352. var scrollX = 0;
  353. var tmpNode = this.paperNode.getParent();
  354. while (tmpNode){
  355. var s = tmpNode.getScroll();
  356. scrollY += s.y;
  357. scrollX += s.x;
  358. tmpNode = tmpNode.getParent();
  359. }
  360. y = y-p.y-scrollY;
  361. x = x-p.x-scrollX;
  362. return {"x": x, "y": y};
  363. },
  364. showWorklog: function(activity, offset, psize){
  365. this.hideCurrentWorklog();
  366. if (!activity.worklogNode) activity.worklogNode = this.createWorkLogNode(activity.worklogs);
  367. this.currentWorklogNode = activity.worklogNode;
  368. this.currentWorklogNode.setStyle("display", "block");
  369. var p = this.getlogNodePosition(activity, activity.worklogNode, offset, psize)
  370. activity.worklogNode.setPosition({"x": p.x, "y": p.y});
  371. },
  372. hideCurrentWorklog: function(){
  373. if (this.currentWorklogNode){
  374. this.currentWorklogNode.setStyle("display", "none");
  375. this.currentWorklogNode = null;
  376. }
  377. },
  378. createWorkLogNode: function(worklogs){
  379. var node = new Element("div", {"styles": this.css.workLogNode});
  380. worklogs.each(function(log, idx){
  381. var workNode = new Element("div", {"styles": this.css.workLogWorkNode}).inject(node);
  382. if ((idx % 2)==0){
  383. workNode.setStyle("background-color", "#FFF");
  384. }else{
  385. workNode.setStyle("background-color", "#EEE");
  386. }
  387. if (log.taskCompletedList.length+log.taskList.length<1){
  388. if (log.connected){
  389. var taskNode = new Element("div", {"styles": this.css.workLogTaskNode}).inject(workNode);
  390. var html = "<div style='font-weight: bold'>"+MWF.xApplication.process.Xform.LP.systemProcess+" </div>";
  391. html += "<div style='text-align: right'>"+log.arrivedTime+"</div>";
  392. taskNode.set("html", html);
  393. }else{
  394. var taskNode = new Element("div", {"styles": this.css.workLogTaskNode}).inject(workNode);
  395. var html = "<div style='font-weight: bold; color: red'>"+MWF.xApplication.process.Xform.LP.systemProcess+" </div>";
  396. taskNode.set("html", html);
  397. }
  398. }else{
  399. log.taskCompletedList.each(function(task){
  400. var taskNode = new Element("div", {"styles": this.css.workLogTaskNode}).inject(workNode);
  401. var html = "<div style='font-weight: bold'>"+task.person.substring(0, task.person.indexOf("@"))+": </div>";
  402. html += "<div style='margin-left: 10px'>["+(task.routeName || "")+"] "+task.opinion+"</div>";
  403. html += "<div style='text-align: right'>"+task.completedTime+"</div>";
  404. taskNode.set("html", html);
  405. }.bind(this));
  406. log.taskList.each(function(task){
  407. var taskNode = new Element("div", {"styles": this.css.workLogTaskNode}).inject(workNode);
  408. var html = "<div style='font-weight: bold; color: red'>"+task.person.substring(0, task.person.indexOf("@"))+" "+MWF.xApplication.process.Xform.LP.processing+" </div>";
  409. taskNode.set("html", html);
  410. }.bind(this));
  411. }
  412. }.bind(this));
  413. node.inject(this.paperNode);
  414. return node;
  415. }
  416. });
  417. MWF.xApplication.process.Xform.widget.Monitor.Animation = new Class({
  418. Implements: [Events],
  419. initialize: function(monitor, log){
  420. }
  421. });