-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripto.js
194 lines (168 loc) · 5.16 KB
/
scripto.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
$(document).ready(function(){
formatEventBind();
removeWhiteSpacesEventBind();
initRenameTab();
showJSONText(true);
viewJsonEventBind();
aboutEventBind();
});
function formatEventBind() {
$('.format').on('click', function(){
showJSONText(true);
var txt = $('#txt-json-expected').val();
if(txt == '') {
alert('Empty text not fotmatted');
return;
}
// preserve newlines, etc - use valid JSON
var s = txt.replace(/\\n/g, "\\n")
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f");
// remove non-printable and other non-valid JSON chars
s = s.replace(/[\u0000-\u0019]+/g,"");
try {
var obj = JSON.parse(s);
var jsonF = JSON.stringify(obj, undefined, 2);
$('#txt-json-expected').val(jsonF);
} catch(e) {
showErrorMessage('Unable to parse input data.');
}
});
}
function viewJsonEventBind() {
$('.viewer').on('click', function(){
var data = $('#txt-json-expected').val();
var s = data.replace(/\\n/g, "\\n")
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f");
// remove non-printable and other non-valid JSON chars
s = s.replace(/[\u0000-\u0019]+/g,"");
try{
var obj = JSON.parse(s);
displayJSON(obj);
}catch(e) {
showErrorMessage('Unable to parse input data.');
}
});
}
function removeWhiteSpacesEventBind() {
$('.compact').on('click', function(){
showJSONText(true);
var txt = $('#txt-json-expected').val();
if(txt == '') {
alert('Empty text not fotmatted');
return;
}
try {
var obj = JSON.parse(txt);
var jsonF = JSON.stringify(obj);
$('#txt-json-expected').val(jsonF);
} catch(e) {
showErrorMessage('Unable to parse input data.');
}
});
}
function aboutEventBind() {
$('.about').on('click', function() {
aboutDialog.dialog('open');
});
}
function displayJSON(input) {
//document.body
$('#view-container').html('');
document.getElementById("view-container").appendChild(
renderjson(input)
);
$('#txt-json-expected').hide();
$('#view-container').show();
}
function syntaxHighlight(json) {
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
function usingWraper(input) {
var wrapper = document.getElementById("output-view");
// Get json-data by javascript-object
var data = {
"firstName": "Jonh",
"lastName": "Smith",
"phones": [
"123-45-67",
"987-65-43"
]
};
// or from a string by JSON.parse(str) method
var dataStr = '{ "firstName": "Jonh", "lastName": "Smith", "phones": ["123-45-67", "987-65-43"]}';
try {
var data = JSON.parse(dataStr);
} catch (e) {}
// Create json-tree
var tree = jsonTree.create(data, wrapper);
// Expand all (or selected) child nodes of root (optional)
tree.expand(function(node) {
return node.childNodes.length < 2 || node.label === 'phoneNumbers';
});
$('#txt-json-expected').hide();
$('#output-view').show();
}
function initRenameTab() {
$('.rename-tab').on('click', function(){
var tID = w2ui.tabs.active;
var tName = XOXO_APP.getTabName(tID);
$('.txt-tab-name input').val(tName);
$(this).hide();
$('.txt-tab-name').show();
});
$('.txt-tab-name').on('focusout', function(){
$('.rename-tab').show();
$(this).hide();
var tID = w2ui.tabs.active;
var nName = $('.txt-tab-name input').val();
XOXO_APP.setTabName(tID, nName);
for(let a of w2ui.tabs.tabs) {
if(a.id == tID) {
a.text = nName;
}
}
w2ui.tabs.refresh()
});
}
function showJSONText(value) {
if(value) {
$('#view-container').hide();
$('#txt-json-expected').show();
} else {
$('#view-container').show();
$('#txt-json-expected').hide();
}
}
function showErrorMessage(message) {
$("#error-div").html(message);
$("#error-div").fadeIn('slow', function() {
setTimeout(function() { $("#error-div").fadeOut('slow') }, 3000);
});
}