Skip to content

Commit 101ec86

Browse files
committed
#12 Add custom field managment to case template admin page
1 parent dd4a0a5 commit 101ec86

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

ui/app/scripts/controllers/admin/AdminCaseTemplatesCtrl.js

+46-8
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,28 @@
88
$scope.templates = [];
99
$scope.metrics = [];
1010
$scope.fields = [];
11+
$scope.templateCustomFields = [];
1112
$scope.templateIndex = -1;
1213

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+
1333
$scope.sortableOptions = {
1434
handle: '.drag-handle',
1535
stop: function(/*e, ui*/) {
@@ -23,6 +43,13 @@
2343
axis: 'y'
2444
};
2545

46+
$scope.keys = function(obj) {
47+
if(!obj) {
48+
return [];
49+
}
50+
return _.keys(obj);
51+
};
52+
2653
$scope.loadCache = function() {
2754
MetricsCacheSrv.all().then(function(metrics){
2855
$scope.metrics = metrics;
@@ -59,6 +86,8 @@
5986

6087
$scope.template = template;
6188
$scope.tags = UtilsSrv.objectify($scope.template.tags, 'text');
89+
90+
$scope.templateCustomFields = getTemplateCustomFields(template.customFields);
6291
});
6392

6493
$scope.templateIndex = index;
@@ -73,7 +102,7 @@
73102
tags: [],
74103
tasks: [],
75104
metricNames: [],
76-
customFieldNames: [],
105+
customFields: {},
77106
description: ''
78107
};
79108
$scope.tags = [];
@@ -134,18 +163,15 @@
134163
};
135164

136165
$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);
142168
} else {
143169
NotificationSrv.log('The custom field [' + field.label + '] has already been added to the template', 'warning');
144170
}
145171
};
146172

147173
$scope.removeCustomField = function(fieldName) {
148-
$scope.template.customFieldNames = _.without($scope.template.customFieldNames, fieldName);
174+
$scope.templateCustomFields = _.without($scope.templateCustomFields, fieldName);
149175
};
150176

151177
$scope.deleteTemplate = function() {
@@ -158,7 +184,19 @@
158184
};
159185

160186
$scope.saveTemplate = function() {
187+
// Set tags
161188
$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+
162200
if (_.isEmpty($scope.template.id)) {
163201
$scope.createTemplate();
164202
} else {
@@ -207,7 +245,7 @@
207245
$scope.template.tasks.push(task);
208246
} else {
209247
$scope.template.tasks = [task];
210-
}
248+
}
211249
}
212250

213251
$uibModalInstance.dismiss();

ui/app/views/partials/admin/case-templates.html

+5-6
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ <h4 class="vpad10 text-primary">
194194
<div class="row">
195195
<div class="col-md-6">
196196
<h4 class="vpad10 text-primary">
197-
Case custom fields ({{template.customFieldNames.length || 0}})
198-
197+
Case custom fields ({{keys(template.customFields).length}})
199198
<span uib-dropdown class="pull-right">
200199
<a href class="dropdown-toggle" uib-dropdown-toggle>
201200
<i class="fa fa-plus"></i>
@@ -210,8 +209,8 @@ <h4 class="vpad10 text-primary">
210209
</span>
211210
</h4>
212211

213-
<div ng-if="template.customFieldNames.length !== 0" ng-init="isCollapsed=true" ui-sortable="sortableFields" ng-model="template.customFieldNames">
214-
<div class="panel panel-default" ng-repeat="m in template.customFieldNames" ng-init="description=fields[m].description">
212+
<div ng-if="templateCustomFields.length !== 0" ng-init="isCollapsed=true" ui-sortable="sortableFields" ng-model="templateCustomFields">
213+
<div class="panel panel-default" ng-repeat="m in templateCustomFields" ng-init="description=fields[m].description">
215214
<div class="panel-heading">
216215
<span class="drag-handle text-primary clickable mr-xxs">
217216
<i class="fa fa-bars"></i>
@@ -233,10 +232,10 @@ <h4 class="vpad10 text-primary">
233232
</div>
234233
</div>
235234
</div>
236-
<div class="text-danger" ng-if="!template.customFieldNames || template.customFieldNames.length === 0">
235+
<div class="text-danger" ng-if="templateCustomFields.length === 0">
237236
<table class="table table-striped">
238237
<tr>
239-
<td>No customfields have been added</td>
238+
<td>No custom fields have been added</td>
240239
</tr>
241240
</table>
242241
</div>

0 commit comments

Comments
 (0)