Merge pull request #1036 from mohsen1/remove-swaggerui-global-instance

Remove swaggerui global instance
This commit is contained in:
Tony Tam
2015-03-17 13:57:18 -07:00
16 changed files with 199 additions and 83 deletions

View File

@@ -33,7 +33,7 @@ window.SwaggerUi = Backbone.Router.extend({
}
// Create an empty div which contains the dom_id
if (! $('#' + this.dom_id)){
if (! $('#' + this.dom_id).length){
$('body').append('<div id="' + this.dom_id + '"></div>') ;
}
@@ -53,7 +53,7 @@ window.SwaggerUi = Backbone.Router.extend({
// Event handler for when the baseUrl/apiKey is entered by user
this.headerView.on('update-swagger-ui', function(data) {
return this.updateSwaggerUi(data);
return that.updateSwaggerUi(data);
});
},
@@ -112,7 +112,8 @@ window.SwaggerUi = Backbone.Router.extend({
this.mainView = new SwaggerUi.Views.MainView({
model: this.api,
el: $('#' + this.dom_id),
swaggerOptions: this.options
swaggerOptions: this.options,
router: this
}).render();
this.showMessage();
switch (this.options.docExpansion) {
@@ -194,4 +195,37 @@ window.SwaggerUi = Backbone.Router.extend({
});
window.SwaggerUi.Views = {};
window.SwaggerUi.Views = {};
// don't break backward compatibility with previous versions and warn users to upgrade their code
(function(){
window.authorizations = {
add: function() {
warn('using window.authorizations is depreciated. Please use waggerUi.api.clientAuthorizations.add().');
if (typeof window.swaggerUi === 'undefined') {
throw new TypeError('window.swaggerUi is not defined');
}
if (window.swaggerUi instanceof SwaggerUi) {
window.swaggerUi.api.clientAuthorizations.add.apply(window.swaggerUi.api.clientAuthorizations, arguments);
}
}
};
window.ApiKeyAuthorization = function() {
warn('window.ApiKeyAuthorization is depreciated. Please use SwaggerClient.ApiKeyAuthorization.');
SwaggerClient.ApiKeyAuthorization.apply(window, arguments);
};
window.PasswordAuthorization = function() {
warn('window.PasswordAuthorization is depreciated. Please use SwaggerClient.PasswordAuthorization.');
SwaggerClient.PasswordAuthorization.apply(window, arguments);
};
function warn(message) {
if ('console' in window && typeof window.console.warn === 'function') {
console.warn(message);
}
}
})();

View File

@@ -82,7 +82,7 @@ if (Function.prototype.bind && console && typeof console.log === "object") {
}, Function.prototype.call);
}
var Docs = {
window.Docs = {
shebang: function() {

View File

@@ -7,7 +7,10 @@ SwaggerUi.Views.ApiKeyButton = Backbone.View.extend({ // TODO: append this to gl
'click #apply_api_key' : 'applyApiKey'
},
initialize: function(){},
initialize: function(opts){
this.options = opts || {};
this.router = this.options.router;
},
render: function(){
var template = this.template();
@@ -18,18 +21,18 @@ SwaggerUi.Views.ApiKeyButton = Backbone.View.extend({ // TODO: append this to gl
applyApiKey: function(){
var keyAuth = new ApiKeyAuthorization(
var keyAuth = new SwaggerClient.ApiKeyAuthorization(
this.model.name,
$('#input_apiKey_entry').val(),
this.model.in
);
window.authorizations.add(this.model.name, keyAuth);
window.swaggerUi.load();
this.router.api.clientAuthorizations.add(this.model.name, keyAuth);
this.router.load();
$('#apikey_container').show();
},
toggleApiKeyContainer: function(){
if ($('#apikey_container').length > 0) {
if ($('#apikey_container').length) {
var elem = $('#apikey_container').first();

View File

@@ -3,7 +3,10 @@
SwaggerUi.Views.BasicAuthButton = Backbone.View.extend({
initialize: function () {},
initialize: function (opts) {
this.options = opts || {};
this.router = this.options.router;
},
render: function(){
var template = this.template();
@@ -20,14 +23,14 @@ SwaggerUi.Views.BasicAuthButton = Backbone.View.extend({
applyPassword: function(){
var username = $('.input_username').val();
var password = $('.input_password').val();
var basicAuth = new PasswordAuthorization('basic', username, password);
window.authorizations.add(this.model.type, basicAuth);
window.swaggerUi.load();
var basicAuth = new SwaggerClient.PasswordAuthorization('basic', username, password);
this.router.api.clientAuthorizations.add(this.model.type, basicAuth);
this.router.load();
$('#basic_auth_container').hide();
},
togglePasswordContainer: function(){
if ($('#basic_auth_container').length > 0) {
if ($('#basic_auth_container').length) {
var elem = $('#basic_auth_container').show();
if (elem.is(':visible')){
elem.slideUp();

View File

@@ -10,6 +10,9 @@ SwaggerUi.Views.MainView = Backbone.View.extend({
initialize: function(opts){
opts = opts || {};
this.router = opts.router;
// set up the UI for input
this.model.auths = [];
var key, value;
@@ -50,12 +53,12 @@ SwaggerUi.Views.MainView = Backbone.View.extend({
var button;
if (auth.type === 'apiKey' && $('#apikey_button').length === 0) {
button = new SwaggerUi.Views.ApiKeyButton({model: auth}).render().el;
button = new SwaggerUi.Views.ApiKeyButton({model: auth, router: this.router}).render().el;
$('.auth_main_container').append(button);
}
if (auth.type === 'basicAuth' && $('#basic_auth_button').length === 0) {
button = new SwaggerUi.Views.BasicAuthButton({model: auth}).render().el;
button = new SwaggerUi.Views.BasicAuthButton({model: auth, router: this.router}).render().el;
$('.auth_main_container').append(button);
}
}
@@ -93,6 +96,7 @@ SwaggerUi.Views.MainView = Backbone.View.extend({
resource.id = resource.id.replace(/\s/g, '_');
var resourceView = new SwaggerUi.Views.ResourceView({
model: resource,
router: this.router,
tagName: 'li',
id: 'resource_' + resource.id,
className: 'resource',

View File

@@ -14,6 +14,7 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
initialize: function(opts) {
opts = opts || {};
this.router = opts.router;
this.auths = opts.auths;
this.parentId = this.model.parentId;
this.nickname = this.model.nickname;
@@ -165,6 +166,7 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
if (signatureModel) {
responseSignatureView = new SwaggerUi.Views.SignatureView({
model: signatureModel,
router: this.router,
tagName: 'div'
});
$('.model-signature', $(this.el)).append(responseSignatureView.render().el);
@@ -200,7 +202,8 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
param.type = type;
}
responseContentTypeView = new SwaggerUi.Views.ResponseContentTypeView({
model: contentTypeModel
model: contentTypeModel,
router: this.router
});
$('.response-content-type', $(this.el)).append(responseContentTypeView.render().el);
ref4 = this.model.parameters;
@@ -229,7 +232,11 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
addStatusCode: function(statusCode) {
// Render status codes
var statusCodeView = new SwaggerUi.Views.StatusCodeView({model: statusCode, tagName: 'tr'});
var statusCodeView = new SwaggerUi.Views.StatusCodeView({
model: statusCode,
tagName: 'tr',
router: this.router
});
$('.operation-status', $(this.el)).append(statusCodeView.render().el);
},

View File

@@ -3,6 +3,7 @@
SwaggerUi.Views.ResourceView = Backbone.View.extend({
initialize: function(opts) {
opts = opts || {};
this.router = opts.router;
this.auths = opts.auths;
if ('' === this.model.description) {
this.model.description = null;
@@ -50,6 +51,7 @@ SwaggerUi.Views.ResourceView = Backbone.View.extend({
// Render an operation and add it to operations li
var operationView = new SwaggerUi.Views.OperationView({
model: operation,
router: this.router,
tagName: 'li',
className: 'endpoint',
swaggerOptions: this.options.swaggerOptions,

View File

@@ -1,19 +1,19 @@
'use strict';
SwaggerUi.Views.StatusCodeView = Backbone.View.extend({
initialize: function () {
initialize: function (opts) {
this.options = opts || {};
this.router = this.options.router;
},
render: function(){
$(this.el).html(Handlebars.templates.status_code(this.model));
// TODO get rid of "swaggerUi" global dependency
if (swaggerUi.api.models.hasOwnProperty(this.model.responseModel)) {
if (this.router.api.models.hasOwnProperty(this.model.responseModel)) {
var responseModel = {
sampleJSON: JSON.stringify(swaggerUi.api.models[this.model.responseModel].createJSONSample(), null, 2),
sampleJSON: JSON.stringify(this.router.api.models[this.model.responseModel].createJSONSample(), null, 2),
isParam: false,
signature: swaggerUi.api.models[this.model.responseModel].getMockSignature(),
signature: this.router.api.models[this.model.responseModel].getMockSignature(),
};
var responseModelView = new SwaggerUi.Views.SignatureView({model: responseModel, tagName: 'div'});