Skip to content

Commit 7d45682

Browse files
committed
Add fieldId lookup based on #560 and #742
1 parent cedc65e commit 7d45682

File tree

6 files changed

+90
-7
lines changed

6 files changed

+90
-7
lines changed

src/directives/field.js

+34-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ sfPath, sfSelect) {
88
replace: false,
99
transclude: false,
1010
scope: true,
11-
require: '^sfSchema',
11+
require: ['^sfSchema', '?^form', '?^^sfKeyController'],
1212
link: {
13-
pre: function(scope, element, attrs, sfSchema) {
13+
pre: function(scope, element, attrs, ctrl) {
14+
var sfSchema = ctrl[0];
15+
var formCtrl = ctrl[1];
16+
var keyCtrl = ctrl[2];
17+
1418
//The ngModelController is used in some templates and
1519
//is needed for error messages,
1620
scope.$on('schemaFormPropagateNgModelController', function(event, ngModel) {
@@ -22,12 +26,35 @@ sfPath, sfSelect) {
2226
// Fetch our form.
2327
scope.form = sfSchema.lookup['f' + attrs.sfField];
2428
},
25-
post: function(scope, element, attrs, sfSchema) {
29+
post: function(scope, element, attrs, ctrl) {
30+
var sfSchema = ctrl[0];
31+
var formCtrl = ctrl[1];
32+
var keyCtrl = ctrl[2];
33+
2634
//Keep error prone logic from the template
2735
scope.showTitle = function() {
2836
return scope.form && scope.form.notitle !== true && scope.form.title;
2937
};
3038

39+
//Normalise names and ids
40+
scope.fieldId = function(prependFormName, omitArrayIndexes) {
41+
var key = scope.parentKey || [];
42+
if(scope.form.key) {
43+
if(typeof key[key.length-1] === 'number') {
44+
var combinedKey = key.concat(scope.form.key.slice(-1));
45+
var formName = (prependFormName && formCtrl && formCtrl.$name) ? formCtrl.$name : undefined;
46+
return sfPath.name(combinedKey, '-', formName, omitArrayIndexes);
47+
}
48+
else {
49+
var formName = (prependFormName && formCtrl && formCtrl.$name) ? formCtrl.$name : undefined;
50+
return sfPath.name(scope.form.key, '-', formName, omitArrayIndexes);
51+
}
52+
}
53+
else {
54+
return '';
55+
}
56+
};
57+
3158
scope.listToCheckboxValues = function(list) {
3259
var values = {};
3360
angular.forEach(list, function(v) {
@@ -155,6 +182,10 @@ sfPath, sfSelect) {
155182
);
156183
};
157184

185+
// append the field-id to the htmlClass
186+
scope.form.htmlClass = scope.form.htmlClass || '';
187+
scope.form.htmlClass += (scope.form.htmlClass ? ' ' : '') + scope.fieldId(false, true);
188+
158189
var form = scope.form;
159190

160191
// Where there is a key there is probably a ngModel

src/directives/keyController.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Directive that handles keys and array indexes
3+
*/
4+
export default function(schemaForm, sfPath) {
5+
return {
6+
scope: true,
7+
require: ['^^sfNewArray'],
8+
link: function(scope, element, attrs, ctrl) {
9+
var currentKey = sfPath.parse(attrs.sfParentKey);
10+
if(currentKey.length > 1) currentKey = currentKey.splice(-1);
11+
12+
scope.parentKey = scope.parentKey || [];
13+
scope.parentKey = scope.parentKey.concat(currentKey, Number(attrs.sfIndex));
14+
15+
scope.arrayIndex = Number(attrs.sfIndex);
16+
scope.arrayIndices = scope.arrayIndices || [];
17+
scope.arrayIndices = scope.arrayIndices.concat(scope.arrayIndex);
18+
}
19+
};
20+
};

src/directives/newArray.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import angular from 'angular';
55
*/
66
export default function(sel, sfPath, schemaForm) {
77
return {
8-
scope: false,
8+
scope: true,
9+
controller: ['$scope', function SFArrayController($scope) {
10+
this.key = ($scope.form && $scope.form.key) ? $scope.form.key : [];
11+
}],
912
link: function(scope, element, attrs) {
1013
scope.min = 0;
1114

src/module.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import changed from './directives/changed';
1313
import field from './directives/field';
1414
import message from './directives/message';
1515
import newArray from './directives/newArray';
16+
import keyController from './directives/keyController';
1617
import schemaFormDirective from './directives/schemaForm';
1718
import schemaValidate from './directives/schemaValidate';
1819

@@ -58,4 +59,5 @@ angular.module('schemaForm', deps)
5859
.directive('sfSchema', [ '$compile', '$http', '$templateCache', '$q', 'schemaForm',
5960
'schemaFormDecorators', 'sfSelect', 'sfPath', 'sfBuilder',
6061
schemaFormDirective ])
61-
.directive('schemaValidate', [ 'sfValidator', '$parse', 'sfSelect', schemaValidate ]);
62+
.directive('schemaValidate', [ 'sfValidator', '$parse', 'sfSelect', schemaValidate ])
63+
.directive('sfKeyController', [ 'schemaForm','sfPath', keyController ]);

src/services/builder.js

+6
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ export default function(sfPathProvider) {
141141
},
142142
array: function(args) {
143143
var items = args.fieldFrag.querySelector('[schema-form-array-items]');
144+
145+
if (args.form.key) {
146+
var arrayDepth = args.form.key.filter(function(e) { return e === '' }).length;
147+
}
148+
144149
if (items) {
145150
var state = angular.copy(args.state);
146151
state.keyRedaction = 0;
@@ -222,6 +227,7 @@ export default function(sfPathProvider) {
222227
};
223228

224229
var build = function(items, decorator, templateFn, slots, path, state, lookup) {
230+
state = state || {};
225231
state = state || {};
226232
lookup = lookup || Object.create(null);
227233
path = path || 'schemaForm.form';

src/services/decorators.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ export default function($compileProvider, sfPathProvider) {
4040
replace: false,
4141
transclude: false,
4242
scope: true,
43-
require: '?^sfSchema',
44-
link: function(scope, element, attrs, sfSchema) {
43+
require: ['^sfSchema', '?^form'],
44+
link: function(scope, element, attrs, ctrl) {
45+
var sfSchema = ctrl[0];
46+
var formCtrl = ctrl[1];
4547

4648
//The ngModelController is used in some templates and
4749
//is needed for error messages,
@@ -56,6 +58,25 @@ export default function($compileProvider, sfPathProvider) {
5658
return scope.form && scope.form.notitle !== true && scope.form.title;
5759
};
5860

61+
//Normalise names and ids
62+
scope.fieldId = function(prependFormName, omitArrayIndexes) {
63+
var key = scope.parentKey || [];
64+
if(scope.form.key) {
65+
if(typeof key[key.length-1] === 'number') {
66+
var combinedKey = key.concat(scope.form.key.slice(-1));
67+
var formName = (prependFormName && formCtrl && formCtrl.$name) ? formCtrl.$name : undefined;
68+
return sfPath.name(combinedKey, '-', formName, omitArrayIndexes);
69+
}
70+
else {
71+
var formName = (prependFormName && formCtrl && formCtrl.$name) ? formCtrl.$name : undefined;
72+
return sfPath.name(scope.form.key, '-', formName, omitArrayIndexes);
73+
}
74+
}
75+
else {
76+
return '';
77+
}
78+
};
79+
5980
scope.listToCheckboxValues = function(list) {
6081
var values = {};
6182
angular.forEach(list, function(v) {

0 commit comments

Comments
 (0)