Merge pull request #1029 from mohsen1/remove-cs-folder
Remove CoffeeScript folder
This commit is contained in:
@@ -1,151 +0,0 @@
|
||||
class SwaggerUi extends Backbone.Router
|
||||
|
||||
# Defaults
|
||||
domEl: $('#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?
|
||||
@domEl = $('#' + options.dom_id)
|
||||
delete options.dom_id
|
||||
|
||||
# Allow domeEl to be specified
|
||||
else if options.domEl?
|
||||
@domEl = options.domEl
|
||||
|
||||
if not options.supportedSubmitMethods?
|
||||
options.supportedSubmitMethods = [
|
||||
'get'
|
||||
'put'
|
||||
'post'
|
||||
'delete'
|
||||
'head'
|
||||
'options'
|
||||
'patch'
|
||||
]
|
||||
|
||||
# Make sure this.domeEl is a jQuery element
|
||||
@domEl = $(@domEl)
|
||||
|
||||
# if domEl is not attached to document append it to <body>
|
||||
if !$.contains(document.documentElement, @domEl.get(0))
|
||||
$('body').append(@domEl)
|
||||
|
||||
@options = options
|
||||
|
||||
# set marked options
|
||||
marked.setOptions(gfm: true)
|
||||
|
||||
# Set the callbacks
|
||||
@options.success = =>
|
||||
@render()
|
||||
@options.progress = (d) => @showMessage(d)
|
||||
@options.failure = (d) =>
|
||||
@onLoadFailure(d)
|
||||
|
||||
# Create view to handle the header inputs if there is header element
|
||||
if $('#header').length
|
||||
@headerView = new HeaderView({el: $('#header')})
|
||||
|
||||
# Event handler for when the baseUrl/apiKey is entered by user
|
||||
@headerView.on 'update-swagger-ui', (data) => @updateSwaggerUi(data)
|
||||
|
||||
# Set an option after initializing
|
||||
setOption: (option,value) ->
|
||||
@options[option] = value
|
||||
|
||||
# Get the value of a previously set option
|
||||
getOption: (option) ->
|
||||
@options[option]
|
||||
|
||||
# Event handler for when url/key is received from user
|
||||
updateSwaggerUi: (data) ->
|
||||
@options.url = data.url
|
||||
@load()
|
||||
|
||||
# Create an api and render
|
||||
load: ->
|
||||
# Initialize the API object
|
||||
@mainView?.clear()
|
||||
url = @options.url
|
||||
if url && url.indexOf("http") isnt 0
|
||||
url = @buildUrl(window.location.href.toString(), url)
|
||||
|
||||
@options.url = url
|
||||
if @headerView
|
||||
@headerView.update(url)
|
||||
|
||||
@api = new SwaggerClient(@options)
|
||||
|
||||
# collapse all sections
|
||||
collapseAll:() ->
|
||||
Docs.collapseEndpointListForResource('')
|
||||
|
||||
# list operations for all sections
|
||||
listAll:() ->
|
||||
Docs.collapseOperationsForResource('')
|
||||
|
||||
# expand operations for all sections
|
||||
expandAll:() ->
|
||||
Docs.expandOperationsForResource('')
|
||||
|
||||
# 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: @domEl, swaggerOptions: @options, router: @}).render()
|
||||
@showMessage()
|
||||
switch @options.docExpansion
|
||||
when "full" then @expandAll()
|
||||
when "list" then @listAll()
|
||||
@renderGFM()
|
||||
@options.onComplete(@api, @) if @options.onComplete
|
||||
setTimeout(
|
||||
=>
|
||||
Docs.shebang()
|
||||
100
|
||||
)
|
||||
|
||||
buildUrl: (base, url) ->
|
||||
if url.indexOf("/") is 0
|
||||
parts = base.split("/")
|
||||
base = parts[0] + "//" + parts[2]
|
||||
base + url
|
||||
else
|
||||
endOfPath = base.length
|
||||
if base.indexOf("?") > -1
|
||||
endOfPath = Math.min(endOfPath, base.indexOf("?"))
|
||||
if base.indexOf("#") > -1
|
||||
endOfPath = Math.min(endOfPath, base.indexOf("#"))
|
||||
base = base.substring(0, endOfPath);
|
||||
if base.indexOf( "/", base.length - 1 ) isnt -1
|
||||
return base + url
|
||||
return base + "/" + url
|
||||
|
||||
# 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'
|
||||
val = $('#message-bar').html data
|
||||
@options.onFailure(data) if @options.onFailure?
|
||||
val
|
||||
|
||||
# Renders GFM for elements with 'markdown' class
|
||||
renderGFM: (data = '') ->
|
||||
$('.markdown').each (index) ->
|
||||
$(this).html(marked($(this).html()))
|
||||
|
||||
window.SwaggerUi = SwaggerUi
|
||||
@@ -1,75 +0,0 @@
|
||||
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={}) ->
|
||||
# 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.swaggerVersion is "2.0"
|
||||
if "validatorUrl" of opts.swaggerOptions
|
||||
# Validator URL specified explicitly
|
||||
@model.validatorUrl = opts.swaggerOptions.validatorUrl
|
||||
else if @model.url.indexOf("localhost") > 0
|
||||
# Localhost override
|
||||
@model.validatorUrl = null
|
||||
else
|
||||
# Default validator
|
||||
@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))
|
||||
|
||||
# Render each resource
|
||||
|
||||
resources = {}
|
||||
counter = 0
|
||||
for resource in @model.apisArray
|
||||
id = resource.name
|
||||
while typeof resources[id] isnt 'undefined'
|
||||
id = id + "_" + counter
|
||||
counter += 1
|
||||
resource.id = id
|
||||
resources[id] = resource
|
||||
@addResource resource, @model.auths
|
||||
|
||||
$('.propWrap').hover(
|
||||
->
|
||||
$('.optionsWrapper', $(this)).show()
|
||||
,->
|
||||
$('.optionsWrapper', $(this)).hide()
|
||||
)
|
||||
@
|
||||
|
||||
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',
|
||||
auths: auths,
|
||||
swaggerOptions: @options.swaggerOptions,
|
||||
parent: @
|
||||
})
|
||||
$('#resources', @el).append resourceView.render().el
|
||||
|
||||
clear: ->
|
||||
$(@el).html ''
|
||||
@@ -1,464 +0,0 @@
|
||||
class OperationView extends Backbone.View
|
||||
invocationUrl: null
|
||||
|
||||
events: {
|
||||
'submit .sandbox' : 'submitOperation'
|
||||
'click .submit' : 'submitOperation'
|
||||
'click .response_hider' : 'hideResponse'
|
||||
'click .toggleOperation' : 'toggleOperationContent'
|
||||
'mouseenter .api-ic' : 'mouseEnter'
|
||||
'mouseout .api-ic' : 'mouseExit'
|
||||
}
|
||||
|
||||
initialize: (opts={}) ->
|
||||
@auths = opts.auths
|
||||
@parentId = @model.parentId
|
||||
@nickname = @model.nickname
|
||||
@
|
||||
|
||||
mouseEnter: (e) ->
|
||||
elem = $(@el).find '.content'
|
||||
x = e.pageX
|
||||
y = e.pageY
|
||||
scX = $(window).scrollLeft()
|
||||
scY = $(window).scrollTop()
|
||||
scMaxX = scX + $(window).width()
|
||||
scMaxY = scY + $(window).height()
|
||||
wd = elem.width()
|
||||
hgh = elem.height()
|
||||
|
||||
if (x + wd > scMaxX)
|
||||
x = scMaxX - wd
|
||||
if (x < scX)
|
||||
x = scX
|
||||
if (y + hgh > scMaxY)
|
||||
y = scMaxY - hgh
|
||||
if (y < scY)
|
||||
y = scY
|
||||
pos = {}
|
||||
pos.top = y
|
||||
pos.left = x
|
||||
elem.css(pos)
|
||||
$(e.currentTarget.parentNode).find('#api_information_panel').show()
|
||||
|
||||
mouseExit: (e) ->
|
||||
$(e.currentTarget.parentNode).find('#api_information_panel').hide()
|
||||
|
||||
render: ->
|
||||
isMethodSubmissionSupported = jQuery.inArray(@model.method, @model.supportedSubmitMethods()) >= 0
|
||||
@model.isReadOnly = true unless isMethodSubmissionSupported
|
||||
|
||||
# 1.2 syntax for description was `notes`
|
||||
@model.description = (@model.description || @model.notes)
|
||||
if @model.description
|
||||
@model.description = @model.description.replace(/(?:\r\n|\r|\n)/g, '<br />')
|
||||
@model.oauth = null
|
||||
modelAuths = @model.authorizations || @model.security
|
||||
if modelAuths
|
||||
if Array.isArray modelAuths
|
||||
for auths in modelAuths
|
||||
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
|
||||
scopeIndex = auths[key].indexOf k
|
||||
if scopeIndex >= 0
|
||||
o = {scope: k, description: v}
|
||||
@model.oauth.scopes.push o
|
||||
else
|
||||
for k, v of modelAuths
|
||||
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 = []
|
||||
for code, value of @model.responses
|
||||
schema = null
|
||||
schemaObj = @model.responses[code].schema
|
||||
if schemaObj and schemaObj['$ref']
|
||||
schema = schemaObj['$ref']
|
||||
if schema.indexOf('#/definitions/') is 0
|
||||
schema = schema.substring('#/definitions/'.length)
|
||||
@model.responseMessages.push {code: code, message: value.description, responseModel: schema }
|
||||
|
||||
if typeof @model.responseMessages is 'undefined'
|
||||
@model.responseMessages = []
|
||||
|
||||
# 2.0
|
||||
signatureModel = null
|
||||
if @model.successResponse
|
||||
successResponse = @model.successResponse
|
||||
for key of successResponse
|
||||
value = successResponse[key]
|
||||
@model.successCode = key
|
||||
if typeof value is 'object' and typeof value.createJSONSample is 'function'
|
||||
signatureModel =
|
||||
sampleJSON: JSON.stringify(value.createJSONSample(), undefined, 2)
|
||||
isParam: false
|
||||
signature: value.getMockSignature()
|
||||
# 1.2
|
||||
else if @model.responseClassSignature and @model.responseClassSignature != 'string'
|
||||
signatureModel =
|
||||
sampleJSON: @model.responseSampleJSON
|
||||
isParam: false
|
||||
signature: @model.responseClassSignature
|
||||
|
||||
|
||||
$(@el).html(Handlebars.templates.operation(@model))
|
||||
|
||||
if signatureModel
|
||||
responseSignatureView = new SignatureView({model: signatureModel, tagName: 'div', parent: @})
|
||||
$('.model-signature', $(@el)).append responseSignatureView.render().el
|
||||
else
|
||||
@model.responseClassSignature = 'string'
|
||||
$('.model-signature', $(@el)).html(@model.type)
|
||||
|
||||
contentTypeModel =
|
||||
isParam: false
|
||||
|
||||
contentTypeModel.consumes = @model.consumes
|
||||
contentTypeModel.produces = @model.produces
|
||||
|
||||
for param in @model.parameters
|
||||
type = param.type || param.dataType || ''
|
||||
if typeof type is 'undefined'
|
||||
schema = param.schema
|
||||
if schema and schema['$ref']
|
||||
ref = schema['$ref']
|
||||
if ref.indexOf('#/definitions/') is 0
|
||||
type = ref.substring('#/definitions/'.length)
|
||||
else
|
||||
type = ref
|
||||
if type and type.toLowerCase() == 'file'
|
||||
if !contentTypeModel.consumes
|
||||
contentTypeModel.consumes = 'multipart/form-data'
|
||||
param.type = type
|
||||
|
||||
responseContentTypeView = new ResponseContentTypeView({model: contentTypeModel, parent: @})
|
||||
$('.response-content-type', $(@el)).append responseContentTypeView.render().el
|
||||
|
||||
# Render each parameter
|
||||
@addParameter param, contentTypeModel.consumes for param in @model.parameters
|
||||
|
||||
# Render each response code
|
||||
@addStatusCode statusCode for statusCode in @model.responseMessages
|
||||
|
||||
@
|
||||
|
||||
addParameter: (param, consumes) ->
|
||||
# Render a parameter
|
||||
param.consumes = consumes
|
||||
paramView = new ParameterView({model: param, tagName: 'tr', readOnly: @model.isReadOnly, parent: @})
|
||||
$('.operation-params', $(@el)).append paramView.render().el
|
||||
|
||||
addStatusCode: (statusCode) ->
|
||||
# Render status codes
|
||||
statusCodeView = new StatusCodeView({model: statusCode, tagName: 'tr', parent: @})
|
||||
$('.operation-status', $(@el)).append statusCodeView.render().el
|
||||
|
||||
submitOperation: (e) ->
|
||||
e?.preventDefault()
|
||||
# Check for errors
|
||||
form = $('.sandbox', $(@el))
|
||||
error_free = true
|
||||
form.find("input.required").each ->
|
||||
$(@).removeClass "error"
|
||||
if jQuery.trim($(@).val()) is ""
|
||||
$(@).addClass "error"
|
||||
$(@).wiggle
|
||||
callback: => $(@).focus()
|
||||
error_free = false
|
||||
form.find("textarea.required").each ->
|
||||
$(@).removeClass "error"
|
||||
if jQuery.trim($(@).val()) is ""
|
||||
$(@).addClass "error"
|
||||
$(@).wiggle
|
||||
callback: => $(@).focus()
|
||||
error_free = false
|
||||
|
||||
# if error free submit it
|
||||
if error_free
|
||||
map = {}
|
||||
opts = {parent: @}
|
||||
|
||||
isFileUpload = false
|
||||
|
||||
for o in form.find("input")
|
||||
if(o.value? && jQuery.trim(o.value).length > 0)
|
||||
map[o.name] = o.value
|
||||
if o.type is "file"
|
||||
map[o.name] = o.files[0]
|
||||
isFileUpload = true
|
||||
|
||||
for o in form.find("textarea")
|
||||
if(o.value? && jQuery.trim(o.value).length > 0)
|
||||
map[o.name] = o.value
|
||||
|
||||
for o in form.find("select")
|
||||
val = this.getSelectedValue o
|
||||
if(val? && jQuery.trim(val).length > 0)
|
||||
map[o.name] = val
|
||||
|
||||
opts.responseContentType = $("div select[name=responseContentType]", $(@el)).val()
|
||||
opts.requestContentType = $("div select[name=parameterContentType]", $(@el)).val()
|
||||
|
||||
$(".response_throbber", $(@el)).show()
|
||||
if isFileUpload
|
||||
@handleFileUpload map, form
|
||||
else
|
||||
@model.do(map, opts, @showCompleteStatus, @showErrorStatus, @)
|
||||
|
||||
success: (response, parent) ->
|
||||
parent.showCompleteStatus response
|
||||
|
||||
handleFileUpload: (map, form) ->
|
||||
for o in form.serializeArray()
|
||||
if(o.value? && jQuery.trim(o.value).length > 0)
|
||||
map[o.name] = o.value
|
||||
|
||||
# requires HTML5 compatible browser
|
||||
bodyParam = new FormData()
|
||||
params = 0
|
||||
|
||||
# add params
|
||||
for param in @model.parameters
|
||||
if param.paramType is 'form' or param.in is 'formData'
|
||||
if param.type.toLowerCase() isnt 'file' and map[param.name] != undefined
|
||||
bodyParam.append(param.name, map[param.name])
|
||||
|
||||
# headers in operation
|
||||
headerParams = {}
|
||||
for param in @model.parameters
|
||||
if param.paramType is 'header'
|
||||
headerParams[param.name] = map[param.name]
|
||||
|
||||
# add files
|
||||
for el in form.find('input[type~="file"]')
|
||||
if typeof el.files[0] isnt 'undefined'
|
||||
bodyParam.append($(el).attr('name'), el.files[0])
|
||||
params += 1
|
||||
|
||||
@invocationUrl =
|
||||
if @model.supportHeaderParams()
|
||||
headerParams = @model.getHeaderParams(map)
|
||||
delete headerParams['Content-Type']
|
||||
@model.urlify(map, false)
|
||||
else
|
||||
@model.urlify(map, true)
|
||||
|
||||
$(".request_url", $(@el)).html("<pre></pre>")
|
||||
$(".request_url pre", $(@el)).text(@invocationUrl);
|
||||
|
||||
obj =
|
||||
type: @model.method
|
||||
url: @invocationUrl
|
||||
headers: headerParams
|
||||
data: bodyParam
|
||||
dataType: 'json'
|
||||
contentType: false
|
||||
processData: false
|
||||
error: (data, textStatus, error) =>
|
||||
@showErrorStatus(@wrap(data), @)
|
||||
success: (data) =>
|
||||
@showResponse(data, @)
|
||||
complete: (data) =>
|
||||
@showCompleteStatus(@wrap(data), @)
|
||||
|
||||
# apply authorizations
|
||||
if window.authorizations
|
||||
window.authorizations.apply obj
|
||||
|
||||
if params is 0
|
||||
obj.data.append("fake", "true");
|
||||
|
||||
jQuery.ajax(obj)
|
||||
false
|
||||
# end of file-upload nastiness
|
||||
|
||||
# wraps a jquery response as a shred response
|
||||
|
||||
wrap: (data) ->
|
||||
headers = {}
|
||||
headerArray = data.getAllResponseHeaders().split("\r")
|
||||
for i in headerArray
|
||||
h = i.match(/^([^:]*?):(.*)$/)
|
||||
if(!h)
|
||||
h = []
|
||||
h.shift()
|
||||
if (h[0] != undefined && h[1] != undefined)
|
||||
headers[h[0].trim()] = h[1].trim()
|
||||
|
||||
o = {}
|
||||
o.content = {}
|
||||
o.content.data = data.responseText
|
||||
o.headers = headers
|
||||
o.request = {}
|
||||
o.request.url = @invocationUrl
|
||||
o.status = data.status
|
||||
o
|
||||
|
||||
getSelectedValue: (select) ->
|
||||
if !select.multiple
|
||||
select.value
|
||||
else
|
||||
options = []
|
||||
options.push opt.value for opt in select.options when opt.selected
|
||||
if options.length > 0
|
||||
options
|
||||
else
|
||||
null
|
||||
|
||||
# handler for hide response link
|
||||
hideResponse: (e) ->
|
||||
e?.preventDefault()
|
||||
$(".response", $(@el)).slideUp()
|
||||
$(".response_hider", $(@el)).fadeOut()
|
||||
|
||||
|
||||
# Show response from server
|
||||
showResponse: (response) ->
|
||||
prettyJson = JSON.stringify(response, null, "\t").replace(/\n/g, "<br>")
|
||||
$(".response_body", $(@el)).html escape(prettyJson)
|
||||
|
||||
# Show error from server
|
||||
showErrorStatus: (data, parent) ->
|
||||
parent.showStatus data
|
||||
|
||||
# show the status codes
|
||||
showCompleteStatus: (data, parent) ->
|
||||
parent.showStatus data
|
||||
|
||||
# Adapted from http://stackoverflow.com/a/2893259/454004
|
||||
formatXml: (xml) ->
|
||||
reg = /(>)(<)(\/*)/g
|
||||
wsexp = /[ ]*(.*)[ ]+\n/g
|
||||
contexp = /(<.+>)(.+\n)/g
|
||||
xml = xml.replace(reg, '$1\n$2$3').replace(wsexp, '$1\n').replace(contexp, '$1\n$2')
|
||||
pad = 0
|
||||
formatted = ''
|
||||
lines = xml.split('\n')
|
||||
indent = 0
|
||||
lastType = 'other'
|
||||
# 4 types of tags - single, closing, opening, other (text, doctype, comment) - 4*4 = 16 transitions
|
||||
transitions =
|
||||
'single->single': 0
|
||||
'single->closing': -1
|
||||
'single->opening': 0
|
||||
'single->other': 0
|
||||
'closing->single': 0
|
||||
'closing->closing': -1
|
||||
'closing->opening': 0
|
||||
'closing->other': 0
|
||||
'opening->single': 1
|
||||
'opening->closing': 0
|
||||
'opening->opening': 1
|
||||
'opening->other': 1
|
||||
'other->single': 0
|
||||
'other->closing': -1
|
||||
'other->opening': 0
|
||||
'other->other': 0
|
||||
|
||||
for ln in lines
|
||||
do (ln) ->
|
||||
|
||||
types =
|
||||
# is this line a single tag? ex. <br />
|
||||
single: Boolean(ln.match(/<.+\/>/))
|
||||
# is this a closing tag? ex. </a>
|
||||
closing: Boolean(ln.match(/<\/.+>/))
|
||||
# is this even a tag (that's not <!something>)
|
||||
opening: Boolean(ln.match(/<[^!?].*>/))
|
||||
|
||||
[type] = (key for key, value of types when value)
|
||||
type = if type is undefined then 'other' else type
|
||||
|
||||
fromTo = lastType + '->' + type
|
||||
lastType = type
|
||||
padding = ''
|
||||
|
||||
indent += transitions[fromTo]
|
||||
padding = (' ' for j in [0...(indent)]).join('')
|
||||
if fromTo == 'opening->closing'
|
||||
#substr removes line break (\n) from prev loop
|
||||
formatted = formatted.substr(0, formatted.length - 1) + ln + '\n'
|
||||
else
|
||||
formatted += padding + ln + '\n'
|
||||
|
||||
formatted
|
||||
|
||||
|
||||
# puts the response data in UI
|
||||
showStatus: (response) ->
|
||||
if response.content is undefined
|
||||
content = response.data
|
||||
url = response.url
|
||||
else
|
||||
content = response.content.data
|
||||
url = response.request.url
|
||||
headers = response.headers
|
||||
|
||||
# if server is nice, and sends content-type back, we can use it
|
||||
contentType = null
|
||||
if headers
|
||||
contentType = headers["Content-Type"] or headers["content-type"]
|
||||
if contentType
|
||||
contentType = contentType.split(";")[0].trim()
|
||||
|
||||
$(".response_body", $(@el)).removeClass 'json'
|
||||
$(".response_body", $(@el)).removeClass 'xml'
|
||||
|
||||
supportsAudioPlayback = (contentType) ->
|
||||
audioElement = document.createElement('audio')
|
||||
return !!(audioElement.canPlayType && audioElement.canPlayType(contentType).replace(/no/, ''))
|
||||
|
||||
if !content
|
||||
code = $('<code />').text("no content")
|
||||
pre = $('<pre class="json" />').append(code)
|
||||
else if contentType is "application/json" || /\+json$/.test(contentType)
|
||||
json = null
|
||||
try
|
||||
json = JSON.stringify(JSON.parse(content), null, " ")
|
||||
catch e
|
||||
json = "can't parse JSON. Raw result:\n\n" + content
|
||||
code = $('<code />').text(json)
|
||||
pre = $('<pre class="json" />').append(code)
|
||||
else if contentType is "application/xml" || /\+xml$/.test(contentType)
|
||||
code = $('<code />').text(@formatXml(content))
|
||||
pre = $('<pre class="xml" />').append(code)
|
||||
else if contentType is "text/html"
|
||||
code = $('<code />').html(_.escape(content))
|
||||
pre = $('<pre class="xml" />').append(code)
|
||||
else if /^image\//.test(contentType)
|
||||
pre = $('<img>').attr('src',url)
|
||||
else if /^audio\//.test(contentType) and supportsAudioPlayback(contentType)
|
||||
pre = $('<audio controls>').append($('<source>').attr('src', url).attr('type', contentType))
|
||||
else
|
||||
# don't know what to render!
|
||||
code = $('<code />').text(content)
|
||||
pre = $('<pre class="json" />').append(code)
|
||||
|
||||
response_body = pre
|
||||
$(".request_url", $(@el)).html("<pre></pre>")
|
||||
$(".request_url pre", $(@el)).text(url);
|
||||
$(".response_code", $(@el)).html "<pre>" + response.status + "</pre>"
|
||||
$(".response_body", $(@el)).html response_body
|
||||
$(".response_headers", $(@el)).html "<pre>" + _.escape(JSON.stringify(response.headers, null, " ")).replace(/\n/g, "<br>") + "</pre>"
|
||||
$(".response", $(@el)).slideDown()
|
||||
$(".response_hider", $(@el)).show()
|
||||
$(".response_throbber", $(@el)).hide()
|
||||
response_body_el = $('.response_body', $(@el))[0]
|
||||
# only highlight the response if response is less than threshold, default state is highlight response
|
||||
opts = @options.swaggerOptions
|
||||
if opts.highlightSizeThreshold && response.data.length > opts.highlightSizeThreshold then response_body_el else hljs.highlightBlock(response_body_el)
|
||||
|
||||
toggleOperationContent: ->
|
||||
elem = $('#' + Docs.escapeResourceName(@parentId + "_" + @nickname + "_content"))
|
||||
if elem.is(':visible') then Docs.collapseOperation(elem) else Docs.expandOperation(elem)
|
||||
@@ -1,59 +0,0 @@
|
||||
class ResourceView extends Backbone.View
|
||||
initialize: (opts={}) ->
|
||||
@auths = opts.auths
|
||||
if "" is @model.description
|
||||
@model.description = null
|
||||
if @model.description?
|
||||
@model.summary = @model.description
|
||||
|
||||
render: ->
|
||||
methods = {}
|
||||
|
||||
|
||||
$(@el).html(Handlebars.templates.resource(@model))
|
||||
|
||||
# Render each operation
|
||||
for operation in @model.operationsArray
|
||||
counter = 0
|
||||
|
||||
id = operation.nickname
|
||||
while typeof methods[id] isnt 'undefined'
|
||||
id = id + "_" + counter
|
||||
counter += 1
|
||||
|
||||
methods[id] = operation
|
||||
|
||||
operation.nickname = id
|
||||
operation.parentId = @model.id
|
||||
@addOperation operation
|
||||
|
||||
$('.toggleEndpointList', @el).click(this.callDocs.bind(this, 'toggleEndpointListForResource'))
|
||||
$('.collapseResource', @el).click(this.callDocs.bind(this, 'collapseOperationsForResource'))
|
||||
$('.expandResource', @el).click(this.callDocs.bind(this, 'expandOperationsForResource'))
|
||||
|
||||
return @
|
||||
|
||||
addOperation: (operation) ->
|
||||
|
||||
operation.number = @number
|
||||
|
||||
# Render an operation and add it to operations li
|
||||
operationView = new OperationView({
|
||||
model: operation,
|
||||
tagName: 'li',
|
||||
className: 'endpoint',
|
||||
swaggerOptions: @options.swaggerOptions,
|
||||
auths: @auths,
|
||||
parent: @
|
||||
})
|
||||
$('.endpoints', $(@el)).append operationView.render().el
|
||||
|
||||
@number++
|
||||
|
||||
#
|
||||
# Generic Event handler (`Docs` is global)
|
||||
#
|
||||
|
||||
callDocs: (fnName, e) ->
|
||||
e.preventDefault()
|
||||
Docs[fnName](e.currentTarget.getAttribute('data-id'))
|
||||
@@ -1,25 +0,0 @@
|
||||
class StatusCodeView extends Backbone.View
|
||||
initialize: ->
|
||||
|
||||
render: ->
|
||||
template = @template()
|
||||
$(@el).html(template(@model))
|
||||
|
||||
models = this.options.parent.options.parent.options.parent.model.models
|
||||
|
||||
if models.hasOwnProperty @model.responseModel
|
||||
models = this.options.parent.options.parent.options.parent.model.models
|
||||
responseModel =
|
||||
sampleJSON: JSON.stringify(models[@model.responseModel].createJSONSample(), null, 2)
|
||||
isParam: false
|
||||
signature: models[@model.responseModel].getMockSignature()
|
||||
|
||||
responseModelView = new SignatureView({model: responseModel, tagName: 'div'})
|
||||
$('.model-signature', @$el).append responseModelView.render().el
|
||||
else
|
||||
$('.model-signature', @$el).html ''
|
||||
@
|
||||
|
||||
template: ->
|
||||
Handlebars.templates.status_code
|
||||
|
||||
Reference in New Issue
Block a user