Monitor.js 19 KB

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