Fix #1040 : add options to be able to sort APIs and operations

This commit is contained in:
Julien Maurel
2015-03-19 10:23:11 +01:00
parent 38ac8428d1
commit 3377380c35
2 changed files with 38 additions and 10 deletions

View File

@@ -91,7 +91,8 @@ validatorUrl | By default, Swagger-UI attempts to validate specs against swagger
dom_id | The id of a dom element inside which SwaggerUi will put the user interface for swagger. dom_id | The id of a dom element inside which SwaggerUi will put the user interface for swagger.
booleanValues | SwaggerUI renders boolean data types as a dropdown. By default it provides a 'true' and 'false' string as the possible choices. You can use this parameter to change the values in dropdown to be something else, for example 0 and 1 by setting booleanValues to new Array(0, 1). booleanValues | SwaggerUI renders boolean data types as a dropdown. By default it provides a 'true' and 'false' string as the possible choices. You can use this parameter to change the values in dropdown to be something else, for example 0 and 1 by setting booleanValues to new Array(0, 1).
docExpansion | Controls how the API listing is displayed. It can be set to 'none' (default), 'list' (shows operations for each resource), or 'full' (fully expanded: shows operations and their details). docExpansion | Controls how the API listing is displayed. It can be set to 'none' (default), 'list' (shows operations for each resource), or 'full' (fully expanded: shows operations and their details).
sorter | Apply a sort to the API list. It can be 'alpha' (sort paths alphanumerically) or 'method' (sort operations by HTTP method). Default is the order returned by the server unchanged. apisSorter | Apply a sort to the API/tags list. It can be 'alpha' (sort by name) or a function (see Array.prototype.sort() to know how sort function works). Default is the order returned by the server unchanged.
operationsSorter | Apply a sort to the operation list of each API. It can be 'alpha' (sort by paths alphanumerically), 'method' (sort by HTTP method) or a function (see Array.prototype.sort() to know how sort function works). Default is the order returned by the server unchanged.
onComplete | This is a callback function parameter which can be passed to be notified of when SwaggerUI has completed rendering successfully. onComplete | This is a callback function parameter which can be passed to be notified of when SwaggerUI has completed rendering successfully.
onFailure | This is a callback function parameter which can be passed to be notified of when SwaggerUI encountered a failure was unable to render. onFailure | This is a callback function parameter which can be passed to be notified of when SwaggerUI encountered a failure was unable to render.
highlightSizeThreshold | Any size response below this threshold will be highlighted syntactically, attempting to highlight large responses can lead to browser hangs, not including a threshold will default to highlight all returned responses. highlightSizeThreshold | Any size response below this threshold will be highlighted syntactically, attempting to highlight large responses can lead to browser hangs, not including a threshold will default to highlight all returned responses.

View File

@@ -1,21 +1,48 @@
'use strict'; 'use strict';
SwaggerUi.Views.MainView = Backbone.View.extend({ SwaggerUi.Views.MainView = Backbone.View.extend({
apisSorters : {
// TODO: sorters were not used in any place, do we need them? alpha : function(a,b){ return a.name.localeCompare(b.name); }
// sorters = { },
// alpha : function(a,b){ return a.path.localeCompare(b.path); }, operationsSorters : {
// method : function(a,b){ return a.method.localeCompare(b.method); }, alpha : function(a,b){ return a.path.localeCompare(b.path); },
// }, method : function(a,b){ return a.method.localeCompare(b.method); }
},
initialize: function(opts){ initialize: function(opts){
var sorterOption, sorterFn, key, value;
opts = opts || {}; opts = opts || {};
this.router = opts.router; this.router = opts.router;
// Sort APIs
if (opts.swaggerOptions.apisSorter) {
sorterOption = opts.swaggerOptions.apisSorter;
if (_.isFunction(sorterOption)) {
sorterFn = sorterOption;
} else {
sorterFn = this.apisSorters[sorterOption];
}
if (_.isFunction(sorterFn)) {
this.model.apisArray.sort(sorterFn);
}
}
// Sort operations of each API
if (opts.swaggerOptions.operationsSorter) {
sorterOption = opts.swaggerOptions.operationsSorter;
if (_.isFunction(sorterOption)) {
sorterFn = sorterOption;
} else {
sorterFn = this.operationsSorters[sorterOption];
}
if (_.isFunction(sorterFn)) {
for (key in this.model.apisArray) {
this.model.apisArray[key].operationsArray.sort(sorterFn);
}
}
}
// set up the UI for input // set up the UI for input
this.model.auths = []; this.model.auths = [];
var key, value;
for (key in this.model.securityDefinitions) { for (key in this.model.securityDefinitions) {
value = this.model.securityDefinitions[key]; value = this.model.securityDefinitions[key];
@@ -109,4 +136,4 @@ SwaggerUi.Views.MainView = Backbone.View.extend({
clear: function(){ clear: function(){
$(this.el).html(''); $(this.el).html('');
} }
}); });