merged from develop_2.0

This commit is contained in:
Tony Tam
2015-02-01 02:27:43 -08:00
parent 1be0f9fa35
commit 038443fc22
87 changed files with 11991 additions and 14394 deletions

View File

@@ -26,12 +26,7 @@ class SwaggerUi extends Backbone.Router
@render()
@options.progress = (d) => @showMessage(d)
@options.failure = (d) =>
if @api and @api.isValid is false
log "not a valid 2.0 spec, loading legacy client"
@api = new SwaggerApi(@options)
@api.build()
else
@onLoadFailure(d)
@onLoadFailure(d)
# Create view to handle the header inputs
@headerView = new HeaderView({el: $('#header')})
@@ -41,11 +36,11 @@ class SwaggerUi extends Backbone.Router
# Set an option after initializing
setOption: (option,value) ->
@options[option] = value
@options[option] = value
# Get the value of a previously set option
getOption: (option) ->
@options[option]
@options[option]
# Event handler for when url/key is received from user
updateSwaggerUi: (data) ->
@@ -57,7 +52,7 @@ class SwaggerUi extends Backbone.Router
# Initialize the API object
@mainView?.clear()
url = @options.url
if url.indexOf("http") isnt 0
if url && url.indexOf("http") isnt 0
url = @buildUrl(window.location.href.toString(), url)
@options.url = url
@@ -87,6 +82,7 @@ class SwaggerUi extends Backbone.Router
switch @options.docExpansion
when "full" then @expandAll()
when "list" then @listAll()
@renderGFM()
@options.onComplete(@api, @) if @options.onComplete
setTimeout(
=>
@@ -95,7 +91,6 @@ class SwaggerUi extends Backbone.Router
)
buildUrl: (base, url) ->
log "base is " + base
if url.indexOf("/") is 0
parts = base.split("/")
base = parts[0] + "//" + parts[2]
@@ -125,4 +120,9 @@ class SwaggerUi extends Backbone.Router
@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

View File

@@ -0,0 +1,5 @@
Handlebars.registerHelper('sanitize', (html) ->
# Strip the script tags from the html, and return it as a Handlebars.SafeString
html = html.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '')
return new Handlebars.SafeString(html)
)

View File

@@ -12,7 +12,6 @@ class BasicAuthButton extends Backbone.View
"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))

View File

@@ -11,20 +11,6 @@ class MainView extends Backbone.View
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
url = @model.info.licenseUrl
@model.info.license = {}
@model.info.license.name = name
@model.info.license.url = url
if !@model.info
@model.info = {}
if !@model.info.version
@model.info.version = @model.apiVersion
if @model.swaggerVersion is "2.0"
if "validatorUrl" of opts.swaggerOptions
# Validator URL specified explicitly
@@ -62,6 +48,13 @@ class MainView extends Backbone.View
resource.id = id
resources[id] = resource
@addResource resource, @model.auths
$('.propWrap').hover(
->
$('.optionsWrapper', $(this)).show()
,->
$('.optionsWrapper', $(this)).hide()
)
@
addResource: (resource, auths) ->

View File

@@ -16,7 +16,7 @@ class OperationView extends Backbone.View
@
mouseEnter: (e) ->
elem = $(e.currentTarget.parentNode).find('#api_information_panel')
elem = $(@el).find '.content'
x = e.pageX
y = e.pageY
scX = $(window).scrollLeft()
@@ -44,7 +44,7 @@ class OperationView extends Backbone.View
$(e.currentTarget.parentNode).find('#api_information_panel').hide()
render: ->
isMethodSubmissionSupported = jQuery.inArray(@model.method, @model.supportedSubmitMethods()) >= 0
isMethodSubmissionSupported = true #jQuery.inArray(@model.method, @model.supportedSubmitMethods) >= 0
@model.isReadOnly = true unless isMethodSubmissionSupported
# 1.2 syntax for description was `notes`
@@ -52,9 +52,10 @@ class OperationView extends Backbone.View
if @model.description
@model.description = @model.description.replace(/(?:\r\n|\r|\n)/g, '<br />')
@model.oauth = null
if @model.authorizations
if Array.isArray @model.authorizations
for auths in @model.authorizations
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]
@@ -62,10 +63,12 @@ class OperationView extends Backbone.View
@model.oauth = {}
@model.oauth.scopes = []
for k, v of auth.value.scopes
o = {scope: k, description: v}
@model.oauth.scopes.push o
scopeIndex = auths[key].indexOf k
if scopeIndex >= 0
o = {scope: k, description: v}
@model.oauth.scopes.push o
else
for k, v of @model.authorizations
for k, v of modelAuths
if k == "oauth2"
if @model.oauth == null
@model.oauth = {}
@@ -88,14 +91,29 @@ class OperationView extends Backbone.View
if typeof @model.responseMessages is 'undefined'
@model.responseMessages = []
$(@el).html(Handlebars.templates.operation(@model))
if @model.responseClassSignature and @model.responseClassSignature != 'string'
# 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'})
$('.model-signature', $(@el)).append responseSignatureView.render().el
else
@@ -109,7 +127,7 @@ class OperationView extends Backbone.View
contentTypeModel.produces = @model.produces
for param in @model.parameters
type = param.type || param.dataType
type = param.type || param.dataType || ''
if typeof type is 'undefined'
schema = param.schema
if schema and schema['$ref']
@@ -144,7 +162,7 @@ class OperationView extends Backbone.View
# Render status codes
statusCodeView = new StatusCodeView({model: statusCode, tagName: 'tr'})
$('.operation-status', $(@el)).append statusCodeView.render().el
submitOperation: (e) ->
e?.preventDefault()
# Check for errors
@@ -182,7 +200,7 @@ class OperationView extends Backbone.View
if(o.value? && jQuery.trim(o.value).length > 0)
map[o.name] = o.value
for o in form.find("select")
for o in form.find("select")
val = this.getSelectedValue o
if(val? && jQuery.trim(val).length > 0)
map[o.name] = val
@@ -226,7 +244,7 @@ class OperationView extends Backbone.View
bodyParam.append($(el).attr('name'), el.files[0])
params += 1
@invocationUrl =
@invocationUrl =
if @model.supportHeaderParams()
headerParams = @model.getHeaderParams(map)
delete headerParams['Content-Type']
@@ -234,10 +252,10 @@ class OperationView extends Backbone.View
else
@model.urlify(map, true)
$(".request_url", $(@el)).html("<pre></pre>")
$(".request_url", $(@el)).html("<pre></pre>")
$(".request_url pre", $(@el)).text(@invocationUrl);
obj =
obj =
type: @model.method
url: @invocationUrl
headers: headerParams
@@ -269,7 +287,10 @@ class OperationView extends Backbone.View
headers = {}
headerArray = data.getAllResponseHeaders().split("\r")
for i in headerArray
h = i.split(':')
h = i.match(/^([^:]*?):(.*)$/)
if(!h)
h = []
h.shift()
if (h[0] != undefined && h[1] != undefined)
headers[h[0].trim()] = h[1].trim()
@@ -283,12 +304,12 @@ class OperationView extends Backbone.View
o
getSelectedValue: (select) ->
if !select.multiple
if !select.multiple
select.value
else
options = []
options.push opt.value for opt in select.options when opt.selected
if options.length > 0
if options.length > 0
options
else
null
@@ -324,7 +345,7 @@ class OperationView extends Backbone.View
lines = xml.split('\n')
indent = 0
lastType = 'other'
# 4 types of tags - single, closing, opening, other (text, doctype, comment) - 4*4 = 16 transitions
# 4 types of tags - single, closing, opening, other (text, doctype, comment) - 4*4 = 16 transitions
transitions =
'single->single': 0
'single->closing': -1
@@ -368,9 +389,9 @@ class OperationView extends Backbone.View
formatted = formatted.substr(0, formatted.length - 1) + ln + '\n'
else
formatted += padding + ln + '\n'
formatted
# puts the response data in UI
showStatus: (response) ->
@@ -389,6 +410,9 @@ class OperationView extends Backbone.View
if contentType
contentType = contentType.split(";")[0].trim()
$(".response_body", $(@el)).removeClass 'json'
$(".response_body", $(@el)).removeClass 'xml'
if !content
code = $('<code />').text("no content")
pre = $('<pre class="json" />').append(code)
@@ -414,7 +438,7 @@ class OperationView extends Backbone.View
pre = $('<pre class="json" />').append(code)
response_body = pre
$(".request_url", $(@el)).html("<pre></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

View File

@@ -21,7 +21,7 @@ class ParameterView extends Backbone.View
@model.type = type
@model.paramType = @model.in || @model.paramType
@model.isBody = true if @model.paramType == 'body'
@model.isBody = true if @model.paramType == 'body' or @model.in == 'body'
@model.isFile = true if type and type.toLowerCase() == 'file'
@model.default = (@model.default || @model.defaultValue)