code to display resources in default/list/expended style. style can be

passed as a option while creating swagerUi object.
In future I would like to achieve this by passing options to templates
and have a template helper method manipulate dom and apply correct
classes.This approach will improve the performance as we need not go
over the entire dom and call Doc.collapseOperationsForResource or
Doc.expandOperationsForResource on each matched element
This commit is contained in:
Arjun Balla
2012-10-23 11:42:26 -07:00
parent 1fc4caae2e
commit 6beaa62b71
3 changed files with 334 additions and 327 deletions

View File

@@ -1,75 +1,78 @@
class SwaggerUi extends Backbone.Router
# Defaults
dom_id: "swagger_ui"
# Attributes
options: null
api: null
headerView: null
mainView: null
# SwaggerUi accepts all the same options as SwaggerApi
initialize: (options={}) ->
# Allow dom_id to be overridden
if options.dom_id?
@dom_id = options.dom_id
delete options.dom_id
# Create an empty div which contains the dom_id
$('body').append('<div id="' + @dom_id + '"></div>') if not $('#' + @dom_id)?
@options = options
# Set the callbacks
@options.success = => @render(options)
@options.progress = (d) => @showMessage(d)
@options.failure = (d) => @onLoadFailure(d, options.doneFailure)
# Create view to handle the header inputs
@headerView = new HeaderView({el: $('#header')})
# Event handler for when the baseUrl/apiKey is entered by user
@headerView.on 'update-swagger-ui', (data) => @updateSwaggerUi(data)
# Event handler for when url/key is received from user
updateSwaggerUi: (data) ->
@options.discoveryUrl = data.discoveryUrl
@options.apiKey = data.apiKey
@load()
# Create an api and render
load: ->
# Initialize the API object
@mainView?.clear()
@headerView.update(@options.discoveryUrl, @options.apiKey)
@api = new SwaggerApi(@options)
# This is bound to success handler for SwaggerApi
# so it gets called when SwaggerApi completes loading
render:(options) ->
@showMessage('Finished Loading Resource Information. Rendering Swagger UI...')
@mainView = new MainView({model: @api, el: $('#' + @dom_id)}).render()
@showMessage()
options.doneSuccess() if options.doneSuccess
setTimeout(
=>
Docs.shebang()
400
)
# Shows message on topbar of the ui
showMessage: (data = '') ->
$('#message-bar').removeClass 'message-fail'
$('#message-bar').addClass 'message-success'
$('#message-bar').html data
# shows message in red
onLoadFailure: (data = '', doneFailure) ->
$('#message-bar').removeClass 'message-success'
$('#message-bar').addClass 'message-fail'
val = $('#message-bar').html data
doneFailure() if doneFailure
val
window.SwaggerUi = SwaggerUi
class SwaggerUi extends Backbone.Router
# Defaults
dom_id: "swagger_ui"
# Attributes
options: null
api: null
headerView: null
mainView: null
# SwaggerUi accepts all the same options as SwaggerApi
initialize: (options={}) ->
# Allow dom_id to be overridden
if options.dom_id?
@dom_id = options.dom_id
delete options.dom_id
# Create an empty div which contains the dom_id
$('body').append('<div id="' + @dom_id + '"></div>') if not $('#' + @dom_id)?
@options = options
# Set the callbacks
@options.success = => @render(options)
@options.progress = (d) => @showMessage(d)
@options.failure = (d) => @onLoadFailure(d, options.doneFailure)
# Create view to handle the header inputs
@headerView = new HeaderView({el: $('#header')})
# Event handler for when the baseUrl/apiKey is entered by user
@headerView.on 'update-swagger-ui', (data) => @updateSwaggerUi(data)
# Event handler for when url/key is received from user
updateSwaggerUi: (data) ->
@options.discoveryUrl = data.discoveryUrl
@options.apiKey = data.apiKey
@load()
# Create an api and render
load: ->
# Initialize the API object
@mainView?.clear()
@headerView.update(@options.discoveryUrl, @options.apiKey)
@api = new SwaggerApi(@options)
# This is bound to success handler for SwaggerApi
# so it gets called when SwaggerApi completes loading
render:(options) ->
@showMessage('Finished Loading Resource Information. Rendering Swagger UI...')
@mainView = new MainView({model: @api, el: $('#' + @dom_id)}).render()
@showMessage()
switch options.docStyle
when "expand" then Docs.expandOperationsForResource('')
when "list" then Docs.collapseOperationsForResource('')
options.doneSuccess() if options.doneSuccess
setTimeout(
=>
Docs.shebang()
400
)
# Shows message on topbar of the ui
showMessage: (data = '') ->
$('#message-bar').removeClass 'message-fail'
$('#message-bar').addClass 'message-success'
$('#message-bar').html data
# shows message in red
onLoadFailure: (data = '', doneFailure) ->
$('#message-bar').removeClass 'message-success'
$('#message-bar').addClass 'message-fail'
val = $('#message-bar').html data
doneFailure() if doneFailure
val
window.SwaggerUi = SwaggerUi