properties-fields-controller.js 6.6 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. * Task listeners
  20. */
  21. var KisBpmFieldsCtrl = [ '$scope', '$modal', '$timeout', '$translate', function($scope, $modal, $timeout, $translate) {
  22. // Config for the modal window
  23. var opts = {
  24. template: 'editor-app/configuration/properties/fields-popup.html',
  25. scope: $scope
  26. };
  27. // Open the dialog
  28. $modal(opts);
  29. }];
  30. var KisBpmFieldsPopupCtrl = [ '$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.fields !== undefined
  34. && $scope.property.value.fields !== 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 happened
  37. $scope.fields = angular.copy($scope.property.value.fields);
  38. for (var i = 0; i < $scope.fields.length; i++)
  39. {
  40. var field = $scope.fields[i];
  41. if (field.stringValue !== undefined && field.stringValue !== '')
  42. {
  43. field.implementation = field.stringValue;
  44. }
  45. else if (field.expression !== undefined && field.expression !== '')
  46. {
  47. field.implementation = field.expression;
  48. }
  49. else if (field.string !== undefined && field.string !== '')
  50. {
  51. field.implementation = field.string;
  52. }
  53. }
  54. } else {
  55. $scope.fields = [];
  56. }
  57. // Array to contain selected properties (yes - we only can select one, but ng-grid isn't smart enough)
  58. $scope.selectedFields = [];
  59. $scope.translationsRetrieved = false;
  60. $scope.labels = {};
  61. var namePromise = $translate('PROPERTY.FIELDS.NAME');
  62. var implementationPromise = $translate('PROPERTY.FIELDS.IMPLEMENTATION');
  63. $q.all([namePromise, implementationPromise]).then(function(results) {
  64. $scope.labels.nameLabel = results[0];
  65. $scope.labels.implementationLabel = results[1];
  66. $scope.translationsRetrieved = true;
  67. // Config for grid
  68. $scope.gridOptions = {
  69. data: 'fields',
  70. enableRowReordering: true,
  71. headerRowHeight: 28,
  72. multiSelect: false,
  73. keepLastSelected: false,
  74. selectedItems: $scope.selectedFields,
  75. columnDefs: [{field: 'name', displayName: $scope.labels.nameLabel},
  76. {field: 'implementation', displayName: $scope.labels.implementationLabel}]
  77. };
  78. });
  79. $scope.fieldDetailsChanged = function() {
  80. if ($scope.selectedFields[0].stringValue != '')
  81. {
  82. $scope.selectedFields[0].implementation = $scope.selectedFields[0].stringValue;
  83. }
  84. else if ($scope.selectedFields[0].expression != '')
  85. {
  86. $scope.selectedFields[0].implementation = $scope.selectedFields[0].expression;
  87. }
  88. else if ($scope.selectedFields[0].string != '')
  89. {
  90. $scope.selectedFields[0].implementation = $scope.selectedFields[0].string;
  91. }
  92. else
  93. {
  94. $scope.selectedFields[0].implementation = '';
  95. }
  96. };
  97. // Click handler for add button
  98. $scope.addNewField = function() {
  99. $scope.fields.push({ name : 'fieldName',
  100. implementation : '',
  101. stringValue : '',
  102. expression: '',
  103. string: ''});
  104. };
  105. // Click handler for remove button
  106. $scope.removeField = function() {
  107. if ($scope.selectedFields.length > 0) {
  108. var index = $scope.fields.indexOf($scope.selectedFields[0]);
  109. $scope.gridOptions.selectItem(index, false);
  110. $scope.fields.splice(index, 1);
  111. $scope.selectedFields.length = 0;
  112. if (index < $scope.fields.length) {
  113. $scope.gridOptions.selectItem(index + 1, true);
  114. } else if ($scope.fields.length > 0) {
  115. $scope.gridOptions.selectItem(index - 1, true);
  116. }
  117. }
  118. };
  119. // Click handler for up button
  120. $scope.moveFieldUp = function() {
  121. if ($scope.selectedFields.length > 0) {
  122. var index = $scope.fields.indexOf($scope.selectedFields[0]);
  123. if (index != 0) { // If it's the first, no moving up of course
  124. // Reason for funny way of swapping, see https://github.com/angular-ui/ng-grid/issues/272
  125. var temp = $scope.fields[index];
  126. $scope.fields.splice(index, 1);
  127. $timeout(function(){
  128. $scope.fields.splice(index + -1, 0, temp);
  129. }, 100);
  130. }
  131. }
  132. };
  133. // Click handler for down button
  134. $scope.moveFieldDown = function() {
  135. if ($scope.selectedFields.length > 0) {
  136. var index = $scope.fields.indexOf($scope.selectedFields[0]);
  137. if (index != $scope.fields.length - 1) { // If it's the last element, no moving down of course
  138. // Reason for funny way of swapping, see https://github.com/angular-ui/ng-grid/issues/272
  139. var temp = $scope.fields[index];
  140. $scope.fields.splice(index, 1);
  141. $timeout(function(){
  142. $scope.fields.splice(index + 1, 0, temp);
  143. }, 100);
  144. }
  145. }
  146. };
  147. // Click handler for save button
  148. $scope.save = function() {
  149. if ($scope.fields.length > 0) {
  150. $scope.property.value = {};
  151. $scope.property.value.fields = $scope.fields;
  152. } else {
  153. $scope.property.value = null;
  154. }
  155. $scope.updatePropertyInModel($scope.property);
  156. $scope.close();
  157. };
  158. $scope.cancel = function() {
  159. $scope.close();
  160. };
  161. // Close button handler
  162. $scope.close = function() {
  163. $scope.property.mode = 'read';
  164. $scope.$hide();
  165. };
  166. }];