From af39ae06037b1b6936594e91f11c9deee382055c Mon Sep 17 00:00:00 2001 From: Waldek Kozba <100assc@gmail.com> Date: Fri, 3 Apr 2015 14:31:23 +0000 Subject: [PATCH 1/2] 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. --- src/main/javascript/helpers/handlebars.js | 21 +++++++++ src/main/javascript/view/OperationView.js | 52 ++++++++++++++++++++- src/main/javascript/view/ParameterView.js | 4 +- src/main/template/param.handlebars | 7 +-- src/main/template/param_list.handlebars | 5 +- src/main/template/param_required.handlebars | 7 +-- 6 files changed, 80 insertions(+), 16 deletions(-) diff --git a/src/main/javascript/helpers/handlebars.js b/src/main/javascript/helpers/handlebars.js index 58a43d70530..cfd05b1c442 100644 --- a/src/main/javascript/helpers/handlebars.js +++ b/src/main/javascript/helpers/handlebars.js @@ -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>/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 = ""; + } else { + result = ""; + } + return new Handlebars.SafeString(result); }); \ No newline at end of file diff --git a/src/main/javascript/view/OperationView.js b/src/main/javascript/view/OperationView.js index d80652e3914..d26e72a07ac 100644 --- a/src/main/javascript/view/OperationView.js +++ b/src/main/javascript/view/OperationView.js @@ -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; } + }); diff --git a/src/main/javascript/view/ParameterView.js b/src/main/javascript/view/ParameterView.js index ba0cb6388a3..4a1ede5b100 100644 --- a/src/main/javascript/view/ParameterView.js +++ b/src/main/javascript/view/ParameterView.js @@ -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); } }); }, diff --git a/src/main/template/param.handlebars b/src/main/template/param.handlebars index 8d3f6731d0e..ebc327d1a71 100644 --- a/src/main/template/param.handlebars +++ b/src/main/template/param.handlebars @@ -21,11 +21,8 @@
{{else}} - {{#if default}} - - {{else}} - - {{/if}} + {{#renderTextParam this}} + {{/renderTextParam}} {{/if}} {{/if}} diff --git a/src/main/template/param_list.handlebars b/src/main/template/param_list.handlebars index e4b1923a6fd..f0079be4c96 100644 --- a/src/main/template/param_list.handlebars +++ b/src/main/template/param_list.handlebars @@ -1,9 +1,10 @@ {{#if required}} {{name}} -{{/if}} +{{else}} {{name}} +{{/if}} - {{#if required}} {{else}} {{#if default}} diff --git a/src/main/template/param_required.handlebars b/src/main/template/param_required.handlebars index d98a537a84a..7ca2d574cc6 100644 --- a/src/main/template/param_required.handlebars +++ b/src/main/template/param_required.handlebars @@ -18,11 +18,8 @@ {{#if isFile}} {{else}} - {{#if default}} - - {{else}} - - {{/if}} + {{#renderTextParam this}} + {{/renderTextParam}} {{/if}} {{/if}} From ad56131a6d9a2b02ca8ca8f5e519c62ad904d49c Mon Sep 17 00:00:00 2001 From: Waldek Kozba <100assc@gmail.com> Date: Fri, 3 Apr 2015 19:02:19 +0000 Subject: [PATCH 2/2] Corrected for Travis build. --- src/main/javascript/helpers/handlebars.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/javascript/helpers/handlebars.js b/src/main/javascript/helpers/handlebars.js index cfd05b1c442..438f032aaa5 100644 --- a/src/main/javascript/helpers/handlebars.js +++ b/src/main/javascript/helpers/handlebars.js @@ -16,13 +16,13 @@ Handlebars.registerHelper('renderTextParam', function(param) { } if(isArray) { - result = ""; + result = ''; } else { - result = ""; + result = ''; } return new Handlebars.SafeString(result); -}); \ No newline at end of file +});