Skip to content
This repository was archived by the owner on Feb 9, 2020. It is now read-only.

Commit 711d603

Browse files
gary-bSomeKittens
authored andcommitted
fix(jsonTree) array expansion
Intermittently arrays could not be expanded in the json tree. This occurred because the sort routine for model paths only accounted for parent child dependency between objects and their dot separated members and not between arrays and their elements. Updating the sort function to detect left bracket as path separator as well as dot
1 parent 159b83f commit 711d603

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

panel/components/json-tree/json-tree.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ function byPathDepth(a, b) { // sort '' first
191191
return -1;
192192
} else if (b === '') {
193193
return 1;
194-
} else { // sort by tree depth
195-
return a.split('.').length - b.split('.').length;
194+
} else { // sort by tree depth, and ensure array objects come before their members
195+
return a.split(/[\.,\[]/).length - b.split(/[\.,\[]/).length;
196196
}
197197
}

panel/components/json-tree/json-tree.spec.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,89 @@ describe('batJsonTree', function () {
6363
});
6464
});
6565

66+
describe('processing of model data', function() {
67+
it('should handle an element of an array being supplied before the array itself', function () {
68+
$rootScope.data = {
69+
'': {
70+
arrayA: { '~array-length': 1 }
71+
},
72+
'arrayA[0]': { hello: 'world' },
73+
'arrayA': [
74+
{ '~object': true }
75+
]
76+
};
77+
compileAndExpectOutputToEqual('arrayA: 0: hello: "world"');
78+
});
79+
80+
it('should handle an element of an array being supplied after the array itself', function () {
81+
$rootScope.data = {
82+
'': {
83+
arrayA: { '~array-length': 1 }
84+
},
85+
'arrayA': [
86+
{ '~object': true }
87+
],
88+
'arrayA[0]': { hello: 'world' }
89+
};
90+
compileAndExpectOutputToEqual('arrayA: 0: hello: "world"');
91+
});
92+
93+
it('should handle an object member being supplied after the object itself', function () {
94+
$rootScope.data = {
95+
'': {
96+
objectA: { '~object': true }
97+
},
98+
objectA: {
99+
objectB: { '~object': true }
100+
},
101+
'objectA.objectB': { hello: 'world' }
102+
};
103+
compileAndExpectOutputToEqual('objectA: objectB: hello: "world"');
104+
});
105+
106+
it('should handle an object member being supplied before the object itself', function () {
107+
$rootScope.data = {
108+
'': {
109+
objectA: { '~object': true }
110+
},
111+
'objectA.objectB': { hello: 'world' },
112+
objectA: {
113+
objectB: { '~object': true }
114+
}
115+
};
116+
compileAndExpectOutputToEqual('objectA: objectB: hello: "world"');
117+
});
118+
119+
it('should handle the root element being supplied first', function () {
120+
$rootScope.data = {
121+
'': {
122+
objectA: { '~object': true }
123+
},
124+
objectA: {
125+
hello: 'world'
126+
}
127+
};
128+
compileAndExpectOutputToEqual('objectA: hello: "world"');
129+
});
130+
131+
it('should handle the root element being supplied after another element', function () {
132+
$rootScope.data = {
133+
objectA: {
134+
hello: 'world'
135+
},
136+
'': {
137+
objectA: { '~object': true }
138+
}
139+
};
140+
compileAndExpectOutputToEqual('objectA: hello: "world"');
141+
});
142+
});
143+
144+
function compileAndExpectOutputToEqual(expected) {
145+
compileTree();
146+
expect(element.text()).toEqual(expected);
147+
}
148+
66149
function compileTree() {
67150
element = compile('<bat-json-tree bat-model="data"></bat-json-tree>');
68151
$rootScope.$apply();

0 commit comments

Comments
 (0)