电子档案
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
7.4 KiB

  1. /* Licensed under the Apache License, Version 2.0 (the "License");
  2. * you may not use this file except in compliance with the License.
  3. * You may obtain a copy of the License at
  4. *
  5. * http://www.apache.org/licenses/LICENSE-2.0
  6. *
  7. * Unless required by applicable law or agreed to in writing, software
  8. * distributed under the License is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. * See the License for the specific language governing permissions and
  11. * limitations under the License.
  12. */
  13. 'use strict';
  14. angular.module('flowableModeler')
  15. .controller('ToolbarController', ['$scope', '$http', '$modal', '$q', '$rootScope', '$translate', '$location', 'editorManager',
  16. function ($scope, $http, $modal, $q, $rootScope, $translate, $location, editorManager) {
  17. $scope.editorFactory.promise.then(function () {
  18. var toolbarItems = FLOWABLE.TOOLBAR_CONFIG.items;
  19. $scope.items = [];
  20. for (var i = 0; i < toolbarItems.length; i++)
  21. {
  22. if ($rootScope.modelData.model.modelType === 'form')
  23. {
  24. if (!toolbarItems[i].disableInForm)
  25. {
  26. $scope.items.push(toolbarItems[i]);
  27. }
  28. }
  29. else
  30. {
  31. $scope.items.push(toolbarItems[i]);
  32. }
  33. }
  34. });
  35. $scope.secondaryItems = FLOWABLE.TOOLBAR_CONFIG.secondaryItems;
  36. // Call configurable click handler (From http://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string)
  37. var executeFunctionByName = function(functionName, context /*, args */) {
  38. var args = Array.prototype.slice.call(arguments).splice(2);
  39. var namespaces = functionName.split(".");
  40. var func = namespaces.pop();
  41. for(var i = 0; i < namespaces.length; i++) {
  42. context = context[namespaces[i]];
  43. }
  44. return context[func].apply(this, args);
  45. };
  46. // Click handler for toolbar buttons
  47. $scope.toolbarButtonClicked = function(buttonIndex) {
  48. // Default behaviour
  49. var buttonClicked = $scope.items[buttonIndex];
  50. var services = { '$scope' : $scope, '$rootScope' : $rootScope, '$http' : $http, '$modal' : $modal, '$q' : $q, '$translate' : $translate, 'editorManager' : editorManager};
  51. executeFunctionByName(buttonClicked.action, window, services);
  52. // Other events
  53. var event = {
  54. type : FLOWABLE.eventBus.EVENT_TYPE_TOOLBAR_BUTTON_CLICKED,
  55. toolbarItem : buttonClicked
  56. };
  57. FLOWABLE.eventBus.dispatch(event.type, event);
  58. };
  59. // Click handler for secondary toolbar buttons
  60. $scope.toolbarSecondaryButtonClicked = function(buttonIndex) {
  61. var buttonClicked = $scope.secondaryItems[buttonIndex];
  62. var services = { '$scope' : $scope, '$http' : $http, '$modal' : $modal, '$q' : $q, '$translate' : $translate, '$location': $location, 'editorManager' : editorManager};
  63. executeFunctionByName(buttonClicked.action, window, services);
  64. };
  65. /* Key bindings */
  66. Mousetrap.bind('mod+z', function(e) {
  67. var services = { '$scope' : $scope, '$rootScope' : $rootScope, '$http' : $http, '$modal' : $modal, '$q' : $q, '$translate' : $translate, 'editorManager' : editorManager};
  68. FLOWABLE.TOOLBAR.ACTIONS.undo(services);
  69. return false;
  70. });
  71. Mousetrap.bind('mod+y', function(e) {
  72. var services = { '$scope' : $scope, '$rootScope' : $rootScope, '$http' : $http, '$modal' : $modal, '$q' : $q, '$translate' : $translate, 'editorManager' : editorManager};
  73. FLOWABLE.TOOLBAR.ACTIONS.redo(services);
  74. return false;
  75. });
  76. Mousetrap.bind('mod+c', function(e) {
  77. var services = { '$scope' : $scope, '$rootScope' : $rootScope, '$http' : $http, '$modal' : $modal, '$q' : $q, '$translate' : $translate, 'editorManager' : editorManager};
  78. FLOWABLE.TOOLBAR.ACTIONS.copy(services);
  79. return false;
  80. });
  81. Mousetrap.bind('mod+v', function(e) {
  82. var services = { '$scope' : $scope, '$rootScope' : $rootScope, '$http' : $http, '$modal' : $modal, '$q' : $q, '$translate' : $translate, 'editorManager' : editorManager};
  83. FLOWABLE.TOOLBAR.ACTIONS.paste(services);
  84. return false;
  85. });
  86. Mousetrap.bind(['del'], function(e) {
  87. var services = { '$scope' : $scope, '$rootScope' : $rootScope, '$http' : $http, '$modal' : $modal, '$q' : $q, '$translate' : $translate, 'editorManager' : editorManager};
  88. FLOWABLE.TOOLBAR.ACTIONS.deleteItem(services);
  89. return false;
  90. });
  91. /* Undo logic */
  92. $scope.undoStack = [];
  93. $scope.redoStack = [];
  94. FLOWABLE.eventBus.addListener(FLOWABLE.eventBus.EVENT_TYPE_UNDO_REDO_RESET,function($scope){
  95. this.undoStack = [];
  96. this.redoStack = [];
  97. if (this.items) {
  98. for(var i = 0; i < this.items.length; i++) {
  99. var item = this.items[i];
  100. if (item.action === 'FLOWABLE.TOOLBAR.ACTIONS.undo' || item.action === "FLOWABLE.TOOLBAR.ACTIONS.redo"){
  101. item.enabled = false;
  102. }
  103. }
  104. }
  105. },$scope);
  106. $scope.editorFactory.promise.then(function() {
  107. // Catch all command that are executed and store them on the respective stacks
  108. editorManager.registerOnEvent(ORYX.CONFIG.EVENT_EXECUTE_COMMANDS, function( evt ){
  109. // If the event has commands
  110. if( !evt.commands ){ return; }
  111. $scope.undoStack.push( evt.commands );
  112. $scope.redoStack = [];
  113. for(var i = 0; i < $scope.items.length; i++)
  114. {
  115. var item = $scope.items[i];
  116. if (item.action === 'FLOWABLE.TOOLBAR.ACTIONS.undo')
  117. {
  118. item.enabled = true;
  119. }
  120. else if (item.action === 'FLOWABLE.TOOLBAR.ACTIONS.redo')
  121. {
  122. item.enabled = false;
  123. }
  124. }
  125. // Update
  126. editorManager.getCanvas().update();
  127. editorManager.updateSelection();
  128. });
  129. });
  130. // Handle enable/disable toolbar buttons
  131. $scope.editorFactory.promise.then(function() {
  132. editorManager.registerOnEvent(ORYX.CONFIG.EVENT_SELECTION_CHANGED, function( evt ){
  133. var elements = evt.elements;
  134. for(var i = 0; i < $scope.items.length; i++) {
  135. var item = $scope.items[i];
  136. if (item.enabledAction && item.enabledAction === 'element') {
  137. var minLength = 1;
  138. if (item.minSelectionCount) {
  139. minLength = item.minSelectionCount;
  140. }
  141. if (elements.length >= minLength && !item.enabled) {
  142. $scope.safeApply(function () {
  143. item.enabled = true;
  144. });
  145. } else if (elements.length == 0 && item.enabled) {
  146. $scope.safeApply(function () {
  147. item.enabled = false;
  148. });
  149. }
  150. }
  151. }
  152. });
  153. });
  154. }]);