-
Notifications
You must be signed in to change notification settings - Fork 14
/
try.html
172 lines (160 loc) · 4.66 KB
/
try.html
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
<html>
<head>
<title>Edit with Jsonary</title>
<script src="../tv4.js"></script>
<style type="text/css" media="screen">
#page-table {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
#editor-data-title, #editor-schema-title {
font-size: 16px;
font-weight: bold;
font-family: Verdana;
text-align: center;
border-bottom: 2px solid black;
height: 25px;
}
#editor-data-title {
left: 0;
width: 50%;
background-color: #CDE;
border-right: 1px solid black;
}
#editor-schema-title {
right: 0;
width: 50%;
background-color: #EDC;
border-left: 1px solid black;
}
#editor-data, #jsonary-data {
width: 100%;
height: 100%;
border-right: 1px solid black;
}
#editor-schema {
width: 100%;
height: 100%;
border-left: 1px solid black;
}
#save-button {
position: absolute;
left: 40%;
right: 40%;
}
</style>
</head>
<body>
<table id="page-table" cellspacing=0 cellpadding=0>
<thead>
<tr>
<td id="editor-data-title">
data
<input type="button" id="data-view-switch" value="switch view"></input>
<input id="save-button" type="button" value="save to URL" onclick="saveToFragment()"></input>
</td>
<td id="editor-schema-title">schema</td>
</tr>
</thead>
<tbody>
<tr>
<td style="position: relative">
<div id="editor-data">{
"foo": "bar"
}</div>
<div id="jsonary-data">Jsonary</div>
</td>
<td style="position: relative">
<div id="editor-schema">{
"type": "object",
"properties": {
"foo": {
"type": "string"
}
},
"required": ["foo"]
}</div>
</td>
</tr>
</tbody>
</table>
<script src="express-site/public/js/ace/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="node-package/super-bundle/jsonary-super-bundle.js" type="text/javascript" charset="utf-8"></script>
<script>
var editorData = ace.edit("editor-data");
editorData.setTheme("ace/theme/dawn");
editorData.getSession().setMode("ace/mode/json");
var editorSchema = ace.edit("editor-schema");
editorSchema.setTheme("ace/theme/dawn");
editorSchema.getSession().setMode("ace/mode/json");
var hash = (window.location.href.indexOf('#') == -1) ? '#' : window.location.href.substring(window.location.href.indexOf('#'));
console.log(hash);
if (hash.charAt(1) == "?") {
var parts = hash.substring(2).split("&");
for (var i = 0; i < parts.length; i++) {
var pair = parts[i].split('=');
var key = pair[0];
var value = decodeURIComponent(pair.length ? parts[i].substring(key.length + 1) : "");
if (key == "data") {
editorData.getSession().setValue(value);
} else if (key == "schema") {
editorSchema.getSession().setValue(value);
}
}
}
var editorElement = document.getElementById('editor-data');
var jsonaryElement = document.getElementById('jsonary-data');
jsonaryElement.style.display = 'none';
var jsonaryView = false;
var jsonaryData = Jsonary.create(null);
function swapViews() {
if (!jsonaryView) {
var dataJson = editorData.getSession().getValue();
try {
var data = JSON.parse(dataJson);
} catch (e) {
return alert("Error parsing data JSON:\n" + e);
}
jsonaryData.setValue(data);
Jsonary.render(jsonaryElement, jsonaryData);
jsonaryElement.style.display = 'block';
editorElement.style.display = 'none';
jsonaryView = true;
} else {
var value = jsonaryData.value();
editorData.getSession().setValue(JSON.stringify(value, null, '\t'));
jsonaryElement.style.display = 'none';
editorElement.style.display = 'block';
jsonaryView = false;
}
}
var schemaKey = "custom-schema";
function updateSchema() {
var schemaJson = editorSchema.getSession().getValue();
try {
var schemaValue = JSON.parse(schemaJson);
} catch (e) {
console.log("Error parsing data JSON:\n" + e);
}
var schema = Jsonary.createSchema(schemaValue);
jsonaryData.removeSchema(schemaKey);
jsonaryData.addSchema(schema, schemaKey);
}
editorSchema.getSession().on('change', updateSchema, 1000);
updateSchema();
document.getElementById('data-view-switch').onclick = swapViews;
swapViews();
function saveToFragment() {
var dataJson = jsonaryView ? jsonaryData.json() : editorData.getSession().getValue();
var schemaJson = editorSchema.getSession().getValue();
var hash = "#?data=" + encodeURIComponent(dataJson) + "&schema=" + encodeURIComponent(schemaJson);
window.location.href = hash;
} </script>
</body>
</html>