github.com/qubitproducts/logspray@v0.2.14/server/swagger-ui/src/main/javascript/view/ParameterView.js (about) 1 'use strict'; 2 3 SwaggerUi.Views.ParameterView = Backbone.View.extend({ 4 events: { 5 'change [name=parameterContentType]' : 'toggleParameterSnippet' 6 }, 7 8 initialize: function(){ 9 Handlebars.registerHelper('isArray', function(param, opts) { 10 var paramType = param.type && param.type.toLowerCase(); 11 if (paramType === 'array' || param.allowMultiple) { 12 return opts.fn(this); 13 } else { 14 return opts.inverse(this); 15 } 16 }); 17 }, 18 19 render: function() { 20 var type = this.model.type || this.model.dataType; 21 var modelType = this.model.modelSignature.type; 22 var modelDefinitions = this.model.modelSignature.definitions; 23 var schema = this.model.schema || {}; 24 var consumes = this.model.consumes || []; 25 var sampleJSON, signatureView; 26 27 if (typeof type === 'undefined') { 28 if (schema.$ref) { 29 var ref = schema.$ref; 30 if (ref.indexOf('#/definitions/') === 0) { 31 type = ref.substring('#/definitions/'.length); 32 } else { 33 type = ref; 34 } 35 } 36 } 37 38 this.model.type = type; 39 this.model.paramType = this.model.in || this.model.paramType; 40 this.model.isBody = this.model.paramType === 'body' || this.model.in === 'body'; 41 this.model.isFile = type && type.toLowerCase() === 'file'; 42 43 // Allow for default === false 44 if(typeof this.model.default === 'undefined') { 45 this.model.default = this.model.defaultValue; 46 } 47 48 this.model.hasDefault = (typeof this.model.default !== 'undefined'); 49 this.model.valueId = 'm' + this.model.name + Math.random(); 50 51 if (this.model.allowableValues) { 52 this.model.isList = true; 53 } 54 55 var isXML = this.contains(consumes, 'xml'); 56 var isJSON = isXML ? this.contains(consumes, 'json') : true; 57 sampleJSON = SwaggerUi.partials.signature.createParameterJSONSample(modelType, modelDefinitions); 58 59 var template = this.template(); 60 $(this.el).html(template(this.model)); 61 62 var signatureModel = { 63 sampleJSON: isJSON ? sampleJSON : false, 64 sampleXML: sampleJSON && isXML ? SwaggerUi.partials.signature.createXMLSample('', schema, modelDefinitions, true) : false, 65 isParam: true, 66 signature: SwaggerUi.partials.signature.getParameterModelSignature(modelType, modelDefinitions), 67 defaultRendering: this.model.defaultRendering 68 }; 69 70 if (sampleJSON) { 71 signatureView = new SwaggerUi.Views.SignatureView({model: signatureModel, tagName: 'div'}); 72 $('.model-signature', $(this.el)).append(signatureView.render().el); 73 } 74 else { 75 $('.model-signature', $(this.el)).html(this.model.signature); 76 } 77 78 var isParam = false; 79 80 if( this.options.swaggerOptions.jsonEditor && this.model.isBody && this.model.schema){ 81 var $self = $(this.el); 82 this.model.jsonEditor = 83 /* global JSONEditor */ 84 new JSONEditor($('.editor_holder', $self)[0], 85 {schema: this.model.schema, startval : this.model.default, 86 ajax:true, 87 disable_properties:true, 88 disable_edit_json:true, 89 iconlib: 'swagger' }); 90 // This is so that the signature can send back the sample to the json editor 91 // TODO: SignatureView should expose an event "onSampleClicked" instead 92 signatureModel.jsonEditor = this.model.jsonEditor; 93 $('.body-textarea', $self).hide(); 94 $('.editor_holder', $self).show(); 95 $('.parameter-content-type', $self) 96 .change(function(e){ 97 if(e.target.value === 'application/xml'){ 98 $('.body-textarea', $self).show(); 99 $('.editor_holder', $self).hide(); 100 this.model.jsonEditor.disable(); 101 } 102 else { 103 $('.body-textarea', $self).hide(); 104 $('.editor_holder', $self).show(); 105 this.model.jsonEditor.enable(); 106 } 107 }); 108 } 109 110 111 if (this.model.isBody) { 112 isParam = true; 113 } 114 115 var contentTypeModel = { 116 isParam: isParam 117 }; 118 119 contentTypeModel.consumes = this.model.consumes; 120 121 if (isParam) { 122 var parameterContentTypeView = new SwaggerUi.Views.ParameterContentTypeView({model: contentTypeModel}); 123 $('.parameter-content-type', $(this.el)).append(parameterContentTypeView.render().el); 124 this.toggleParameterSnippet(); 125 } 126 127 else { 128 var responseContentTypeView = new SwaggerUi.Views.ResponseContentTypeView({model: contentTypeModel}); 129 $('.response-content-type', $(this.el)).append(responseContentTypeView.render().el); 130 this.toggleResponseSnippet(); 131 } 132 133 return this; 134 }, 135 136 contains: function (consumes, type) { 137 return consumes.filter(function (val) { 138 if (val.indexOf(type) > -1) { 139 return true; 140 } 141 }).length; 142 }, 143 144 toggleParameterSnippet: function () { 145 var contentType = this.$('[name=parameterContentType]').val(); 146 147 this.toggleSnippet(contentType); 148 }, 149 150 toggleResponseSnippet: function () { 151 var contentEl = this.$('[name=responseContentType]'); 152 153 if (!contentEl.length) { return; } 154 155 this.toggleSnippet(contentEl.val()); 156 }, 157 158 toggleSnippet: function (type) { 159 type = type || ''; 160 if (type.indexOf('xml') > -1) { 161 this.$('.snippet_xml').show(); 162 this.$('.snippet_json').hide(); 163 } else { 164 this.$('.snippet_json').show(); 165 this.$('.snippet_xml').hide(); 166 } 167 }, 168 169 // Return an appropriate template based on if the parameter is a list, readonly, required 170 template: function(){ 171 if (this.model.isList) { 172 return Handlebars.templates.param_list; 173 } else { 174 if (this.options.readOnly) { 175 if (this.model.required) { 176 return Handlebars.templates.param_readonly_required; 177 } else { 178 return Handlebars.templates.param_readonly; 179 } 180 } else { 181 if (this.model.required) { 182 return Handlebars.templates.param_required; 183 } else { 184 return Handlebars.templates.param; 185 } 186 } 187 } 188 } 189 });