Skip to content

Commit 8b31db9

Browse files
committed
Update to fix combinedKey and getKey
1 parent 275cd42 commit 8b31db9

File tree

6 files changed

+141
-65
lines changed

6 files changed

+141
-65
lines changed

dist/bootstrap-decorator.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/schema-form.js

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2726,8 +2726,16 @@ return /******/ (function(modules) { // webpackBootstrap
27262726
value: true
27272727
});
27282728

2729+
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
2730+
27292731
exports.default = function ($parse, $compile, $http, $templateCache, $interpolate, $q, sfErrorMessage, sfPath, sfSelect) {
27302732

2733+
var keyFormat = {
2734+
COMPLETE: '*',
2735+
PATH: 'string',
2736+
INDICES: 'number'
2737+
};
2738+
27312739
return {
27322740
restrict: 'AE',
27332741
replace: false,
@@ -2761,21 +2769,58 @@ return /******/ (function(modules) { // webpackBootstrap
27612769
return scope.form && scope.form.notitle !== true && scope.form.title;
27622770
};
27632771

2764-
//Normalise names and ids
2765-
scope.fieldId = function (prependFormName, omitArrayIndexes) {
2766-
var key = scope.parentKey || [];
2767-
if (scope.form.key) {
2768-
if (typeof key[key.length - 1] === 'number') {
2769-
var combinedKey = key.concat(scope.form.key.slice(-1));
2770-
var formName = prependFormName && formCtrl && formCtrl.$name ? formCtrl.$name : undefined;
2771-
return sfPath.name(combinedKey, '-', formName, omitArrayIndexes);
2772+
scope.getKey = function (requiredFormat) {
2773+
var format = requiredFormat || keyFormat.COMPLETE;
2774+
var key = scope.parentKey ? scope.parentKey.slice(0, scope.parentKey.length - 1) : [];
2775+
2776+
if (typeof scope.$index === 'number') {
2777+
key = key.concat(scope.$index);
2778+
};
2779+
2780+
if (scope.form.key && scope.form.key.length) {
2781+
if (typeof key[key.length - 1] === 'number' && scope.form.key.length >= 1) {
2782+
scope.completeKey = key.concat(scope.form.key.slice(-1));
27722783
} else {
2773-
var formName = prependFormName && formCtrl && formCtrl.$name ? formCtrl.$name : undefined;
2774-
return sfPath.name(scope.form.key, '-', formName, omitArrayIndexes);
2784+
scope.completeKey = scope.form.key.slice();
2785+
};
2786+
};
2787+
2788+
if (!Array.isArray(scope.completeKey)) {
2789+
return undefined;
2790+
};
2791+
2792+
if (format === keyFormat.COMPLETE) {
2793+
return scope.completeKey;
2794+
};
2795+
2796+
return scope.completeKey.reduce(function (output, input, i) {
2797+
if (-1 !== [format].indexOf(typeof input === 'undefined' ? 'undefined' : _typeof(input))) {
2798+
return output.concat(input);
27752799
}
2800+
return output;
2801+
}, []);
2802+
};
2803+
if (scope.form.key) scope.completeKey = scope.getKey();
2804+
2805+
scope.path = function (modelPath) {
2806+
var i = -1;
2807+
modelPath = modelPath.replace(/\[\]/gi, function (matched) {
2808+
i++;
2809+
return scope.$i[i];
2810+
});
2811+
return scope.$eval(modelPath, scope);
2812+
};
2813+
2814+
//Normalise names and ids
2815+
scope.fieldId = function (prependFormName, omitArrayIndexes) {
2816+
var formName = prependFormName && formCtrl && formCtrl.$name ? formCtrl.$name : undefined;
2817+
var key = scope.getKey();
2818+
2819+
if (Array.isArray(key)) {
2820+
return sfPath.name(key, '-', formName, omitArrayIndexes);
27762821
} else {
27772822
return '';
2778-
}
2823+
};
27792824
};
27802825

27812826
scope.listToCheckboxValues = function (list) {
@@ -2954,16 +2999,18 @@ return /******/ (function(modules) { // webpackBootstrap
29542999
// Default behavior can be supplied as a globalOption, and behavior can be overridden
29553000
// in the form definition.
29563001
scope.$on('$destroy', function () {
3002+
var key = scope.getKey();
3003+
29573004
// If the entire schema form is destroyed we don't touch the model
29583005
if (!scope.externalDestructionInProgress) {
29593006
var destroyStrategy = form.destroyStrategy || scope.options && scope.options.destroyStrategy || 'remove';
29603007
// No key no model, and we might have strategy 'retain'
2961-
if (form.key && destroyStrategy !== 'retain') {
3008+
if (key && destroyStrategy !== 'retain') {
29623009

29633010
// Get the object that has the property we wan't to clear.
29643011
var obj = scope.model;
2965-
if (form.key.length > 1) {
2966-
obj = sfSelect(form.key.slice(0, form.key.length - 1), obj);
3012+
if (key.length > 1) {
3013+
obj = sfSelect(key.slice(0, key.length - 1), obj);
29673014
}
29683015

29693016
// We can get undefined here if the form hasn't been filled out entirely
@@ -2975,17 +3022,17 @@ return /******/ (function(modules) { // webpackBootstrap
29753022
var type = form.schema && form.schema.type || '';
29763023

29773024
// Empty means '',{} and [] for appropriate types and undefined for the rest
2978-
//console.log('destroy', destroyStrategy, form.key, type, obj);
3025+
//console.log('destroy', destroyStrategy, key, type, obj);
29793026
if (destroyStrategy === 'empty' && type.indexOf('string') !== -1) {
2980-
obj[form.key.slice(-1)] = '';
3027+
obj[key.slice(-1)] = '';
29813028
} else if (destroyStrategy === 'empty' && type.indexOf('object') !== -1) {
2982-
obj[form.key.slice(-1)] = {};
3029+
obj[key.slice(-1)] = {};
29833030
} else if (destroyStrategy === 'empty' && type.indexOf('array') !== -1) {
2984-
obj[form.key.slice(-1)] = [];
3031+
obj[key.slice(-1)] = [];
29853032
} else if (destroyStrategy === 'null') {
2986-
obj[form.key.slice(-1)] = null;
3033+
obj[key.slice(-1)] = null;
29873034
} else {
2988-
delete obj[form.key.slice(-1)];
3035+
delete obj[key.slice(-1)];
29893036
}
29903037
}
29913038
}
@@ -3126,7 +3173,7 @@ return /******/ (function(modules) { // webpackBootstrap
31263173
return {
31273174
scope: true,
31283175
controller: ['$scope', function SFArrayController($scope) {
3129-
this.key = $scope.form && $scope.form.key ? $scope.form.key : [];
3176+
this.key = $scope.form && $scope.form.key ? $scope.form.key.splice(0, -2) : [];
31303177
}],
31313178
link: function link(scope, element, attrs) {
31323179
scope.min = 0;
@@ -3392,14 +3439,6 @@ return /******/ (function(modules) { // webpackBootstrap
33923439
scope.arrayIndices = scope.arrayIndices || [];
33933440
scope.arrayIndices = scope.arrayIndices.concat(scope.arrayIndex);
33943441
scope.$i = scope.arrayIndices;
3395-
scope.path = function (modelPath) {
3396-
var i = -1;
3397-
modelPath = modelPath.replace(/\[\]/gi, function (matched) {
3398-
i++;
3399-
return scope.$i[i];
3400-
});
3401-
return modelPath;
3402-
};
34033442
}
34043443
};
34053444
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"coveralls": "^2.11.0",
4646
"gulp": "^3.5.6",
4747
"gulp-jscs": "^1.1.0",
48-
"json-schema-form-core": "git://github.com/json-schema-form/json-schema-form-core.git#develop",
48+
"json-schema-form-core": "https://github.com/json-schema-form/json-schema-form-core.git#develop",
4949
"karma": "^0.13.22",
5050
"karma-babel-preprocessor": "^6.0.1",
5151
"karma-chai-sinon": "^0.1.5",
@@ -70,7 +70,7 @@
7070
"url": "https://raw.githubusercontent.com/json-schema-form/angular-schema-form/master/LICENSE"
7171
}
7272
],
73-
"autoupdate": {
73+
"ignore-for-now-autoupdate": {
7474
"source": "git",
7575
"target": "git://github.com/json-schema-form/angular-schema-form.git",
7676
"basePath": "/dist/",

src/directives/field.js

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import angular from 'angular';
33
export default function($parse, $compile, $http, $templateCache, $interpolate, $q, sfErrorMessage,
44
sfPath, sfSelect) {
55

6+
const keyFormat = {
7+
COMPLETE: '*',
8+
PATH: 'string',
9+
INDICES: 'number'
10+
};
11+
612
return {
713
restrict: 'AE',
814
replace: false,
@@ -36,23 +42,60 @@ sfPath, sfSelect) {
3642
return scope.form && scope.form.notitle !== true && scope.form.title;
3743
};
3844

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);
45+
scope.getKey = function(requiredFormat) {
46+
let format = requiredFormat || keyFormat.COMPLETE;
47+
let key = (scope.parentKey) ? scope.parentKey.slice(0, scope.parentKey.length-1) : [] ;
48+
49+
if (typeof scope.$index === 'number') {
50+
key = key.concat(scope.$index);
51+
};
52+
53+
if(scope.form.key && scope.form.key.length) {
54+
if(typeof key[key.length-1] === 'number' && scope.form.key.length >= 1) {
55+
scope.completeKey = key.concat(scope.form.key.slice(-1));
4756
}
4857
else {
49-
var formName = (prependFormName && formCtrl && formCtrl.$name) ? formCtrl.$name : undefined;
50-
return sfPath.name(scope.form.key, '-', formName, omitArrayIndexes);
58+
scope.completeKey = scope.form.key.slice();
59+
};
60+
};
61+
62+
if(!Array.isArray(scope.completeKey)) {
63+
return undefined;
64+
};
65+
66+
if (format === keyFormat.COMPLETE) {
67+
return scope.completeKey;
68+
};
69+
70+
return scope.completeKey.reduce((output, input, i) => {
71+
if (-1 !== [ format ].indexOf((typeof input))) {
72+
return output.concat(input);
5173
}
74+
return output;
75+
}, []);
76+
};
77+
if(scope.form.key) scope.completeKey = scope.getKey();
78+
79+
scope.path = function(modelPath) {
80+
var i = -1;
81+
modelPath = modelPath.replace(/\[\]/gi, function(matched){
82+
i++;
83+
return scope.$i[i];
84+
});
85+
return scope.$eval(modelPath, scope);
86+
}
87+
88+
//Normalise names and ids
89+
scope.fieldId = function(prependFormName, omitArrayIndexes) {
90+
let formName = (prependFormName && formCtrl && formCtrl.$name) ? formCtrl.$name : undefined;
91+
let key = scope.getKey();
92+
93+
if(Array.isArray(key)) {
94+
return sfPath.name(key, '-', formName, omitArrayIndexes);
5295
}
5396
else {
5497
return '';
55-
}
98+
};
5699
};
57100

58101
scope.listToCheckboxValues = function(list) {
@@ -244,17 +287,19 @@ sfPath, sfSelect) {
244287
// Default behavior can be supplied as a globalOption, and behavior can be overridden
245288
// in the form definition.
246289
scope.$on('$destroy', function() {
290+
let key = scope.getKey();
291+
247292
// If the entire schema form is destroyed we don't touch the model
248293
if (!scope.externalDestructionInProgress) {
249294
var destroyStrategy = form.destroyStrategy ||
250295
(scope.options && scope.options.destroyStrategy) || 'remove';
251296
// No key no model, and we might have strategy 'retain'
252-
if (form.key && destroyStrategy !== 'retain') {
297+
if (key && destroyStrategy !== 'retain') {
253298

254299
// Get the object that has the property we wan't to clear.
255300
var obj = scope.model;
256-
if (form.key.length > 1) {
257-
obj = sfSelect(form.key.slice(0, form.key.length - 1), obj);
301+
if (key.length > 1) {
302+
obj = sfSelect(key.slice(0, key.length - 1), obj);
258303
}
259304

260305
// We can get undefined here if the form hasn't been filled out entirely
@@ -266,17 +311,17 @@ sfPath, sfSelect) {
266311
var type = (form.schema && form.schema.type) || '';
267312

268313
// Empty means '',{} and [] for appropriate types and undefined for the rest
269-
//console.log('destroy', destroyStrategy, form.key, type, obj);
314+
//console.log('destroy', destroyStrategy, key, type, obj);
270315
if (destroyStrategy === 'empty' && type.indexOf('string') !== -1) {
271-
obj[form.key.slice(-1)] = '';
316+
obj[key.slice(-1)] = '';
272317
} else if (destroyStrategy === 'empty' && type.indexOf('object') !== -1) {
273-
obj[form.key.slice(-1)] = {};
318+
obj[key.slice(-1)] = {};
274319
} else if (destroyStrategy === 'empty' && type.indexOf('array') !== -1) {
275-
obj[form.key.slice(-1)] = [];
320+
obj[key.slice(-1)] = [];
276321
} else if (destroyStrategy === 'null') {
277-
obj[form.key.slice(-1)] = null;
322+
obj[key.slice(-1)] = null;
278323
} else {
279-
delete obj[form.key.slice(-1)];
324+
delete obj[key.slice(-1)];
280325
}
281326
}
282327
}

src/directives/keyController.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ export default function(schemaForm, sfPath) {
1616
scope.arrayIndices = scope.arrayIndices || [];
1717
scope.arrayIndices = scope.arrayIndices.concat(scope.arrayIndex);
1818
scope.$i = scope.arrayIndices;
19-
scope.path = function(modelPath) {
20-
var i = -1;
21-
modelPath = modelPath.replace(/\[\]/gi, function(matched){
22-
i++;
23-
return scope.$i[i];
24-
});
25-
return modelPath;
26-
}
2719
}
2820
};
2921
};

src/directives/newArray.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default function(sel, sfPath, schemaForm) {
77
return {
88
scope: true,
99
controller: ['$scope', function SFArrayController($scope) {
10-
this.key = ($scope.form && $scope.form.key) ? $scope.form.key : [];
10+
this.key = ($scope.form && $scope.form.key) ? $scope.form.key.splice(0, -2) : [];
1111
}],
1212
link: function(scope, element, attrs) {
1313
scope.min = 0;

0 commit comments

Comments
 (0)