懒羊羊
2023-12-04 b3e9a47f4c17c0d708398698432a109420e7d9e1
提交 | 用户 | 时间
1ac2bc 1 /*
2  * Activiti Modeler component part of the Activiti project
3  * Copyright 2005-2014 Alfresco Software, Ltd. All rights reserved.
4
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 'use strict';
20
21 angular.module('activitiModeler')
22     .controller('ToolbarController', ['$scope', '$http', '$modal', '$q', '$rootScope', '$translate', '$location', function ($scope, $http, $modal, $q, $rootScope, $translate, $location) {
23
24         $scope.editorFactory.promise.then(function () {
25             $scope.items = KISBPM.TOOLBAR_CONFIG.items;
26         });
27         
28         $scope.secondaryItems = KISBPM.TOOLBAR_CONFIG.secondaryItems;
29
30         // Call configurable click handler (From http://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string)
31         var executeFunctionByName = function(functionName, context /*, args */) {
32             var args = Array.prototype.slice.call(arguments).splice(2);
33             var namespaces = functionName.split(".");
34             var func = namespaces.pop();
35             for(var i = 0; i < namespaces.length; i++) {
36                 context = context[namespaces[i]];
37             }
38             return context[func].apply(this, args);
39         };
40
41         // Click handler for toolbar buttons
42         $scope.toolbarButtonClicked = function(buttonIndex) {
43
44             // Default behaviour
45             var buttonClicked = $scope.items[buttonIndex];
46             var services = { '$scope' : $scope, '$rootScope' : $rootScope, '$http' : $http, '$modal' : $modal, '$q' : $q, '$translate' : $translate};
47             executeFunctionByName(buttonClicked.action, window, services);
48
49             // Other events
50             var event = {
51                 type : KISBPM.eventBus.EVENT_TYPE_TOOLBAR_BUTTON_CLICKED,
52                 toolbarItem : buttonClicked
53             };
54             KISBPM.eventBus.dispatch(event.type, event);
55         };
56         
57         // Click handler for secondary toolbar buttons
58         $scope.toolbarSecondaryButtonClicked = function(buttonIndex) {
59             var buttonClicked = $scope.secondaryItems[buttonIndex];
60             var services = { '$scope' : $scope, '$http' : $http, '$modal' : $modal, '$q' : $q, '$translate' : $translate, '$location': $location};
61             executeFunctionByName(buttonClicked.action, window, services);
62         };
63         
64         /* Key bindings */
65         Mousetrap.bind(['command+z', 'ctrl+z'], function(e) {
66             var services = { '$scope' : $scope, '$rootScope' : $rootScope, '$http' : $http, '$modal' : $modal, '$q' : $q, '$translate' : $translate};
67             KISBPM.TOOLBAR.ACTIONS.undo(services);
68             return false;
69         });
70         
71         Mousetrap.bind(['command+y', 'ctrl+y'], function(e) {
72             var services = { '$scope' : $scope, '$rootScope' : $rootScope, '$http' : $http, '$modal' : $modal, '$q' : $q, '$translate' : $translate};
73             KISBPM.TOOLBAR.ACTIONS.redo(services);
74             return false;
75         });
76         
77         Mousetrap.bind(['command+c', 'ctrl+c'], function(e) {
78             var services = { '$scope' : $scope, '$rootScope' : $rootScope, '$http' : $http, '$modal' : $modal, '$q' : $q, '$translate' : $translate};
79             KISBPM.TOOLBAR.ACTIONS.copy(services);
80             return false;
81         });
82         
83         Mousetrap.bind(['command+v', 'ctrl+v'], function(e) {
84             var services = { '$scope' : $scope, '$rootScope' : $rootScope, '$http' : $http, '$modal' : $modal, '$q' : $q, '$translate' : $translate};
85             KISBPM.TOOLBAR.ACTIONS.paste(services);
86             return false;
87         });
88         
89         Mousetrap.bind(['del'], function(e) {
90             var services = { '$scope' : $scope, '$rootScope' : $rootScope, '$http' : $http, '$modal' : $modal, '$q' : $q, '$translate' : $translate};
91             KISBPM.TOOLBAR.ACTIONS.deleteItem(services);
92             return false;
93         });
94
95         /* Undo logic */
96
97         $scope.undoStack = [];
98         $scope.redoStack = [];
99
100         $scope.editorFactory.promise.then(function() {
101
102             // Catch all command that are executed and store them on the respective stacks
103             $scope.editor.registerOnEvent(ORYX.CONFIG.EVENT_EXECUTE_COMMANDS, function( evt ){
104
105                 // If the event has commands
106                 if( !evt.commands ){ return; }
107
108                 $scope.undoStack.push( evt.commands );
109                 $scope.redoStack = [];
110                 
111                 for(var i = 0; i < $scope.items.length; i++) 
112                 {
113                     var item = $scope.items[i];
114                     if (item.action === 'KISBPM.TOOLBAR.ACTIONS.undo')
115                     {
116                         item.enabled = true;
117                     }
118                     else if (item.action === 'KISBPM.TOOLBAR.ACTIONS.redo')
119                     {
120                         item.enabled = false;
121                     }
122                 }
123
124                 // Update
125                 $scope.editor.getCanvas().update();
126                 $scope.editor.updateSelection();
127
128             });
129
130         });
131         
132         // Handle enable/disable toolbar buttons 
133         $scope.editorFactory.promise.then(function() {
134             $scope.editor.registerOnEvent(ORYX.CONFIG.EVENT_SELECTION_CHANGED, function( evt ){
135                 var elements = evt.elements;
136                 
137                 for(var i = 0; i < $scope.items.length; i++) 
138                 {
139                     var item = $scope.items[i];
140                     if (item.enabledAction && item.enabledAction === 'element')
141                     {
142                         var minLength = 1;
143                         if(item.minSelectionCount) {
144                             minLength = item.minSelectionCount;
145                         }
146                         if (elements.length >= minLength && !item.enabled) {
147                             $scope.safeApply(function () {
148                                 item.enabled = true;
149                             });
150                         }
151                         else if (elements.length == 0 && item.enabled) {
152                             $scope.safeApply(function () {
153                                 item.enabled = false;
154                             });
155                         }
156                     }
157                 }
158             });
159             
160         });
161
162     }]);