/
index.html
146 lines (126 loc) · 4.93 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>
<meta charset="utf-8" />
<title>JINGdigital Api Doc Editor</title>
<!-- Foundation CSS framework (Bootstrap and jQueryUI also supported) -->
<link rel='stylesheet' href='//cdn.jsdelivr.net/foundation/5.0.2/css/foundation.min.css'>
<!-- Font Awesome icons (Bootstrap, Foundation, and jQueryUI also supported) -->
<link rel='stylesheet' href='//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.css'>
</head>
<body>
<div class='row'>
<div class='medium-12 columns'>
<h1>JINGdigital Api Doc Editor</h1>
</div>
</div>
<div class='row'>
<div class='medium-12 columns'>
<p>
<b>Guide</b>:<br />
1. Click "Generate" button to generate api doc json.<br />
2. Click "Edit" button to edit api doc json using forms.<br />
3. Click "Properties" button to add item (e.g. api、header、param、field etc.) .<br />
</p>
</div>
</div>
<div class='row'>
<div class='medium-12-columns'>
<button id='submit' class='tiny'>Generate</button>
<button id='edit' class='tiny'>Edit</button>
<button id="copy" class='tiny' data-clipboard-target="#output">Copy to clipboard</button>
<button id='restore' class='secondary tiny'>Restore to Default</button>
<!-- Trigger -->
<span id='valid_indicator' class='label'></span>
</div>
<textarea id='output' style="width: 100%; height: 300px; font-family: monospace;" class='medium-12 columns'></textarea>
</div>
<div class='row'>
<div id='editor_holder' class='medium-12 columns'></div>
</div>
<footer style="text-align: center">Powered by <a href="https://github.com/jdorn/json-editor" target="_blank">json-editor</a>.</footer>
<!-- Json Editor -->
<script src="./dist/jsoneditor.min.js"></script>
<!-- Clipboard Js -->
<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
// Set the default CSS theme and icon library globally
JSONEditor.defaults.theme = 'foundation5';
JSONEditor.defaults.iconlib = 'fontawesome4';
</script>
<script>
// Initialize ClipboardJs
new ClipboardJS('#copy');
// This is the starting value for the editor
// We will use this to seed the initial editor
// and to provide a "Restore to Default" button.
var cached_json_str = localStorage.getItem('apidoc-json');
var default_value = {
"api": [
{
"url": "",
"type": "get",
"header": {
"fields": {
"Header": []
}
}
}
]
};
var starting_value = cached_json_str ? JSON.parse(cached_json_str) : default_value;
// Initialize the editor
var editor = new JSONEditor(document.getElementById('editor_holder'),{
// Enable fetching schemas via ajax
ajax: true,
display_required_only: true,
// The schema for the editor
schema: {
$ref: "apiDocSchema.json",
format: "grid"
},
// Seed the form with a starting value
startval: starting_value
});
var generateJson = function() {
// Get the value from the editor
console.log(editor.getValue());
var json_str = JSON.stringify(editor.getValue(), null, 2);
document.getElementById('output').value = json_str;
localStorage.setItem('apidoc-json', json_str);
};
// Hook up the submit button to log to the console
document.getElementById('submit').addEventListener('click', generateJson);
var editJson = function() {
editor.setValue(JSON.parse(document.getElementById('output').value));
};
document.getElementById('edit').addEventListener('click',editJson);
document.getElementById('output').addEventListener('change',editJson);
// Hook up the Restore to Default button
document.getElementById('restore').addEventListener('click',function() {
editor.setValue(default_value);
//Editor change event can not be triggered
var json_str = JSON.stringify(default_value, null, 2);
document.getElementById('output').value = json_str;
});
// Hook up the validation indicator to update its
// status whenever the editor changes
editor.on('change',function() {
// Get an array of errors from the validator
var errors = editor.validate();
var indicator = document.getElementById('valid_indicator');
// Not valid
if(errors.length) {
indicator.className = 'label alert';
indicator.textContent = 'not valid';
}
// Valid
else {
indicator.className = 'label success';
indicator.textContent = 'valid';
generateJson();
}
});
</script>
</body>
</html>