updated to v2

This commit is contained in:
Tony Tam
2012-07-11 09:57:27 -07:00
parent a2c1c18f75
commit d2eb882e52
278 changed files with 46216 additions and 11130 deletions

View File

@@ -0,0 +1,79 @@
class SwaggerUi extends Backbone.Router
# Routes
routes:
'' : 'load'
# Defaults
dom_id: "swagger_ui"
# Attributes
options: null
api: null
headerView: null
mainView: null
# SwaggerUi accepts all the same options as SwaggerApi
initialize: (options={}) ->
Backbone.history.start pushState: true
# 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.progress = (d) => @showMessage(d)
@options.failure = (d) => @onLoadFailure(d)
# 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)
@load()
# 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: ->
@showMessage('Finished Loading Resource Information. Rendering Swagger UI...')
@mainView = new MainView({model: @api, el: $('#' + @dom_id)}).render()
@showMessage()
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 = '') ->
$('#message-bar').removeClass 'message-success'
$('#message-bar').addClass 'message-fail'
$('#message-bar').html data
window.SwaggerUi = SwaggerUi