Merge branch 'JSONEditor' into JSONEditorMaster

Conflicts:
	dist/swagger-ui.js
	dist/swagger-ui.min.js
	src/main/less/screen.less
This commit is contained in:
unknown
2015-09-10 10:53:15 +02:00
49 changed files with 2904 additions and 2546 deletions

View File

@@ -1,3 +1,4 @@
/*global JSONEditor*/
'use strict';
window.SwaggerUi = Backbone.Router.extend({
@@ -62,6 +63,16 @@ window.SwaggerUi = Backbone.Router.extend({
this.headerView.on('update-swagger-ui', function(data) {
return that.updateSwaggerUi(data);
});
// JSon Editor custom theming
JSONEditor.defaults.iconlibs.swagger = JSONEditor.AbstractIconLib.extend({
mapping: {
collapse: 'collapse',
expand: 'expand'
},
icon_prefix: 'swagger-'
});
},
// Set an option after initializing

View File

@@ -69,6 +69,16 @@ SwaggerUi.Views.MainView = Backbone.View.extend({
this.model.validatorUrl = 'http://online.swagger.io/validator';
}
}
// JSonEditor requires type='object' to be present on defined types, we add it if it's missing
// is there any valid case were it should not be added ?
var def;
for(def in this.model.definitions){
if (this.model.definitions[def].type === null){
this.model.definitions[def].type = 'object';
}
}
},
render: function(){
@@ -119,6 +129,11 @@ SwaggerUi.Views.MainView = Backbone.View.extend({
addResource: function(resource, auths){
// Render a resource and add it to resources li
resource.id = resource.id.replace(/\s/g, '_');
// Make all definitions available at the root of the resource so that they can
// be loaded by the JSonEditor
resource.definitions = this.model.definitions;
var resourceView = new SwaggerUi.Views.ResourceView({
model: resource,
router: this.router,

View File

@@ -238,10 +238,27 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
addParameter: function(param, consumes) {
// Render a parameter
param.consumes = consumes;
// Copy this param JSON spec so that it will be available for JsonEditor
if(param.schema){
$.extend(true, param.schema, this.model.definitions[param.type]);
param.schema.definitions = this.model.definitions;
// This is required for JsonEditor to display the root properly
param.schema.type = 'object';
// This is the title that will be used by JsonEditor for the root
// Since we already display the parameter's name in the Parameter column
// We set this to space, we can't set it to null or space otherwise JsonEditor
// will replace it with the text "root" which won't look good on screen
param.schema.title = ' ';
}
var paramView = new SwaggerUi.Views.ParameterView({
model: param,
tagName: 'tr',
readOnly: this.model.isReadOnly
readOnly: this.model.isReadOnly,
swaggerOptions: this.options.swaggerOptions
});
$('.operation-params', $(this.el)).append(paramView.render().el);
},
@@ -318,6 +335,16 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
opts[key] = this.options.swaggerOptions[key];
}
}
var pi;
for(pi = 0; pi < this.model.parameters.length; pi++){
var p = this.model.parameters[pi];
if( p.jsonEditor && p.jsonEditor.isEnabled()){
var json = p.jsonEditor.getValue();
map[p.name] = JSON.stringify(json);
}
}
opts.responseContentType = $('div select[name=responseContentType]', $(this.el)).val();
opts.requestContentType = $('div select[name=parameterContentType]', $(this.el)).val();
$('.response_throbber', $(this.el)).show();

View File

@@ -62,6 +62,37 @@ SwaggerUi.Views.ParameterView = Backbone.View.extend({
var isParam = false;
if( this.options.swaggerOptions.jsonEditor && this.model.isBody && this.model.schema){
var $self = $(this.el);
this.model.jsonEditor =
/* global JSONEditor */
new JSONEditor($('.editor_holder', $self)[0],
{schema: this.model.schema, startval : this.model.default,
ajax:true,
disable_properties:true,
disable_edit_json:true,
iconlib: 'swagger' });
// This is so that the signature can send back the sample to the json editor
// TODO: SignatureView should expose an event "onSampleClicked" instead
signatureModel.jsonEditor = this.model.jsonEditor;
$('.body-textarea', $self).hide();
$('.editor_holder', $self).show();
$('.parameter-content-type', $self)
.change(function(e){
if(e.target.value === 'application/xml'){
$('.body-textarea', $self).show();
$('.editor_holder', $self).hide();
this.model.jsonEditor.disable();
}
else {
$('.body-textarea', $self).hide();
$('.editor_holder', $self).show();
this.model.jsonEditor.enable();
}
});
}
if (this.model.isBody) {
isParam = true;
}

View File

@@ -34,6 +34,7 @@ SwaggerUi.Views.ResourceView = Backbone.View.extend({
operation.nickname = id;
operation.parentId = this.model.id;
operation.definitions = this.model.definitions; // make Json Schema available for JSonEditor in this operation
this.addOperation(operation);
}

View File

@@ -56,6 +56,10 @@ SwaggerUi.Views.SignatureView = Backbone.View.extend({
// Fix for bug in IE 10/11 which causes placeholder text to be copied to "value"
if ($.trim(textArea.val()) === '' || textArea.prop('placeholder') === textArea.val()) {
textArea.val(this.model.sampleJSON);
// TODO move this code outside of the view and expose an event instead
if( this.model.jsonEditor && this.model.jsonEditor.isEnabled()){
this.model.jsonEditor.setValue(JSON.parse(this.model.sampleJSON));
}
}
}
}