|
1 | 1 | /**
|
2 | 2 | * jQuery json-viewer
|
3 | 3 | * @author: Alexandre Bodelot <alexandre.bodelot@gmail.com>
|
| 4 | + * @link: https://github.com/abodelot/jquery.json-viewer |
4 | 5 | */
|
5 |
| -(function($){ |
| 6 | +(function($) { |
6 | 7 |
|
7 | 8 | /**
|
8 | 9 | * Check if arg is either an array with at least 1 element, or a dict with at least 1 key
|
|
28 | 29 | function json2html(json, options) {
|
29 | 30 | var html = '';
|
30 | 31 | if (typeof json === 'string') {
|
31 |
| - /* Escape tags */ |
| 32 | + // Escape tags |
32 | 33 | json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
33 | 34 | if (options.withLinks == undefined){
|
34 | 35 | options.withLinks = true;
|
35 | 36 | }
|
36 | 37 | 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>'; |
38 | 39 | } else {
|
39 | 40 | html += '<span class="json-string">"' + json + '"</span>';
|
40 | 41 | }
|
|
49 | 50 | html += '[<ol class="json-array">';
|
50 | 51 | for (var i = 0; i < json.length; ++i) {
|
51 | 52 | html += '<li>';
|
52 |
| - /* Add toggle button if item is collapsable */ |
| 53 | + // Add toggle button if item is collapsable |
53 | 54 | if (isCollapsable(json[i])) {
|
54 | 55 | html += '<a href class="json-toggle"></a>';
|
55 | 56 | }
|
56 | 57 | html += json2html(json[i], options);
|
57 |
| - /* Add comma if item is not last */ |
| 58 | + // Add comma if item is not last |
58 | 59 | if (i < json.length - 1) {
|
59 | 60 | html += ',';
|
60 | 61 | }
|
|
73 | 74 | html += '<li>';
|
74 | 75 | var keyRepr = options.withQuotes ?
|
75 | 76 | '<span class="json-string">"' + key + '"</span>' : key;
|
76 |
| - /* Add toggle button if item is collapsable */ |
| 77 | + // Add toggle button if item is collapsable |
77 | 78 | if (isCollapsable(json[key])) {
|
78 | 79 | html += '<a href class="json-toggle">' + keyRepr + '</a>';
|
79 | 80 | } else {
|
80 | 81 | html += keyRepr;
|
81 | 82 | }
|
82 | 83 | html += ': ' + json2html(json[key], options);
|
83 |
| - /* Add comma if item is not last */ |
| 84 | + // Add comma if item is not last |
84 | 85 | if (--key_count > 0) {
|
85 | 86 | html += ',';
|
86 | 87 | }
|
|
103 | 104 | $.fn.jsonViewer = function(json, options) {
|
104 | 105 | options = options || {};
|
105 | 106 |
|
106 |
| - /* jQuery chaining */ |
| 107 | + // jQuery chaining |
107 | 108 | return this.each(function() {
|
108 | 109 |
|
109 |
| - /* Transform to HTML */ |
| 110 | + // Transform to HTML |
110 | 111 | var html = json2html(json, options);
|
111 | 112 | if (isCollapsable(json)) {
|
112 | 113 | html = '<a href class="json-toggle"></a>' + html;
|
113 | 114 | }
|
114 | 115 |
|
115 |
| - /* Insert HTML in target DOM element */ |
| 116 | + // Insert HTML in target DOM element |
116 | 117 | $(this).html(html);
|
| 118 | + $(this).addClass('json-document'); |
117 | 119 |
|
118 |
| - /* Bind click on toggle buttons */ |
| 120 | + // Bind click on toggle buttons |
119 | 121 | $(this).off('click');
|
120 | 122 | $(this).on('click', 'a.json-toggle', function() {
|
121 | 123 | var target = $(this).toggleClass('collapsed').siblings('ul.json-dict, ol.json-array');
|
|
130 | 132 | return false;
|
131 | 133 | });
|
132 | 134 |
|
133 |
| - /* Simulate click on toggle button when placeholder is clicked */ |
| 135 | + // Simulate click on toggle button when placeholder is clicked |
134 | 136 | $(this).on('click', 'a.json-placeholder', function() {
|
135 | 137 | $(this).siblings('a.json-toggle').click();
|
136 | 138 | return false;
|
137 | 139 | });
|
138 | 140 |
|
139 | 141 | if (options.collapsed == true) {
|
140 |
| - /* Trigger click to collapse all nodes */ |
| 142 | + // Trigger click to collapse all nodes |
141 | 143 | $(this).find('a.json-toggle').click();
|
142 | 144 | }
|
143 | 145 | });
|
|
0 commit comments