Compare commits

...
7 Commits
Author SHA1 Message Date
Tony Tam 356ae491aa fixed duplicate model, updated example name 2012-11-27 16:03:54 -08:00
Tony Tam 7bc8109e88 re-merged #96 2012-11-27 15:04:12 -08:00
Tony Tam 3fd37c3ef6 Merge pull request #102 from sequielo/patch-1
Update src/main/javascript/doc.js
2012-11-27 13:48:06 -08:00
Tony Tam 586c6d6292 Merge pull request #103 from pose/xml-pretty-print
Adding XML pretty printing to responses
2012-11-27 13:47:51 -08:00
Alberto Pose 12bdd38406 Adding fallback to XML when JSON response parsing fails. 2012-11-27 18:24:32 -03:00
sequielo 369b639329 Update src/main/javascript/doc.js
fixed #101: expand operations for resource when resource paramenter is an empty string.
2012-11-27 00:14:47 -02:00
Tony Tam 3f9ed33f7f added missing files per #100 2012-11-26 13:47:34 -08:00
7 changed files with 124 additions and 15 deletions
@@ -0,0 +1,19 @@
class ContentTypeView extends Backbone.View
initialize: ->
render: ->
template = @template()
$(@el).html(template(@model))
@isParam = @model.isParam
if @isParam
$('label[for=contentType]', $(@el)).text('Parameter content type:')
else
$('label[for=contentType]', $(@el)).text('Response Content Type')
@
template: ->
Handlebars.templates.content_type
@@ -130,6 +130,7 @@ class OperationView extends Backbone.View
responseContentTypeField = $('.content > .content-type > div > select[name=contentType]', $(@el)).val()
if responseContentTypeField
obj.headers = if obj.headers? then obj.headers else {}
obj.headers.accept = responseContentTypeField
jQuery.ajax(obj)
@@ -146,7 +147,7 @@ class OperationView extends Backbone.View
# Show response from server
showResponse: (response) ->
prettyJson = JSON.stringify(response, null, "\t").replace(/\n/g, "<br>")
$(".response_body", $(@el)).html prettyJson
$(".response_body", $(@el)).html escape(prettyJson)
# Show error from server
@@ -157,12 +158,74 @@ class OperationView extends Backbone.View
showCompleteStatus: (data) ->
@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: (data) ->
try
response_body = '<pre class="json"><code>' + JSON.stringify(JSON.parse(data.responseText), null, 2) + "</code></pre>"
code = $('<code />').text(JSON.stringify(JSON.parse(data.responseText), null, 2))
pre = $('<pre class="json" />').append(code)
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>"
code = $('<code />').text(@formatXml(data.responseText))
pre = $('<pre class="xml" />').append(code)
response_body = pre
$(".response_code", $(@el)).html "<pre>" + data.status + "</pre>"
$(".response_body", $(@el)).html response_body
$(".response_headers", $(@el)).html "<pre>" + data.getAllResponseHeaders() + "</pre>"
@@ -172,5 +235,5 @@ class OperationView extends Backbone.View
hljs.highlightBlock($('.response_body', $(@el))[0])
toggleOperationContent: ->
elem = $('#' + @model.resourceName + "_" + @model.nickname + "_" + @model.httpMethod + "_content");
elem = $('#' + @model.resourceName + "_" + @model.nickname + "_" + @model.httpMethod + "_" + @model.number + "_content")
if elem.is(':visible') then Docs.collapseOperation(elem) else Docs.expandOperation(elem)
@@ -4,11 +4,18 @@ class ResourceView extends Backbone.View
render: ->
$(@el).html(Handlebars.templates.resource(@model))
@number = 0
# Render each operation
@addOperation operation for operation in @model.operationsArray
@
addOperation: (operation) ->
operation.number = @number
# Render an operation and add it to operations li
operationView = new OperationView({model: operation, tagName: 'li', className: 'endpoint'})
$('.endpoints', $(@el)).append operationView.render().el
@number++
+10
View File
@@ -126,6 +126,11 @@ var Docs = {
// Expand resource
expandEndpointListForResource: function(resource) {
var resource = Docs.escapeResourceName(resource);
if (resource == '') {
$('.resource ul.endpoints').slideDown();
return;
}
$('li#resource_' + resource).addClass('active');
var elem = $('li#resource_' + resource + ' ul.endpoints');
@@ -144,6 +149,11 @@ var Docs = {
expandOperationsForResource: function(resource) {
// Make sure the resource container is open..
Docs.expandEndpointListForResource(resource);
if (resource == '') {
$('.resource ul.endpoints li.operation div.content').slideDown();
return;
}
$('li#resource_' + Docs.escapeResourceName(resource) + ' li.operation div.content').each(function() {
Docs.expandOperation($(this));
@@ -0,0 +1,8 @@
{{#if supportedContentTypes}}
<label for="contentType"></label>
<select name="contentType">
{{#each supportedContentTypes}}
<option value="{{{this}}}">{{{this}}}</option>
{{/each}}
</select>
{{/if}}
+12 -10
View File
@@ -1,32 +1,34 @@
<ul class='operations' >
<li class='{{httpMethod}} operation' id='{{resourceName}}_{{nickname}}_{{httpMethod}}'>
<li class='{{httpMethod}} operation' id='{{resourceName}}_{{nickname}}_{{httpMethod}}_{{number}}'>
<div class='heading'>
<h3>
<span class='http_method'>
<a href='#!/{{resourceName}}/{{nickname}}_{{httpMethod}}' class="toggleOperation">{{httpMethod}}</a>
<a href='#!/{{resourceName}}/{{nickname}}_{{httpMethod}}_{{number}}' class="toggleOperation">{{httpMethod}}</a>
</span>
<span class='path'>
<a href='#!/{{resourceName}}/{{nickname}}_{{httpMethod}}' class="toggleOperation">{{path}}</a>
<a href='#!/{{resourceName}}/{{nickname}}_{{httpMethod}}_{{number}}' class="toggleOperation">{{path}}</a>
</span>
</h3>
<ul class='options'>
<li>
<a href='#!/{{resourceName}}/{{nickname}}_{{httpMethod}}' class="toggleOperation">{{{summary}}}</a>
<a href='#!/{{resourceName}}/{{nickname}}_{{httpMethod}}_{{number}}' class="toggleOperation">{{{summary}}}</a>
</li>
</ul>
</div>
<div class='content' id='{{resourceName}}_{{nickname}}_{{httpMethod}}_content' style='display:none'>
<div class='content' id='{{resourceName}}_{{nickname}}_{{httpMethod}}_{{number}}_content' style='display:none'>
{{#if notes}}
<h4>Implementation Notes</h4>
<p>{{{notes}}}</p>
{{/if}}
{{#if responseClass}}
<h4>Response Model</h4>
<span class="model-signature"></span>
<h4>Response Class</h4>
{{#if responseClassSignature}}
<p><span class="model-signature"></span></p>
{{else}}
<p>{{{responseClass}}}</p>
{{/if}}
{{/if}}
<br/>
<div class="content-type" />
<form accept-charset='UTF-8' class='sandbox'>
<div style='margin:0;padding:0;display:inline'></div>
<h4>Parameters</h4>
@@ -79,4 +81,4 @@
</div>
</div>
</li>
</ul>
</ul>
+1 -1
View File
@@ -1,7 +1,7 @@
<div>
<ul class="signature-nav">
<li><a class="description-link" href="#">Model</a></li>
<li><a class="snippet-link" href="#">Example JSON</a></li>
<li><a class="snippet-link" href="#">Model Schema</a></li>
</ul>
<div>