summaryrefslogtreecommitdiffstats
path: root/dcae_dmaapbc_webapp/src/main/webapp/app/fusion/ase/scripts/arrayManipulation.js
blob: 2f041f3a1d6077f11d92e820147bb06f75c4aa73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//Contains functions to update, create and retrieve arrays. Also uses arrays to determine properties (lNode, rNode) of arrows

var missingStarts = [];
var missingEnds = [];

//Stores module's properties in its array
function storeXY (arrayType,id) {
    'use strict';
    var index;
    for ( i = 0; i<arrayType.length; i++){
        if (arrayType[i][1] == id) {
            index = i;
            arrayType[i][3] = parseInt(document.getElementById(id).getAttribute('data_x'));
            arrayType[i][4] = parseInt(document.getElementById(id).getAttribute('data_y'));
            arrayType[i][5] = $("#"+id).width();
            arrayType[i][6] = arrayType[i][3] + arrayType[i][5];
            arrayType[i][7] = arrayType[i][4] + $("#"+id).height();
        }
    }
    return index;
}

//Returns lifeline coordinates in the array lifelineX[]
function getLifelines () {
    lifelineX.length = 0;
    lifelineX=[];
    for (var i = 0; i<nodeArr.length; i++){
        if (!$('#'+nodeArr[i][1]).hasClass('expanded')){
            lifelineX.push([nodeArr[i][3] + (nodeArr[i][5])/2,nodeArr[i][1]]);
        }
    }
    for (i = 0; i<lifelineX.length; i++){
        if(lifelineX[i]===null){
            lifelineX.splice(i,1);
            i--;
        }
    }
}

//Finds a module in its array then stores user-inputed text
function storeText (arrayType,id,text) {
    var index;
    for (var i = 0; i<arrayType.length; i++){
        if (arrayType[i][1] == id) {
            index = i;
            arrayType[i][2] = text;        
        }    
    }
    return index;
}

//Recalls an arrays values and returns the items index
function recallArray (arrayType, id) {
    var index;
    for (var i = 0; i<arrayType.length; i++){
        if (arrayType[i][1] == id) {
            index = i;
            text = arrayType[i][2];
            lX = arrayType[i][3];
            lY = arrayType[i][4];
            wide = arrayType[i][5];
            rX = arrayType[i][6];
            rY = arrayType[i][7];
        }    
    }
    return index;
}

//Stores a module when given properties and a type. After storing it appends it to the DOM
//function storeArray (arrayType, i, id, text, lX, lY, wide, direction, height,bpmn,tosca_ID,arrowDescription, messageType) {
function storeArray (arrayType, i, id, text, lX, lY, wide, direction, height,bpmn,tosca_ID,arrowDescription, messageType, role) {
    var tag = "";
    var dropOffLocation;
    if (arrayType == "nodeArr"){
        //var nodeHeight = $('.drag-1').height(); //DOES NOT WORK
        var nodeHeight = 36; //FIXES THE PROBLEM, DOES NOT ALLOW RESIZE
        var nodeHeightCorrected = nodeHeight+16;
        
        var gridHeight = document.getElementById('grid').clientHeight;
        var gridHeightCorrected = gridHeight - 90;
        tag = '<div class="nodeDraggable drag-drop drag-1 child can-drop placed verticallyScrollable" '+
                    'id=' + id + ' data_x="' +lX+ '" data_y="' +lY+ '"' +
                    'style="transform: translate(' + lX + 'px, ' + lY + 'px); -webkit-transform: translate(' + lX + 'px, ' + lY + 'px);">' +
                        '<div class="nodeText nodeEllipsis">'+ text +'</div>'+
                    '<div class="verticalLine" style="top:'+nodeHeightCorrected+'px;height:'+gridHeightCorrected+'px;"></div>'+
                  '</div>';
        dropOffLocation = document.getElementById("nodeChildrenDroppedOffHere");
        dropOffLocation.innerHTML += tag;
        var index = nodeArr.push([document.getElementById(id),id,text,parseInt(lX),parseInt(lY),wide,parseInt(lX)+parseInt(wide),parseInt(lY)]);
        if (tosca_ID !==null){
            document.getElementById(id).setAttribute('taska_id',tosca_ID);
        }
        if ( text !== null ){ 
            document.getElementById(id).setAttribute('sname',text);
        }
        // When Role is added uncomment. 
        console.log(role);
        if ( role !== null ){ 
            document.getElementById(id).setAttribute('role_id',role);
        }

        if (isOverlapped(id) === true){
            shiftRight(document.getElementById(id),160);
        }
        if (isOverlapped(id) === false){
            for (i=0;i<arrowArr.length;i++){
                determineLRNode(arrowArr[i][1],id);
            }
        }
        nodeArr[index-1][10] = bpmn;
        if (bpmn !== '' && bpmn !== null){
            $('#'+nodeArr[index-1][1]).addClass('bpmn');
        }
    }
    if (arrayType === "arrowArr"){
        if (direction === "right"){
            tag = '<div class="arrowDraggable resizable child gridDropzone can-drop arrowPlaced" '+
            'style="margin-left: 27px; transform: translate('+ lX +'px, '+lY+'px); -webkit-transform: translate('+ lX +'px, '+lY+'px);width: '+ wide +'px;" '+
            'id="'+id+'" data_x="'+lX+'" data_y="'+lY+'" direction="right" message_type="'+messageType+'">'+
                '<div class="arrow resizable" style="display:inline; width:100%;"><div class="makeEllipsis arrowText">'+text+'</div></div>'+
                '<div class="triangle" style="display:inline;"></div>'+
          '</div>';
        }
        if (direction === "left"){
            tag = '<div class="arrowDraggable resizable child gridDropzone can-drop arrowPlaced" '+
            'style="margin-left: 27px; transform: translate('+ lX +'px, '+lY+'px); -webkit-transform: translate('+ lX +'px, '+lY+'px);width: '+ wide +'px;" '+
            'id="'+id+'" data_x="'+lX+'" data_y="'+lY+'" direction="left" message_type="'+messageType+'">'+
                '<div class="leftTriangle" style="display:inline;"></div>'+
                '<div class="leftArrow" style="display:inline; width:100%;"><div class="makeEllipsis arrowText">' + text + '</div></div>'+
          '</div>';
        }
        if (direction === "self"){
            tag = '<div class="arrowDraggable resizable child gridDropzone can-drop arrowPlaced" '+
            'style="margin-left: 27px; transform: translate('+ lX +'px, '+lY+'px); -webkit-transform: translate('+ lX +'px, '+lY+'px);width: 0px;" '+
            'id="'+id+'" data_x="'+lX+'" data_y="'+lY+'" direction="self" message_type="'+messageType+'">'+
                '<div style="display:inline; width:100%"><img src="./images/selfArrow.png" style="cursor:e-resize;">'+
                '<div class="makeEllipsisSelf arrowText" style="width:75px;font-size:12px;margin-left:3px;text-align:left;display:inline;">' + text + '</div></div>'+
          '</div>';
        }
                    
        
        dropOffLocation = document.getElementById("arrowChildrenDroppedOffHere");
        dropOffLocation.innerHTML += tag;
        arrowArr.push([document.getElementById(id),id,text,parseInt(lX),lY,parseInt(wide),parseInt(lX)+parseInt(wide),parseInt(lY),"","",arrowDescription]);
        
        determineLRNode(id,"arrow");
    }
    if (arrayType == "noteArr"){
        tag = '<div class="postit note child gridDropzone can-drop notePlaced" '+
                    'id=' + id + ' data_x="' +lX+ '" data_y="' +lY+ '"' +
                    'style="transform: translate(' + lX + 'px, ' + lY + 'px); -webkit-transform: translate(' + lX + 'px, ' + lY + 'px);width: '+wide+'px; height: '+height+'px;">' +
                        text +
                  '</div>';
        dropOffLocation = document.getElementById("noteChildrenDroppedOffHere");
        dropOffLocation.innerHTML += tag;
        noteArr.push([document.getElementById(id),id,text,parseInt(lX),parseInt(lY),wide,parseInt(lX)+parseInt(wide),parseInt(lY)+parseInt(height)]);
        
    }
    
}

var layers = {};
var nodesInLayer = [];
function determineLayers () {
    var doAgain = true;
    layers.length = 0;
    var layerNumber = -1;
    while (doAgain === true){
        layerNumber++;
        nodesInLayer.length = 0;
        var classToAssign = 'layer'+layerNumber;
        for (var i=0; i<nodeArr.length; i++){
            if (nodeArr[i][8]===null || nodeArr[i][8]===0){
                //console.log(nodeArr[i][1]);
                $('#'+nodeArr[i][1]).addClass(classToAssign);
                nodesInLayer.push(nodeArr[i][1]);        
            }
        }
        if (nodesInLayer.length === 0){
            doAgain = false;
        }
        else{
            layers[classToAssign] = nodesInLayer;
        }    
        doAgain = false;
    }
    
    
    //console.log(layers);
}

//Determines what nodes an arrow is connecting. 
//When from = "arrow" it finds the left and right nodes. When from = nodeUUID it finds the arrows attached to that node
function determineLRNode (id, from) {
    var connections = 0;
    // tolerance for subtle difference between x coordinate of nodes (midpoint) and x coordinates of arrow (end points) due to zoom in/out
    // this enables the arrows stay connected to nodes after re-scale window size.
    var tol = 2;
    var arrowIndex;
    getLifelines();
    for (var i = 0; i<arrowArr.length; i++){
        if (arrowArr[i][1] == id) { //finds correct arrow
            arrowIndex = i;
        }
    }

    if (from == "arrow"){
        arrowArr[arrowIndex][8]=null;
        arrowArr[arrowIndex][9]=null;
        for (var j = 0; j<lifelineX.length;j++){
            if (Math.abs(arrowArr[arrowIndex][3] - lifelineX[j][0]) <= tol ){
                arrowArr[arrowIndex][8] = lifelineX[j][1];
                connections++;
            }
            if (Math.abs(arrowArr[arrowIndex][6] + 12 - lifelineX[j][0]) <= tol ){
                arrowArr[arrowIndex][9] = lifelineX[j][1];
                connections++;
            }
            else if (  (Math.abs(arrowArr[arrowIndex][6] - lifelineX[j][0]) <= tol ) && document.getElementById(arrowArr[arrowIndex][1]).getAttribute("direction") == "self") { //Allows for self arrow
                arrowArr[arrowIndex][9] = lifelineX[j][1];
                connections++;
            }
        }
        changeColors();
        
    }
    
    else { //Handles constructor of type node uuid
        var nodeIndex;
        for (i=0; i<lifelineX.length;i++){
            if (lifelineX[i][1] == from) { //Finds node index, corresponds to lifeline index
                nodeIndex = i;
            }
        }
        if (Math.abs(arrowArr[arrowIndex][3] - lifelineX[nodeIndex][0]) <= tol ){ //If the x is equal to the new node lifeline
            arrowArr[arrowIndex][8] = nodeArr[nodeIndex][1]; //store to arrow's leftNode
            if (arrowArr[arrowIndex][9] !== null){ //if it has a right node
                connections = 2;
            }
            else {
                connections = 1;
            }
            changeColors();
        }
    }
    if (document.getElementById(id).getAttribute("direction") == "left"){
        var swap = arrowArr[arrowIndex][8];
        arrowArr[arrowIndex][8] = arrowArr[arrowIndex][9];
        arrowArr[arrowIndex][9] = swap;
    }
    warning(); //Displays warning on the sidebar if an arrow lacks an endpoint
    
    function changeColors () {
        if (connections == 2){        
            document.getElementById(id).style.background = "rgba(0,255,0,.30)"; //Green
            setTimeout(function (){
                document.getElementById(id).style.background = "rgba(0,255,0,0)"; //Fade out
            }, 500);
        }
        else if (connections == 1){
            document.getElementById(id).style.background = "rgba(255,255,0,.30)"; //Yellow
        }
        else if (connections === 0){
            document.getElementById(id).style.background = "rgba(255,0,0,.30)"; //Red
        }
    }    
}

var repeater;
function warning() {
    var allow = testAllConnections();
    if (allow === false) {
        document.getElementById("warning").innerHTML = "*Link missing lifeline";
    }
    else {
        document.getElementById("warning").innerHTML = "";
    }
    if (allow === false){
        document.getElementById("saveButton").style.cursor = "no-drop";
        //document.getElementById("printButton").style.cursor = "no-drop";
    }
    else {
        document.getElementById("saveButton").style.cursor = "pointer";
        //document.getElementById("printButton").style.cursor = "pointer";
    }
}


function blinkMissingArrows () {
    testAllConnections();
    var allMissing = missingStarts.concat(missingEnds);
    for (var i = 0; i<allMissing.length; i++){
        for (var j = i+1; j<allMissing.length;j++){
            if(allMissing[i]===allMissing[j])
                allMissing.splice(j--,1);
        }
    }
    var originalBG=[];
    for (i = 0; i<allMissing.length; i++) {
        id = allMissing[i];
        originalBG.push(document.getElementById(id).style.background);
        document.getElementById(id).style.background = "rgba(255,0,0,.80)";
    }
    setTimeout(function (){
    for (var i = 0; i<allMissing.length; i++) {
        id = allMissing[i];        
        document.getElementById(id).style.background = originalBG[i];
    }}, 1000);
}

function testAllConnections () {
    var allow = true;
    missingStarts = [];
    missingEnds = [];
    
    for (var i = 0; i<arrowArr.length;i++){
        result = determineNodeStart(arrowArr[i][1]);
        if (result === null){
            missingStarts.push(arrowArr[i][1]);
            allow=false;
        }
        result = determineNodeEnd(arrowArr[i][1]);
        if (result === null){
            missingEnds.push(arrowArr[i][1]);
            allow=false;
        }
    }
    return allow;
}

function determineNodeStart (id) {
    for (var i = 0; i<arrowArr.length; i++){
        if (arrowArr[i][1] === id) { //finds correct arrow
            return arrowArr[i][8];            
        }
    }
}
function determineNodeEnd (id) {
    for (var i = 0; i<arrowArr.length; i++){
        if (arrowArr[i][1] == id) { //finds correct arrow
            return arrowArr[i][9];
        }
    }
}

//Generates uuid
function guid() {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000)
        .toString(16)
        .substring(1);
    }
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
    s4() + '-' + s4() + s4() + s4();
}