Skip to content

Commit 15f101a

Browse files
committed
Merge remote-tracking branch 'origin/development' into development
# Conflicts: # dist/angular-schema-form.js # dist/angular-schema-form.min.js # karma.conf.js # package.json # src/directives/sf-schema.directive.spec.js
2 parents c036ff4 + 22764c7 commit 15f101a

File tree

6 files changed

+224
-179
lines changed

6 files changed

+224
-179
lines changed

docs/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,3 +1707,6 @@ function FormCtrl($scope) {
17071707
```
17081708
17091709
Note that arrays inside arrays won't work with conditional.
1710+
1711+
#### i18n/ l10n
1712+
While the library does not specifically address i18n/l10n (Internationalization/ Localization) it is certainly possible as <a href="https://plnkr.co/edit/WKduZL0Xe2lmkV9N5mk8?p=preview">this plunker</a> demonstrates.

src/directives/sf-field.directive.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export default function($parse, $compile, $interpolate, sfErrorMessage, sfPath,
5858
if(scope.form.key && scope.form.key.length) {
5959
if(typeof key[key.length-1] === 'number' && scope.form.key.length >= 1) {
6060
let trim = scope.form.key.length - key.length;
61-
scope.completeKey = key.concat(scope.form.key.slice(-trim));
61+
scope.completeKey =
62+
trim > 0 ? key.concat(scope.form.key.slice(-trim)) : key;
6263
}
6364
else {
6465
scope.completeKey = scope.form.key.slice();
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
chai.should();
2+
3+
var runSync = function (scope, tmpl) {
4+
var directiveScope = tmpl.isolateScope();
5+
var stub = sinon.stub(directiveScope, 'resolveReferences', function(schema, form) {
6+
directiveScope.render(schema, form);
7+
});
8+
scope.$apply();
9+
}
10+
11+
describe('sf-field.directive.js',function() {
12+
beforeEach(module('schemaForm'));
13+
beforeEach(
14+
module(function($sceProvider){
15+
$sceProvider.enabled(false);
16+
})
17+
);
18+
19+
var keyTests = [
20+
{
21+
name: 'array of objects',
22+
targetKey: ['arrayOfObjects', 0, 'stringVal'],
23+
24+
schema: {
25+
type: 'object',
26+
properties: {
27+
arrayOfObjects: {
28+
type: 'array',
29+
items: {
30+
type: 'object',
31+
properties: {
32+
stringVal: {
33+
type: 'string',
34+
'x-schema-form': {
35+
htmlClass: 'targetKey'
36+
}
37+
}
38+
}
39+
}
40+
}
41+
}
42+
},
43+
44+
form: [
45+
{
46+
key: 'arrayOfObjects',
47+
items: [
48+
{
49+
key: 'arrayOfObjects[].stringVal'
50+
}
51+
]
52+
}
53+
]
54+
},
55+
56+
{
57+
name: 'array of strings',
58+
targetKey: ['arrayOfStrings', 0],
59+
60+
schema: {
61+
type: 'object',
62+
properties: {
63+
arrayOfStrings: {
64+
type: 'array',
65+
items: {
66+
type: 'string',
67+
'x-schema-form': {
68+
htmlClass: 'targetKey'
69+
}
70+
}
71+
}
72+
}
73+
}
74+
}
75+
];
76+
77+
keyTests.forEach(function(keyTest) {
78+
it('should generate correct form keys for ' + keyTest.name, function(done) {
79+
inject(function($compile,$rootScope) {
80+
var scope = $rootScope.$new();
81+
scope.model = {};
82+
scope.schema = keyTest.schema;
83+
84+
var tmpl = angular.element('<form sf-schema="schema" sf-model="model"></form>');
85+
86+
$compile(tmpl)(scope);
87+
runSync(scope, tmpl);
88+
89+
tmpl.children().find('.targetKey').scope().form.key.should.deep.equal(keyTest.targetKey);
90+
done();
91+
});
92+
});
93+
94+
if (keyTest.form) {
95+
it('should generate correct form keys for ' + keyTest.name + ' with user specified form', function(done) {
96+
inject(function($compile,$rootScope) {
97+
var scope = $rootScope.$new();
98+
scope.model = {};
99+
scope.schema = keyTest.schema;
100+
scope.form = keyTest.form;
101+
102+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="model"></form>');
103+
104+
$compile(tmpl)(scope);
105+
runSync(scope, tmpl);
106+
107+
tmpl.children().find('.targetKey').scope().form.key.should.deep.equal(keyTest.targetKey);
108+
done();
109+
});
110+
});
111+
}
112+
});
113+
});

src/directives/sf-schema.directive.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ sfSelect, sfBuilder) {
176176
if (angular.isDefined(prop['default'])) {
177177
let val = sfSelect(path, scope.model);
178178
if (angular.isUndefined(val)) {
179-
sfSelect(path, scope.model, prop['default']);
179+
let defVal = prop['default'];
180+
if (angular.isObject(defVal)) defVal = angular.copy(defVal);
181+
sfSelect(path, scope.model, defVal);
180182
}
181183
}
182184
});

src/directives/sf-schema.directive.spec.js

Lines changed: 29 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,197 +2207,49 @@ describe('sf-schema.directive.js', function() {
22072207
});
22082208
});
22092209

2210-
it('should not add "has-success" class to radios field if a correct value is entered, but disableSuccessState is set on form', function() {
2211-
var field = {
2212-
name: 'radios',
2213-
property: {
2214-
type: 'boolean',
2215-
},
2216-
form: {
2217-
key: [ "field" ],
2218-
type: "radios",
2219-
titleMap: [
2220-
{
2221-
"value": false,
2222-
"name": "No way",
2223-
},
2224-
{
2225-
"value": true,
2226-
"name": "OK",
2227-
},
2228-
],
2229-
},
2230-
};
2231-
2232-
inject(function($compile, $rootScope) {
2210+
/*
2211+
TODO
2212+
it('should handle onChange for array type', function () {
2213+
inject(function($compile,$rootScope){
22332214
var scope = $rootScope.$new();
2234-
scope.model = {},
2235-
scope.schema = {
2236-
type: 'object',
2237-
properties: {
2238-
field: field.property,
2239-
},
2240-
};
2241-
scope.form = [ field.form ];
2242-
2243-
var tmpl = angular.element('<form name="theForm" sf-schema="schema" sf-form="form" sf-model="model"></form>');
2244-
$compile(tmpl)(scope);
2245-
runSync(scope, tmpl);
2246-
var ngModelCtrl = tmpl.find('.field').scope().ngModel;
2247-
ngModelCtrl.$valid = true;
2248-
ngModelCtrl.$pristine = false;
2249-
$rootScope.$apply();
2250-
tmpl.find('.field').hasClass('has-success').should.be.true;
2251-
scope.form[0].disableSuccessState = true;
2252-
$rootScope.$apply();
2253-
tmpl.find('.field').hasClass('has-success').should.be.false;
2254-
});
2255-
});
2256-
2257-
it('should not add "has-error" class to radios field if invalid value is entered, but disableErrorState is set on form', function() {
2258-
var field = {
2259-
name: 'radios',
2260-
property: {
2261-
type: 'boolean',
2262-
},
2263-
form: {
2264-
key: [ "field" ],
2265-
type: "radios",
2266-
titleMap: [
2267-
{
2268-
"value": false,
2269-
"name": "No way",
2270-
},
2271-
{
2272-
"value": true,
2273-
"name": "OK",
2274-
},
2275-
],
2276-
},
2277-
};
2215+
scope.obj = {};
22782216
2279-
inject(function($compile, $rootScope) {
2280-
var scope = $rootScope.$new();
2281-
scope.model = {
2282-
field: field.errorValue,
2283-
};
22842217
scope.schema = {
2285-
type: 'object',
2286-
properties: {
2287-
field: field.property,
2288-
},
2218+
"type": "object",
2219+
"properties": {
2220+
"arr" : {
2221+
"type": "array",
2222+
"items": {
2223+
"type": "object",
2224+
"properties": {
2225+
"name": {
2226+
"type": "string",
2227+
"default": "Name"
2228+
}
2229+
}
2230+
}
2231+
}
2232+
}
22892233
};
2290-
scope.form = [ field.form ];
22912234
2292-
var tmpl = angular.element('<form name="theForm" sf-schema="schema" sf-form="form" sf-model="model"></form>');
2293-
$compile(tmpl)(scope);
2294-
runSync(scope, tmpl);
2295-
var ngModelCtrl = tmpl.find('.field').scope().ngModel;
2296-
ngModelCtrl.$invalid = true;
2297-
ngModelCtrl.$pristine = false;
2298-
$rootScope.$apply();
2299-
tmpl.find('.field').hasClass('has-error').should.be.true;
2300-
scope.form[0].disableErrorState = true;
2301-
$rootScope.$apply();
2302-
tmpl.find('.field').hasClass('has-error').should.be.false;
2303-
});
2304-
});
2235+
scope.form = [{key : "arr", startEmpty : true, onChange: sinon.spy()}];
23052236
2306-
it('should not add "has-success" class to radios-inline field if a correct value is entered, but disableSuccessState is set on form', function() {
2307-
var field = {
2308-
name: 'radios',
2309-
property: {
2310-
type: 'boolean',
2311-
},
2312-
form: {
2313-
key: [ "field" ],
2314-
type: "radios",
2315-
titleMap: [
2316-
{
2317-
"value": false,
2318-
"name": "No way",
2319-
},
2320-
{
2321-
"value": true,
2322-
"name": "OK",
2323-
},
2324-
],
2325-
},
2326-
};
2327-
2328-
inject(function($compile, $rootScope) {
2329-
var scope = $rootScope.$new();
2330-
scope.model = {},
2331-
scope.schema = {
2332-
type: 'object',
2333-
properties: {
2334-
field: field.property,
2335-
},
2336-
};
2337-
scope.form = [ field.form ];
2237+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="obj"></form>');
23382238
2339-
var tmpl = angular.element('<form name="theForm" sf-schema="schema" sf-form="form" sf-model="model"></form>');
23402239
$compile(tmpl)(scope);
2341-
runSync(scope, tmpl);
2342-
var ngModelCtrl = tmpl.find('.field').scope().ngModel;
2343-
ngModelCtrl.$valid = true;
2344-
ngModelCtrl.$pristine = false;
23452240
$rootScope.$apply();
2346-
tmpl.find('.field').hasClass('has-success').should.be.true;
2347-
scope.form[0].disableSuccessState = true;
2348-
$rootScope.$apply();
2349-
tmpl.find('.field').hasClass('has-success').should.be.false;
2350-
});
2351-
});
23522241
2353-
it('should not add "has-error" class to radios-inline field if invalid value is entered, but disableErrorState is set on form', function() {
2354-
var field = {
2355-
name: 'radios',
2356-
property: {
2357-
type: 'boolean',
2358-
},
2359-
form: {
2360-
key: [ "field" ],
2361-
type: "radios",
2362-
titleMap: [
2363-
{
2364-
"value": false,
2365-
"name": "No way",
2366-
},
2367-
{
2368-
"value": true,
2369-
"name": "OK",
2370-
},
2371-
],
2372-
},
2373-
};
2242+
scope.form[0].onChange.should.not.have.been.called;
23742243
2375-
inject(function($compile, $rootScope) {
2376-
var scope = $rootScope.$new();
2377-
scope.model = {
2378-
field: field.errorValue,
2379-
};
2380-
scope.schema = {
2381-
type: 'object',
2382-
properties: {
2383-
field: { type: 'boolean' },
2384-
},
2385-
};
2386-
scope.form = [ field.form ];
2244+
tmpl.find('button.close').click();
2245+
//scope.form[0].onChange.should.have.been.called;
2246+
//scope.form[0].onChange.should.have.been.calledWith([]);
23872247
2388-
var tmpl = angular.element('<form name="theForm" sf-schema="schema" sf-form="form" sf-model="model"></form>');
2389-
$compile(tmpl)(scope);
2390-
runSync(scope, tmpl);
2391-
var ngModelCtrl = tmpl.find('.field').scope().ngModel;
2392-
ngModelCtrl.$invalid = true;
2393-
ngModelCtrl.$pristine = false;
2394-
$rootScope.$apply();
2395-
tmpl.find('.field').hasClass('has-error').should.be.true;
2396-
scope.form[0].disableErrorState = true;
2397-
$rootScope.$apply();
2398-
tmpl.find('.field').hasClass('has-error').should.be.false;
2248+
tmpl.find('button.btn-default').click();
2249+
//scope.form[0].onChange.should.have.been.calledWith([{name : "Name"}]);
23992250
});
24002251
});
2252+
*/
24012253
});
24022254

24032255

0 commit comments

Comments
 (0)