懒羊羊
2023-11-14 8286c62256f23bc2367a6729c0f46f84215e380b
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
/*
 * Activiti Modeler component part of the Activiti project
 * Copyright 2005-2014 Alfresco Software, Ltd. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
/*
 * Controller for morph shape selection
 */
 
var KisBpmShapeSelectionCtrl = [ '$rootScope', '$scope', '$timeout', '$translate', function($rootScope, $scope, $timeout, $translate) {
 
    $scope.selectedMorphShapes = [];
    
    $scope.availableMorphShapes = [];
 
    for (var i = 0; i < $scope.morphShapes.length; i++)
    {
        if ($scope.morphShapes[i].id != $scope.currentSelectedShape.getStencil().idWithoutNs())
        {
            $scope.availableMorphShapes.push($scope.morphShapes[i]);
        }
    }
        
    // Config for grid
    $scope.gridOptions = {
        data: 'availableMorphShapes',
        enableRowReordering: true,
        headerRowHeight: 28,
        multiSelect: false,
        keepLastSelected : false,
        selectedItems: $scope.selectedMorphShapes,
        columnDefs: [{ field: 'objectId', displayName: 'Icon', width: 50, cellTemplate: 'editor-app/popups/icon-template.html?version=' + Date.now() },
            { field: 'name', displayName: 'Name'}]
    };
 
    // Click handler for save button
    $scope.select = function() {
 
        if ($scope.selectedMorphShapes.length > 0) 
        {
            var MorphTo = ORYX.Core.Command.extend({
                construct: function(shape, stencil, facade){
                    this.shape = shape;
                    this.stencil = stencil;
                    this.facade = facade;
                },
                execute: function(){
                    
                    var shape = this.shape;
                    var stencil = this.stencil;
                    var resourceId = shape.resourceId;
                    
                    // Serialize all attributes
                    var serialized = shape.serialize();
                    stencil.properties().each((function(prop) {
                        if(prop.readonly()) {
                            serialized = serialized.reject(function(serProp) {
                                return serProp.name==prop.id();
                            });
                        }
                    }).bind(this));
            
                    // Get shape if already created, otherwise create a new shape
                    if (this.newShape){
                        newShape = this.newShape;
                        this.facade.getCanvas().add(newShape);
                    } else {
                        newShape = this.facade.createShape({
                                        type: stencil.id(),
                                        namespace: stencil.namespace(),
                                        resourceId: resourceId
                                    });
                    }
                    
                    // calculate new bounds using old shape's upperLeft and new shape's width/height
                    var boundsObj = serialized.find(function(serProp){
                        return (serProp.prefix === "oryx" && serProp.name === "bounds");
                    });
                    
                    var changedBounds = null;
                    
                    if (!this.facade.getRules().preserveBounds(shape.getStencil())) {
                        
                        var bounds = boundsObj.value.split(",");
                        if (parseInt(bounds[0], 10) > parseInt(bounds[2], 10)) { // if lowerRight comes first, swap array items
                            var tmp = bounds[0];
                            bounds[0] = bounds[2];
                            bounds[2] = tmp;
                            tmp = bounds[1];
                            bounds[1] = bounds[3];
                            bounds[3] = tmp;
                        }
                        bounds[2] = parseInt(bounds[0], 10) + newShape.bounds.width();
                        bounds[3] = parseInt(bounds[1], 10) + newShape.bounds.height();
                        boundsObj.value = bounds.join(",");
                        
                    }  else {
                        
                        var height = shape.bounds.height();
                        var width  = shape.bounds.width();
                        
                        // consider the minimum and maximum size of
                        // the new shape
                        
                        if (newShape.minimumSize) {
                            if (shape.bounds.height() < newShape.minimumSize.height) {
                                height = newShape.minimumSize.height;
                            }
                            
                            
                            if (shape.bounds.width() < newShape.minimumSize.width) {
                                width = newShape.minimumSize.width;
                            }
                        }
                        
                        if(newShape.maximumSize) {
                            if(shape.bounds.height() > newShape.maximumSize.height) {
                                height = newShape.maximumSize.height;
                            }    
                            
                            if(shape.bounds.width() > newShape.maximumSize.width) {
                                width = newShape.maximumSize.width;
                            }
                        }
                        
                        changedBounds = {
                            a : {
                                x: shape.bounds.a.x,
                                y: shape.bounds.a.y
                            },
                            b : {
                                x: shape.bounds.a.x + width,
                                y: shape.bounds.a.y + height
                            }                        
                        };
                        
                    }
                    
                    var oPos = shape.bounds.center();
                    if(changedBounds !== null) {
                        newShape.bounds.set(changedBounds);
                    }
                    
                    // Set all related dockers
                    this.setRelatedDockers(shape, newShape);
                    
                    // store DOM position of old shape
                    var parentNode = shape.node.parentNode;
                    var nextSibling = shape.node.nextSibling;
                    
                    // Delete the old shape
                    this.facade.deleteShape(shape);
                    
                    // Deserialize the new shape - Set all attributes
                    newShape.deserialize(serialized);
                    /*
                     * Change color to default if unchanged
                     * 23.04.2010
                     */
                    if(shape.getStencil().property("oryx-bgcolor") 
                            && shape.properties["oryx-bgcolor"]
                            && shape.getStencil().property("oryx-bgcolor").value().toUpperCase()== shape.properties["oryx-bgcolor"].toUpperCase()){
                            if(newShape.getStencil().property("oryx-bgcolor")){
                                newShape.setProperty("oryx-bgcolor", newShape.getStencil().property("oryx-bgcolor").value());
                            }
                    }    
                    if(changedBounds !== null) {
                        newShape.bounds.set(changedBounds);
                    }
                    
                    if(newShape.getStencil().type()==="edge" || (newShape.dockers.length==0 || !newShape.dockers[0].getDockedShape())) {
                        newShape.bounds.centerMoveTo(oPos);
                    } 
                    
                    if(newShape.getStencil().type()==="node" && (newShape.dockers.length==0 || !newShape.dockers[0].getDockedShape())) {
                        this.setRelatedDockers(newShape, newShape);
                        
                    }
                    
                    // place at the DOM position of the old shape
                    if(nextSibling) parentNode.insertBefore(newShape.node, nextSibling);
                    else parentNode.appendChild(newShape.node);
                    
                    // Set selection
                    this.facade.setSelection([newShape]);
                    this.facade.getCanvas().update();
                    this.facade.updateSelection();
                    this.newShape = newShape;
                    
                },
                rollback: function(){
                    
                    if (!this.shape || !this.newShape || !this.newShape.parent) {return;}
                    
                    // Append shape to the parent
                    this.newShape.parent.add(this.shape);
                    // Set dockers
                    this.setRelatedDockers(this.newShape, this.shape);
                    // Delete new shape
                    this.facade.deleteShape(this.newShape);
                    // Set selection
                    this.facade.setSelection([this.shape]);
                    // Update
                    this.facade.getCanvas().update();
                    this.facade.updateSelection();
                },
                
                /**
                 * Set all incoming and outgoing edges from the shape to the new shape
                 * @param {Shape} shape
                 * @param {Shape} newShape
                 */
                setRelatedDockers: function(shape, newShape){
                    
                    if(shape.getStencil().type()==="node") {
                        
                        (shape.incoming||[]).concat(shape.outgoing||[])
                            .each(function(i) { 
                                i.dockers.each(function(docker) {
                                    if (docker.getDockedShape() == shape) {
                                        var rPoint = Object.clone(docker.referencePoint);
                                        // Move reference point per percent
 
                                        var rPointNew = {
                                            x: rPoint.x*newShape.bounds.width()/shape.bounds.width(),
                                            y: rPoint.y*newShape.bounds.height()/shape.bounds.height()
                                        };
 
                                        docker.setDockedShape(newShape);
                                        // Set reference point and center to new position
                                        docker.setReferencePoint(rPointNew);
                                        if(i instanceof ORYX.Core.Edge) {
                                            docker.bounds.centerMoveTo(rPointNew);
                                        } else {
                                            var absXY = shape.absoluteXY();
                                            docker.bounds.centerMoveTo({x:rPointNew.x+absXY.x, y:rPointNew.y+absXY.y});
                                            //docker.bounds.moveBy({x:rPointNew.x-rPoint.x, y:rPointNew.y-rPoint.y});
                                        }
                                    }
                                });    
                            });
                        
                        // for attached events
                        if(shape.dockers.length>0&&shape.dockers.first().getDockedShape()) {
                            newShape.dockers.first().setDockedShape(shape.dockers.first().getDockedShape());
                            newShape.dockers.first().setReferencePoint(Object.clone(shape.dockers.first().referencePoint));
                        }
                    
                    } else { // is edge
                        newShape.dockers.first().setDockedShape(shape.dockers.first().getDockedShape());
                        newShape.dockers.first().setReferencePoint(shape.dockers.first().referencePoint);
                        newShape.dockers.last().setDockedShape(shape.dockers.last().getDockedShape());
                        newShape.dockers.last().setReferencePoint(shape.dockers.last().referencePoint);
                    }
                }
            });
            
            var stencil = undefined;
            var stencilSets = $scope.editor.getStencilSets().values();
            
            var stencilId = $scope.selectedMorphShapes[0].id;
            if ($scope.selectedMorphShapes[0].genericTaskId)
            {
                stencilId = $scope.selectedMorphShapes[0].genericTaskId;
            }
            
            for (var i = 0; i < stencilSets.length; i++)
            {
                var stencilSet = stencilSets[i];
                var nodes = stencilSet.nodes();
                for (var j = 0; j < nodes.length; j++)
                {
                    if (nodes[j].idWithoutNs() === stencilId)
                    {
                        stencil = nodes[j];
                        break;
                    }
                }
            }
            
            if (!stencil) return;
            
            // Create and execute command (for undo/redo)            
            var command = new MorphTo($scope.currentSelectedShape, stencil, $scope.editor);
            $scope.editor.executeCommands([command]);
        }
 
        $scope.close();
    };
 
    $scope.cancel = function() {
        $scope.$hide();
    };
 
    // Close button handler
    $scope.close = function() {
        $scope.$hide();
    };
 
}];