updated for auth
This commit is contained in:
31
src/main/coffeescript/view/ApiKeyButton.coffee
Normal file
31
src/main/coffeescript/view/ApiKeyButton.coffee
Normal file
@@ -0,0 +1,31 @@
|
||||
class ApiKeyButton extends Backbone.View
|
||||
initialize: ->
|
||||
|
||||
render: ->
|
||||
template = @template()
|
||||
$(@el).html(template(@model))
|
||||
|
||||
@
|
||||
|
||||
events:
|
||||
"click #apikey_button" : "toggleApiKeyContainer"
|
||||
"click #apply_api_key" : "applyApiKey"
|
||||
|
||||
applyApiKey: ->
|
||||
window.authorizations.add(@model.name, new ApiKeyAuthorization(@model.name, $("#input_apiKey_entry").val(), @model.in))
|
||||
window.swaggerUi.load()
|
||||
elem = $('#apikey_container').show()
|
||||
|
||||
toggleApiKeyContainer: ->
|
||||
if $('#apikey_container').length > 0
|
||||
elem = $('#apikey_container').first()
|
||||
if elem.is ':visible'
|
||||
elem.hide()
|
||||
else
|
||||
# hide others
|
||||
$('.auth_container').hide()
|
||||
elem.show()
|
||||
|
||||
template: ->
|
||||
Handlebars.templates.apikey_button_view
|
||||
|
||||
35
src/main/coffeescript/view/BasicAuthButton.coffee
Normal file
35
src/main/coffeescript/view/BasicAuthButton.coffee
Normal file
@@ -0,0 +1,35 @@
|
||||
class BasicAuthButton extends Backbone.View
|
||||
initialize: ->
|
||||
|
||||
render: ->
|
||||
template = @template()
|
||||
$(@el).html(template(@model))
|
||||
|
||||
@
|
||||
|
||||
events:
|
||||
"click #basic_auth_button" : "togglePasswordContainer"
|
||||
"click #apply_basic_auth" : "applyPassword"
|
||||
|
||||
applyPassword: ->
|
||||
console.log "applying password"
|
||||
username = $(".input_username").val()
|
||||
password = $(".input_password").val()
|
||||
window.authorizations.add(@model.type, new PasswordAuthorization("basic", username, password))
|
||||
window.swaggerUi.load()
|
||||
elem = $('#basic_auth_container').hide()
|
||||
|
||||
|
||||
togglePasswordContainer: ->
|
||||
if $('#basic_auth_container').length > 0
|
||||
elem = $('#basic_auth_container').show()
|
||||
if elem.is ':visible'
|
||||
elem.slideUp()
|
||||
else
|
||||
# hide others
|
||||
$('.auth_container').hide()
|
||||
elem.show()
|
||||
|
||||
template: ->
|
||||
Handlebars.templates.basic_auth_button_view
|
||||
|
||||
@@ -5,14 +5,12 @@ class MainView extends Backbone.View
|
||||
}
|
||||
|
||||
initialize: (opts={}) ->
|
||||
if opts.swaggerOptions.sorter
|
||||
sorterName = opts.swaggerOptions.sorter
|
||||
sorter = sorters[sorterName]
|
||||
if @model.apisArray
|
||||
for route in @model.apisArray
|
||||
route.operationsArray.sort sorter
|
||||
if (sorterName == "alpha") # sort top level paths if alpha
|
||||
@model.apisArray.sort sorter
|
||||
# set up the UI for input
|
||||
@model.auths = []
|
||||
for key, value of @model.securityDefinitions
|
||||
auth = {name: key, type: value.type, value: value}
|
||||
@model.auths.push auth
|
||||
|
||||
|
||||
if @model.info and @model.info.license and typeof @model.info.license is 'string'
|
||||
name = @model.info.license
|
||||
@@ -39,6 +37,16 @@ class MainView extends Backbone.View
|
||||
@model.validatorUrl = "http://online.swagger.io/validator"
|
||||
|
||||
render: ->
|
||||
if @model.securityDefinitions
|
||||
for name of @model.securityDefinitions
|
||||
auth = @model.securityDefinitions[name]
|
||||
if auth.type is "apiKey" and $("#apikey_button").length is 0
|
||||
button = new ApiKeyButton({model: auth}).render().el
|
||||
$('.auth_main_container').append button
|
||||
if auth.type is "basicAuth" and $("#basic_auth_button").length is 0
|
||||
button = new BasicAuthButton({model: auth}).render().el
|
||||
$('.auth_main_container').append button
|
||||
|
||||
# Render the outer container for resources
|
||||
$(@el).html(Handlebars.templates.main(@model))
|
||||
|
||||
@@ -53,13 +61,20 @@ class MainView extends Backbone.View
|
||||
counter += 1
|
||||
resource.id = id
|
||||
resources[id] = resource
|
||||
@addResource resource
|
||||
@addResource resource, @model.auths
|
||||
@
|
||||
|
||||
addResource: (resource) ->
|
||||
addResource: (resource, auths) ->
|
||||
# Render a resource and add it to resources li
|
||||
resource.id = resource.id.replace(/\s/g, '_')
|
||||
resourceView = new ResourceView({model: resource, tagName: 'li', id: 'resource_' + resource.id, className: 'resource', swaggerOptions: @options.swaggerOptions})
|
||||
resourceView = new ResourceView({
|
||||
model: resource,
|
||||
tagName: 'li',
|
||||
id: 'resource_' + resource.id,
|
||||
className: 'resource',
|
||||
auths: auths,
|
||||
swaggerOptions: @options.swaggerOptions
|
||||
})
|
||||
$('#resources').append resourceView.render().el
|
||||
|
||||
clear: ->
|
||||
|
||||
@@ -10,7 +10,10 @@ class OperationView extends Backbone.View
|
||||
'mouseout .api-ic' : 'mouseExit'
|
||||
}
|
||||
|
||||
initialize: ->
|
||||
initialize: (opts={}) ->
|
||||
@auths = opts.auths
|
||||
|
||||
@
|
||||
|
||||
mouseEnter: (e) ->
|
||||
elem = $(e.currentTarget.parentNode).find('#api_information_panel')
|
||||
@@ -49,15 +52,28 @@ class OperationView extends Backbone.View
|
||||
if @model.description
|
||||
@model.description = @model.description.replace(/(?:\r\n|\r|\n)/g, '<br />')
|
||||
@model.oauth = null
|
||||
log @model.authorizations
|
||||
if @model.authorizations
|
||||
for k, v of @model.authorizations
|
||||
if k == "oauth2"
|
||||
if @model.oauth == null
|
||||
@model.oauth = {}
|
||||
if @model.oauth.scopes is undefined
|
||||
@model.oauth.scopes = []
|
||||
for o in v
|
||||
@model.oauth.scopes.push o
|
||||
if Array.isArray @model.authorizations
|
||||
for auths in @model.authorizations
|
||||
for key, auth of auths
|
||||
for a of @auths
|
||||
auth = @auths[a]
|
||||
if auth.type == 'oauth2'
|
||||
@model.oauth = {}
|
||||
@model.oauth.scopes = []
|
||||
for k, v of auth.value.scopes
|
||||
o = {scope: k, description: v}
|
||||
@model.oauth.scopes.push o
|
||||
else
|
||||
for k, v of @model.authorizations
|
||||
if k == "oauth2"
|
||||
if @model.oauth == null
|
||||
@model.oauth = {}
|
||||
if @model.oauth.scopes is undefined
|
||||
@model.oauth.scopes = []
|
||||
for o in v
|
||||
@model.oauth.scopes.push o
|
||||
|
||||
if typeof @model.responses isnt 'undefined'
|
||||
@model.responseMessages = []
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
class ResourceView extends Backbone.View
|
||||
initialize: ->
|
||||
initialize: (opts={}) ->
|
||||
@auths = opts.auths
|
||||
if "" is @model.description
|
||||
@model.description = null
|
||||
|
||||
@@ -36,7 +37,13 @@ class ResourceView extends Backbone.View
|
||||
operation.number = @number
|
||||
|
||||
# Render an operation and add it to operations li
|
||||
operationView = new OperationView({model: operation, tagName: 'li', className: 'endpoint', swaggerOptions: @options.swaggerOptions})
|
||||
operationView = new OperationView({
|
||||
model: operation,
|
||||
tagName: 'li',
|
||||
className: 'endpoint',
|
||||
swaggerOptions: @options.swaggerOptions,
|
||||
auths: @auths
|
||||
})
|
||||
$('.endpoints', $(@el)).append operationView.render().el
|
||||
|
||||
@number++
|
||||
|
||||
Reference in New Issue
Block a user