forked from josdejong/jsoneditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.js
283 lines (247 loc) · 9.02 KB
/
interface.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* @file interface.js
*
* @brief
* JsonEditor is an editor to display and edit JSON data in a treeview.
*
* Supported browsers: Chrome, Firefox, Safari, Opera, Internet Explorer 8+
*
* @license
* This json editor is open sourced with the intention to use the editor as
* a component in your own application. Not to just copy and monetize the editor
* as it is.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* Copyright (C) 2011-2012 Jos de Jong, http://jsoneditoronline.org
*
* @author Jos de Jong, <[email protected]>
* @date 2012-05-30
*/
var editor = null;
var formatter = null;
var main = {};
main.formatterToEditor = function() {
try {
editor.set(formatter.get());
}
catch (err) {
main.showError(err);
}
};
main.editorToFormatter = function () {
try {
formatter.set(editor.get());
}
catch (err) {
main.showError(err);
}
};
main.eventParams = {};
main.onMouseDown = function (event) {
var leftButtonDown = event.which ? (event.which == 1) : (event.button == 1);
if (!leftButtonDown) {
return;
}
if (!main.eventParams.mousedown) {
main.eventParams.mousedown = true;
main.eventParams.mousemove =
JSONEditor.Events.addEventListener(document, 'mousemove', main.onMouseMove);
main.eventParams.mouseup =
JSONEditor.Events.addEventListener(document, 'mouseup', main.onMouseUp);
main.eventParams.screenX = event.screenX;
main.eventParams.splitterFraction = main.splitterFraction;
}
JSONEditor.Events.preventDefault(event);
};
main.onMouseMove = function (event) {
var width = (window.innerWidth || document.body.offsetWidth ||
document.documentElement.offsetWidth);
var diff = event.screenX - main.eventParams.screenX;
main.splitterFraction = main.eventParams.splitterFraction + diff / width;
if (main.splitterFraction < 0.1) {
main.splitterFraction = 0.1;
}
if (main.splitterFraction > 0.9) {
main.splitterFraction = 0.9;
}
main.resize();
JSONEditor.Events.preventDefault(event);
};
main.onMouseUp = function (event) {
if (main.eventParams.mousedown) {
JSONEditor.Events.removeEventListener(document, 'mousemove', main.eventParams.mousemove);
JSONEditor.Events.removeEventListener(document, 'mouseup', main.eventParams.mouseup);
main.eventParams.mousemove = undefined;
main.eventParams.mouseup = undefined;
main.eventParams.mousedown = false;
}
JSONEditor.Events.preventDefault(event);
};
main.load = function() {
var json = {
"Name": "John Smith",
"Age": 32,
"Employed": true,
"Address": {
"Street": "701 First Ave.",
"City": "Sunnyvale, CA 95125",
"Country": "United States"
},
"Children": [
{
"Name": "Richard",
"Age": 7
},
{
"Name": "Susan",
"Age": 4
},
{
"Name": "James",
"Age": 3
}
]
};
try {
// formatter
var container = document.getElementById("jsonformatter");
formatter = new JSONFormatter(container);
formatter.set(json);
formatter.onError = function (err) {
main.showError(err);
};
// editor
container = document.getElementById("jsoneditor");
editor = new JSONEditor(container);
editor.set(json);
// splitter
var domSplitter = document.getElementById('splitter');
domSplitter.innerHTML =
'<br><br><br>' +
'<button id="toForm" onclick="main.formatterToEditor();" title="JSON to Editor" class="convert"> <img src="img/treeRightTriangleBlack.png" style="width:8px; height:8px; margin-left:2px;"></button><br>' +
'<br>' +
'<button id="toJSON" onclick="main.editorToFormatter();" title="Editor to JSON" class="convert"><img src="img/treeLeftTriangleBlack.png" style="width:8px; height:8px; margin-right:2px;"></button>';
JSONEditor.Events.addEventListener(domSplitter, "mousedown", main.onMouseDown);
// resize
JSONEditor.Events.addEventListener(window, 'resize', main.resize);
// TODO: implement a focus method
formatter.textarea.focus();
// TODO: a nicer method to check for changes
var formatterLastContent;
var editorLastContent;
function checkChange () {
try {
// check for change in formatter
var formatterJSON = formatter.get();
var formatterContent = JSON.stringify(formatterJSON);
if (formatterContent != formatterLastContent) {
formatterLastContent = formatterContent;
editorLastContent = formatterContent;
editor.set(formatterJSON);
}
else {
// check for change in editor
var editorJSON = editor.get();
var editorContent = JSON.stringify(editorJSON);
if (editorContent != editorLastContent) {
editorLastContent = editorContent;
formatterLastContent = editorContent;
formatter.set(editorJSON);
}
}
}
catch (err) {
main.showError(err);
}
setTimeout(checkChange, 1000);
}
/* TODO: use checkChange
checkChange();
*/
} catch (err) {
var msg = err.message || err;
main.showError('Error: ' + msg);
}
};
main.splitterFraction = 0.5;
main.resize = function() {
var domEditor = document.getElementById('jsoneditor');
var domFormatter = document.getElementById('jsonformatter');
var domSplitter = document.getElementById('splitter');
var domAd = document.getElementById('ad');
var width = window.innerWidth || document.body.offsetWidth || document.documentElement.offsetWidth;
var height = window.innerHeight || document.body.offsetHeight || document.documentElement.offsetHeight;
var adWidth = domAd ? domAd.clientWidth : 0;
var splitterWidth = domSplitter.clientWidth;
if (adWidth) {
width -= (adWidth + 15); // Not so nice, +15 here for the margin
}
var splitterLeft = width * main.splitterFraction;
// resize formatter
domFormatter.style.width = (splitterLeft) + 'px';
// resize editor
domEditor.style.left = (splitterLeft + splitterWidth) + 'px';
domEditor.style.width = (width - splitterLeft - splitterWidth) + 'px';
//editor.onResize(); // TODO
};
main.errorFrame = undefined;
main.showError = function (message) {
if (!main.errorFrame) {
var width = 500;
var top = 5;
var windowWidth = document.body.offsetWidth || window.innerWidth;
main.errorFrame = document.createElement('div');
main.errorFrame.style.position = 'absolute';
main.errorFrame.style.left = (windowWidth - width) / 2 + 'px';
main.errorFrame.style.width = width + 'px';
main.errorFrame.style.top = top + 'px';
document.body.appendChild(main.errorFrame);
}
var error = document.createElement('div');
error.className = 'error';
error.style.position = 'relative';
main.errorFrame.appendChild(error);
var table = document.createElement('table');
table.style.width = '100%';
error.appendChild(table);
var tbody = document.createElement('tbody');
table.appendChild(tbody);
var tr = document.createElement('tr');
tbody.appendChild(tr);
var tdMessage = document.createElement('td');
tdMessage.innerHTML = message;
tr.appendChild(tdMessage);
var tdClose = document.createElement('td');
tdClose.style.textAlign = 'right';
tdClose.style.verticalAlign = 'top';
tr.appendChild(tdClose);
var closeDiv = document.createElement('button');
closeDiv.innerHTML = '×';
closeDiv.title = 'Close error message';
tdClose.appendChild(closeDiv);
closeDiv.onclick = function () {
if (error.parentNode) {
error.parentNode.removeChild(error);
}
if (main.errorFrame.childNodes.length == 0) {
main.errorFrame.parentNode.removeChild(main.errorFrame);
main.errorFrame = undefined;
}
}
};
main.hideAds = function() {
var domAd = document.getElementById("ad");
domAd.parentNode.removeChild(domAd);
main.resize();
};