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. debugger;
  101. if (this.process){
  102. this.isPlaying = true;
  103. this.toolbar.childrenButton[0].setDisable(true);
  104. //this.toolbar.childrenButton[1].setDisable(false);
  105. //this.toolbar.childrenButton[2].setDisable(false);
  106. this.processReturnStyle();
  107. this.playBegin();
  108. this.playToNextActivity();
  109. }
  110. },
  111. playBegin: function(){
  112. var src = "../x_component_process_Xform/widget/$Monitor/"+this.options.style+"/fly.png";
  113. this.playIcon = this.paper.image(src, this.process.begin.center.x-16, this.process.begin.center.y-16, 32, 32);
  114. this.playLogNode = null;
  115. this.playsStatus = {
  116. "index": 0
  117. };
  118. this.isPlaying = true;
  119. },
  120. playGetNextActivity: function(){
  121. var log = this.worklog[this.playsStatus.index];
  122. var activityType = log.fromActivityType;
  123. var activity = (activityType.toLowerCase()=="begin") ? this.process.begin : this.process[activityType+"s"][log.fromActivity];
  124. return {"log": log, "activity": activity};
  125. },
  126. playToNextActivity: function(route){
  127. var activity = this.playGetNextActivity();
  128. if (this.playsStatus.index == 0){
  129. this.playToActivityStop(activity.activity, activity.log);
  130. }else{
  131. this.playMoveToActivity(activity.activity, activity.log, route);
  132. }
  133. },
  134. playMoveToActivity: function(activity, log, route){
  135. if (route){
  136. var points = [route.beginPoint];
  137. points = points.concat(route.positionPoints, [route.endPoint], [{"x": activity.center.x, "y": activity.center.y}]);
  138. this.playMoveByRoutePoint(points, function(){
  139. this.playToActivityStop(activity, log, route);
  140. }.bind(this));
  141. }else{
  142. this.playToActivityStop(activity, log, route);
  143. }
  144. },
  145. playMoveByRoutePoint: function(points, callback){
  146. debugger;
  147. var p = {"x": this.playIcon.attr("x").toFloat(), "y": this.playIcon.attr("y").toFloat()};
  148. var toP = points.shift();
  149. var d = MWFRaphael.getPointDistance(p, toP);
  150. var ms = d/0.2;
  151. this.playIcon.animate({"x": toP.x-16, "y": toP.y-16}, ms, "linear", function() {
  152. if (points.length) {
  153. this.playMoveByRoutePoint(points, callback);
  154. } else {
  155. if (callback) callback();
  156. }
  157. }.bind(this));
  158. },
  159. playToActivityStop: function(activity, log, prevRoute){
  160. this.playIcon.attr({
  161. "x": activity.center.x-16,
  162. "y": activity.center.y-16
  163. });
  164. if (log.connected){
  165. activity.shap.attr(this.css.passedActivityShap);
  166. }else{
  167. activity.shap.attr(this.css.currentActivityShap);
  168. }
  169. var route = this.process.routes[log.route];
  170. if (prevRoute){
  171. prevRoute.line.attr(this.css.passedRouteShap);
  172. prevRoute.point.attr(this.css.passedRouteFillShap);
  173. prevRoute.arrow.attr(this.css.passedRouteFillShap);
  174. }
  175. this.showPlayLog(activity,log);
  176. this.playsStatus.index++;
  177. window.setTimeout(function(){
  178. if (this.worklog.length<=this.playsStatus.index){
  179. this.playStop();
  180. }else{
  181. this.playLogNode.destroy();
  182. this.playLogNode = null;
  183. this.playToNextActivity(route);
  184. }
  185. }.bind(this), 2000);
  186. },
  187. showPlayLog: function(activity,log){
  188. var offset = this.paperNode.getPosition(this.paperNode.getOffsetParent());
  189. var size = this.paperNode.getSize();
  190. this.playLogNode = this.createWorkLogNode([log]);
  191. this.playLogNode.setStyle("display", "block");
  192. var p = this.getlogNodePosition(activity, this.playLogNode, offset, size);
  193. this.playLogNode.setPosition({"x": p.x, "y": p.y});
  194. },
  195. playStop: function(){
  196. this.playIcon.remove();
  197. if (this.playLogNode) this.playLogNode.destroy();
  198. this.playLogNode = null;
  199. this.playsStatus = {
  200. "index": 0
  201. };
  202. this.isPlaying = false;
  203. this.toolbar.childrenButton[0].setDisable(false);
  204. //this.toolbar.childrenButton[1].setDisable(true);
  205. //this.toolbar.childrenButton[2].setDisable(true);
  206. this.loadWorkLog();
  207. },
  208. processReturnStyle: function(){
  209. this.worklog.each(function(log){
  210. var activityType = log.fromActivityType;
  211. var activity = (activityType.toLowerCase()=="begin") ? this.process.begin : this.process[activityType+"s"][log.fromActivity];
  212. activity.shap.attr(activity.style.shap);
  213. activity.passedCount = 0;
  214. activity.worklogs = [];
  215. var route = this.process.routes[log.route];
  216. if (route){
  217. route.line.attr(this.process.css.route.line.normal);
  218. route.point.attr(this.process.css.route.decision.normal);
  219. route.arrow.attr(this.process.css.route.arrow.normal);
  220. }
  221. if (log.taskCompletedList && log.taskCompletedList.length){
  222. log.taskCompletedList.each(function(tc){
  223. if (tc.processingType === "appendTask"){
  224. if (activity.routes && activity.routes.length){
  225. activity.routes.each(function(r){
  226. if (tc.routeName === r.data.name){
  227. r.line.attr(this.process.css.route.line.normal);
  228. r.point.attr(this.process.css.route.decision.normal);
  229. r.arrow.attr(this.process.css.route.arrow.normal);
  230. }
  231. }.bind(this));
  232. }
  233. }
  234. }.bind(this));
  235. }
  236. if (activity.countSet) activity.countSet.remove();
  237. }.bind(this));
  238. },
  239. loadPaper: function(){
  240. MWFRaphael.load(function(){
  241. this.paperInNode = new Element("div", {"styles": this.css.paperInNode}).inject(this.paperNode);
  242. this.paper = Raphael(this.paperInNode, "98%", "99%");
  243. if (layout.mobile){
  244. var s = this.paper.canvas.getSize();
  245. var x = s.x*2;
  246. var y = s.y*2;
  247. this.paper.canvas.set({
  248. "viewBox": "0 0 "+x+" "+y+"",
  249. "preserveAspectRatio": "xMinYMin meet"
  250. });
  251. }
  252. this.paper.container = this.paperNode;
  253. MWF.xDesktop.requireApp("process.ProcessDesigner", "Process", function(){
  254. this.process = new MWF.APPPD.Process(this.paper, this.processData, this, {"style":"flat", "isView": true,
  255. "onPostLoad": function(){
  256. this.loadWorkLog();
  257. this.fireEvent("postLoad");
  258. }.bind(this)
  259. });
  260. this.process.load();
  261. }.bind(this));
  262. }.bind(this));
  263. },
  264. getProcess: function(callback){
  265. this.action = MWF.Actions.get("x_processplatform_assemble_surface");
  266. //this.action = new MWF.xApplication.process.Xform.widget.RestActions("x_processplatform_assemble_surface");
  267. //this.action = new MWF.xApplication.process.Xform.widget.RestActions("x_processplatform_assemble_designer");
  268. this.action.getProcess(function(json){
  269. if (callback) callback(json);
  270. }, null, this.processid)
  271. },
  272. loadWorkLog: function(){
  273. this.countNodes = [];
  274. var activitys = {};
  275. this.worklogToken = {};
  276. this.worklog.each(function(log){
  277. this.worklogToken[log.fromActivityToken] = log;
  278. var activityType = log.fromActivityType;
  279. var activity = (activityType.toLowerCase()=="begin") ? this.process.begin : this.process[activityType+"s"][log.fromActivity];
  280. if (log.connected){
  281. activity.shap.attr(this.css.passedActivityShap);
  282. }else{
  283. activity.shap.attr(this.css.currentActivityShap);
  284. }
  285. var route = this.process.routes[log.route];
  286. if (route){
  287. route.line.attr(this.css.passedRouteShap);
  288. route.point.attr(this.css.passedRouteFillShap);
  289. route.arrow.attr(this.css.passedRouteFillShap);
  290. }
  291. debugger;
  292. if (log.taskCompletedList && log.taskCompletedList.length){
  293. log.taskCompletedList.each(function(tc){
  294. if (tc.processingType === "appendTask"){
  295. if (activity.routes && activity.routes.length){
  296. activity.routes.each(function(r){
  297. if (tc.routeName === r.data.name){
  298. r.line.attr(this.css.passedRouteShap);
  299. r.point.attr(this.css.passedRouteFillShap);
  300. r.arrow.attr(this.css.passedRouteFillShap);
  301. }
  302. }.bind(this));
  303. }
  304. }
  305. }.bind(this));
  306. }
  307. var passedCount = log.taskCompletedList.length;
  308. //var passedCount = log.taskCompletedList.length || 1;
  309. if (passedCount) activity.passedCount = (activity.passedCount) ? activity.passedCount+passedCount : passedCount;
  310. if (!activity.worklogs) activity.worklogs = [];
  311. activity.worklogs.push(log);
  312. if (!activitys[log.fromActivity]) activitys[log.fromActivity] = activity
  313. }.bind(this));
  314. var offset = this.paperNode.getPosition(this.paperNode.getOffsetParent());
  315. var size = this.paperNode.getSize();
  316. Object.each(activitys, function(activity){
  317. this.writePassCount(activity);
  318. this.writeWorkLog(activity, offset, size);
  319. }.bind(this));
  320. },
  321. writePassCount: function(activity){
  322. if (activity.passedCount){
  323. var x = activity.point.x+activity.width;
  324. var y = activity.point.y;
  325. var shap = this.paper.circle(x, y, 9);
  326. shap.attr(this.css.activityPassedCount);
  327. text = this.paper.text(x, y, activity.passedCount);
  328. text.attr(this.css.activityPassedCountText);
  329. activity.countSet = this.paper.set();
  330. activity.countSet.push(shap, text);
  331. }
  332. },
  333. writeWorkLog: function(activity, offset, size){
  334. var _self = this;
  335. activity.set.click(function(e){
  336. if (!_self.isPlaying){
  337. if (this.process.selectedActivitys.length){
  338. if (!this.noselected){
  339. this.selected();
  340. _self.showWorklog(this, offset, size);
  341. }
  342. this.noselected = false;
  343. }
  344. if (this.countSet) this.countSet.toFront();
  345. }
  346. e.stopPropagation();
  347. }.bind(activity));
  348. activity.set.mousedown(function(e){
  349. if (!_self.isPlaying) {
  350. if (!this.process.selectedActivitys.length) {
  351. this.selected();
  352. _self.showWorklog(this, offset, size);
  353. }
  354. if (this.countSet) this.countSet.toFront();
  355. }
  356. e.stopPropagation();
  357. }.bind(activity));
  358. this.paper.canvas.addEvent("click", function(e){
  359. if (!_self.isPlaying) {
  360. if (this.unSelectedEvent) {
  361. if (this.currentSelected || this.selectedActivitys.length) {
  362. this.unSelected(e);
  363. _self.hideCurrentWorklog();
  364. }
  365. } else {
  366. this.unSelectedEvent = true;
  367. }
  368. }
  369. }.bind(this.process));
  370. },
  371. getlogNodePosition: function(activity, node, offset, psize){
  372. var size = node.getSize();
  373. var y = 0;
  374. var x = activity.point.x+activity.width+15+offset.x;
  375. tmpX = x + size.x;
  376. if (tmpX>offset.x+psize.x){
  377. x = activity.point.x - size.x - 15 + offset.x;
  378. if (x<offset.x){
  379. y = activity.point.y-size.y-15+offset.y;
  380. x = activity.center.x - (size.x/2) + offset.x;
  381. }else{
  382. y = activity.center.y - (size.y/2) + offset.y;
  383. if (y<offset.y){
  384. y = offset.y
  385. }
  386. }
  387. }else{
  388. y = activity.center.y - (size.y/2) + offset.y;
  389. if (y<offset.y){
  390. y = offset.y
  391. }
  392. }
  393. var p = this.paperNode.getScroll();
  394. var scrollY = 0;
  395. var scrollX = 0;
  396. var tmpNode = this.paperNode.getParent();
  397. while (tmpNode){
  398. var s = tmpNode.getScroll();
  399. scrollY += s.y;
  400. scrollX += s.x;
  401. tmpNode = tmpNode.getParent();
  402. }
  403. y = y-p.y-scrollY;
  404. x = x-p.x-scrollX;
  405. return {"x": x, "y": y};
  406. },
  407. showWorklog: function(activity, offset, psize){
  408. this.hideCurrentWorklog();
  409. if (!activity.worklogNode) activity.worklogNode = this.createWorkLogNode(activity.worklogs);
  410. this.currentWorklogNode = activity.worklogNode;
  411. this.currentWorklogNode.setStyle("display", "block");
  412. var p = this.getlogNodePosition(activity, activity.worklogNode, offset, psize)
  413. activity.worklogNode.setPosition({"x": p.x, "y": p.y});
  414. },
  415. hideCurrentWorklog: function(){
  416. if (this.currentWorklogNode){
  417. this.currentWorklogNode.setStyle("display", "none");
  418. this.currentWorklogNode = null;
  419. }
  420. },
  421. createWorkLogNode: function(worklogs){
  422. var node = new Element("div", {"styles": this.css.workLogNode});
  423. worklogs.each(function(log, idx){
  424. var workNode = new Element("div", {"styles": this.css.workLogWorkNode}).inject(node);
  425. if ((idx % 2)==0){
  426. workNode.setStyle("background-color", "#FFF");
  427. }else{
  428. workNode.setStyle("background-color", "#EEE");
  429. }
  430. if (log.taskCompletedList.length+log.taskList.length<1){
  431. if (log.connected){
  432. var taskNode = new Element("div", {"styles": this.css.workLogTaskNode}).inject(workNode);
  433. var html = "<div style='font-weight: bold'>"+MWF.xApplication.process.Xform.LP.systemProcess+" </div>";
  434. html += "<div style='text-align: right'>"+log.arrivedTime+"</div>";
  435. taskNode.set("html", html);
  436. }else{
  437. var taskNode = new Element("div", {"styles": this.css.workLogTaskNode}).inject(workNode);
  438. var html = "<div style='font-weight: bold; color: red'>"+MWF.xApplication.process.Xform.LP.systemProcess+" </div>";
  439. taskNode.set("html", html);
  440. }
  441. }else{
  442. log.taskCompletedList.each(function(task){
  443. var taskNode = new Element("div", {"styles": this.css.workLogTaskNode}).inject(workNode);
  444. var html = "<div style='font-weight: bold'>"+task.person.substring(0, task.person.indexOf("@"))+": </div>";
  445. html += "<div style='margin-left: 10px'>["+(task.routeName || "")+"] "+task.opinion+"</div>";
  446. html += "<div style='text-align: right'>"+task.completedTime+"</div>";
  447. taskNode.set("html", html);
  448. }.bind(this));
  449. log.taskList.each(function(task){
  450. var taskNode = new Element("div", {"styles": this.css.workLogTaskNode}).inject(workNode);
  451. var html = "<div style='font-weight: bold; color: red'>"+task.person.substring(0, task.person.indexOf("@"))+" "+MWF.xApplication.process.Xform.LP.processing+" </div>";
  452. taskNode.set("html", html);
  453. }.bind(this));
  454. }
  455. }.bind(this));
  456. node.inject(this.paperNode);
  457. return node;
  458. }
  459. });
  460. MWF.xApplication.process.Xform.widget.Monitor.Animation = new Class({
  461. Implements: [Events],
  462. initialize: function(monitor, log){
  463. }
  464. });