Monitor.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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": (layout.mobile) ? this.css.paperNodeMobile : 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 (log.taskCompletedList && log.taskCompletedList.length){
  220. log.taskCompletedList.each(function(tc){
  221. if (tc.processingType === "appendTask"){
  222. if (activity.routes && activity.routes.length){
  223. activity.routes.each(function(r){
  224. if (tc.routeName === r.data.name){
  225. r.line.attr(this.process.css.route.line.normal);
  226. r.point.attr(this.process.css.route.decision.normal);
  227. r.arrow.attr(this.process.css.route.arrow.normal);
  228. }
  229. }.bind(this));
  230. }
  231. }
  232. }.bind(this));
  233. }
  234. if (activity.countSet) activity.countSet.remove();
  235. }.bind(this));
  236. },
  237. loadPaper: function(){
  238. MWFRaphael.load(function(){
  239. this.paperInNode = new Element("div", {"styles": this.css.paperInNode}).inject(this.paperNode);
  240. this.paper = Raphael(this.paperInNode, "98%", "99%");
  241. if (layout.mobile){
  242. var s = this.paper.canvas.getSize();
  243. var x = s.x*2;
  244. var y = s.y*2;
  245. this.paper.canvas.set({
  246. "viewBox": "0 0 "+x+" "+y+"",
  247. "preserveAspectRatio": "xMinYMin meet"
  248. });
  249. }
  250. this.paper.container = this.paperNode;
  251. MWF.xDesktop.requireApp("process.ProcessDesigner", "Process", function(){
  252. this.process = new MWF.APPPD.Process(this.paper, this.processData, this, {"style":"flat", "isView": true,
  253. "onPostLoad": function(){
  254. this.loadWorkLog();
  255. this.fireEvent("postLoad");
  256. }.bind(this)
  257. });
  258. this.process.load();
  259. }.bind(this));
  260. }.bind(this));
  261. },
  262. getProcess: function(callback){
  263. this.action = MWF.Actions.get("x_processplatform_assemble_surface");
  264. //this.action = new MWF.xApplication.process.Xform.widget.RestActions("x_processplatform_assemble_surface");
  265. //this.action = new MWF.xApplication.process.Xform.widget.RestActions("x_processplatform_assemble_designer");
  266. this.action.getProcess(function(json){
  267. if (callback) callback(json);
  268. }, null, this.processid)
  269. },
  270. loadWorkLog: function(){
  271. this.countNodes = [];
  272. var activitys = {};
  273. this.worklogToken = {};
  274. this.worklog.each(function(log){
  275. this.worklogToken[log.fromActivityToken] = log;
  276. var activityType = log.fromActivityType;
  277. var activity = (activityType.toLowerCase()=="begin") ? this.process.begin : this.process[activityType+"s"][log.fromActivity];
  278. if (log.connected){
  279. activity.shap.attr(this.css.passedActivityShap);
  280. }else{
  281. activity.shap.attr(this.css.currentActivityShap);
  282. }
  283. var route = this.process.routes[log.route];
  284. if (route){
  285. route.line.attr(this.css.passedRouteShap);
  286. route.point.attr(this.css.passedRouteFillShap);
  287. route.arrow.attr(this.css.passedRouteFillShap);
  288. }
  289. if (log.taskCompletedList && log.taskCompletedList.length){
  290. log.taskCompletedList.each(function(tc){
  291. if (tc.processingType === "appendTask"){
  292. if (activity.routes && activity.routes.length){
  293. activity.routes.each(function(r){
  294. if (tc.routeName === r.data.name){
  295. r.line.attr(this.css.passedRouteShap);
  296. r.point.attr(this.css.passedRouteFillShap);
  297. r.arrow.attr(this.css.passedRouteFillShap);
  298. }
  299. }.bind(this));
  300. }
  301. }
  302. }.bind(this));
  303. }
  304. var passedCount = log.taskCompletedList.length;
  305. //var passedCount = log.taskCompletedList.length || 1;
  306. if (passedCount) activity.passedCount = (activity.passedCount) ? activity.passedCount+passedCount : passedCount;
  307. if (!activity.worklogs) activity.worklogs = [];
  308. activity.worklogs.push(log);
  309. if (!activitys[log.fromActivity]) activitys[log.fromActivity] = activity
  310. }.bind(this));
  311. var offset = this.paperNode.getPosition(this.paperNode.getOffsetParent());
  312. var size = this.paperNode.getSize();
  313. Object.each(activitys, function(activity){
  314. this.writePassCount(activity);
  315. this.writeWorkLog(activity, offset, size);
  316. }.bind(this));
  317. },
  318. writePassCount: function(activity){
  319. if (activity.passedCount){
  320. var x = activity.point.x+activity.width;
  321. var y = activity.point.y;
  322. var shap = this.paper.circle(x, y, 9);
  323. shap.attr(this.css.activityPassedCount);
  324. text = this.paper.text(x, y, activity.passedCount);
  325. text.attr(this.css.activityPassedCountText);
  326. activity.countSet = this.paper.set();
  327. activity.countSet.push(shap, text);
  328. }
  329. },
  330. writeWorkLog: function(activity, offset, size){
  331. var _self = this;
  332. activity.set.click(function(e){
  333. if (!_self.isPlaying){
  334. if (this.process.selectedActivitys.length){
  335. if (!this.noselected){
  336. this.selected();
  337. _self.showWorklog(this, offset, size);
  338. }
  339. this.noselected = false;
  340. }
  341. if (this.countSet) this.countSet.toFront();
  342. }
  343. e.stopPropagation();
  344. }.bind(activity));
  345. activity.set.mousedown(function(e){
  346. if (!_self.isPlaying) {
  347. if (!this.process.selectedActivitys.length) {
  348. this.selected();
  349. _self.showWorklog(this, offset, size);
  350. }
  351. if (this.countSet) this.countSet.toFront();
  352. }
  353. e.stopPropagation();
  354. }.bind(activity));
  355. this.paper.canvas.addEvent("click", function(e){
  356. if (!_self.isPlaying) {
  357. if (this.unSelectedEvent) {
  358. if (this.currentSelected || this.selectedActivitys.length) {
  359. this.unSelected(e);
  360. _self.hideCurrentWorklog();
  361. }
  362. } else {
  363. this.unSelectedEvent = true;
  364. }
  365. }
  366. }.bind(this.process));
  367. },
  368. getlogNodePosition: function(activity, node, offset, psize){
  369. var size = node.getSize();
  370. var y = 0;
  371. var x = activity.point.x+activity.width+15+offset.x;
  372. tmpX = x + size.x;
  373. if (tmpX>offset.x+psize.x){
  374. x = activity.point.x - size.x - 15 + offset.x;
  375. if (x<offset.x){
  376. y = activity.point.y-size.y-15+offset.y;
  377. x = activity.center.x - (size.x/2) + offset.x;
  378. }else{
  379. y = activity.center.y - (size.y/2) + offset.y;
  380. if (y<offset.y){
  381. y = offset.y
  382. }
  383. }
  384. }else{
  385. y = activity.center.y - (size.y/2) + offset.y;
  386. if (y<offset.y){
  387. y = offset.y
  388. }
  389. }
  390. var p = this.paperNode.getScroll();
  391. var scrollY = 0;
  392. var scrollX = 0;
  393. var tmpNode = this.paperNode.getParent();
  394. while (tmpNode){
  395. var s = tmpNode.getScroll();
  396. scrollY += s.y;
  397. scrollX += s.x;
  398. tmpNode = tmpNode.getParent();
  399. }
  400. y = y-p.y-scrollY;
  401. x = x-p.x-scrollX;
  402. return {"x": x, "y": y};
  403. },
  404. showWorklog: function(activity, offset, psize){
  405. this.hideCurrentWorklog();
  406. if (!activity.worklogNode) activity.worklogNode = this.createWorkLogNode(activity.worklogs);
  407. this.currentWorklogNode = activity.worklogNode;
  408. this.currentWorklogNode.setStyle("display", "block");
  409. var p = this.getlogNodePosition(activity, activity.worklogNode, offset, psize)
  410. activity.worklogNode.setPosition({"x": p.x, "y": p.y});
  411. },
  412. hideCurrentWorklog: function(){
  413. if (this.currentWorklogNode){
  414. this.currentWorklogNode.setStyle("display", "none");
  415. this.currentWorklogNode = null;
  416. }
  417. },
  418. createWorkLogNode: function(worklogs){
  419. var node = new Element("div", {"styles": this.css.workLogNode});
  420. worklogs.each(function(log, idx){
  421. var workNode = new Element("div", {"styles": this.css.workLogWorkNode}).inject(node);
  422. if ((idx % 2)==0){
  423. workNode.setStyle("background-color", "#FFF");
  424. }else{
  425. workNode.setStyle("background-color", "#EEE");
  426. }
  427. if (log.taskCompletedList.length+log.taskList.length<1){
  428. if (log.connected){
  429. var taskNode = new Element("div", {"styles": this.css.workLogTaskNode}).inject(workNode);
  430. var html = "<div style='font-weight: bold'>"+MWF.xApplication.process.Xform.LP.systemProcess+" </div>";
  431. html += "<div style='text-align: right'>"+log.arrivedTime+"</div>";
  432. taskNode.set("html", html);
  433. }else{
  434. var taskNode = new Element("div", {"styles": this.css.workLogTaskNode}).inject(workNode);
  435. var html = "<div style='font-weight: bold; color: red'>"+MWF.xApplication.process.Xform.LP.systemProcess+" </div>";
  436. taskNode.set("html", html);
  437. }
  438. }else{
  439. log.taskCompletedList.each(function(task){
  440. var taskNode = new Element("div", {"styles": this.css.workLogTaskNode}).inject(workNode);
  441. var html = "<div style='font-weight: bold'>"+task.person.substring(0, task.person.indexOf("@"))+": </div>";
  442. html += "<div style='margin-left: 10px'>["+(task.routeName || "")+"] "+task.opinion+"</div>";
  443. html += "<div style='text-align: right'>"+task.completedTime+"</div>";
  444. taskNode.set("html", html);
  445. }.bind(this));
  446. log.taskList.each(function(task){
  447. var taskNode = new Element("div", {"styles": this.css.workLogTaskNode}).inject(workNode);
  448. var html = "<div style='font-weight: bold; color: red'>"+task.person.substring(0, task.person.indexOf("@"))+" "+MWF.xApplication.process.Xform.LP.processing+" </div>";
  449. taskNode.set("html", html);
  450. }.bind(this));
  451. }
  452. }.bind(this));
  453. node.inject(this.paperNode);
  454. return node;
  455. }
  456. });
  457. MWF.xApplication.process.Xform.widget.Monitor.Animation = new Class({
  458. Implements: [Events],
  459. initialize: function(monitor, log){
  460. }
  461. });