forked from josdejong/jsoneditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
148 lines (122 loc) · 4.03 KB
/
index.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
<!DOCTYPE HTML>
<html>
<head>
<title>JSONEditor | Load and save</title>
<link href="dist/jsoneditor.css" rel="stylesheet" type="text/css">
<script src="dist/jsoneditor.js"></script>
<script src="data/schema.js"></script>
<script src="data/templates.js"></script>
<script src="https://bgrins.github.io/filereader.js/filereader.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2014-11-29/FileSaver.min.js"></script>
<style>
html, body {
font: 11pt sans-serif;
}
#jsoneditor {
width: 100%;
height: 100%;
}
.localStorageLinks{
color: blue;
cursor: pointer;
margin: 5px;
}
</style>
</head>
<body>
<span>Local Storage: </span><span id="localStorageFiles"></span>
<p>
<input type="button" id="saveToLocalStorage" value="Save to local storage" style="display: none"/>
</p>
<p>
<input type="file" id="loadDocument" value="Load"/>
<input type="button" id="saveDocument" value="Download JSON" />
</p>
<div id="jsoneditor"></div>
<script>
var skipedEvents = ['mousedown', 'mouseout', 'mousein', 'mouseover']
var tempVal;
const options = {
mode: 'tree',
modes: ['code', 'form', 'text', 'tree', 'view', 'preview'], // allowed modes
schema: schema,
templates: templates,
onError: function (err) {
alert(err.toString())
},
onModeChange: function (newMode, oldMode) {
console.log('Mode switched from', oldMode, 'to', newMode)
},
onEvent: function(node, event) {
if (event.type === 'focus') {
tempVal = node.value;
// console.log("EVENT >>>>>>", event.type, node);
}
if (event.type === 'blur') {
if(node.value !== tempVal){
console.log("changed from", tempVal, 'to', node.value)
document.getElementById('saveToLocalStorage').disabled = false;
document.getElementById('saveToLocalStorage').value = 'Save to local storage';
}else{
// console.log("NOT CHANGED")
}
// console.log("EVENT >>>>>>", event.type, node);
}
}
}
// create the editor
const editor = new JSONEditor(document.getElementById('jsoneditor'), options)
// Load a JSON document
FileReaderJS.setupInput(document.getElementById('loadDocument'), {
readAsDefault: 'Text',
on: {
load: function (event, file) {
editor.setText(event.target.result)
}
}
})
// Save a JSON document
document.getElementById('saveDocument').onclick = function () {
// Save Dialog
let fname = window.prompt("Save as...")
// Check json extension in file name
if (fname.indexOf(".") === -1) {
fname = fname + ".json"
} else {
if (fname.split('.').pop().toLowerCase() === "json") {
// Nothing to do
} else {
fname = fname.split('.')[0] + ".json"
}
}
const blob = new Blob([editor.getText()], {type: 'application/json;charset=utf-8'})
saveAs(blob, fname)
}
function showLocalStorageContent() {
var localStorageJSONs = [];
Object.keys(localStorage).forEach((key)=>{
localStorageJSONs.push("<span class=\"localStorageLinks\" onclick=\"openFromLocalStorage('"+key+"')\">"+key+"</span>");
})
document.getElementById('localStorageFiles').innerHTML = localStorageJSONs.join(' ');
}
showLocalStorageContent();
function openFromLocalStorage(key) {
editor.set(JSON.parse(localStorage[key]));
document.getElementById('saveToLocalStorage').style.display = 'block';
}
document.getElementById('saveToLocalStorage').onclick = function () {
localStorage[editor.get().patient.name] = editor.getText();
}
setInterval(()=>{
let aux = editor.get();
if (aux.patient && aux.patient.name) {
document.getElementById('saveToLocalStorage').style.display = 'block';
document.getElementById('saveToLocalStorage').click();
document.getElementById('saveToLocalStorage').disabled = true;
document.getElementById('saveToLocalStorage').value = 'Saved';
console.log('saved');
}
}, 5000);
</script>
</body>
</html>