|
8 | 8 | $scope.templates = [];
|
9 | 9 | $scope.metrics = [];
|
10 | 10 | $scope.fields = [];
|
| 11 | + $scope.templateCustomFields = []; |
11 | 12 | $scope.templateIndex = -1;
|
12 | 13 |
|
| 14 | + /** |
| 15 | + * Convert the template custom fields definition to a list of ordered field names |
| 16 | + * to be used for drag&drop sorting feature |
| 17 | + */ |
| 18 | + var getTemplateCustomFields = function(customFields) { |
| 19 | + var result = []; |
| 20 | + |
| 21 | + result = _.pluck(_.sortBy(_.map(customFields, function(definition, name){ |
| 22 | + return { |
| 23 | + name: name, |
| 24 | + order: definition.order |
| 25 | + } |
| 26 | + }), function(item){ |
| 27 | + return item.order; |
| 28 | + }), 'name'); |
| 29 | + |
| 30 | + return result; |
| 31 | + } |
| 32 | + |
13 | 33 | $scope.sortableOptions = {
|
14 | 34 | handle: '.drag-handle',
|
15 | 35 | stop: function(/*e, ui*/) {
|
|
23 | 43 | axis: 'y'
|
24 | 44 | };
|
25 | 45 |
|
| 46 | + $scope.keys = function(obj) { |
| 47 | + if(!obj) { |
| 48 | + return []; |
| 49 | + } |
| 50 | + return _.keys(obj); |
| 51 | + }; |
| 52 | + |
26 | 53 | $scope.loadCache = function() {
|
27 | 54 | MetricsCacheSrv.all().then(function(metrics){
|
28 | 55 | $scope.metrics = metrics;
|
|
59 | 86 |
|
60 | 87 | $scope.template = template;
|
61 | 88 | $scope.tags = UtilsSrv.objectify($scope.template.tags, 'text');
|
| 89 | + |
| 90 | + $scope.templateCustomFields = getTemplateCustomFields(template.customFields); |
62 | 91 | });
|
63 | 92 |
|
64 | 93 | $scope.templateIndex = index;
|
|
73 | 102 | tags: [],
|
74 | 103 | tasks: [],
|
75 | 104 | metricNames: [],
|
76 |
| - customFieldNames: [], |
| 105 | + customFields: {}, |
77 | 106 | description: ''
|
78 | 107 | };
|
79 | 108 | $scope.tags = [];
|
|
134 | 163 | };
|
135 | 164 |
|
136 | 165 | $scope.addCustomField = function(field) {
|
137 |
| - var fields = $scope.template.customFieldNames || []; |
138 |
| - |
139 |
| - if(fields.indexOf(field.name) === -1) { |
140 |
| - fields.push(field.name); |
141 |
| - $scope.template.customFieldNames = fields; |
| 166 | + if($scope.templateCustomFields.indexOf(field.name) === -1) { |
| 167 | + $scope.templateCustomFields.push(field.name); |
142 | 168 | } else {
|
143 | 169 | NotificationSrv.log('The custom field [' + field.label + '] has already been added to the template', 'warning');
|
144 | 170 | }
|
145 | 171 | };
|
146 | 172 |
|
147 | 173 | $scope.removeCustomField = function(fieldName) {
|
148 |
| - $scope.template.customFieldNames = _.without($scope.template.customFieldNames, fieldName); |
| 174 | + $scope.templateCustomFields = _.without($scope.templateCustomFields, fieldName); |
149 | 175 | };
|
150 | 176 |
|
151 | 177 | $scope.deleteTemplate = function() {
|
|
158 | 184 | };
|
159 | 185 |
|
160 | 186 | $scope.saveTemplate = function() {
|
| 187 | + // Set tags |
161 | 188 | $scope.template.tags = _.pluck($scope.tags, 'text');
|
| 189 | + |
| 190 | + // Set custom fields |
| 191 | + $scope.template.customFields = {}; |
| 192 | + _.each($scope.templateCustomFields, function(value, index) { |
| 193 | + var fieldDef = $scope.fields[value]; |
| 194 | + |
| 195 | + $scope.template.customFields[value] = {}; |
| 196 | + $scope.template.customFields[value][fieldDef.type] = null; |
| 197 | + $scope.template.customFields[value].order = index + 1; |
| 198 | + }); |
| 199 | + |
162 | 200 | if (_.isEmpty($scope.template.id)) {
|
163 | 201 | $scope.createTemplate();
|
164 | 202 | } else {
|
|
207 | 245 | $scope.template.tasks.push(task);
|
208 | 246 | } else {
|
209 | 247 | $scope.template.tasks = [task];
|
210 |
| - } |
| 248 | + } |
211 | 249 | }
|
212 | 250 |
|
213 | 251 | $uibModalInstance.dismiss();
|
|
0 commit comments