懒羊羊
2023-08-30 1ac2bc1590406d9babec036e154d8d08f34a6aa1
提交 | 用户 | 时间
1ac2bc 1  /**
2  * Class to generate an image based the diagram interchange information in a
3  * BPMN 2.0 process.
4  *
5  * @author (Javascript) Dmitry Farafonov
6  */
7  
8 var ProcessDiagramGenerator = {    
9     options: {},
10     
11     processDiagramCanvas: [],
12     
13     activityDrawInstructions:{},
14     
15     processDiagrams: {},
16     
17     diagramBreadCrumbs: null,
18     
19     init: function(){
20         // start event
21         this.activityDrawInstructions["startEvent"] = function(){
22             var activityImpl = this.activity;
23             var processDiagramCanvas = this.processDiagramCanvas;
24             processDiagramCanvas.setConextObject(activityImpl);
25             
26             processDiagramCanvas.drawNoneStartEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight());
27         };
28         
29         // start timer event
30         this.activityDrawInstructions["startTimerEvent"] = function(){
31             var activityImpl = this.activity;
32             var processDiagramCanvas = this.processDiagramCanvas;
33             processDiagramCanvas.setConextObject(activityImpl);
34             
35             var isInterrupting = activityImpl.getProperty("isInterrupting");
36             processDiagramCanvas.drawTimerStartEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), isInterrupting, activityImpl.getProperty("name"));
37         };
38         
39         // start event
40         this.activityDrawInstructions["messageStartEvent"] = function(){
41             var activityImpl = this.activity;
42             var processDiagramCanvas = this.processDiagramCanvas;
43             processDiagramCanvas.setConextObject(activityImpl);
44             
45             var isInterrupting = activityImpl.getProperty("isInterrupting");
46             processDiagramCanvas.drawMessageStartEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), isInterrupting, activityImpl.getProperty("name"));
47         };
48         
49         // start signal event
50         this.activityDrawInstructions["startSignalEvent"] = function(){
51             var activityImpl = this.activity;
52             var processDiagramCanvas = this.processDiagramCanvas;
53             processDiagramCanvas.setConextObject(activityImpl);
54             
55             var isInterrupting = activityImpl.getProperty("isInterrupting");
56             processDiagramCanvas.drawSignalStartEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), isInterrupting, activityImpl.getProperty("name"));
57         };
58         
59         // start multiple event
60         this.activityDrawInstructions["startMultipleEvent"] = function(){
61             var activityImpl = this.activity;
62             var processDiagramCanvas = this.processDiagramCanvas;
63             processDiagramCanvas.setConextObject(activityImpl);
64             
65             var isInterrupting = activityImpl.getProperty("isInterrupting");
66             processDiagramCanvas.drawMultipleStartEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), isInterrupting, activityImpl.getProperty("name"));
67         };
68         
69         // signal catch
70         this.activityDrawInstructions["intermediateSignalCatch"] = function(){
71             var activityImpl = this.activity;
72             var processDiagramCanvas = this.processDiagramCanvas;
73             processDiagramCanvas.setConextObject(activityImpl);
74             
75             var isInterrupting = activityImpl.getProperty("isInterrupting");
76             processDiagramCanvas.drawCatchingSignalEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), isInterrupting, null);
77             
78             var label = ProcessDiagramGenerator.getActivitiLabel(activityImpl);
79             if (label)
80               processDiagramCanvas.drawLabel(label.text, label.x, label.y, label.width, label.height);
81         };
82         
83         // message catch
84         this.activityDrawInstructions["intermediateMessageCatch"] = function(){
85             var activityImpl = this.activity;
86             var processDiagramCanvas = this.processDiagramCanvas;
87             processDiagramCanvas.setConextObject(activityImpl);
88             
89             var isInterrupting = activityImpl.getProperty("isInterrupting");
90             processDiagramCanvas.drawCatchingMessageEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), isInterrupting, null);
91             
92             var label = ProcessDiagramGenerator.getActivitiLabel(activityImpl);
93       if (label)
94         processDiagramCanvas.drawLabel(label.text, label.x, label.y, label.width, label.height);
95         };
96         
97         // multiple catch
98         this.activityDrawInstructions["intermediateMultipleCatch"] = function(){
99             var activityImpl = this.activity;
100             var processDiagramCanvas = this.processDiagramCanvas;
101             processDiagramCanvas.setConextObject(activityImpl);
102             
103             var isInterrupting = activityImpl.getProperty("isInterrupting");
104             processDiagramCanvas.drawCatchingMultipleEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), isInterrupting, null);
105             
106             var label = ProcessDiagramGenerator.getActivitiLabel(activityImpl);
107       if (label)
108         processDiagramCanvas.drawLabel(label.text, label.x, label.y, label.width, label.height);
109         };
110         
111         
112         
113         // signal throw
114         this.activityDrawInstructions["intermediateSignalThrow"] = function(){
115             var activityImpl = this.activity;
116             var processDiagramCanvas = this.processDiagramCanvas;
117             processDiagramCanvas.setConextObject(activityImpl);
118             
119             processDiagramCanvas.drawThrowingSignalEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), activityImpl.getProperty("name"));
120             
121             var label = ProcessDiagramGenerator.getActivitiLabel(activityImpl);
122       if (label)
123         processDiagramCanvas.drawLabel(label.text, label.x, label.y, label.width, label.height);
124         };
125         
126         // message throw
127         this.activityDrawInstructions["intermediateMessageThrow"] = function(){
128             var activityImpl = this.activity;
129             var processDiagramCanvas = this.processDiagramCanvas;
130             processDiagramCanvas.setConextObject(activityImpl);
131             
132             processDiagramCanvas.drawThrowingMessageEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), activityImpl.getProperty("name"));
133             
134             var label = ProcessDiagramGenerator.getActivitiLabel(activityImpl);
135       if (label)
136         processDiagramCanvas.drawLabel(label.text, label.x, label.y, label.width, label.height);
137         };
138         
139         // multiple throw
140         this.activityDrawInstructions["intermediateMultipleThrow"] = function(){
141             var activityImpl = this.activity;
142             var processDiagramCanvas = this.processDiagramCanvas;
143             processDiagramCanvas.setConextObject(activityImpl);
144             
145             processDiagramCanvas.drawThrowingMultipleEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), activityImpl.getProperty("name"));
146             
147             var label = ProcessDiagramGenerator.getActivitiLabel(activityImpl);
148       if (label)
149         processDiagramCanvas.drawLabel(label.text, label.x, label.y, label.width, label.height);
150         };
151         
152         // none throw
153         this.activityDrawInstructions["intermediateThrowEvent"] = function() {
154             var activityImpl = this.activity;
155             var processDiagramCanvas = this.processDiagramCanvas;
156             processDiagramCanvas.setConextObject(activityImpl);
157             
158             processDiagramCanvas.drawThrowingNoneEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), activityImpl.getProperty("name"));
159             
160             var label = ProcessDiagramGenerator.getActivitiLabel(activityImpl);
161       if (label)
162         processDiagramCanvas.drawLabel(label.text, label.x, label.y, label.width, label.height);
163         };
164         
165         // end event
166         this.activityDrawInstructions["endEvent"] = function(){
167             var activityImpl = this.activity;
168             var processDiagramCanvas = this.processDiagramCanvas;
169             processDiagramCanvas.setConextObject(activityImpl);
170             
171             processDiagramCanvas.drawNoneEndEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight());
172         };
173         
174         // error end event
175         this.activityDrawInstructions["errorEndEvent"] = function(){
176             var activityImpl = this.activity;
177             var processDiagramCanvas = this.processDiagramCanvas;
178             processDiagramCanvas.setConextObject(activityImpl);
179             
180             processDiagramCanvas.drawErrorEndEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), null);
181             
182             var label = ProcessDiagramGenerator.getActivitiLabel(activityImpl);
183       if (label)
184         processDiagramCanvas.drawLabel(label.text, label.x, label.y, label.width, label.height);
185         };
186         
187         // message end event
188         this.activityDrawInstructions["messageEndEvent"] = function(){
189             var activityImpl = this.activity;
190             var processDiagramCanvas = this.processDiagramCanvas;
191             processDiagramCanvas.setConextObject(activityImpl);
192             
193             processDiagramCanvas.drawMessageEndEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), null);
194             
195             var label = ProcessDiagramGenerator.getActivitiLabel(activityImpl);
196       if (label)
197         processDiagramCanvas.drawLabel(label.text, label.x, label.y, label.width, label.height);
198         };
199         
200         // signal end event
201         this.activityDrawInstructions["signalEndEvent"] = function(){
202             var activityImpl = this.activity;
203             var processDiagramCanvas = this.processDiagramCanvas;
204             processDiagramCanvas.setConextObject(activityImpl);
205             
206             processDiagramCanvas.drawSignalEndEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), null);
207             
208             var label = ProcessDiagramGenerator.getActivitiLabel(activityImpl);
209       if (label)
210         processDiagramCanvas.drawLabel(label.text, label.x, label.y, label.width, label.height);
211         };
212         
213         // multiple end event
214         this.activityDrawInstructions["multipleEndEvent"] = function(){
215             var activityImpl = this.activity;
216             var processDiagramCanvas = this.processDiagramCanvas;
217             processDiagramCanvas.setConextObject(activityImpl);
218             
219             processDiagramCanvas.drawMultipleEndEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), null);
220             
221             var label = ProcessDiagramGenerator.getActivitiLabel(activityImpl);
222       if (label)
223         processDiagramCanvas.drawLabel(label.text, label.x, label.y, label.width, label.height);
224         };
225         
226         // terminate end event
227         this.activityDrawInstructions["terminateEndEvent"] = function(){
228             var activityImpl = this.activity;
229             var processDiagramCanvas = this.processDiagramCanvas;
230             processDiagramCanvas.setConextObject(activityImpl);
231             
232             processDiagramCanvas.drawTerminateEndEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight());
233             
234             var label = ProcessDiagramGenerator.getActivitiLabel(activityImpl);
235       if (label)
236         processDiagramCanvas.drawLabel(label.text, label.x, label.y, label.width, label.height);
237         };
238         
239         // error start event
240         this.activityDrawInstructions["errorStartEvent"] = function(){
241             var activityImpl = this.activity;
242             var processDiagramCanvas = this.processDiagramCanvas;
243             processDiagramCanvas.setConextObject(activityImpl);
244             
245             processDiagramCanvas.drawErrorStartEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), activityImpl.getProperty("name"));
246             
247             var label = ProcessDiagramGenerator.getActivitiLabel(activityImpl);
248       if (label)
249         processDiagramCanvas.drawLabel(label.text, label.x, label.y, label.width, label.height);
250         };
251         
252         // task
253         this.activityDrawInstructions["task"] = function(){
254             var activityImpl = this.activity;
255             var processDiagramCanvas = this.processDiagramCanvas;
256             processDiagramCanvas.setConextObject(activityImpl);
257             
258             // TODO: 
259             //console.error("task is not implemented yet");
260             /*
261             var activityImpl = this;
262             processDiagramCanvas.drawTask(activityImpl.getProperty("name"), activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), thickBorder);
263             */
264         };
265         
266         
267         // user task
268         this.activityDrawInstructions["userTask"] = function(){
269             var activityImpl = this.activity;
270             var processDiagramCanvas = this.processDiagramCanvas;
271             processDiagramCanvas.setConextObject(activityImpl);
272             
273             processDiagramCanvas.drawUserTask(activityImpl.getProperty("name"), activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight());
274         };
275         
276         // script task
277         this.activityDrawInstructions["scriptTask"] = function(){
278             var activityImpl = this.activity;
279             var processDiagramCanvas = this.processDiagramCanvas;
280             processDiagramCanvas.setConextObject(activityImpl);
281             
282             processDiagramCanvas.drawScriptTask(activityImpl.getProperty("name"), activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight());
283         };
284         
285         // service task
286         this.activityDrawInstructions["serviceTask"] = function(){
287             var activityImpl = this.activity;
288             var processDiagramCanvas = this.processDiagramCanvas;
289             processDiagramCanvas.setConextObject(activityImpl);
290             
291             processDiagramCanvas.drawServiceTask(activityImpl.getProperty("name"), activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight());
292         };
293
294         // receive task
295         this.activityDrawInstructions["receiveTask"] = function(){
296             var activityImpl = this.activity;
297             var processDiagramCanvas = this.processDiagramCanvas;
298             processDiagramCanvas.setConextObject(activityImpl);
299             
300             processDiagramCanvas.drawReceiveTask(activityImpl.getProperty("name"), activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight());
301         };
302         
303         // send task
304         this.activityDrawInstructions["sendTask"] = function(){
305             var activityImpl = this.activity;
306             var processDiagramCanvas = this.processDiagramCanvas;
307             processDiagramCanvas.setConextObject(activityImpl);
308             
309             processDiagramCanvas.drawSendTask(activityImpl.getProperty("name"), activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight());
310         };
311
312         // manual task
313         this.activityDrawInstructions["manualTask"] = function(){
314             var activityImpl = this.activity;
315             var processDiagramCanvas = this.processDiagramCanvas;
316             processDiagramCanvas.setConextObject(activityImpl);
317         
318             processDiagramCanvas.drawManualTask(activityImpl.getProperty("name"), activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight());
319         };
320
321         // businessRuleTask task
322         this.activityDrawInstructions["businessRuleTask"] = function(){
323             var activityImpl = this.activity;
324             var processDiagramCanvas = this.processDiagramCanvas;
325             processDiagramCanvas.setConextObject(activityImpl);
326             
327             processDiagramCanvas.drawBusinessRuleTask(activityImpl.getProperty("name"), activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight());
328         };
329
330         // exclusive gateway
331         this.activityDrawInstructions["exclusiveGateway"] = function(){
332             var activityImpl = this.activity;
333             var processDiagramCanvas = this.processDiagramCanvas;
334             processDiagramCanvas.setConextObject(activityImpl);
335             
336             processDiagramCanvas.drawExclusiveGateway(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight());
337         };
338         
339         // inclusive gateway
340         this.activityDrawInstructions["inclusiveGateway"] = function(){
341             var activityImpl = this.activity;
342             var processDiagramCanvas = this.processDiagramCanvas;
343             processDiagramCanvas.setConextObject(activityImpl);
344             
345             processDiagramCanvas.drawInclusiveGateway(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight());
346         };
347         
348         // parallel gateway
349         this.activityDrawInstructions["parallelGateway"] = function(){
350             var activityImpl = this.activity;
351             var processDiagramCanvas = this.processDiagramCanvas;
352             processDiagramCanvas.setConextObject(activityImpl);
353             
354             processDiagramCanvas.drawParallelGateway(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight());
355         };
356         
357         // eventBasedGateway
358         this.activityDrawInstructions["eventBasedGateway"] = function(){
359             var activityImpl = this.activity;
360             var processDiagramCanvas = this.processDiagramCanvas;
361             processDiagramCanvas.setConextObject(activityImpl);
362             
363             processDiagramCanvas.drawEventBasedGateway(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight());
364         };
365         
366         // Boundary timer
367         this.activityDrawInstructions["boundaryTimer"] = function(){
368             var activityImpl = this.activity;
369             var processDiagramCanvas = this.processDiagramCanvas;
370             processDiagramCanvas.setConextObject(activityImpl);
371             
372             var isInterrupting = activityImpl.getProperty("isInterrupting");
373             processDiagramCanvas.drawCatchingTimerEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), isInterrupting, null);
374             
375             var label = ProcessDiagramGenerator.getActivitiLabel(activityImpl);
376       if (label)
377         processDiagramCanvas.drawLabel(label.text, label.x, label.y, label.width, label.height);
378         };
379         
380         // Boundary catch error
381         this.activityDrawInstructions["boundaryError"] = function(){
382             var activityImpl = this.activity;
383             var processDiagramCanvas = this.processDiagramCanvas;
384             processDiagramCanvas.setConextObject(activityImpl);
385             
386             var isInterrupting = activityImpl.getProperty("isInterrupting");
387             processDiagramCanvas.drawCatchingErrorEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), isInterrupting, null);
388             
389             var label = ProcessDiagramGenerator.getActivitiLabel(activityImpl);
390       if (label)
391         processDiagramCanvas.drawLabel(label.text, label.x, label.y, label.width, label.height);
392         };
393         
394         // Boundary signal event
395         this.activityDrawInstructions["boundarySignal"] = function(){
396             var activityImpl = this.activity;
397             var processDiagramCanvas = this.processDiagramCanvas;
398             processDiagramCanvas.setConextObject(activityImpl);
399             
400             var isInterrupting = activityImpl.getProperty("isInterrupting");
401             processDiagramCanvas.drawCatchingSignalEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), isInterrupting, null);
402             
403             var label = ProcessDiagramGenerator.getActivitiLabel(activityImpl);
404       if (label)
405         processDiagramCanvas.drawLabel(label.text, label.x, label.y, label.width, label.height);
406         };
407
408     // Boundary message event
409     this.activityDrawInstructions["boundaryMessage"] = function(){
410         var activityImpl = this.activity;
411         var processDiagramCanvas = this.processDiagramCanvas;
412         processDiagramCanvas.setConextObject(activityImpl);
413
414         var isInterrupting = activityImpl.getProperty("isInterrupting");
415         processDiagramCanvas.drawCatchingMessageEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), isInterrupting, null);
416
417         var label = ProcessDiagramGenerator.getActivitiLabel(activityImpl);
418         if (label)
419             processDiagramCanvas.drawLabel(label.text, label.x, label.y, label.width, label.height);
420     };
421         
422         // timer catch event
423         this.activityDrawInstructions["intermediateTimer"] = function(){
424             var activityImpl = this.activity;
425             var processDiagramCanvas = this.processDiagramCanvas;
426             processDiagramCanvas.setConextObject(activityImpl);
427             
428             var isInterrupting = null;
429             processDiagramCanvas.drawCatchingTimerEvent(activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight(), isInterrupting, activityImpl.getProperty("name"));
430         };
431         
432         // subprocess
433         this.activityDrawInstructions["subProcess"] = function(){
434             var activityImpl = this.activity;
435             var processDiagramCanvas = this.processDiagramCanvas;
436             // TODO: 
437             
438             processDiagramCanvas.setConextObject(activityImpl);
439             
440             var isExpanded = activityImpl.getProperty("isExpanded");
441             var isTriggeredByEvent = activityImpl.getProperty("triggeredByEvent");
442             if(isTriggeredByEvent == undefined) {
443               isTriggeredByEvent = true;
444             }
445             // TODO: check why isTriggeredByEvent = true when undefined
446             isTriggeredByEvent = false;
447             
448             if (isExpanded != undefined && isExpanded == false) {
449               processDiagramCanvas.drawCollapsedSubProcess(activityImpl.getProperty("name"), activityImpl.getX(), activityImpl.getY(),
450                       activityImpl.getWidth(), activityImpl.getHeight(), isTriggeredByEvent);
451             } else {
452               processDiagramCanvas.drawExpandedSubProcess(activityImpl.getProperty("name"), activityImpl.getX(), activityImpl.getY(),
453                       activityImpl.getWidth(), activityImpl.getHeight(), isTriggeredByEvent);
454             }
455             
456             //console.error("subProcess is not implemented yet");
457         };
458         
459         // call activity
460         this.activityDrawInstructions["callActivity"] = function(){
461             var activityImpl = this.activity;
462             var processDiagramCanvas = this.processDiagramCanvas;
463             processDiagramCanvas.setConextObject(activityImpl);
464             processDiagramCanvas.drawCollapsedCallActivity(activityImpl.getProperty("name"), activityImpl.getX(), activityImpl.getY(), activityImpl.getWidth(), activityImpl.getHeight());
465         };
466         
467         $(document).ready(function(){
468           // Protect right click on SVG elements (and on canvas too)
469           document.body.oncontextmenu = function(event) {
470             if (window.event.srcElement.tagName == "shape" || window.event.srcElement.tagName == "DIV" && window.event.srcElement.parentElement.className == "diagram") {
471
472               // IE DIAGRAM CANVAS OR SHAPE DETECTED!
473               return false;
474             }
475             return (!Object.isSVGElement(window.event.srcElement));
476           };
477         });
478     },
479     
480      getActivitiLabel:function(activityImpl){
481        /*
482          TODO: Label object should be in activityImpl and looks like:
483          {
484            x: 250,
485            y: 250,
486            width: 80,
487            height: 30
488          }
489          And then:
490          if (!activityImpl.label)
491            return null;
492          var label = activityImpl.label;
493          label.text = activityImpl.name;
494          return label;
495        */
496
497        // But now default label for all events is:
498        return {
499          text: activityImpl.getProperty("name"),
500          x: activityImpl.getX() + .5 + activityImpl.getWidth()/2,
501          y: activityImpl.getY() + .5 + activityImpl.getHeight() + ICON_PADDING,
502          width: 100,
503          height: 0
504        };
505     },
506         
507     generateDiagram: function(processDefinitionDiagramLayout){
508         // Init canvas
509         var processDefinitionId = processDefinitionDiagramLayout.processDefinition.id;
510         //console.log("Init canvas ", processDefinitionId);
511         
512         if (this.getProcessDiagram(processDefinitionId) != undefined) {
513             // TODO: may be reset canvas if exists.. Or just show
514             //console.log("ProcessDiagram '" + processDefinitionId + "' is already generated. Just show it.");
515             return;
516         }
517         var processDiagram = this.initProcessDiagramCanvas(processDefinitionDiagramLayout);
518         var processDiagramCanvas = processDiagram.diagramCanvas;
519         
520         // Draw pool shape, if process is participant in collaboration
521         
522         if(processDefinitionDiagramLayout.participantProcess != undefined) {
523           //console.log("Draw pool shape");
524           var pProc = processDefinitionDiagramLayout.participantProcess;
525           processDiagramCanvas.drawPoolOrLane(pProc.x, pProc.y, pProc.width, pProc.height, pProc.name);
526         }
527         
528         var laneSets = processDefinitionDiagramLayout.laneSets;
529         var activities = processDefinitionDiagramLayout.activities;
530         var sequenceFlows = processDefinitionDiagramLayout.sequenceFlows;
531         
532         
533         pb1.set('value', 0);
534         var cnt = 0;
535         if (laneSets) 
536             for(var i in laneSets) {
537                 cnt += laneSets[i].lanes.length;
538             }
539         if (activities) 
540             cnt += activities.length;
541         if (sequenceFlows) 
542             cnt += sequenceFlows.length;
543         var step = (cnt>0)? 100/cnt : 0;
544         var progress = 0;
545         //console.log("progress bar step: ", step);
546         
547         var task1 = new $.AsyncQueue();
548         
549             // Draw lanes
550             
551             task1.add(function (task1) {
552                 if (!laneSets) laneSets = [];
553                 //console.log("> draw lane sets, count:", laneSets.length)
554             });
555             
556             for(var i in laneSets) {
557                 var laneSet = laneSets[i];
558                 //laneSet.id, laneSet.name
559                 
560                 task1.add(laneSet.lanes,function (task1, lane) {
561                     progress += step;
562                     pb1.set('value', parseInt(progress));
563                     
564                     //console.log("--> laneId: " + lane.name + ", name: " + lane.name);
565                     
566                     processDiagramCanvas.drawPoolOrLane(lane.x, lane.y, lane.width, lane.height, lane.name);
567                 });
568             }
569             
570             // Draw activities
571             
572             task1.add(function (task1) {
573                 if (!activities) activities = [];
574                 //console.log("> draw activities, count:", activities.length)
575             });
576             
577             var activitiesLength = activities.length;
578             task1.add(activities,function (task1, activityJson) {
579                 var activity = new ActivityImpl(activityJson);
580                 activitiesLength --;
581                 progress += step;
582                 pb1.set('value', parseInt(progress));
583                 //console.log(activitiesLength, "--> activityId: " + activity.getId() + ", name: " + activity.getProperty("name"));
584                 ProcessDiagramGenerator.drawActivity(processDiagramCanvas, activity);
585             });
586             
587             // Draw sequence-flows
588             
589             task1.add(function (task1) {
590                 if (!sequenceFlows) sequenceFlows = [];
591                 //console.log("> draw sequence flows, count:", sequenceFlows.length)
592             });
593             
594             var flowsLength = sequenceFlows.length;
595             task1.add(sequenceFlows,function (task1, flow) {
596                 var waypoints = [];
597                 for(var j in flow.xPointArray) {
598                     waypoints[j] = {x: flow.xPointArray[j], y: flow.yPointArray[j]};
599                 }
600                 var isDefault = flow.isDefault;
601                 var isConditional = flow.isConditional;
602                 var isHighLighted = flow.isHighLighted;
603                 
604                 // TODO: add source and destination for sequence flows in REST
605                 // parse for test
606                     var f = flow.flow;
607                     var matches = f.match(/\((.*)\)--.*-->\((.*)\)/);
608                     var sourceActivityId, destinationActivityId;
609                     if (matches != null) {
610                         sourceActivityId = matches[1];
611                         destinationActivityId = matches[2];
612                     }
613                     flow.sourceActivityId = sourceActivityId;
614                     flow.destinationActivityId = destinationActivityId;
615                 //
616                 flowsLength--;
617                 progress += step;
618                 pb1.set('value', parseInt(progress));
619                 
620                 //console.log(flowsLength, "--> flow: " + flow.flow);
621                 
622                 processDiagramCanvas.setConextObject(flow);
623                 processDiagramCanvas.drawSequenceflow(waypoints, isConditional, isDefault, isHighLighted);
624             });
625             
626             task1.onComplete(function(){
627                 if (progress<100)
628                     pb1.set('value', 100);
629                 //console.log("COMPLETE!!!");
630                     
631                 //console.timeEnd('generateDiagram');
632             });
633             
634             task1.run();
635     },
636     
637     getProcessDiagram: function (processDefinitionId) {
638         return this.processDiagrams[processDefinitionId];
639     },
640     
641     initProcessDiagramCanvas: function (processDefinitionDiagramLayout) {
642         var minX = 0;
643         var maxX = 0;
644         var minY = 0;
645         var maxY = 0;
646         
647         if(processDefinitionDiagramLayout.participantProcess != undefined) {
648           var pProc = processDefinitionDiagramLayout.participantProcess;
649           
650           minX = pProc.x;
651           maxX = pProc.x + pProc.width;
652           minY = pProc.y;
653           maxY = pProc.y + pProc.height;
654         }
655
656         var activities = processDefinitionDiagramLayout.activities;
657         for(var i in activities) {
658             var activityJson = activities[i];
659             var activity = new ActivityImpl(activityJson);
660             
661             // width
662             if (activity.getX() + activity.getWidth() > maxX) {
663                 maxX = activity.getX() + activity.getWidth();
664             }
665             if (activity.getX() < minX) {
666                 minX = activity.getX();
667             }
668             // height
669             if (activity.getY() + activity.getHeight() > maxY) {
670                 maxY = activity.getY() + activity.getHeight();
671             }
672             if (activity.getY() < minY) {
673                 minY = activity.getY();
674             }
675         }
676         
677         var sequenceFlows = processDefinitionDiagramLayout.sequenceFlows;
678         for(var i in sequenceFlows) {
679             var flow = sequenceFlows[i];
680             var waypoints = [];
681             for(var j in flow.xPointArray) {
682                 waypoints[j] = {x: flow.xPointArray[j], y: flow.yPointArray[j]};
683                 
684                 // width
685                 if (waypoints[j].x > maxX) {
686                     maxX = waypoints[j].x;
687                 }
688                 if (waypoints[j].x < minX) {
689                     minX = waypoints[j].x;
690                 }
691                 // height
692                 if (waypoints[j].y > maxY) {
693                     maxY = waypoints[j].y;
694                 }
695                 if (waypoints[j].y < minY) {
696                     minY = waypoints[j].y;
697                 }
698             }
699         }
700         
701         var laneSets = processDefinitionDiagramLayout.laneSets;
702         for(var i in laneSets) {
703             var laneSet = laneSets[i];
704             //laneSet.id, laneSet.name
705             
706             for(var j in laneSet.lanes) {
707                 var lane = laneSet.lanes[j];
708                 // width
709                 if (lane.x + lane.width > maxX) {
710                   maxX = lane.x + lane.width;
711                 }
712                 if (lane.x < minX) {
713                   minX = lane.x;
714                 }
715                 // height
716                 if (lane.y + lane.height > maxY) {
717                   maxY = lane.y + lane.height;
718                 }
719                 if (lane.y < minY) {
720                   minY = lane.y;
721                 }
722             }
723         }
724     
725         var diagramCanvas = new ProcessDiagramCanvas();
726         if (diagramCanvas) {
727             
728             // create div in diagramHolder
729             var diagramHolder = document.getElementById(this.options.diagramHolderId);
730             if (!diagramHolder)
731                 throw {msg: "Diagram holder not found", error: "diagramHolderNotFound"};
732             var div = document.createElement("DIV");
733             div.id = processDefinitionDiagramLayout.processDefinition.id;
734             div.className = "diagram";
735             diagramHolder.appendChild(div);
736             
737             diagramCanvas.init(maxX + 20, maxY + 20, processDefinitionDiagramLayout.processDefinition.id);
738             this.processDiagrams[processDefinitionDiagramLayout.processDefinition.id] = {
739                 processDefinitionDiagramLayout: processDefinitionDiagramLayout,
740                 diagramCanvas: diagramCanvas
741             };
742         }
743         return this.getProcessDiagram(processDefinitionDiagramLayout.processDefinition.id);
744         //return new DefaultProcessDiagramCanvas(maxX + 10, maxY + 10, minX, minY);
745     },
746     
747     drawActivity: function(processDiagramCanvas, activity, highLightedActivities) {
748         var type = activity.getProperty("type");
749         var drawInstruction = this.activityDrawInstructions[type];
750         if (drawInstruction != null) {    
751             drawInstruction.apply({processDiagramCanvas:processDiagramCanvas, activity:activity});
752         } else {
753             //console.error("no drawInstruction for " + type + ": ", activity);
754         }
755         
756         // Actually draw the markers
757         if (activity.getProperty("multiInstance") != undefined || activity.getProperty("collapsed") != undefined) {
758             //console.log(activity.getProperty("name"), activity.properties);
759             var multiInstanceSequential = (activity.getProperty("multiInstance") == "sequential");
760             var multiInstanceParallel = (activity.getProperty("multiInstance") == "parrallel");
761             var collapsed = activity.getProperty("collapsed");
762                 processDiagramCanvas.drawActivityMarkers(activity.getX(), activity.getY(), activity.getWidth(), activity.getHeight(), 
763                     multiInstanceSequential, multiInstanceParallel, collapsed);
764         }
765         /*
766         processDiagramCanvas.drawActivityMarkers(activity.getX(), activity.getY(), activity.getWidth(), activity.getHeight(), multiInstanceSequential,
767               multiInstanceParallel, collapsed);
768         */
769
770         // TODO: Draw highlighted activities if they are present
771         
772     },
773     
774     setHighLights: function(highLights){
775         if (highLights.processDefinitionId == undefined) {
776             //console.error("Process instance " + highLights.processInstanceId + " doesn't exist");
777             return;
778         }
779         
780         var processDiagram = this.getProcessDiagram(highLights.processDefinitionId);
781         if (processDiagram == undefined) {
782             //console.error("Process diagram " + highLights.processDefinitionId + " not found");
783             return;
784         }
785         
786         var processDiagramCanvas = processDiagram.diagramCanvas;
787         
788         // TODO: remove highLightes from all activities before set new highLight
789         for (var i in highLights.activities) {
790             var activityId = highLights.activities[i];
791             processDiagramCanvas.highLightActivity(activityId);
792         }
793         
794         // TODO: remove highLightes from all flows before set new highLight
795         for (var i in highLights.flows) {
796             var flowId = highLights.flows[i];
797             var object = processDiagramCanvas.g.getById(flowId);
798             var flow = object.data("contextObject");
799             flow.isHighLighted = true;
800             processDiagramCanvas.highLightFlow(flowId);
801         }
802     },
803     
804     drawHighLights: function(processInstanceId) {
805         // Load highLights for the processInstanceId
806         /*
807         var url = Lang.sub(this.options.processInstanceHighLightsUrl, {processInstanceId: processInstanceId});
808         $.ajax({
809             url: url,
810             type: 'GET',
811             dataType: 'json',
812             cache: false,
813             async: true,
814         }).done(function(data) {
815             var highLights = data;
816             if (!highLights) {
817                 console.log("highLights not found");
818                 return;
819             }
820             
821             console.log("highLights[" + highLights.processDefinitionId + "][" + processInstanceId + "]: ", highLights);
822             
823             ProcessDiagramGenerator.setHighLights(highLights);
824         }).fail(function(jqXHR, textStatus){
825             console.log('Get HighLights['+processDefinitionId+'] failure: ', textStatus, jqXHR);
826         });
827         */
828         ActivitiRest.getHighLights(processInstanceId, this._drawHighLights);
829     },
830     _drawHighLights: function() {
831         var highLights = this.highLights;
832         ProcessDiagramGenerator.setHighLights(highLights);
833     },
834     
835     // Load processDefinition
836     
837     drawDiagram: function(processDefinitionId) {
838         // Hide all diagrams
839         var diagrams = $("#" + this.options.diagramHolderId + " div.diagram");
840         diagrams.addClass("hidden");
841     
842     
843         // If processDefinitionId doesn't contain ":" then it's a "processDefinitionKey", not an id.
844         // Get process definition by key
845         if (processDefinitionId.indexOf(":") < 0) {
846             ActivitiRest.getProcessDefinitionByKey(processDefinitionId, this._drawDiagram);
847         } else {
848             this._drawDiagram.apply({processDefinitionId: processDefinitionId});
849         }
850     },
851     _drawDiagram: function() {
852         var processDefinitionId = this.processDefinitionId;
853         
854         ProcessDiagramGenerator.addBreadCrumbsItem(processDefinitionId);
855         
856         
857         // Check if processDefinition is already loaded and rendered
858         
859         
860         var processDiagram = ProcessDiagramGenerator.getProcessDiagram(processDefinitionId);
861
862         if (processDiagram != undefined && processDiagram != null) {
863             //console.log("Process diagram " + processDefinitionId + " is already loaded");
864             //return;
865             
866             var diagram = document.getElementById(processDefinitionId);
867             $(diagram).removeClass("hidden");
868             
869             // Regenerate image
870             var processDefinitionDiagramLayout = processDiagram.processDefinitionDiagramLayout;
871             ProcessDiagramGenerator.generateDiagram(processDefinitionDiagramLayout);
872             
873             return;
874         }
875
876         //console.time('loadDiagram');
877         
878         // Load processDefinition
879         
880         ActivitiRest.getProcessDefinition(processDefinitionId, ProcessDiagramGenerator._generateDiagram);
881     },
882     _generateDiagram: function() {
883         var processDefinitionDiagramLayout = this.processDefinitionDiagramLayout;
884         
885         //console.log("process-definition-diagram-layout["+processDefinitionDiagramLayout.processDefinition.id+"]: ", processDefinitionDiagramLayout);
886         
887         //console.timeEnd('loadDiagram');
888         //console.time('generateDiagram');
889         
890         pb1.set('value', 0);
891         ProcessDiagramGenerator.generateDiagram(processDefinitionDiagramLayout);
892     },
893     
894     getProcessDefinitionByKey: function(processDefinitionKey) {
895         var url = Lang.sub(this.options.processDefinitionByKeyUrl, {processDefinitionKey: processDefinitionKey});
896         
897         var processDefinition;
898         $.ajax({
899             url: url,
900             type: 'POST',
901             dataType: 'json',
902             cache: false,
903             async: false
904         }).done(function(data) { 
905             //console.log("ajax returned data");
906             //console.log("ajax returned data:", data);
907             processDefinition = data;
908             if (!processDefinition) {
909                 //console.error("Process definition '" + processDefinitionKey + "' not found");
910             }
911         }).fail(function(jqXHR, textStatus){
912             //console.error('Get diagram layout['+processDefinitionKey+'] failure: ', textStatus, jqXHR);
913         });
914         
915         if (processDefinition) {
916             //console.log("Get process definition by key '" + processDefinitionKey + "': ", processDefinition.id);
917             return processDefinition;
918         } else {
919             return null;
920         }
921     },
922     
923     addBreadCrumbsItem: function(processDefinitionId){
924         var TPL_UL_CONTAINER = '<ul></ul>',
925             TPL_LI_CONTAINER = '<li id="{id}", processDefinitionId="{processDefinitionId}"><span>{name}</span></li>';
926         
927         if (!this.diagramBreadCrumbs)
928             this.diagramBreadCrumbs = $("#" + this.options.diagramBreadCrumbsId);
929         if (!this.diagramBreadCrumbs) return;
930         
931         
932         var ul = this.diagramBreadCrumbs.find("ul");
933         //console.log("ul: ", ul);
934         if (ul.size() == 0) {
935             ul = $(TPL_UL_CONTAINER);
936             this.diagramBreadCrumbs.append(ul);
937             
938         }
939         var liListOld = ul.find("li");
940         //console.warn("liListOld", liListOld);
941         
942         // TODO: if there is any items after current then remove that before adding new item (m.b. it is a duplicate)
943         var currentBreadCrumbsItemId = this.currentBreadCrumbsItemId;
944             found = false;
945         liListOld.each(
946             function(index, item) {
947                 //console.warn("item:", $(this));
948                 if (!found && currentBreadCrumbsItemId == $(this).attr("id")) {
949                     found = true;
950                     return;
951                 }
952                 if (found) {
953                     //console.warn("remove ", $(this).attr("id"));
954                     $(this).remove();
955                 }
956             }
957         );
958         
959         var liListNew = ul.find("li");
960         
961         //console.log("liListNew size: ", liListNew.size());
962         var values = {
963             id: 'breadCrumbsItem_' + liListNew.size(),
964             processDefinitionId: processDefinitionId,
965             name: processDefinitionId
966         };
967         
968         
969         var tpl = Lang.sub(TPL_LI_CONTAINER, values);
970         //console.log("tpl: ", tpl);
971         ul.append(tpl);
972         
973         var li = ul.find("#" + values.id);
974         //console.warn("li:", li);
975         $('#' + values.id).on('click', this._breadCrumbsItemClick);
976         
977         ul.find("li").removeClass("selected");
978         li.attr("num", liListNew.size());
979         li.addClass("selected");
980         this.currentBreadCrumbsItemId = li.attr("id");
981     },
982     _breadCrumbsItemClick: function(){
983         var li = $(this),
984             id = li.attr("id"),
985             processDefinitionId = li.attr("processDefinitionId");
986         //console.warn("_breadCrumbsItemClick: ", id, ", processDefinitionId: ", processDefinitionId);
987         
988         var ul = ProcessDiagramGenerator.diagramBreadCrumbs.one("ul");
989         ul.find("li").removeClass("selected");
990         li.addClass("selected");
991         ProcessDiagramGenerator.currentBreadCrumbsItemId = li.attr("id");
992         
993         // Hide all diagrams
994         var diagrams = $("#"+ProcessDiagramGenerator.options.diagramHolderId+" div.diagram");
995         diagrams.addClass("hidden");
996         
997         var processDiagram = ProcessDiagramGenerator.getProcessDiagram(processDefinitionId);
998         
999         var diagram = document.getElementById(processDefinitionId);
1000         if (!diagram) return;
1001         $(diagram).removeClass("hidden");
1002         
1003         // Regenerate image
1004         var processDefinitionDiagramLayout = processDiagram.processDefinitionDiagramLayout;
1005         ProcessDiagramGenerator.generateDiagram(processDefinitionDiagramLayout);
1006     },
1007     
1008     showFlowInfo: function(flow){
1009         var diagramInfo = $("#" + this.options.diagramInfoId);
1010         if (!diagramInfo) return;
1011         
1012         var values = {
1013             flow: flow.flow,
1014             isDefault: (flow.isDefault)? "true":"",
1015             isConditional: (flow.isConditional)? "true":"",
1016             isHighLighted: (flow.isHighLighted)? "true":"",
1017             sourceActivityId: flow.sourceActivityId,
1018             destinationActivityId: flow.destinationActivityId
1019         };
1020         var TPL_FLOW_INFO = '<div>{flow}</div>' 
1021                 + '<div><b>sourceActivityId</b>: {sourceActivityId}</div>'
1022                 + '<div><b>destinationActivityId</b>: {destinationActivityId}</div>'
1023                 + '<div><b>isDefault</b>: {isDefault}</div>'
1024                 + '<div><b>isConditional</b>: {isConditional}</div>'
1025                 + '<div><b>isHighLighted</b>: {isHighLighted}</div>';
1026         var tpl = Lang.sub(TPL_FLOW_INFO, values);
1027         //console.log("info: ", tpl);
1028         diagramInfo.html(tpl);
1029     },
1030     
1031     showActivityInfo: function(activity){
1032         var diagramInfo = $("#" + this.options.diagramInfoId);
1033         if (!diagramInfo) return;
1034         
1035         var values = {
1036             activityId: activity.getId(),
1037             name: activity.getProperty("name"),
1038             type: activity.getProperty("type")
1039         };
1040         var TPL_ACTIVITY_INFO = '' 
1041                 + '<div><b>activityId</b>: {activityId}</div>'
1042                 + '<div><b>name</b>: {name}</div>'
1043                 + '<div><b>type</b>: {type}</div>';
1044         var TPL_CALLACTIVITY_INFO = ''
1045                 + '<div><b>collapsed</b>: {collapsed}</div>'
1046                 + '<div><b>processDefinitonKey</b>: {processDefinitonKey}</div>';
1047         
1048         var template = TPL_ACTIVITY_INFO;
1049         if (activity.getProperty("type") == "callActivity") {
1050             values.collapsed = activity.getProperty("collapsed");
1051             values.processDefinitonKey = activity.getProperty("processDefinitonKey");
1052             template += TPL_CALLACTIVITY_INFO;
1053         } else if (activity.getProperty("type") == "callActivity") {
1054         
1055         }
1056                 
1057         var tpl = Lang.sub(template, values);
1058         //console.log("info: ", tpl);
1059         diagramInfo.html(tpl);
1060     },
1061     
1062     hideInfo: function(){
1063       var diagramInfo = $("#" + this.options.diagramInfoId);
1064       if (!diagramInfo) return;
1065       diagramInfo.html("");
1066     },
1067     
1068     vvoid: function(){}
1069 };
1070
1071 var Lang = {
1072     SUBREGEX: /\{\s*([^\|\}]+?)\s*(?:\|([^\}]*))?\s*\}/g,
1073     UNDEFINED: 'undefined',
1074     isUndefined: function(o) {
1075         return typeof o === Lang.UNDEFINED;
1076     },
1077     sub: function(s, o) {
1078         return ((s.replace) ? s.replace(Lang.SUBREGEX, function(match, key) {
1079             return (!Lang.isUndefined(o[key])) ? o[key] : match;
1080         }) : s);
1081     }
1082 };
1083
1084 if (Lang.isUndefined(console)) {
1085     console = { log: function() {}, warn: function() {}, error: function() {}};
1086 }
1087 ProcessDiagramGenerator.init();