From 3377380c353413daa1857131bcfefeca63cc4f8a Mon Sep 17 00:00:00 2001 From: Julien Maurel Date: Thu, 19 Mar 2015 10:23:11 +0100 Subject: [PATCH] Fix #1040 : add options to be able to sort APIs and operations --- README.md | 3 +- src/main/javascript/view/MainView.js | 45 ++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2c2e9f33dc9..cd62fc8edba 100644 --- a/README.md +++ b/README.md @@ -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. 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). -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. 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. diff --git a/src/main/javascript/view/MainView.js b/src/main/javascript/view/MainView.js index 92c20790f2a..fd618b9051b 100644 --- a/src/main/javascript/view/MainView.js +++ b/src/main/javascript/view/MainView.js @@ -1,21 +1,48 @@ 'use strict'; SwaggerUi.Views.MainView = Backbone.View.extend({ - - // TODO: sorters were not used in any place, do we need them? - // sorters = { - // alpha : function(a,b){ return a.path.localeCompare(b.path); }, - // method : function(a,b){ return a.method.localeCompare(b.method); }, - // }, - + apisSorters : { + alpha : function(a,b){ return a.name.localeCompare(b.name); } + }, + operationsSorters : { + alpha : function(a,b){ return a.path.localeCompare(b.path); }, + method : function(a,b){ return a.method.localeCompare(b.method); } + }, initialize: function(opts){ + var sorterOption, sorterFn, key, value; opts = opts || {}; 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 this.model.auths = []; - var key, value; for (key in this.model.securityDefinitions) { value = this.model.securityDefinitions[key]; @@ -109,4 +136,4 @@ SwaggerUi.Views.MainView = Backbone.View.extend({ clear: function(){ $(this.el).html(''); } -}); \ No newline at end of file +});