properties-message-definitions-controller.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /*
  19. * Execution listeners
  20. */
  21. angular.module('activitiModeler').controller('ActivitiMessageDefinitionsCtrl', ['$scope', '$modal', function ($scope, $modal) {
  22. // Config for the modal window
  23. var opts = {
  24. template: 'editor-app/configuration/properties/message-definitions-popup.html?version=' + Date.now(),
  25. scope: $scope
  26. };
  27. // Open the dialog
  28. $modal(opts);
  29. }]);
  30. //Need a separate controller for the modal window due to https://github.com/angular-ui/bootstrap/issues/259
  31. // Will be fixed in a newer version of Angular UI
  32. angular.module('activitiModeler').controller('ActivitiMessageDefinitionsPopupCtrl',
  33. ['$scope', '$q', '$translate', '$timeout', function ($scope, $q, $translate, $timeout) {
  34. // Put json representing mesage definitions on scope
  35. if ($scope.property.value !== undefined && $scope.property.value !== null && $scope.property.value.length > 0) {
  36. if ($scope.property.value.constructor == String) {
  37. $scope.messageDefinitions = JSON.parse($scope.property.value);
  38. }
  39. else {
  40. // Note that we clone the json object rather then setting it directly,
  41. // this to cope with the fact that the user can click the cancel button and no changes should have happened
  42. $scope.messageDefinitions = angular.copy($scope.property.value);
  43. }
  44. } else {
  45. $scope.messageDefinitions = [];
  46. }
  47. // Array to contain selected mesage definitions (yes - we only can select one, but ng-grid isn't smart enough)
  48. $scope.selectedMessages = [];
  49. $scope.translationsRetrieved = false;
  50. $scope.labels = {};
  51. var idPromise = $translate('PROPERTY.MESSAGEDEFINITIONS.ID');
  52. var namePromise = $translate('PROPERTY.MESSAGEDEFINITIONS.NAME');
  53. $q.all([idPromise, namePromise]).then(function (results) {
  54. $scope.labels.idLabel = results[0];
  55. $scope.labels.nameLabel = results[1];
  56. $scope.translationsRetrieved = true;
  57. // Config for grid
  58. $scope.gridOptions = {
  59. data: 'messageDefinitions',
  60. headerRowHeight: 28,
  61. enableRowSelection: true,
  62. enableRowHeaderSelection: false,
  63. multiSelect: false,
  64. keepLastSelected : false,
  65. selectedItems: $scope.selectedMessages,
  66. columnDefs: [
  67. {field: 'id', displayName: $scope.labels.idLabel},
  68. {field: 'name', displayName: $scope.labels.nameLabel}]
  69. };
  70. });
  71. // Click handler for add button
  72. $scope.addNewMessageDefinition = function () {
  73. var newMessageDefinition = {id: '', name: ''};
  74. $scope.messageDefinitions.push(newMessageDefinition);
  75. $timeout(function () {
  76. $scope.gridOptions.selectItem($scope.messageDefinitions.length - 1, true);
  77. });
  78. };
  79. // Click handler for remove button
  80. $scope.removeMessageDefinition = function () {
  81. if ($scope.selectedMessages && $scope.selectedMessages.length > 0) {
  82. var index = $scope.messageDefinitions.indexOf($scope.selectedMessages[0]);
  83. $scope.gridOptions.selectItem(index, false);
  84. $scope.messageDefinitions.splice(index, 1);
  85. $scope.selectedMessages.length = 0;
  86. if (index < $scope.messageDefinitions.length) {
  87. $scope.gridOptions.selectItem(index + 1, true);
  88. } else if ($scope.messageDefinitions.length > 0) {
  89. $scope.gridOptions.selectItem(index - 1, true);
  90. }
  91. }
  92. };
  93. // Click handler for save button
  94. $scope.save = function () {
  95. if ($scope.messageDefinitions.length > 0) {
  96. $scope.property.value = $scope.messageDefinitions;
  97. } else {
  98. $scope.property.value = null;
  99. }
  100. $scope.updatePropertyInModel($scope.property);
  101. $scope.close();
  102. };
  103. $scope.cancel = function () {
  104. $scope.property.mode = 'read';
  105. $scope.$hide();
  106. };
  107. // Close button handler
  108. $scope.close = function () {
  109. $scope.property.mode = 'read';
  110. $scope.$hide();
  111. };
  112. }]);