Skip to content

Commit

Permalink
Multiple values for array type parameters as separated lines in the t…
Browse files Browse the repository at this point in the history
…extarea.

Improved required parameter handling for the array type.
Improved default value handling for the array type.
  • Loading branch information
valdemon committed Apr 3, 2015
1 parent 1ea7786 commit af39ae0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 16 deletions.
21 changes: 21 additions & 0 deletions src/main/javascript/helpers/handlebars.js
Expand Up @@ -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);
});
52 changes: 50 additions & 2 deletions src/main/javascript/view/OperationView.js
Expand Up @@ -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 = {
Expand All @@ -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');
Expand Down Expand Up @@ -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;
}

});
4 changes: 2 additions & 2 deletions src/main/javascript/view/ParameterView.js
Expand Up @@ -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);
}
});
},
Expand Down
7 changes: 2 additions & 5 deletions src/main/template/param.handlebars
Expand Up @@ -21,11 +21,8 @@
<input type="file" name='{{name}}'/>
<div class="parameter-content-type" />
{{else}}
{{#if default}}
<input class='parameter' minlength='0' name='{{name}}' placeholder='' type='text' value='{{default}}'/>
{{else}}
<input class='parameter' minlength='0' name='{{name}}' placeholder='' type='text' value=''/>
{{/if}}
{{#renderTextParam this}}
{{/renderTextParam}}
{{/if}}
{{/if}}

Expand Down
5 changes: 3 additions & 2 deletions src/main/template/param_list.handlebars
@@ -1,9 +1,10 @@
{{#if required}}
<td class='code required'>{{name}}</td>
{{/if}}
{{else}}
<td class='code'>{{name}}</td>
{{/if}}
<td>
<select {{#isArray this}} multiple='multiple'{{/isArray}} class='parameter' name='{{name}}'>
<select {{#isArray this}} multiple='multiple'{{/isArray}} class={{#if required}}'parameter required'{{else}}'parameter'{{/if}} name='{{name}}'>
{{#if required}}
{{else}}
{{#if default}}
Expand Down
7 changes: 2 additions & 5 deletions src/main/template/param_required.handlebars
Expand Up @@ -18,11 +18,8 @@
{{#if isFile}}
<input class='parameter' class='required' type='file' name='{{name}}'/>
{{else}}
{{#if default}}
<input class='parameter required' minlength='1' name='{{name}}' placeholder='(required)' type='text' value='{{default}}'/>
{{else}}
<input class='parameter required' minlength='1' name='{{name}}' placeholder='(required)' type='text' value=''/>
{{/if}}
{{#renderTextParam this}}
{{/renderTextParam}}
{{/if}}
{{/if}}
</td>
Expand Down

0 comments on commit af39ae0

Please sign in to comment.