Skip to content

Commit e5ab17b

Browse files
PaulBerniermohsen1
authored andcommitted
Add openAtDepth function + minor fixes (#34)
* Build after the fix of typo 'appendChildren' * Add openAtDepth function * Fix readme about configurations * Fix call to toggleOpen before any rendering * Add some documentation in README * Add example for openAtDepth in README
1 parent fcf482e commit e5ab17b

File tree

5 files changed

+153
-18
lines changed

5 files changed

+153
-18
lines changed

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,35 @@ Default:
4343
hoverPreviewEnabled: false,
4444
hoverPreviewArrayCount: 100,
4545
hoverPreviewFieldCount: 5,
46-
theme: ''
46+
theme: '',
47+
animateOpen: true,
48+
animateClose: true
4749
}
4850
```
4951
Available configurations:
5052
##### Hover Preview
5153
* `hoverPreviewEnabled`: enable preview on hover.
5254
* `hoverPreviewArrayCount`: number of array items to show in preview Any array larger than this number will be shown as `Array[XXX]` where `XXX` is length of the array.
5355
* `hoverPreviewFieldCount`: number of object properties to show for object preview. Any object with more properties that thin number will be truncated.
56+
57+
##### Theme
58+
* `theme`: a string that can be any of these options: `['dark']`. Look at [`src/style.less`](src/style.less) for making new themes.
59+
60+
##### Animation
5461
* `animateOpen`: enable animation when expanding json object. True by default.
5562
* `animateClose`: enable animation when closing json object. True by default.
5663

57-
##### Theme
58-
* `theme`: a string that can be any of these options: `['dark']`. Look at [`src/style.less`](src/style.less) for making new themes.
64+
#### `openAtDepth([depth])`
65+
66+
```js
67+
const formatter = new Formatter({ ... });
68+
document.body.appendChild(formatter.render());
69+
formatter.openAtDepth(3);
70+
```
71+
72+
##### `depth` (`Number`)
73+
Default: `1`
74+
This number indicates up to how many levels the rendered tree should open. It allows use cases such as collapse all levels (with value `0`) or expand all levels (with value `Infinity`).
5975

6076
### Development
6177
Install the dependencies:

dist/json-formatter.js

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

dist/json-formatter.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,38 @@ export = class JSONFormatter {
200200
toggleOpen() {
201201
this.isOpen = !this.isOpen;
202202

203-
if (this.isOpen) {
204-
this.appendChildren(this.config.animateOpen);
205-
} else{
206-
this.removeChildren(this.config.animateClose);
203+
if (this.element) {
204+
if (this.isOpen) {
205+
this.appendChildren(this.config.animateOpen);
206+
} else{
207+
this.removeChildren(this.config.animateClose);
208+
}
209+
this.element.classList.toggle(cssClass('open'));
210+
}
211+
}
212+
213+
/**
214+
* Open all children up to a certain depth.
215+
* Allows actions such as expand all/collapse all
216+
*
217+
*/
218+
openAtDepth(depth = 1) {
219+
if (depth < 0) {
220+
return;
207221
}
208222

223+
this.open = depth;
224+
this.isOpen = (depth !== 0);
225+
209226
if (this.element) {
210-
this.element.classList.toggle(cssClass('open'));
227+
this.removeChildren(false);
228+
229+
if (depth === 0) {
230+
this.element.classList.remove(cssClass('open'));
231+
} else {
232+
this.appendChildren(this.config.animateOpen);
233+
this.element.classList.add(cssClass('open'));
234+
}
211235
}
212236
}
213237

test/spec.ts

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,76 @@ describe('url string', ()=> {
6565
it('should make a link and add class "url"', ()=> {
6666
expect(formatter.render().querySelector('a.json-formatter-url')).not.to.equal(null);
6767
});
68-
});
68+
});
69+
70+
describe('openAtDepth after rendering', ()=> {
71+
const formatter = new JSONFormatter({depth1: {depth2: {depth3 : {depth4: 21}}}}, Infinity, {animateOpen: false, animateClose: false});
72+
const element = formatter.render();
73+
74+
it('should open at depth 1', ()=> {
75+
formatter.openAtDepth();
76+
expect(element.outerHTML).to.contain('depth1');
77+
expect(element.outerHTML).to.not.contain('depth2');
78+
expect(element.outerHTML).to.not.contain('depth3');
79+
expect(element.outerHTML).to.not.contain('depth4');
80+
});
81+
82+
it('should collapses all', ()=> {
83+
formatter.openAtDepth(0);
84+
expect(element.outerHTML).to.not.contain('depth1');
85+
});
86+
87+
it('should expand all', ()=> {
88+
formatter.openAtDepth(Infinity);
89+
expect(element.outerHTML).to.contain('depth1');
90+
expect(element.outerHTML).to.contain('depth2');
91+
expect(element.outerHTML).to.contain('depth3');
92+
expect(element.outerHTML).to.contain('depth4');
93+
expect(element.outerHTML).to.contain('21');
94+
});
95+
});
96+
97+
describe('openAtDepth before any rendering', ()=> {
98+
const formatter = new JSONFormatter({depth1: {depth2: {depth3 : {depth4: 21}}}}, Infinity, {animateOpen: false, animateClose: false});
99+
100+
it('should open at depth 1', ()=> {
101+
formatter.openAtDepth();
102+
const element = formatter.render();
103+
expect(element.outerHTML).to.contain('depth1');
104+
expect(element.outerHTML).to.not.contain('depth2');
105+
expect(element.outerHTML).to.not.contain('depth3');
106+
expect(element.outerHTML).to.not.contain('depth4');
107+
});
108+
});
109+
110+
describe('toggleOpen after rendering', ()=> {
111+
112+
it('should toggle', ()=> {
113+
const formatter = new JSONFormatter({depth1: {depth2: {depth3 : {depth4: 21}}}}, Infinity, {animateOpen: false, animateClose: false});
114+
const element = formatter.render();
115+
116+
expect(element.outerHTML).to.contain('Object');
117+
expect(element.outerHTML).to.contain('depth1');
118+
119+
formatter.toggleOpen();
120+
121+
expect(element.outerHTML).to.contain('Object');
122+
expect(element.outerHTML).to.not.contain('depth1');
123+
expect(element.outerHTML).to.not.contain('depth2');
124+
expect(element.outerHTML).to.not.contain('depth3');
125+
expect(element.outerHTML).to.not.contain('depth4');
126+
});
127+
});
128+
129+
describe('toggleOpen before any rendering', ()=> {
130+
it('should toggle', ()=> {
131+
const formatter = new JSONFormatter({depth1: {depth2: {depth3 : {depth4: 21}}}}, Infinity, {animateOpen: false, animateClose: false});
132+
formatter.toggleOpen();
133+
const element = formatter.render();
134+
expect(element.outerHTML).to.contain('Object');
135+
expect(element.outerHTML).to.not.contain('depth1');
136+
expect(element.outerHTML).to.not.contain('depth2');
137+
expect(element.outerHTML).to.not.contain('depth3');
138+
expect(element.outerHTML).to.not.contain('depth4');
139+
});
140+
});

0 commit comments

Comments
 (0)