]>
Commit | Line | Data |
---|---|---|
b18ac5f8 DM |
1 | Ext.require([ |
2 | 'Ext.tree.*', | |
3 | 'Ext.data.*', | |
4 | 'Ext.window.MessageBox' | |
5 | ]); | |
6 | ||
7 | // avoid errors when running without development tools | |
8 | if (!Ext.isDefined(Ext.global.console)) { | |
9 | var console = { | |
10 | dir: function() {}, | |
11 | log: function() {} | |
12 | }; | |
13 | } | |
14 | ||
15 | Ext.onReady(function() { | |
16 | ||
17 | Ext.define('pve-param-schema', { | |
18 | extend: 'Ext.data.Model', | |
19 | fields: [ | |
20 | 'name', 'type', 'typetext', 'description', 'enum', | |
21 | 'minimum', 'maximum', 'minLength', 'maxLength', | |
22 | 'pattern', 'title', 'requires', 'format', 'default', | |
23 | 'disallow', 'extends', 'links', | |
24 | { | |
25 | name: 'optional', | |
26 | type: 'boolean' | |
27 | } | |
28 | ] | |
29 | }); | |
30 | ||
31 | var store = Ext.create('Ext.data.TreeStore', { | |
32 | model: Ext.define('pve-api-doc', { | |
33 | extend: 'Ext.data.Model', | |
34 | fields: [ | |
35 | 'path', 'info', 'text', | |
36 | ] | |
37 | }), | |
38 | proxy: { | |
39 | type: 'memory', | |
40 | data: pveapi | |
41 | }, | |
42 | sorters: [{ | |
43 | property: 'leaf', | |
44 | direction: 'ASC' | |
45 | }, { | |
46 | property: 'text', | |
47 | direction: 'ASC' | |
48 | }] | |
49 | }); | |
50 | ||
51 | var render_text = function(value, metaData, record) { | |
52 | var pdef = record.data; | |
53 | ||
54 | metaData.style = 'white-space:pre-wrap;' | |
55 | ||
56 | return value; | |
57 | }; | |
58 | ||
59 | var render_type = function(value, metaData, record) { | |
60 | var pdef = record.data; | |
61 | ||
62 | return pdef['enum'] ? 'enum' : (pdef.type || 'string'); | |
63 | }; | |
64 | ||
65 | var render_format = function(value, metaData, record) { | |
66 | var pdef = record.data; | |
67 | ||
68 | metaData.style = 'white-space:normal;' | |
69 | ||
70 | if (pdef.typetext) | |
71 | return pdef.typetext; | |
72 | ||
73 | if (pdef['enum']) | |
74 | return pdef['enum'].join(' | '); | |
75 | ||
76 | if (pdef.format) | |
77 | return pdef.format; | |
78 | ||
79 | if (pdef.pattern) | |
80 | return pdef.pattern; | |
81 | ||
82 | return ''; | |
83 | }; | |
84 | ||
85 | var render_docu = function(data) { | |
86 | var md = data.info; | |
87 | ||
88 | console.log(data); | |
89 | ||
90 | var items = []; | |
91 | ||
92 | Ext.Array.each(['GET', 'POST', 'PUT', 'DELETE'], function(method) { | |
93 | var info = md[method]; | |
94 | if (info) { | |
95 | ||
96 | var sections = [ | |
97 | { | |
98 | title: 'Description', | |
99 | html: info.description, | |
100 | bodyPadding: 10 | |
101 | } | |
102 | ]; | |
103 | ||
104 | if (info.parameters && info.parameters.properties) { | |
105 | ||
106 | var pstore = Ext.create('Ext.data.Store', { | |
107 | model: 'pve-param-schema', | |
108 | proxy: { | |
109 | type: 'memory' | |
110 | }, | |
111 | groupField: 'optional', | |
112 | sorters: [ | |
113 | { | |
114 | property: 'name', | |
115 | direction: 'ASC' | |
116 | } | |
117 | ] | |
118 | }); | |
119 | ||
120 | Ext.Object.each(info.parameters.properties, function(name, pdef) { | |
121 | pdef.name = name; | |
122 | pstore.add(pdef); | |
123 | }); | |
124 | ||
125 | pstore.sort(); | |
126 | ||
127 | var groupingFeature = Ext.create('Ext.grid.feature.Grouping',{ | |
128 | enableGroupingMenu: false, | |
129 | groupHeaderTpl: '<tpl if="name">Optional</tpl><tpl if="!name">Required</tpl>' | |
130 | }); | |
131 | ||
132 | sections.push({ | |
133 | xtype: 'gridpanel', | |
134 | title: 'Parameters', | |
135 | features: [groupingFeature], | |
136 | store: pstore, | |
137 | viewConfig: { | |
138 | trackOver: false, | |
139 | stripeRows: true | |
140 | }, | |
141 | columns: [ | |
142 | { | |
143 | header: 'Name', | |
144 | dataIndex: 'name' | |
145 | }, | |
146 | { | |
147 | header: 'Type', | |
148 | dataIndex: 'type', | |
149 | renderer: render_type, | |
150 | }, | |
151 | { | |
152 | header: 'Format', | |
153 | dataIndex: 'type', | |
154 | renderer: render_format, | |
155 | flex: 1 | |
156 | }, | |
157 | { | |
158 | header: 'Description', | |
159 | dataIndex: 'description', | |
160 | renderer: render_text, | |
161 | flex: 2 | |
162 | } | |
163 | ] | |
164 | }); | |
165 | ||
166 | } | |
167 | ||
168 | if (info.returns) { | |
169 | ||
170 | var rtype = info.returns.type; | |
171 | if (!rtype && info.returns.items) | |
172 | rtype = 'array'; | |
173 | if (!rtype) | |
174 | rtype = 'object'; | |
175 | ||
176 | sections.push({ | |
177 | title: 'Returns: ' + rtype | |
178 | }); | |
179 | } | |
180 | ||
181 | var permhtml = ''; | |
182 | if (!info.permissions) { | |
183 | permhtml = "Root only."; | |
184 | } else if (info.permissions.user) { | |
185 | if (info.permissions.user === 'world') { | |
186 | permhtml += "Accessible without any authententification."; | |
187 | } else if (info.permissions.user === 'all') { | |
188 | permhtml += "Accessible by all authententicated users."; | |
189 | } else { | |
190 | permhtml += 'Onyl accessible by user "' + | |
191 | info.permissions.user + '"'; | |
192 | } | |
193 | } else if (info.permissions.path && info.permissions.privs) { | |
194 | permhtml += '<table><tr><td>Path:</td><td>' + | |
195 | info.permissions.path + '</td></tr>' + | |
196 | '<tr><td style="padding-right:10px;">Permissions:</td><td>' + | |
197 | info.permissions.privs.join(' ') + | |
198 | '</td></tr></table>'; | |
199 | } else { | |
200 | permhtml += "Root only."; | |
201 | } | |
202 | ||
203 | sections.push({ | |
204 | title: 'Required permissions', | |
205 | bodyPadding: 10, | |
206 | html: permhtml | |
207 | }); | |
208 | ||
209 | ||
210 | items.push({ | |
211 | title: method, | |
212 | autoScroll: true, | |
213 | defaults: { | |
214 | border: false | |
215 | }, | |
216 | items: sections | |
217 | }); | |
218 | } | |
219 | }); | |
220 | ||
221 | var ct = Ext.getCmp('docview'); | |
222 | ct.setTitle("Path: " + data.path); | |
223 | ct.removeAll(true); | |
224 | ct.add(items); | |
225 | }; | |
226 | ||
227 | var tree = Ext.create('Ext.tree.Panel', { | |
228 | title: 'Resource Tree', | |
229 | store: store, | |
230 | width: 200, | |
231 | region: 'west', | |
232 | split: true, | |
233 | margins: '5 0 5 5', | |
234 | rootVisible: false, | |
235 | listeners: { | |
236 | selectionchange: function(v, selections) { | |
237 | if (!selections[0]) | |
238 | return; | |
239 | var rec = selections[0]; | |
240 | render_docu(rec.data); | |
241 | } | |
242 | } | |
243 | }); | |
244 | ||
245 | Ext.create('Ext.container.Viewport', { | |
246 | layout: 'border', | |
247 | renderTo: Ext.getBody(), | |
248 | items: [ | |
249 | tree, | |
250 | { | |
251 | xtype: 'tabpanel', | |
252 | title: 'Documentation', | |
253 | id: 'docview', | |
254 | region: 'center', | |
255 | margins: '5 5 5 0', | |
256 | layout: 'fit', | |
257 | items: [] | |
258 | } | |
259 | ] | |
260 | }); | |
261 | ||
262 | }); |