Skip to content

Commit 4d94fe8

Browse files
author
Mohsen Azimi
committed
Merge branch 'master' of github.com:mohsen1/json-formatter-js
2 parents 6719549 + 58d454b commit 4d94fe8

File tree

9 files changed

+47
-37
lines changed

9 files changed

+47
-37
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
sudo: required
12
language: node_js
2-
3+
node_js: [stable]
34
before_install:
45
- export DISPLAY=:99.0
5-
- sh -e /etc/init.d/xvfb start
6+
- sh -e /etc/init.d/xvfb start

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
JSON Formatter started as an [AngularJS directive](https://github.com/mohsen1/json-formatter). This is pure JavaScript implementation of the same module.
88

9+
**[Live Demo](http://azimi.me/json-formatter-js/)**
910

1011
### Usage
1112

@@ -47,9 +48,11 @@ Default:
4748
```
4849
Available configurations:
4950
##### Hover Preview
50-
* `hoverPreviewEnabled`: enable preview on hover
51+
* `hoverPreviewEnabled`: enable preview on hover.
5152
* `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.
5253
* `hoverPreviewFieldCount`: number of object properties to show for object preview. Any object with more properties that thin number will be truncated.
54+
* `animateOpen`: enable animation when expanding json object. True by default.
55+
* `animateClose`: enable animation when closing json object. True by default.
5356

5457
##### Theme
5558
* `theme`: a string that can be any of these options: `['dark']`. Look at [`src/style.less`](src/style.less) for making new themes.

dist/bundle.js

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

dist/bundle.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.

index.html

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
border-radius: 5px;
1616
padding: 10px;
1717
}
18-
code {
18+
h1 code {
1919
font-size: 2em;
2020
}
2121
pre {
2222
font-size: 1.5rem;
2323
background: #efefef;
2424
padding: 1em;
2525
}
26+
textarea {
27+
font-family: monospace;
28+
}
2629
</style>
2730
</head>
2831
<body>
@@ -74,24 +77,34 @@ <h2>Playground</h2>
7477
<div id="live-result"></div>
7578
</td>
7679
</tr>
80+
<tr>
81+
<p>
82+
<input type="checkbox" id="hoverPreviewEnabled">
83+
<label for="hoverPreviewEnabled"><code>hoverPreviewEnabled</code></label>
84+
</p>
85+
</tr>
7786
</table>
7887
</div>
7988
<h2>Examples</h2>
8089
<div class="result"></div>
8190
<script>
8291
var live = document.getElementById('live');
92+
var hoverPreviewEnabledCheckbox = document.getElementById('hoverPreviewEnabled');
93+
8394

8495
function render() {
96+
live.style.backgroundColor = 'transparent';
8597
var result = document.getElementById('live-result');
8698
try {
87-
var formatter = new JSONFormatter(JSON.parse(live.value));
99+
var formatter = new JSONFormatter(JSON.parse(live.value), 1, {hoverPreviewEnabled: hoverPreviewEnabledCheckbox.checked});
88100
result.innerHTML = '';
89101
result.appendChild(formatter.render());
90102
} catch(e) {
91-
console.error(e);
103+
live.style.backgroundColor = 'rgba(255, 87, 34, 0.35)';
92104
}
93105
}
94106
live.addEventListener('keyup', render);
107+
hoverPreviewEnabledCheckbox.addEventListener('change', render);
95108
render();
96109
</script>
97110
<script>
@@ -160,4 +173,4 @@ <h2>Examples</h2>
160173
})
161174
</script>
162175
</body>
163-
</html>
176+
</html>

server.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict';
22
var webpack = require('webpack');
33
var webpackDevServer = require('webpack-dev-server');
4-
4+
var PORT = process.env.PORT || 8080;
55
var config = require("./webpack.config.js");
6-
config.entry.app.unshift("webpack-dev-server/client?http://localhost:8080/", "webpack/hot/dev-server");
6+
config.entry.app.unshift(`webpack-dev-server/client?http://localhost:{PORT}/`, "webpack/hot/dev-server");
77
var compiler = webpack(config);
88
var server = new webpackDevServer(compiler, {
99
progress: true,
1010
quiet: true,
1111
publicPath: config.output.publicPath
1212
});
13-
server.listen(8080, console.log.bind(null, 'Development server started at http://localhost:8080'));
13+
server.listen(PORT, console.log.bind(null, `Development server started at http://localhost:${PORT}`));

src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ interface JSONFormatterConfiguration {
2424
hoverPreviewEnabled: boolean;
2525
hoverPreviewArrayCount: number;
2626
hoverPreviewFieldCount: number;
27+
animateOpen: boolean;
28+
animateClose: boolean;
2729
theme: string;
2830
};
2931

3032
const _defaultConfig: JSONFormatterConfiguration = {
3133
hoverPreviewEnabled: false,
3234
hoverPreviewArrayCount: 100,
3335
hoverPreviewFieldCount: 5,
36+
animateOpen: true,
37+
animateClose: true,
3438
theme: null
3539
};
3640

@@ -134,7 +138,7 @@ export = class JSONFormatter {
134138

135139
/*
136140
* is this an object?
137-
* Note: In this context objects are array as well
141+
* Note: In this context arrays are object as well
138142
*/
139143
private get isObject(): boolean {
140144
return isObject(this.json);
@@ -197,9 +201,9 @@ export = class JSONFormatter {
197201
this.isOpen = !this.isOpen;
198202

199203
if (this.isOpen) {
200-
this.appendChildern(true);
204+
this.appendChildern(this.config.animateOpen);
201205
} else{
202-
this.removeChildren(true);
206+
this.removeChildren(this.config.animateClose);
203207
}
204208

205209
if (this.element) {

test/karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = function(config) {
1616

1717
// List of files / patterns to load in the browser
1818
files: [
19+
'dist/bundle.js',
1920
'test/spec.ts'
2021
],
2122

test/spec.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
declare const describe;
44
declare const it;
55
declare const expect;
6-
7-
import JSONFormatter from '../dist/bundle.js'
6+
declare const JSONFormatter;
87

98

109
describe('null', ()=> {
11-
it('should render "null"', function () {
10+
it('should render "null"', ()=> {
1211
const formatter = new JSONFormatter(null);
1312

1413
expect(formatter.render().innerText).to.contain('null');
@@ -41,11 +40,6 @@ describe('string', ()=> {
4140

4241
expect(formatter.render().innerText).to.contain('Hello World');
4342
});
44-
45-
it('should assing date class to date string', ()=> {
46-
const formatter = new JSONFormatter('2015-12-05T18:58:53.727Z');
47-
expect(formatter.render().querySelector('.json-formatter-date')).not.to.be.null;
48-
});
4943
});
5044

5145
describe('date string', ()=> {
@@ -55,9 +49,9 @@ describe('date string', ()=> {
5549
expect(formatter.render().innerText).to.contain('"' + (new Date(0)).toString() + '"');
5650
});
5751

58-
it('add the "date" class', ()=> {
59-
60-
expect(formatter.render().querySelector('.json-formatter-date')).not.to.equal(null);
52+
it('should assing date class to date string', ()=> {
53+
const formatter = new JSONFormatter('2015-12-05T18:58:53.727Z');
54+
expect(formatter.render().querySelector('.json-formatter-date')).not.to.be.null;
6155
});
6256
});
6357

0 commit comments

Comments
 (0)