This commit is contained in:
Ayush Gupta
2012-11-16 12:01:30 +05:30
15 changed files with 322 additions and 9 deletions
+1
View File
@@ -10,6 +10,7 @@ sourceFiles = [
'view/OperationView'
'view/StatusCodeView'
'view/ParameterView'
'view/SignatureView'
]
File diff suppressed because one or more lines are too long
+41
View File
@@ -334,6 +334,17 @@
return returnVal;
};
SwaggerModel.prototype.createJSONSample = function(modelToIgnore) {
var prop, result, _i, _len, _ref;
result = {};
_ref = this.properties;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
prop = _ref[_i];
result[prop.name] = prop.getSampleValue(modelToIgnore);
}
return result;
};
return SwaggerModel;
})();
@@ -363,6 +374,24 @@
}
}
SwaggerModelProperty.prototype.getSampleValue = function(modelToIgnore) {
var result;
if ((this.refModel != null) && (!(this.refModel === modelToIgnore))) {
result = this.refModel.createJSONSample(this.refModel);
} else {
if (this.isArray) {
result = this.refDataType;
} else {
result = this.dataType;
}
}
if (this.isArray) {
return [result];
} else {
return result;
}
};
SwaggerModelProperty.prototype.toString = function() {
var str;
str = this.name + ': ' + this.dataTypeWithRef;
@@ -424,6 +453,7 @@
parameter.allowableValues.values = this.resource.api.booleanValues;
}
parameter.signature = this.getSignature(parameter.dataType, this.resource.models);
parameter.sampleJSON = this.getSampleJSON(parameter.dataType, this.resource.models);
if (parameter.allowableValues != null) {
if (parameter.allowableValues.valueType === "RANGE") {
parameter.isRange = true;
@@ -478,6 +508,17 @@
}
};
SwaggerOperation.prototype.getSampleJSON = function(dataType, models) {
var isPrimitive, listType, val;
listType = this.isListType(dataType);
isPrimitive = ((listType != null) && models[listType]) || (models[dataType] != null) ? false : true;
val = isPrimitive ? void 0 : (listType != null ? models[listType].createJSONSample() : models[dataType].createJSONSample());
if (val) {
val = listType ? [val] : val;
return JSON.stringify(val, null, 2);
}
};
SwaggerOperation.prototype["do"] = function(args, callback, error) {
var body, headers;
if (args == null) {
@@ -134,7 +134,7 @@ class OperationView extends Backbone.View
# puts the response data in UI
showStatus: (data) ->
try
response_body = "<pre>" + JSON.stringify(JSON.parse(data.responseText), null, 2).replace(/\n/g, "<br>") + "</pre>"
response_body = '<pre class="json"><code>' + JSON.stringify(JSON.parse(data.responseText), null, 2) + "</code></pre>"
catch error
response_body = "<span style='color:red'>&nbsp;&nbsp;&nbsp;[unable to parse as json; raw response below]</span><br><pre>" + data.responseText + "</pre>"
$(".response_code", $(@el)).html "<pre>" + data.status + "</pre>"
@@ -143,6 +143,7 @@ class OperationView extends Backbone.View
$(".response", $(@el)).slideDown()
$(".response_hider", $(@el)).show()
$(".response_throbber", $(@el)).hide()
hljs.highlightBlock($('.response_body', $(@el))[0])
toggleOperationContent: ->
elem = $('#' + @model.resourceName + "_" + @model.nickname + "_" + @model.httpMethod + "_content");
@@ -8,6 +8,12 @@ class ParameterView extends Backbone.View
template = @template()
$(@el).html(template(@model))
if @model.sampleJSON
signatureView = new SignatureView({model: @model, tagName: 'div'})
$('.model-signature', $(@el)).append signatureView.render().el
else
$('.model-signature', $(@el)).html(@model.signature)
@
# Return an appropriate template based on if the parameter is a list, readonly, required
@@ -0,0 +1,43 @@
class SignatureView extends Backbone.View
events: {
'click a.description-link' : 'switchToDescription'
'click a.snippet-link' : 'switchToSnippet'
'mousedown .snippet' : 'snippetToTextArea'
}
initialize: ->
render: ->
template = @template()
$(@el).html(template(@model))
@switchToDescription()
@
template: ->
Handlebars.templates.signature
# handler for show signature
switchToDescription: (e) ->
e?.preventDefault()
$(".snippet", $(@el)).hide()
$(".description", $(@el)).show()
$('.description-link', $(@el)).addClass('selected')
$('.snippet-link', $(@el)).removeClass('selected')
# handler for show sample
switchToSnippet: (e) ->
e?.preventDefault()
$(".description", $(@el)).hide()
$(".snippet", $(@el)).show()
$('.snippet-link', $(@el)).addClass('selected')
$('.description-link', $(@el)).removeClass('selected')
# handler for snippet to text area
snippetToTextArea: (e) ->
e?.preventDefault()
textArea = $('textarea', $(@el.parentNode.parentNode.parentNode))
if $.trim(textArea.val()) == ''
textArea.val(@model.sampleJSON)
+135
View File
@@ -0,0 +1,135 @@
/*
Original style from softwaremaniacs.org (c) Ivan Sagalaev <Maniac@SoftwareManiacs.Org>
*/
pre code {
display: block; padding: 0.5em;
background: #F0F0F0;
}
pre code,
pre .subst,
pre .tag .title,
pre .lisp .title,
pre .clojure .built_in,
pre .nginx .title {
color: black;
}
pre .string,
pre .title,
pre .constant,
pre .parent,
pre .tag .value,
pre .rules .value,
pre .rules .value .number,
pre .preprocessor,
pre .ruby .symbol,
pre .ruby .symbol .string,
pre .aggregate,
pre .template_tag,
pre .django .variable,
pre .smalltalk .class,
pre .addition,
pre .flow,
pre .stream,
pre .bash .variable,
pre .apache .tag,
pre .apache .cbracket,
pre .tex .command,
pre .tex .special,
pre .erlang_repl .function_or_atom,
pre .markdown .header {
color: #800;
}
pre .comment,
pre .annotation,
pre .template_comment,
pre .diff .header,
pre .chunk,
pre .markdown .blockquote {
color: #888;
}
pre .number,
pre .date,
pre .regexp,
pre .literal,
pre .smalltalk .symbol,
pre .smalltalk .char,
pre .go .constant,
pre .change,
pre .markdown .bullet,
pre .markdown .link_url {
color: #080;
}
pre .label,
pre .javadoc,
pre .ruby .string,
pre .decorator,
pre .filter .argument,
pre .localvars,
pre .array,
pre .attr_selector,
pre .important,
pre .pseudo,
pre .pi,
pre .doctype,
pre .deletion,
pre .envvar,
pre .shebang,
pre .apache .sqbracket,
pre .nginx .built_in,
pre .tex .formula,
pre .erlang_repl .reserved,
pre .prompt,
pre .markdown .link_label,
pre .vhdl .attribute,
pre .clojure .attribute,
pre .coffeescript .property {
color: #88F
}
pre .keyword,
pre .id,
pre .phpdoc,
pre .title,
pre .built_in,
pre .aggregate,
pre .css .tag,
pre .javadoctag,
pre .phpdoc,
pre .yardoctag,
pre .smalltalk .class,
pre .winutils,
pre .bash .variable,
pre .apache .tag,
pre .go .typename,
pre .tex .command,
pre .markdown .strong,
pre .request,
pre .status {
font-weight: bold;
}
pre .markdown .emphasis {
font-style: italic;
}
pre .nginx .built_in {
font-weight: normal;
}
pre .coffeescript .javascript,
pre .javascript .xml,
pre .tex .formula,
pre .xml .javascript,
pre .xml .vbscript,
pre .xml .css,
pre .xml .cdata {
opacity: 0.5;
}
+67 -3
View File
@@ -1509,9 +1509,73 @@ body ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operatio
font-size: 1em;
line-height: 1.5em;
}
.model-signature span {
.model-signature .description span {
font-size: 0.9em;
line-height: 1.5em;
}
.model-signature span:nth-child(odd) { color:#333; }
.model-signature span:nth-child(even) { color:#C5862B; }
.model-signature .description span:nth-child(odd) { color:#333; }
.model-signature .description span:nth-child(even) { color:#C5862B; }
.model-signature .signature-nav a {
text-decoration: none;
color: #AAA;
}
.model-signature pre {
font-size: .85em;
line-height: 1.2em;
overflow: auto;
max-height: 200px;
cursor: pointer;
}
.model-signature pre:hover {
background-color: #ffffdd;
}
.model-signature .snippet small {
font-size: 0.75em;
}
.model-signature .signature-container {
clear: both;
}
.model-signature .signature-nav a:hover {
text-decoration: underline;
color: black;
}
.model-signature .signature-nav .selected {
color: black;
text-decoration: none;
}
.model-signature ul.signature-nav {
float: none;
clear: both;
overflow: hidden;
margin: 0;
padding: 0;
display: block;
clear: none;
float: right;
margin-right: 5px;
margin-bottom: 5px;
}
.model-signature ul.signature-nav li {
float: left;
clear: none;
margin: 0;
padding: 2px 10px;
border-right: 1px solid #dddddd;
}
.model-signature ul.signature-nav li:last-child {
padding-right: 0;
border-right: none;
}
pre code {
background: none;
}
+3
View File
@@ -3,6 +3,7 @@
<head>
<title>Swagger UI</title>
<link href='http://fonts.googleapis.com/css?family=Droid+Sans:400,700' rel='stylesheet' type='text/css'/>
<link href='css/hightlight.default.css' media='screen' rel='stylesheet' type='text/css'/>
<link href='css/screen.css' media='screen' rel='stylesheet' type='text/css'/>
<script src='lib/jquery-1.8.0.min.js' type='text/javascript'></script>
<script src='lib/jquery.slideto.min.js' type='text/javascript'></script>
@@ -13,6 +14,7 @@
<script src='lib/backbone-min.js' type='text/javascript'></script>
<script src='lib/swagger.js' type='text/javascript'></script>
<script src='swagger-ui.js' type='text/javascript'></script>
<script src='lib/highlight.7.3.pack.js' type='text/javascript'></script>
<style type="text/css">
.swagger-ui-wrap {
@@ -54,6 +56,7 @@
console.log(swaggerApi);
console.log(swaggerUi);
}
$('pre code').each(function(i, e) {hljs.highlightBlock(e)});
},
onFailure: function(data) {
if(console) {
+1 -1
View File
@@ -22,6 +22,6 @@
</td>
<td>{{{description}}}</td>
<td>
<span class="model-signature">{{{signature}}}</span>
<span class="model-signature"></span>
</td>
+1 -1
View File
@@ -18,4 +18,4 @@
</select>
</td>
<td>{{{description}}}</td>
<td><span class="model-signature">{{{signature}}}</span></td>
<td><span class="model-signature"></span></td>
+1 -1
View File
@@ -7,4 +7,4 @@
{{/if}}
</td>
<td>{{{description}}}</td>
<td><span class="model-signature">{{{signature}}}</span></td>
<td><span class="model-signature"></span></td>
@@ -7,4 +7,4 @@
{{/if}}
</td>
<td>{{{description}}}</td>
<td><span class="model-signature">{{{signature}}}</span></td>
<td><span class="model-signature"></span></td>
+1 -1
View File
@@ -25,4 +25,4 @@
<td>
<strong>{{{description}}}</strong>
</td>
<td><span class="model-signature">{{{signature}}}</span></td>
<td><span class="model-signature"></span></td>
+18
View File
@@ -0,0 +1,18 @@
<div>
<ul class="signature-nav">
<li><a class="description-link" href="#">Description</a></li>
<li><a class="snippet-link" href="#">Snippet</a></li>
</ul>
<div>
<div class="signature-container">
<div class="description">
{{{signature}}}
</div>
<div class="snippet">
<pre><code>{{sampleJSON}}</code></pre>
<small>Click to set as parameter value</small>
</div>
</div>