Merge pull request #1022 from mohsen1/remove-globals

Remove global references to swaggerUi object
This commit is contained in:
Tony Tam
2015-03-11 12:49:29 -07:00
8 changed files with 100 additions and 57 deletions

68
dist/swagger-ui.js vendored
View File

@@ -15,7 +15,7 @@ SwaggerUi = (function(superClass) {
return SwaggerUi.__super__.constructor.apply(this, arguments);
}
SwaggerUi.prototype.dom_id = "swagger_ui";
SwaggerUi.prototype.domEl = $('#swagger_ui');
SwaggerUi.prototype.options = null;
@@ -30,14 +30,17 @@ SwaggerUi = (function(superClass) {
options = {};
}
if (options.dom_id != null) {
this.dom_id = options.dom_id;
this.domEl = $('#' + options.dom_id);
delete options.dom_id;
} else if (options.domEl != null) {
this.domEl = options.domEl;
}
if (options.supportedSubmitMethods == null) {
options.supportedSubmitMethods = ['get', 'put', 'post', 'delete', 'head', 'options', 'patch'];
}
if ($('#' + this.dom_id) == null) {
$('body').append('<div id="' + this.dom_id + '"></div>');
this.domEl = $(this.domEl);
if (!$.contains(document.documentElement, this.domEl.get(0))) {
$('body').append(this.domEl);
}
this.options = options;
marked.setOptions({
@@ -58,14 +61,16 @@ SwaggerUi = (function(superClass) {
return _this.onLoadFailure(d);
};
})(this);
this.headerView = new HeaderView({
el: $('#header')
});
return this.headerView.on('update-swagger-ui', (function(_this) {
return function(data) {
return _this.updateSwaggerUi(data);
};
})(this));
if ($('#header').length) {
this.headerView = new HeaderView({
el: $('#header')
});
return this.headerView.on('update-swagger-ui', (function(_this) {
return function(data) {
return _this.updateSwaggerUi(data);
};
})(this));
}
};
SwaggerUi.prototype.setOption = function(option, value) {
@@ -91,7 +96,9 @@ SwaggerUi = (function(superClass) {
url = this.buildUrl(window.location.href.toString(), url);
}
this.options.url = url;
this.headerView.update(url);
if (this.headerView) {
this.headerView.update(url);
}
return this.api = new SwaggerClient(this.options);
};
@@ -111,8 +118,9 @@ SwaggerUi = (function(superClass) {
this.showMessage('Finished Loading Resource Information. Rendering Swagger UI...');
this.mainView = new MainView({
model: this.api,
el: $('#' + this.dom_id),
swaggerOptions: this.options
el: this.domEl,
swaggerOptions: this.options,
router: this
}).render();
this.showMessage();
switch (this.options.docExpansion) {
@@ -1293,9 +1301,10 @@ MainView = (function(superClass) {
id: 'resource_' + resource.id,
className: 'resource',
auths: auths,
swaggerOptions: this.options.swaggerOptions
swaggerOptions: this.options.swaggerOptions,
parent: this
});
return $('#resources').append(resourceView.render().el);
return $('#resources', this.el).append(resourceView.render().el);
};
MainView.prototype.clear = function() {
@@ -1477,7 +1486,8 @@ OperationView = (function(superClass) {
if (signatureModel) {
responseSignatureView = new SignatureView({
model: signatureModel,
tagName: 'div'
tagName: 'div',
parent: this
});
$('.model-signature', $(this.el)).append(responseSignatureView.render().el);
} else {
@@ -1512,7 +1522,8 @@ OperationView = (function(superClass) {
param.type = type;
}
responseContentTypeView = new ResponseContentTypeView({
model: contentTypeModel
model: contentTypeModel,
parent: this
});
$('.response-content-type', $(this.el)).append(responseContentTypeView.render().el);
ref4 = this.model.parameters;
@@ -1534,7 +1545,8 @@ OperationView = (function(superClass) {
paramView = new ParameterView({
model: param,
tagName: 'tr',
readOnly: this.model.isReadOnly
readOnly: this.model.isReadOnly,
parent: this
});
return $('.operation-params', $(this.el)).append(paramView.render().el);
};
@@ -1543,7 +1555,8 @@ OperationView = (function(superClass) {
var statusCodeView;
statusCodeView = new StatusCodeView({
model: statusCode,
tagName: 'tr'
tagName: 'tr',
parent: this
});
return $('.operation-status', $(this.el)).append(statusCodeView.render().el);
};
@@ -2121,7 +2134,8 @@ ResourceView = (function(superClass) {
tagName: 'li',
className: 'endpoint',
swaggerOptions: this.options.swaggerOptions,
auths: this.auths
auths: this.auths,
parent: this
});
$('.endpoints', $(this.el)).append(operationView.render().el);
return this.number++;
@@ -2251,14 +2265,16 @@ StatusCodeView = (function(superClass) {
StatusCodeView.prototype.initialize = function() {};
StatusCodeView.prototype.render = function() {
var responseModel, responseModelView, template;
var models, responseModel, responseModelView, template;
template = this.template();
$(this.el).html(template(this.model));
if (swaggerUi.api.models.hasOwnProperty(this.model.responseModel)) {
models = this.options.parent.options.parent.options.parent.model.models;
if (models.hasOwnProperty(this.model.responseModel)) {
models = this.options.parent.options.parent.options.parent.model.models;
responseModel = {
sampleJSON: JSON.stringify(swaggerUi.api.models[this.model.responseModel].createJSONSample(), null, 2),
sampleJSON: JSON.stringify(models[this.model.responseModel].createJSONSample(), null, 2),
isParam: false,
signature: swaggerUi.api.models[this.model.responseModel].getMockSignature()
signature: models[this.model.responseModel].getMockSignature()
};
responseModelView = new SignatureView({
model: responseModel,

File diff suppressed because one or more lines are too long

View File

@@ -57,7 +57,7 @@ function coffeescript () {
return gulp
.src(['./src/main/coffeescript/**/*.coffee'])
.pipe(coffee({bare: true}))
.on('error', gutil.log);
.on('error', log);
}
/**
@@ -93,7 +93,7 @@ gulp.task('less', ['clean'], function() {
'./src/main/less/reset.less'
])
.pipe(less())
.on('error', gutil.log)
.on('error', log)
.pipe(gulp.dest('./src/main/html/css/'))
.pipe(connect.reload());
});
@@ -108,20 +108,20 @@ gulp.task('copy', ['less'], function() {
gulp
.src(['./lib/**/*.{js,map}'])
.pipe(gulp.dest('./dist/lib'))
.on('error', gutil.log)
.on('error', log)
// copy all files inside html folder
gulp
.src(['./src/main/html/**/*'])
.pipe(gulp.dest('./dist'))
.on('error', gutil.log)
.on('error', log)
});
/**
* Watch for changes and recompile
*/
gulp.task('watch', function() {
return watch(['./src/**/*.{coffee,js,less,handlebars}'], function() {
return watch(['./src/**/*.{coffee,js,less,handlebars,html}'], function() {
gulp.start('default');
});
});
@@ -136,6 +136,10 @@ gulp.task('connect', function() {
});
});
function log(error) {
console.log(error.toString());
}
gulp.task('default', ['dist', 'copy']);
gulp.task('serve', ['connect', 'watch'])
gulp.task('serve', ['connect', 'watch', 'default'])

View File

@@ -1,7 +1,7 @@
class SwaggerUi extends Backbone.Router
# Defaults
dom_id: "swagger_ui"
domEl: $('#swagger_ui')
# Attributes
options: null
@@ -13,14 +13,30 @@ class SwaggerUi extends Backbone.Router
initialize: (options={}) ->
# Allow dom_id to be overridden
if options.dom_id?
@dom_id = options.dom_id
@domEl = $('#' + options.dom_id)
delete options.dom_id
if not options.supportedSubmitMethods?
options.supportedSubmitMethods = ['get','put','post','delete','head','options','patch']
# Allow domeEl to be specified
else if options.domEl?
@domEl = options.domEl
# Create an empty div which contains the dom_id
$('body').append('<div id="' + @dom_id + '"></div>') if not $('#' + @dom_id)?
if not options.supportedSubmitMethods?
options.supportedSubmitMethods = [
'get'
'put'
'post'
'delete'
'head'
'options'
'patch'
]
# Make sure this.domeEl is a jQuery element
@domEl = $(@domEl)
# if domEl is not attached to document append it to <body>
if !$.contains(document.documentElement, @domEl.get(0))
$('body').append(@domEl)
@options = options
@@ -34,11 +50,12 @@ class SwaggerUi extends Backbone.Router
@options.failure = (d) =>
@onLoadFailure(d)
# Create view to handle the header inputs
@headerView = new HeaderView({el: $('#header')})
# Create view to handle the header inputs if there is header element
if $('#header').length
@headerView = new HeaderView({el: $('#header')})
# Event handler for when the baseUrl/apiKey is entered by user
@headerView.on 'update-swagger-ui', (data) => @updateSwaggerUi(data)
# Event handler for when the baseUrl/apiKey is entered by user
@headerView.on 'update-swagger-ui', (data) => @updateSwaggerUi(data)
# Set an option after initializing
setOption: (option,value) ->
@@ -62,7 +79,8 @@ class SwaggerUi extends Backbone.Router
url = @buildUrl(window.location.href.toString(), url)
@options.url = url
@headerView.update(url)
if @headerView
@headerView.update(url)
@api = new SwaggerClient(@options)
@@ -82,7 +100,7 @@ class SwaggerUi extends Backbone.Router
# so it gets called when SwaggerApi completes loading
render:() ->
@showMessage('Finished Loading Resource Information. Rendering Swagger UI...')
@mainView = new MainView({model: @api, el: $('#' + @dom_id), swaggerOptions: @options}).render()
@mainView = new MainView({model: @api, el: @domEl, swaggerOptions: @options, router: @}).render()
@showMessage()
switch @options.docExpansion
when "full" then @expandAll()

View File

@@ -66,9 +66,10 @@ class MainView extends Backbone.View
id: 'resource_' + resource.id,
className: 'resource',
auths: auths,
swaggerOptions: @options.swaggerOptions
swaggerOptions: @options.swaggerOptions,
parent: @
})
$('#resources').append resourceView.render().el
$('#resources', @el).append resourceView.render().el
clear: ->
$(@el).html ''

View File

@@ -115,7 +115,7 @@ class OperationView extends Backbone.View
$(@el).html(Handlebars.templates.operation(@model))
if signatureModel
responseSignatureView = new SignatureView({model: signatureModel, tagName: 'div'})
responseSignatureView = new SignatureView({model: signatureModel, tagName: 'div', parent: @})
$('.model-signature', $(@el)).append responseSignatureView.render().el
else
@model.responseClassSignature = 'string'
@@ -142,7 +142,7 @@ class OperationView extends Backbone.View
contentTypeModel.consumes = 'multipart/form-data'
param.type = type
responseContentTypeView = new ResponseContentTypeView({model: contentTypeModel})
responseContentTypeView = new ResponseContentTypeView({model: contentTypeModel, parent: @})
$('.response-content-type', $(@el)).append responseContentTypeView.render().el
# Render each parameter
@@ -156,12 +156,12 @@ class OperationView extends Backbone.View
addParameter: (param, consumes) ->
# Render a parameter
param.consumes = consumes
paramView = new ParameterView({model: param, tagName: 'tr', readOnly: @model.isReadOnly})
paramView = new ParameterView({model: param, tagName: 'tr', readOnly: @model.isReadOnly, parent: @})
$('.operation-params', $(@el)).append paramView.render().el
addStatusCode: (statusCode) ->
# Render status codes
statusCodeView = new StatusCodeView({model: statusCode, tagName: 'tr'})
statusCodeView = new StatusCodeView({model: statusCode, tagName: 'tr', parent: @})
$('.operation-status', $(@el)).append statusCodeView.render().el
submitOperation: (e) ->

View File

@@ -43,7 +43,8 @@ class ResourceView extends Backbone.View
tagName: 'li',
className: 'endpoint',
swaggerOptions: @options.swaggerOptions,
auths: @auths
auths: @auths,
parent: @
})
$('.endpoints', $(@el)).append operationView.render().el

View File

@@ -5,11 +5,14 @@ class StatusCodeView extends Backbone.View
template = @template()
$(@el).html(template(@model))
if swaggerUi.api.models.hasOwnProperty @model.responseModel
models = this.options.parent.options.parent.options.parent.model.models
if models.hasOwnProperty @model.responseModel
models = this.options.parent.options.parent.options.parent.model.models
responseModel =
sampleJSON: JSON.stringify(swaggerUi.api.models[@model.responseModel].createJSONSample(), null, 2)
sampleJSON: JSON.stringify(models[@model.responseModel].createJSONSample(), null, 2)
isParam: false
signature: swaggerUi.api.models[@model.responseModel].getMockSignature()
signature: models[@model.responseModel].getMockSignature()
responseModelView = new SignatureView({model: responseModel, tagName: 'div'})
$('.model-signature', @$el).append responseModelView.render().el