forked from jensoleg/swagger-ui
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathParameterView.js
112 lines (96 loc) · 3.06 KB
/
ParameterView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
/*
* [TODO] defaultProperties is not take in the required properties into consideration, this implementation respects the specs of JSON Editor v0.7.22
{
{
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
},
defaultProperties": ["name"]
}
*/
function setDefaultProperties(obj) {
if (obj instanceof Object) {
for (var k in obj){
if(obj.hasOwnProperty("type") && obj.type == "object") {
obj.defaultProperties = obj.required ? obj.required : [];
}
// recursive call to setDefaultProperties
setDefaultProperties( obj[k] );
}
} else {
// not an Object, break the recursion.
};
}
SwaggerUi.Views.ParameterView = Backbone.View.extend({
initialize: function(){
Handlebars.registerHelper('isArray', function(param, opts) {
if (param.type.toLowerCase() === 'array' || param.allowMultiple) {
opts.fn(this);
} else {
opts.inverse(this);
}
});
},
render: function() {
var type = this.model.type || this.model.dataType;
if (typeof type === 'undefined') {
var schema = this.model.schema;
if (schema && schema.$ref) {
var ref = schema.$ref;
if (ref.indexOf('#/definitions/') === 0) {
type = ref.substring('#/definitions/'.length);
} else {
type = ref;
}
}
}
this.model.type = type;
this.model.paramType = this.model.in || this.model.paramType;
this.model.isBody = this.model.paramType === 'body' || this.model.in === 'body';
this.model.isFile = type && type.toLowerCase() === 'file';
this.model.default = (this.model.default || this.model.defaultValue);
if(this.model.format === 'password') {
this.model.inputType = 'password';
} else if(this.model.type === 'integer') {
this.model.inputType = 'number';
} else if(this.model.format === 'email') {
this.model.inputType = 'email';
} else {
this.model.inputType = 'text';
}
if (this.model.allowableValues) {
this.model.isList = true;
}
var template = this.template();
$(this.el).html(template(this.model));
var signatureModel = {
sampleJSON: this.model.sampleJSON,
isParam: true,
signature: this.model.signature,
defaultRendering: this.model.defaultRendering
};
var isParam = false;
if (this.model.isBody) {
isParam = true;
}
var contentTypeModel = {
isParam: isParam
};
contentTypeModel.consumes = this.model.consumes;
if (isParam) {
var parameterContentTypeView = new SwaggerUi.Views.ParameterContentTypeView({model: contentTypeModel});
$('.parameter-content-type', $(this.el)).append(parameterContentTypeView.render().el);
}
else {
var responseContentTypeView = new SwaggerUi.Views.ResponseContentTypeView({model: contentTypeModel});
$('.response-content-type', $(this.el)).append(responseContentTypeView.render().el);
}
return this;
},
template: function(){
return Handlebars.templates.param;
}
});