From db6419414c5fe9984282662b9c7fbefba593179b Mon Sep 17 00:00:00 2001 From: Chris Hatch Date: Sun, 20 Jul 2014 20:18:19 +0800 Subject: [PATCH 1/3] #254 alphabetical sort of apis and operations under apis new option sortAlphabetical=true|false --- src/main/coffeescript/SwaggerUi.coffee | 2 +- src/main/coffeescript/view/MainView.coffee | 12 ++++++++++-- src/main/html/index.html | 3 ++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/coffeescript/SwaggerUi.coffee b/src/main/coffeescript/SwaggerUi.coffee index fc0474fdc1d..909cd6d787c 100644 --- a/src/main/coffeescript/SwaggerUi.coffee +++ b/src/main/coffeescript/SwaggerUi.coffee @@ -55,7 +55,7 @@ class SwaggerUi extends Backbone.Router # so it gets called when SwaggerApi completes loading render:() -> @showMessage('Finished Loading Resource Information. Rendering Swagger UI...') - @mainView = new MainView({model: @api, el: $('#' + @dom_id)}).render() + @mainView = new MainView({model: @api, el: $('#' + @dom_id), swaggerOptions: @options}).render() @showMessage() switch @options.docExpansion when "full" then Docs.expandOperationsForResource('') diff --git a/src/main/coffeescript/view/MainView.coffee b/src/main/coffeescript/view/MainView.coffee index ae03f663113..3237fd86b35 100644 --- a/src/main/coffeescript/view/MainView.coffee +++ b/src/main/coffeescript/view/MainView.coffee @@ -1,5 +1,12 @@ class MainView extends Backbone.View - initialize: -> + initialize: (opts={}) -> + if opts.swaggerOptions.sortAlphabetically == true + pathSorter = (a,b) -> return a.path.localeCompare(b.path) + # sort apis + @model.apisArray.sort pathSorter + # sort operations + for route in @model.apisArray + route.operationsArray.sort pathSorter render: -> # Render the outer container for resources @@ -10,6 +17,7 @@ class MainView extends Backbone.View resources = {} counter = 0 for resource in @model.apisArray + console.info(resource); id = resource.name while typeof resources[id] isnt 'undefined' id = id + "_" + counter @@ -25,4 +33,4 @@ class MainView extends Backbone.View $('#resources').append resourceView.render().el clear: -> - $(@el).html '' \ No newline at end of file + $(@el).html '' diff --git a/src/main/html/index.html b/src/main/html/index.html index 5af095a0e23..0de9093689b 100644 --- a/src/main/html/index.html +++ b/src/main/html/index.html @@ -47,7 +47,8 @@ onFailure: function(data) { log("Unable to Load SwaggerUI"); }, - docExpansion: "none" + docExpansion: "none", + sortAlphabetically: true }); $('#input_apiKey').change(function() { From 8ad46f08131da5cc5ef8bd3bdc090ad811f86c25 Mon Sep 17 00:00:00 2001 From: Chris Hatch Date: Sun, 20 Jul 2014 20:21:28 +0800 Subject: [PATCH 2/3] remove console.info --- src/main/coffeescript/view/MainView.coffee | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/coffeescript/view/MainView.coffee b/src/main/coffeescript/view/MainView.coffee index 3237fd86b35..e7ea1fd2cac 100644 --- a/src/main/coffeescript/view/MainView.coffee +++ b/src/main/coffeescript/view/MainView.coffee @@ -17,7 +17,6 @@ class MainView extends Backbone.View resources = {} counter = 0 for resource in @model.apisArray - console.info(resource); id = resource.name while typeof resources[id] isnt 'undefined' id = id + "_" + counter From 5d2bed70257c117554a0d66c216e7a123a37ff09 Mon Sep 17 00:00:00 2001 From: Chris Hatch Date: Sun, 27 Jul 2014 21:18:18 +0800 Subject: [PATCH 3/3] provide option sorter=[alpha|method] --- README.md | 1 + src/main/coffeescript/view/MainView.coffee | 19 ++++++++++++------- src/main/html/index.html | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6998d56633a..a8c1fec82b4 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ To use swagger-ui you should take a look at the [source of swagger-ui html page] * *dom_id parameter* is the 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. * *onComplete* is a callback function parameter which can be passed to be notified of when SwaggerUI has completed rendering successfully. * *onFailure* is a callback function parameter which can be passed to be notified of when SwaggerUI encountered a failure was unable to render. * All other parameters are explained in greater detail below diff --git a/src/main/coffeescript/view/MainView.coffee b/src/main/coffeescript/view/MainView.coffee index e7ea1fd2cac..4f41a7d1ac5 100644 --- a/src/main/coffeescript/view/MainView.coffee +++ b/src/main/coffeescript/view/MainView.coffee @@ -1,13 +1,18 @@ class MainView extends Backbone.View + sorters = { + 'alpha' : (a,b) -> return a.path.localeCompare(b.path), + 'method' : (a,b) -> return a.method.localeCompare(b.method), + } + initialize: (opts={}) -> - if opts.swaggerOptions.sortAlphabetically == true - pathSorter = (a,b) -> return a.path.localeCompare(b.path) - # sort apis - @model.apisArray.sort pathSorter - # sort operations + if opts.swaggerOptions.sorter + sorterName = opts.swaggerOptions.sorter + sorter = sorters[sorterName] for route in @model.apisArray - route.operationsArray.sort pathSorter - + route.operationsArray.sort sorter + if (sorterName == "alpha") # sort top level paths if alpha + @model.apisArray.sort sorter + render: -> # Render the outer container for resources $(@el).html(Handlebars.templates.main(@model)) diff --git a/src/main/html/index.html b/src/main/html/index.html index 0de9093689b..caf4ef0ed38 100644 --- a/src/main/html/index.html +++ b/src/main/html/index.html @@ -48,7 +48,7 @@ log("Unable to Load SwaggerUI"); }, docExpansion: "none", - sortAlphabetically: true + sorter : "alpha" }); $('#input_apiKey').change(function() {