Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1d72b55

Browse files
committedMar 10, 2019
Improved style of toggle arrows
1 parent 8f8d055 commit 1d72b55

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
lines changed
 

‎demo.html

+16-8
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@
1313
margin: 0 100px;
1414
font-family: sans-serif;
1515
}
16+
p.options input[type=checkbox] {
17+
vertical-align: middle;
18+
}
1619
textarea#json-input {
1720
width: 100%;
1821
height: 200px;
1922
}
2023
pre#json-renderer {
2124
border: 1px solid #aaa;
22-
padding: 0.5em 1.5em;
2325
}
2426
</style>
2527

2628
<script>
2729
$(function() {
28-
$('#btn-json-viewer').click(function() {
30+
function renderJson() {
2931
try {
3032
var input = eval('(' + $('#json-input').val() + ')');
3133
}
@@ -38,10 +40,16 @@
3840
withLinks: $('#with-links').is(':checked')
3941
};
4042
$('#json-renderer').jsonViewer(input, options);
41-
});
43+
}
44+
45+
// Generate on click
46+
$('#btn-json-viewer').click(renderJson);
47+
48+
// Generate on option change
49+
$('p.options input[type=checkbox]').click(renderJson);
4250

43-
// Display JSON sample on load
44-
$('#btn-json-viewer').click();
51+
// Display JSON sample on page load
52+
renderJson();
4553
});
4654
</script>
4755
</head>
@@ -52,21 +60,21 @@ <h1><a href="https://github.com/abodelot/jquery.json-viewer">jQuery json-viewer<
5260
"id": 1001,
5361
"type": "donut",
5462
"name": "Cake",
55-
"description": "http://en.wikipedia.org/wiki/Doughnut",
63+
"description": "https://en.wikipedia.org/wiki/Doughnut",
5664
"price": 2.55,
5765
"available": {
5866
store: 42,
5967
warehouse: 600
6068
},
61-
"topping": [
69+
"toppings": [
6270
{ "id": 5001, "type": "None" },
6371
{ "id": 5002, "type": "Glazed" },
6472
{ "id": 5005, "type": "Sugar" },
6573
{ "id": 5003, "type": "Chocolate" },
6674
{ "id": 5004, "type": "Maple" }
6775
]
6876
}</textarea>
69-
<p>
77+
<p class="options">
7078
Options:
7179
<label><input type="checkbox" id="collapsed" />Collapse nodes</label>
7280
<label><input type="checkbox" id="with-quotes" />Keys with quotes</label>

‎json-viewer/jquery.json-viewer.css

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/* Root element */
2+
.json-document {
3+
padding: 1em 2em;
4+
}
5+
16
/* Syntax highlighting for JSON objects */
27
ul.json-dict, ol.json-array {
38
list-style-type: none;
@@ -23,17 +28,22 @@ a.json-toggle:focus {
2328
outline: none;
2429
}
2530
a.json-toggle:before {
26-
color: #aaa;
31+
font-size: 1.1em;
32+
color: #c0c0c0;
2733
content: "\25BC"; /* down arrow */
2834
position: absolute;
2935
display: inline-block;
3036
width: 1em;
31-
left: -1em;
37+
text-align: center;
38+
line-height: 1em;
39+
left: -1.2em;
40+
}
41+
a.json-toggle:hover:before {
42+
color: #aaa;
3243
}
3344
a.json-toggle.collapsed:before {
34-
transform: rotate(-90deg); /* Use rotated down arrow, prevents right arrow appearing smaller than down arrow in some browsers */
35-
-ms-transform: rotate(-90deg);
36-
-webkit-transform: rotate(-90deg);
45+
/* Use rotated down arrow, prevents right arrow appearing smaller than down arrow in some browsers */
46+
transform: rotate(-90deg);
3747
}
3848

3949
/* Collapsable placeholder links */

‎json-viewer/jquery.json-viewer.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/**
22
* jQuery json-viewer
33
* @author: Alexandre Bodelot <alexandre.bodelot@gmail.com>
4+
* @link: https://github.com/abodelot/jquery.json-viewer
45
*/
5-
(function($){
6+
(function($) {
67

78
/**
89
* Check if arg is either an array with at least 1 element, or a dict with at least 1 key
@@ -28,13 +29,13 @@
2829
function json2html(json, options) {
2930
var html = '';
3031
if (typeof json === 'string') {
31-
/* Escape tags */
32+
// Escape tags
3233
json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
3334
if (options.withLinks == undefined){
3435
options.withLinks = true;
3536
}
3637
if (options.withLinks && isUrl(json)) {
37-
html += '<a href="' + json + '" class="json-string">' + json + '</a>';
38+
html += '<a href="' + json + '" class="json-string" target="_blank">' + json + '</a>';
3839
} else {
3940
html += '<span class="json-string">"' + json + '"</span>';
4041
}
@@ -49,12 +50,12 @@
4950
html += '[<ol class="json-array">';
5051
for (var i = 0; i < json.length; ++i) {
5152
html += '<li>';
52-
/* Add toggle button if item is collapsable */
53+
// Add toggle button if item is collapsable
5354
if (isCollapsable(json[i])) {
5455
html += '<a href class="json-toggle"></a>';
5556
}
5657
html += json2html(json[i], options);
57-
/* Add comma if item is not last */
58+
// Add comma if item is not last
5859
if (i < json.length - 1) {
5960
html += ',';
6061
}
@@ -73,14 +74,14 @@
7374
html += '<li>';
7475
var keyRepr = options.withQuotes ?
7576
'<span class="json-string">"' + key + '"</span>' : key;
76-
/* Add toggle button if item is collapsable */
77+
// Add toggle button if item is collapsable
7778
if (isCollapsable(json[key])) {
7879
html += '<a href class="json-toggle">' + keyRepr + '</a>';
7980
} else {
8081
html += keyRepr;
8182
}
8283
html += ': ' + json2html(json[key], options);
83-
/* Add comma if item is not last */
84+
// Add comma if item is not last
8485
if (--key_count > 0) {
8586
html += ',';
8687
}
@@ -103,19 +104,20 @@
103104
$.fn.jsonViewer = function(json, options) {
104105
options = options || {};
105106

106-
/* jQuery chaining */
107+
// jQuery chaining
107108
return this.each(function() {
108109

109-
/* Transform to HTML */
110+
// Transform to HTML
110111
var html = json2html(json, options);
111112
if (isCollapsable(json)) {
112113
html = '<a href class="json-toggle"></a>' + html;
113114
}
114115

115-
/* Insert HTML in target DOM element */
116+
// Insert HTML in target DOM element
116117
$(this).html(html);
118+
$(this).addClass('json-document');
117119

118-
/* Bind click on toggle buttons */
120+
// Bind click on toggle buttons
119121
$(this).off('click');
120122
$(this).on('click', 'a.json-toggle', function() {
121123
var target = $(this).toggleClass('collapsed').siblings('ul.json-dict, ol.json-array');
@@ -130,14 +132,14 @@
130132
return false;
131133
});
132134

133-
/* Simulate click on toggle button when placeholder is clicked */
135+
// Simulate click on toggle button when placeholder is clicked
134136
$(this).on('click', 'a.json-placeholder', function() {
135137
$(this).siblings('a.json-toggle').click();
136138
return false;
137139
});
138140

139141
if (options.collapsed == true) {
140-
/* Trigger click to collapse all nodes */
142+
// Trigger click to collapse all nodes
141143
$(this).find('a.json-toggle').click();
142144
}
143145
});

0 commit comments

Comments
 (0)
Please sign in to comment.