#1248 createXMLSample show XML example for parameter

This commit is contained in:
Anna Bodnia
2016-01-15 15:29:37 +02:00
parent 4443883546
commit ea688851f9
6 changed files with 153 additions and 58 deletions

View File

@@ -15,10 +15,12 @@ SwaggerUi.Views.ParameterView = Backbone.View.extend({
var type = this.model.type || this.model.dataType;
var modelType = this.model.modelSignature.type;
var modelDefinitions = this.model.modelSignature.definitions;
var schema = this.model.schema || {};
var consumes = this.model.consumes || [];
if (typeof type === 'undefined') {
var schema = this.model.schema;
if (schema && schema.$ref) {
if (schema.$ref) {
var ref = schema.$ref;
if (ref.indexOf('#/definitions/') === 0) {
type = ref.substring('#/definitions/'.length);
@@ -45,11 +47,18 @@ SwaggerUi.Views.ParameterView = Backbone.View.extend({
this.model.isList = true;
}
var isXML = consumes.filter(function (val) {
if (val.indexOf('xml') > -1) {
return true;
}
}).length;
var template = this.template();
$(this.el).html(template(this.model));
var signatureModel = {
sampleJSON: SwaggerUi.partials.signature.createParameterJSONSample(modelType, modelDefinitions),
sampleXML: isXML ? SwaggerUi.partials.signature.createXMLSample(schema, modelDefinitions) : false,
isParam: true,
signature: SwaggerUi.partials.signature.getParameterModelSignature(modelType, modelDefinitions),
defaultRendering: this.model.defaultRendering

View File

@@ -4,7 +4,9 @@ SwaggerUi.Views.SignatureView = Backbone.View.extend({
events: {
'click a.description-link' : 'switchToDescription',
'click a.snippet-link' : 'switchToSnippet',
'mousedown .snippet' : 'snippetToTextArea'
'click a.snippet-xml-link' : 'switchToXMLSnippet',
'mousedown .snippet_json' : 'jsonSnippetMouseDown',
'mousedown .snippet_xml' : 'xmlSnippetMouseDown'
},
initialize: function () {
@@ -20,12 +22,6 @@ SwaggerUi.Views.SignatureView = Backbone.View.extend({
} else {
this.switchToSnippet();
}
this.isParam = this.model.isParam;
if (this.isParam) {
$('.notice', $(this.el)).text('Click to set as parameter value');
}
return this;
},
@@ -38,33 +34,59 @@ SwaggerUi.Views.SignatureView = Backbone.View.extend({
$('.description', $(this.el)).show();
$('.description-link', $(this.el)).addClass('selected');
$('.snippet-link', $(this.el)).removeClass('selected');
$('.snippet-xml-link', $(this.el)).removeClass('selected');
},
// handler for show sample
switchToSnippet: function(e){
if (e) { e.preventDefault(); }
$('.snippet_json', $(this.el)).show();
$('.description', $(this.el)).hide();
$('.snippet', $(this.el)).show();
$('.snippet_xml', $(this.el)).hide();
$('.snippet-link', $(this.el)).addClass('selected');
$('.description-link', $(this.el)).removeClass('selected');
$('.snippet-xml-link', $(this.el)).removeClass('selected');
},
switchToXMLSnippet: function (e) {
if (e) { e.preventDefault();}
$('.snippet_xml', $(this.el)).show();
$('.snippet_json', $(this.el)).hide();
$('.description', $(this.el)).hide();
$('.snippet-xml-link', $(this.el)).addClass('selected');
$('.description-link', $(this.el)).removeClass('selected');
$('.snippet-link', $(this.el)).removeClass('selected');
},
// handler for snippet to text area
snippetToTextArea: function(e) {
if (this.isParam) {
snippetToTextArea: function(val) {
var textArea = $('textarea', $(this.el.parentNode.parentNode.parentNode));
// Fix for bug in IE 10/11 which causes placeholder text to be copied to "value"
if ($.trim(textArea.val()) === '' || textArea.prop('placeholder') === textArea.val()) {
textArea.val(val);
// TODO move this code outside of the view and expose an event instead
if( this.model.jsonEditor && this.model.jsonEditor.isEnabled()){
this.model.jsonEditor.setValue(JSON.parse(this.model.sampleJSON));
}
}
},
jsonSnippetMouseDown: function (e) {
if (this.model.isParam) {
if (e) { e.preventDefault(); }
var textArea = $('textarea', $(this.el.parentNode.parentNode.parentNode));
this.snippetToTextArea(this.model.sampleJSON);
}
},
// Fix for bug in IE 10/11 which causes placeholder text to be copied to "value"
if ($.trim(textArea.val()) === '' || textArea.prop('placeholder') === textArea.val()) {
textArea.val(this.model.sampleJSON);
// TODO move this code outside of the view and expose an event instead
if( this.model.jsonEditor && this.model.jsonEditor.isEnabled()){
this.model.jsonEditor.setValue(JSON.parse(this.model.sampleJSON));
}
}
xmlSnippetMouseDown: function (e) {
if (this.model.isParam) {
if (e) { e.preventDefault(); }
this.snippetToTextArea(this.model.sampleXML);
}
}
});

View File

@@ -811,6 +811,8 @@ SwaggerUi.partials.signature = (function () {
xml = definition.xml || {};
$ref = definition.$ref;
if (!_.isObject(definition)) { return getErrorMessage(); }
if (_.isString($ref)) {
return getModelXML($ref, models);
}