]>
Commit | Line | Data |
---|---|---|
8556628c TL |
1 | /*global apiSchema*/ |
2 | ||
4ae75df3 | 3 | Ext.onReady(function() { |
4ae75df3 DC |
4 | Ext.define('pmx-param-schema', { |
5 | extend: 'Ext.data.Model', | |
52428d60 | 6 | fields: [ |
4ae75df3 DC |
7 | 'name', 'type', 'typetext', 'description', 'verbose_description', |
8 | 'enum', 'minimum', 'maximum', 'minLength', 'maxLength', | |
9 | 'pattern', 'title', 'requires', 'format', 'default', | |
10 | 'disallow', 'extends', 'links', | |
11 | { | |
12 | name: 'optional', | |
52428d60 TL |
13 | type: 'boolean', |
14 | }, | |
15 | ], | |
4ae75df3 DC |
16 | }); |
17 | ||
52428d60 | 18 | let store = Ext.define('pmx-updated-treestore', { |
4ae75df3 DC |
19 | extend: 'Ext.data.TreeStore', |
20 | model: Ext.define('pmx-api-doc', { | |
21 | extend: 'Ext.data.Model', | |
52428d60 | 22 | fields: [ |
4ae75df3 | 23 | 'path', 'info', 'text', |
52428d60 | 24 | ], |
4ae75df3 | 25 | }), |
52428d60 TL |
26 | proxy: { |
27 | type: 'memory', | |
8556628c | 28 | data: apiSchema, |
52428d60 TL |
29 | }, |
30 | sorters: [{ | |
31 | property: 'leaf', | |
32 | direction: 'ASC', | |
33 | }, { | |
34 | property: 'text', | |
35 | direction: 'ASC', | |
4ae75df3 DC |
36 | }], |
37 | filterer: 'bottomup', | |
38 | doFilter: function(node) { | |
39 | this.filterNodes(node, this.getFilters().getFilterFn(), true); | |
40 | }, | |
41 | ||
42 | filterNodes: function(node, filterFn, parentVisible) { | |
52428d60 | 43 | let me = this, |
4ae75df3 DC |
44 | bottomUpFiltering = me.filterer === 'bottomup', |
45 | match = filterFn(node) && parentVisible || (node.isRoot() && !me.getRootVisible()), | |
46 | childNodes = node.childNodes, | |
47 | len = childNodes && childNodes.length, i, matchingChildren; | |
48 | ||
49 | if (len) { | |
50 | for (i = 0; i < len; ++i) { | |
51 | matchingChildren = me.filterNodes(childNodes[i], filterFn, match || bottomUpFiltering) || matchingChildren; | |
52 | } | |
53 | if (bottomUpFiltering) { | |
54 | match = matchingChildren || match; | |
55 | } | |
56 | } | |
57 | ||
58 | node.set("visible", match, me._silentOptions); | |
59 | return match; | |
60 | }, | |
61 | ||
62 | }).create(); | |
63 | ||
52428d60 TL |
64 | let render_description = function(value, metaData, record) { |
65 | let pdef = record.data; | |
4ae75df3 DC |
66 | |
67 | value = pdef.verbose_description || value; | |
68 | ||
69 | // TODO: try to render asciidoc correctly | |
70 | ||
52428d60 | 71 | metaData.style = 'white-space:pre-wrap;'; |
4ae75df3 DC |
72 | |
73 | return Ext.htmlEncode(value); | |
74 | }; | |
75 | ||
52428d60 TL |
76 | let render_type = function(value, metaData, record) { |
77 | let pdef = record.data; | |
4ae75df3 | 78 | |
52428d60 | 79 | return pdef.enum ? 'enum' : pdef.type || 'string'; |
4ae75df3 DC |
80 | }; |
81 | ||
82 | let render_simple_format = function(pdef, type_fallback) { | |
52428d60 | 83 | if (pdef.typetext) {return pdef.typetext;} |
4ae75df3 | 84 | |
52428d60 | 85 | if (pdef.enum) {return pdef.enum.join(' | ');} |
4ae75df3 | 86 | |
52428d60 | 87 | if (pdef.format) {return pdef.format;} |
4ae75df3 | 88 | |
52428d60 | 89 | if (pdef.pattern) {return pdef.pattern;} |
4ae75df3 | 90 | |
52428d60 | 91 | if (pdef.type === 'boolean') {return `<true|false>`;} |
4ae75df3 | 92 | |
52428d60 | 93 | if (type_fallback && pdef.type) {return `<${pdef.type}>`;} |
4ae75df3 DC |
94 | }; |
95 | ||
96 | let render_format = function(value, metaData, record) { | |
97 | let pdef = record.data; | |
98 | ||
52428d60 | 99 | metaData.style = 'white-space:normal;'; |
4ae75df3 DC |
100 | |
101 | if (pdef.type === 'array' && pdef.items) { | |
102 | let format = render_simple_format(pdef.items, true); | |
103 | return `[${Ext.htmlEncode(format)}, ...]`; | |
104 | } | |
105 | ||
106 | return Ext.htmlEncode(render_simple_format(pdef) || ''); | |
107 | }; | |
108 | ||
52428d60 | 109 | let real_path = function(path) { |
4ae75df3 DC |
110 | return path.replace(/^.*\/_upgrade_(\/)?/, "/"); |
111 | }; | |
112 | ||
52428d60 | 113 | let permission_text = function(permission) { |
4ae75df3 DC |
114 | let permhtml = ""; |
115 | ||
116 | if (permission.user) { | |
117 | if (!permission.description) { | |
118 | if (permission.user === 'world') { | |
119 | permhtml += "Accessible without any authentication."; | |
120 | } else if (permission.user === 'all') { | |
121 | permhtml += "Accessible by all authenticated users."; | |
122 | } else { | |
123 | permhtml += 'Onyl accessible by user "' + | |
124 | permission.user + '"'; | |
125 | } | |
126 | } | |
127 | } else if (permission.check) { | |
128 | permhtml += "<pre>Check: " + | |
52428d60 | 129 | Ext.htmlEncode(Ext.JSON.encode(permission.check)) + "</pre>"; |
4ae75df3 DC |
130 | } else if (permission.userParam) { |
131 | permhtml += `<div>Check if user matches parameter '${permission.userParam}'`; | |
132 | } else if (permission.or) { | |
133 | permhtml += "<div>Or<div style='padding-left: 10px;'>"; | |
134 | Ext.Array.each(permission.or, function(sub_permission) { | |
135 | permhtml += permission_text(sub_permission); | |
52428d60 | 136 | }); |
4ae75df3 DC |
137 | permhtml += "</div></div>"; |
138 | } else if (permission.and) { | |
139 | permhtml += "<div>And<div style='padding-left: 10px;'>"; | |
140 | Ext.Array.each(permission.and, function(sub_permission) { | |
141 | permhtml += permission_text(sub_permission); | |
52428d60 | 142 | }); |
4ae75df3 DC |
143 | permhtml += "</div></div>"; |
144 | } else { | |
145 | //console.log(permission); | |
146 | permhtml += "Unknown syntax!"; | |
147 | } | |
148 | ||
149 | return permhtml; | |
150 | }; | |
151 | ||
52428d60 TL |
152 | let render_docu = function(data) { |
153 | let md = data.info; | |
4ae75df3 | 154 | |
52428d60 | 155 | let items = []; |
4ae75df3 | 156 | |
4ae75df3 | 157 | Ext.Array.each(['GET', 'POST', 'PUT', 'DELETE'], function(method) { |
52428d60 | 158 | let info = md[method]; |
4ae75df3 | 159 | if (info) { |
52428d60 | 160 | let usage = ""; |
4ae75df3 DC |
161 | |
162 | usage += "<table><tr><td>HTTP: </td><td>" | |
163 | + method + " " + real_path("/api2/json" + data.path) + "</td></tr>"; | |
164 | ||
165 | if (typeof cliusage === 'function') { | |
166 | usage += cliusage(method, real_path(data.path)); | |
167 | } | |
168 | ||
52428d60 | 169 | let sections = [ |
4ae75df3 DC |
170 | { |
171 | title: 'Description', | |
172 | html: Ext.htmlEncode(info.description), | |
52428d60 | 173 | bodyPadding: 10, |
4ae75df3 DC |
174 | }, |
175 | { | |
176 | title: 'Usage', | |
177 | html: usage, | |
52428d60 TL |
178 | bodyPadding: 10, |
179 | }, | |
4ae75df3 DC |
180 | ]; |
181 | ||
182 | if (info.parameters && info.parameters.properties) { | |
52428d60 | 183 | let pstore = Ext.create('Ext.data.Store', { |
4ae75df3 DC |
184 | model: 'pmx-param-schema', |
185 | proxy: { | |
52428d60 | 186 | type: 'memory', |
4ae75df3 DC |
187 | }, |
188 | groupField: 'optional', | |
189 | sorters: [ | |
190 | { | |
191 | property: 'name', | |
52428d60 TL |
192 | direction: 'ASC', |
193 | }, | |
194 | ], | |
4ae75df3 DC |
195 | }); |
196 | ||
197 | Ext.Object.each(info.parameters.properties, function(name, pdef) { | |
198 | pdef.name = name; | |
199 | pstore.add(pdef); | |
200 | }); | |
201 | ||
202 | pstore.sort(); | |
203 | ||
52428d60 | 204 | let groupingFeature = Ext.create('Ext.grid.feature.Grouping', { |
4ae75df3 | 205 | enableGroupingMenu: false, |
52428d60 | 206 | groupHeaderTpl: '<tpl if="groupValue">Optional</tpl><tpl if="!groupValue">Required</tpl>', |
4ae75df3 DC |
207 | }); |
208 | ||
209 | sections.push({ | |
210 | xtype: 'gridpanel', | |
211 | title: 'Parameters', | |
212 | features: [groupingFeature], | |
213 | store: pstore, | |
214 | viewConfig: { | |
215 | trackOver: false, | |
52428d60 | 216 | stripeRows: true, |
4ae75df3 DC |
217 | }, |
218 | columns: [ | |
219 | { | |
220 | header: 'Name', | |
221 | dataIndex: 'name', | |
52428d60 | 222 | flex: 1, |
4ae75df3 DC |
223 | }, |
224 | { | |
225 | header: 'Type', | |
226 | dataIndex: 'type', | |
227 | renderer: render_type, | |
52428d60 | 228 | flex: 1, |
4ae75df3 DC |
229 | }, |
230 | { | |
231 | header: 'Default', | |
232 | dataIndex: 'default', | |
52428d60 | 233 | flex: 1, |
4ae75df3 DC |
234 | }, |
235 | { | |
236 | header: 'Format', | |
237 | dataIndex: 'type', | |
238 | renderer: render_format, | |
52428d60 | 239 | flex: 2, |
4ae75df3 DC |
240 | }, |
241 | { | |
242 | header: 'Description', | |
243 | dataIndex: 'description', | |
244 | renderer: render_description, | |
52428d60 TL |
245 | flex: 6, |
246 | }, | |
247 | ], | |
4ae75df3 | 248 | }); |
4ae75df3 DC |
249 | } |
250 | ||
251 | if (info.returns) { | |
52428d60 TL |
252 | let retinf = info.returns; |
253 | let rtype = retinf.type; | |
254 | if (!rtype && retinf.items) {rtype = 'array';} | |
255 | if (!rtype) {rtype = 'object';} | |
4ae75df3 | 256 | |
52428d60 | 257 | let rpstore = Ext.create('Ext.data.Store', { |
4ae75df3 DC |
258 | model: 'pmx-param-schema', |
259 | proxy: { | |
52428d60 | 260 | type: 'memory', |
4ae75df3 DC |
261 | }, |
262 | groupField: 'optional', | |
263 | sorters: [ | |
264 | { | |
265 | property: 'name', | |
52428d60 TL |
266 | direction: 'ASC', |
267 | }, | |
268 | ], | |
4ae75df3 DC |
269 | }); |
270 | ||
52428d60 | 271 | let properties; |
4ae75df3 DC |
272 | if (rtype === 'array' && retinf.items.properties) { |
273 | properties = retinf.items.properties; | |
274 | } | |
275 | ||
276 | if (rtype === 'object' && retinf.properties) { | |
277 | properties = retinf.properties; | |
278 | } | |
279 | ||
280 | Ext.Object.each(properties, function(name, pdef) { | |
281 | pdef.name = name; | |
282 | rpstore.add(pdef); | |
283 | }); | |
284 | ||
285 | rpstore.sort(); | |
286 | ||
52428d60 | 287 | let groupingFeature = Ext.create('Ext.grid.feature.Grouping', { |
4ae75df3 | 288 | enableGroupingMenu: false, |
52428d60 | 289 | groupHeaderTpl: '<tpl if="groupValue">Optional</tpl><tpl if="!groupValue">Obligatory</tpl>', |
4ae75df3 | 290 | }); |
52428d60 | 291 | let returnhtml; |
4ae75df3 DC |
292 | if (retinf.items) { |
293 | returnhtml = '<pre>items: ' + Ext.htmlEncode(JSON.stringify(retinf.items, null, 4)) + '</pre>'; | |
294 | } | |
295 | ||
296 | if (retinf.properties) { | |
297 | returnhtml = returnhtml || ''; | |
298 | returnhtml += '<pre>properties:' + Ext.htmlEncode(JSON.stringify(retinf.properties, null, 4)) + '</pre>'; | |
299 | } | |
300 | ||
52428d60 | 301 | let rawSection = Ext.create('Ext.panel.Panel', { |
4ae75df3 DC |
302 | bodyPadding: '0px 10px 10px 10px', |
303 | html: returnhtml, | |
52428d60 | 304 | hidden: true, |
4ae75df3 DC |
305 | }); |
306 | ||
307 | sections.push({ | |
308 | xtype: 'gridpanel', | |
309 | title: 'Returns: ' + rtype, | |
310 | features: [groupingFeature], | |
311 | store: rpstore, | |
312 | viewConfig: { | |
313 | trackOver: false, | |
52428d60 | 314 | stripeRows: true, |
4ae75df3 DC |
315 | }, |
316 | columns: [ | |
317 | { | |
318 | header: 'Name', | |
319 | dataIndex: 'name', | |
52428d60 | 320 | flex: 1, |
4ae75df3 DC |
321 | }, |
322 | { | |
323 | header: 'Type', | |
324 | dataIndex: 'type', | |
325 | renderer: render_type, | |
52428d60 | 326 | flex: 1, |
4ae75df3 DC |
327 | }, |
328 | { | |
329 | header: 'Default', | |
330 | dataIndex: 'default', | |
52428d60 | 331 | flex: 1, |
4ae75df3 DC |
332 | }, |
333 | { | |
334 | header: 'Format', | |
335 | dataIndex: 'type', | |
336 | renderer: render_format, | |
52428d60 | 337 | flex: 2, |
4ae75df3 DC |
338 | }, |
339 | { | |
340 | header: 'Description', | |
341 | dataIndex: 'description', | |
342 | renderer: render_description, | |
52428d60 TL |
343 | flex: 6, |
344 | }, | |
4ae75df3 DC |
345 | ], |
346 | bbar: [ | |
347 | { | |
348 | xtype: 'button', | |
349 | text: 'Show RAW', | |
350 | handler: function(btn) { | |
351 | rawSection.setVisible(!rawSection.isVisible()); | |
352 | btn.setText(rawSection.isVisible() ? 'Hide RAW' : 'Show RAW'); | |
52428d60 TL |
353 | }, |
354 | }, | |
355 | ], | |
4ae75df3 DC |
356 | }); |
357 | ||
358 | sections.push(rawSection); | |
4ae75df3 DC |
359 | } |
360 | ||
361 | if (!data.path.match(/\/_upgrade_/)) { | |
52428d60 | 362 | let permhtml = ''; |
4ae75df3 DC |
363 | |
364 | if (!info.permissions) { | |
365 | permhtml = "Root only."; | |
366 | } else { | |
367 | if (info.permissions.description) { | |
368 | permhtml += "<div style='white-space:pre-wrap;padding-bottom:10px;'>" + | |
369 | Ext.htmlEncode(info.permissions.description) + "</div>"; | |
370 | } | |
371 | permhtml += permission_text(info.permissions); | |
372 | } | |
373 | ||
374 | if (info.allowtoken !== undefined && !info.allowtoken) { | |
52428d60 | 375 | permhtml += "<br />This API endpoint is not available for API tokens."; |
4ae75df3 DC |
376 | } |
377 | ||
378 | sections.push({ | |
379 | title: 'Required permissions', | |
380 | bodyPadding: 10, | |
52428d60 | 381 | html: permhtml, |
4ae75df3 DC |
382 | }); |
383 | } | |
384 | ||
385 | items.push({ | |
386 | title: method, | |
387 | autoScroll: true, | |
388 | defaults: { | |
52428d60 | 389 | border: false, |
4ae75df3 | 390 | }, |
52428d60 | 391 | items: sections, |
4ae75df3 DC |
392 | }); |
393 | } | |
394 | }); | |
395 | ||
52428d60 TL |
396 | let ct = Ext.getCmp('docview'); |
397 | ct.setTitle("Path: " + real_path(data.path)); | |
4ae75df3 DC |
398 | ct.removeAll(true); |
399 | ct.add(items); | |
400 | ct.setActiveTab(0); | |
401 | }; | |
402 | ||
403 | Ext.define('Ext.form.SearchField', { | |
404 | extend: 'Ext.form.field.Text', | |
405 | alias: 'widget.searchfield', | |
406 | ||
407 | emptyText: 'Search...', | |
408 | ||
409 | flex: 1, | |
410 | ||
411 | inputType: 'search', | |
412 | listeners: { | |
52428d60 TL |
413 | 'change': function() { |
414 | let value = this.getValue(); | |
4ae75df3 DC |
415 | if (!Ext.isEmpty(value)) { |
416 | store.filter({ | |
417 | property: 'path', | |
418 | value: value, | |
52428d60 | 419 | anyMatch: true, |
4ae75df3 DC |
420 | }); |
421 | } else { | |
422 | store.clearFilter(); | |
423 | } | |
52428d60 TL |
424 | }, |
425 | }, | |
4ae75df3 DC |
426 | }); |
427 | ||
52428d60 | 428 | let tree = Ext.create('Ext.tree.Panel', { |
4ae75df3 DC |
429 | title: 'Resource Tree', |
430 | tbar: [ | |
431 | { | |
432 | xtype: 'searchfield', | |
52428d60 | 433 | }, |
4ae75df3 DC |
434 | ], |
435 | tools: [ | |
436 | { | |
437 | type: 'expand', | |
438 | tooltip: 'Expand all', | |
439 | tooltipType: 'title', | |
440 | callback: (tree) => tree.expandAll(), | |
441 | }, | |
442 | { | |
443 | type: 'collapse', | |
444 | tooltip: 'Collapse all', | |
445 | tooltipType: 'title', | |
446 | callback: (tree) => tree.collapseAll(), | |
447 | }, | |
448 | ], | |
449 | store: store, | |
450 | width: 200, | |
451 | region: 'west', | |
452 | split: true, | |
453 | margins: '5 0 5 5', | |
454 | rootVisible: false, | |
455 | listeners: { | |
456 | selectionchange: function(v, selections) { | |
52428d60 TL |
457 | if (!selections[0]) {return;} |
458 | let rec = selections[0]; | |
4ae75df3 DC |
459 | render_docu(rec.data); |
460 | location.hash = '#' + rec.data.path; | |
52428d60 TL |
461 | }, |
462 | }, | |
4ae75df3 DC |
463 | }); |
464 | ||
465 | Ext.create('Ext.container.Viewport', { | |
466 | layout: 'border', | |
467 | renderTo: Ext.getBody(), | |
468 | items: [ | |
469 | tree, | |
470 | { | |
471 | xtype: 'tabpanel', | |
472 | title: 'Documentation', | |
473 | id: 'docview', | |
474 | region: 'center', | |
475 | margins: '5 5 5 0', | |
476 | layout: 'fit', | |
52428d60 TL |
477 | items: [], |
478 | }, | |
479 | ], | |
4ae75df3 DC |
480 | }); |
481 | ||
52428d60 TL |
482 | let deepLink = function() { |
483 | let path = window.location.hash.substring(1).replace(/\/\s*$/, ''); | |
484 | let endpoint = store.findNode('path', path); | |
4ae75df3 DC |
485 | |
486 | if (endpoint) { | |
487 | tree.getSelectionModel().select(endpoint); | |
488 | tree.expandPath(endpoint.getPath()); | |
489 | render_docu(endpoint.data); | |
490 | } | |
52428d60 | 491 | }; |
4ae75df3 DC |
492 | window.onhashchange = deepLink; |
493 | ||
494 | deepLink(); | |
4ae75df3 | 495 | }); |