Multiple values for array type parameters as separated lines in the textarea.

Improved required parameter handling for the array type.
Improved default value handling for the array type.
This commit is contained in:
Waldek Kozba
2015-04-03 14:31:23 +00:00
parent 1ea77868aa
commit af39ae0603
6 changed files with 80 additions and 16 deletions

View File

@@ -4,4 +4,25 @@ Handlebars.registerHelper('sanitize', function(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);
});
Handlebars.registerHelper('renderTextParam', function(param) {
var result;
var isArray = param.type.toLowerCase() === 'array' || param.allowMultiple;
var defaultValue = isArray && Array.isArray(param.default) ? param.default.join('\n') : param.default;
if (typeof defaultValue === 'undefined') {
defaultValue = '';
}
if(isArray) {
result = "<textarea class='body-textarea" + (param.required ? " required" : "") + "' name='" + param.name + "'"
+ " placeholder='Provide multiple values in new lines" + (param.required ? " (at least one required)." : ".") + "'>"
+ defaultValue + "</textarea>";
} else {
result = "<input class='parameter'" + (param.required ? " class='required'" : "") + " minlength='" + (param.required ? 1 : 0)
+ "' name='" + param.name +"' placeholder='" + (param.required ? "(required)" : "") + "'"
+ " type='text' value='" + defaultValue + "'/>";
}
return new Handlebars.SafeString(result);
});

View File

@@ -275,6 +275,20 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
error_free = false;
}
});
form.find('select.required').each(function() {
$(this).removeClass('error');
if (this.selectedIndex === -1) {
$(this).addClass('error');
$(this).wiggle({
callback: (function(_this) {
return function() {
$(_this).focus();
};
})(this)
});
error_free = false;
}
});
if (error_free) {
map = {};
opts = {
@@ -295,8 +309,9 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
ref2 = form.find('textarea');
for (m = 0, len1 = ref2.length; m < len1; m++) {
o = ref2[m];
if ((o.value !== null) && jQuery.trim(o.value).length > 0) {
map[o.name] = o.value;
val = this.getTextAreaValue(o);
if ((val !== null) && jQuery.trim(val).length > 0) {
map[o.name] = val;
}
}
ref3 = form.find('select');
@@ -661,5 +676,38 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
} else {
Docs.expandOperation(elem);
}
},
getTextAreaValue: function(textArea) {
var param, parsed, result, i;
if (textArea.value === null || jQuery.trim(textArea.value).length === 0) {
return null;
}
param = this.getParamByName(textArea.name);
if (param && param.type && param.type.toLowerCase() === 'array') {
parsed = textArea.value.split('\n');
result = [];
for (i = 0; i < parsed.length; i++) {
if (parsed[i] !== null && jQuery.trim(parsed[i]).length > 0) {
result.push(parsed[i]);
}
}
return result.length > 0 ? result : null;
} else {
return textArea.value;
}
},
getParamByName: function(name) {
var i;
if (this.model.parameters) {
for(i = 0; i < this.model.parameters.length; i++) {
if (this.model.parameters[i].name === name) {
return this.model.parameters[i];
}
}
}
return null;
}
});

View File

@@ -4,9 +4,9 @@ SwaggerUi.Views.ParameterView = Backbone.View.extend({
initialize: function(){
Handlebars.registerHelper('isArray', function(param, opts) {
if (param.type.toLowerCase() === 'array' || param.allowMultiple) {
opts.fn(this);
return opts.fn(this);
} else {
opts.inverse(this);
return opts.inverse(this);
}
});
},