properties-form-properties-controller.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. * Form Properties
  20. */
  21. var KisBpmFormPropertiesCtrl = [ '$scope', '$modal', '$timeout', '$translate', function($scope, $modal, $timeout, $translate) {
  22. // Config for the modal window
  23. var opts = {
  24. template: 'editor-app/configuration/properties/form-properties-popup.html?version=' + Date.now(),
  25. scope: $scope
  26. };
  27. // Open the dialog
  28. $modal(opts);
  29. }];
  30. var KisBpmFormPropertiesPopupCtrl = ['$scope', '$q', '$translate', function($scope, $q, $translate) {
  31. // Put json representing form properties on scope
  32. if ($scope.property.value !== undefined && $scope.property.value !== null
  33. && $scope.property.value.formProperties !== undefined
  34. && $scope.property.value.formProperties !== null) {
  35. // Note that we clone the json object rather then setting it directly,
  36. // this to cope with the fact that the user can click the cancel button and no changes should have happended
  37. $scope.formProperties = angular.copy($scope.property.value.formProperties);
  38. } else {
  39. $scope.formProperties = [];
  40. }
  41. // Array to contain selected properties (yes - we only can select one, but ng-grid isn't smart enough)
  42. $scope.selectedProperties = [];
  43. $scope.translationsRetrieved = false;
  44. $scope.labels = {};
  45. var idPromise = $translate('PROPERTY.FORMPROPERTIES.ID');
  46. var namePromise = $translate('PROPERTY.FORMPROPERTIES.NAME');
  47. var typePromise = $translate('PROPERTY.FORMPROPERTIES.TYPE');
  48. $q.all([idPromise, namePromise, typePromise]).then(function(results) {
  49. $scope.labels.idLabel = results[0];
  50. $scope.labels.nameLabel = results[1];
  51. $scope.labels.typeLabel = results[2];
  52. $scope.translationsRetrieved = true;
  53. // Config for grid
  54. $scope.gridOptions = {
  55. data: 'formProperties',
  56. enableRowReordering: true,
  57. headerRowHeight: 28,
  58. multiSelect: false,
  59. keepLastSelected : false,
  60. selectedItems: $scope.selectedProperties,
  61. columnDefs: [{ field: 'id', displayName: $scope.labels.idLabel },
  62. { field: 'name', displayName: $scope.labels.nameLabel},
  63. { field: 'type', displayName: $scope.labels.typeLabel}]
  64. };
  65. });
  66. // Handler for when the value of the type dropdown changes
  67. $scope.propertyTypeChanged = function() {
  68. // Check date. If date, show date pattern
  69. if ($scope.selectedProperties[0].type === 'date') {
  70. $scope.selectedProperties[0].datePattern = 'MM-dd-yyyy hh:mm';
  71. } else {
  72. delete $scope.selectedProperties[0].datePattern;
  73. }
  74. // Check enum. If enum, show list of options
  75. if ($scope.selectedProperties[0].type === 'enum') {
  76. $scope.selectedProperties[0].enumValues = [ {value: 'value 1'}, {value: 'value 2'}];
  77. } else {
  78. delete $scope.selectedProperties[0].enumValues;
  79. }
  80. };
  81. // Click handler for + button after enum value
  82. var valueIndex = 3;
  83. $scope.addEnumValue = function(index) {
  84. $scope.selectedProperties[0].enumValues.splice(index + 1, 0, {value: 'value ' + valueIndex++});
  85. };
  86. // Click handler for - button after enum value
  87. $scope.removeEnumValue = function(index) {
  88. $scope.selectedProperties[0].enumValues.splice(index, 1);
  89. };
  90. // Click handler for add button
  91. var propertyIndex = 1;
  92. $scope.addNewProperty = function() {
  93. $scope.formProperties.push({ id : 'new_property_' + propertyIndex++,
  94. name : '',
  95. type : 'string',
  96. readable: true,
  97. writable: true});
  98. };
  99. // Click handler for remove button
  100. $scope.removeProperty = function() {
  101. if ($scope.selectedProperties.length > 0) {
  102. var index = $scope.formProperties.indexOf($scope.selectedProperties[0]);
  103. $scope.gridOptions.selectItem(index, false);
  104. $scope.formProperties.splice(index, 1);
  105. $scope.selectedProperties.length = 0;
  106. if (index < $scope.formProperties.length) {
  107. $scope.gridOptions.selectItem(index + 1, true);
  108. } else if ($scope.formProperties.length > 0) {
  109. $scope.gridOptions.selectItem(index - 1, true);
  110. }
  111. }
  112. };
  113. // Click handler for up button
  114. $scope.movePropertyUp = function() {
  115. if ($scope.selectedProperties.length > 0) {
  116. var index = $scope.formProperties.indexOf($scope.selectedProperties[0]);
  117. if (index != 0) { // If it's the first, no moving up of course
  118. // Reason for funny way of swapping, see https://github.com/angular-ui/ng-grid/issues/272
  119. var temp = $scope.formProperties[index];
  120. $scope.formProperties.splice(index, 1);
  121. $timeout(function(){
  122. $scope.formProperties.splice(index + -1, 0, temp);
  123. }, 100);
  124. }
  125. }
  126. };
  127. // Click handler for down button
  128. $scope.movePropertyDown = function() {
  129. if ($scope.selectedProperties.length > 0) {
  130. var index = $scope.formProperties.indexOf($scope.selectedProperties[0]);
  131. if (index != $scope.formProperties.length - 1) { // If it's the last element, no moving down of course
  132. // Reason for funny way of swapping, see https://github.com/angular-ui/ng-grid/issues/272
  133. var temp = $scope.formProperties[index];
  134. $scope.formProperties.splice(index, 1);
  135. $timeout(function(){
  136. $scope.formProperties.splice(index + 1, 0, temp);
  137. }, 100);
  138. }
  139. }
  140. };
  141. // Click handler for save button
  142. $scope.save = function() {
  143. if ($scope.formProperties.length > 0) {
  144. $scope.property.value = {};
  145. $scope.property.value.formProperties = $scope.formProperties;
  146. } else {
  147. $scope.property.value = null;
  148. }
  149. $scope.updatePropertyInModel($scope.property);
  150. $scope.close();
  151. };
  152. $scope.cancel = function() {
  153. $scope.$hide();
  154. $scope.property.mode = 'read';
  155. };
  156. // Close button handler
  157. $scope.close = function() {
  158. $scope.$hide();
  159. $scope.property.mode = 'read';
  160. };
  161. }];