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

@@ -18,18 +18,20 @@
"trailing": true, "trailing": true,
"smarttabs": true, "smarttabs": true,
"globals": { "globals": {
"Backbone": false,
// Libraries
"_": false, "_": false,
"$": false, "$": false,
"Backbone": false,
"Handlebars": false,
"jQuery": false, "jQuery": false,
"marked": false, "marked": false,
"Docs": false,
"SwaggerClient": false, "SwaggerClient": false,
"Handlebars": false,
"ApiKeyAuthorization": false,
"PasswordAuthorization": false,
"hljs": false, "hljs": false,
"SwaggerUi": false, "SwaggerUi": false,
"swaggerUi": false // TODO: remove me
// Global object
// TODO: remove these
"Docs": false
} }
} }

View File

@@ -73,12 +73,12 @@ You may choose to customize Swagger UI for your organization. Here is an overvie
To use swagger-ui you should take a look at the [source of swagger-ui html page](https://github.com/swagger-api/swagger-ui/blob/master/dist/index.html) and customize it. This basically requires you to instantiate a SwaggerUi object and call load() on it as below: To use swagger-ui you should take a look at the [source of swagger-ui html page](https://github.com/swagger-api/swagger-ui/blob/master/dist/index.html) and customize it. This basically requires you to instantiate a SwaggerUi object and call load() on it as below:
```javascript ```javascript
window.swaggerUi = new SwaggerUi({ var swaggerUi = new SwaggerUi({
url:"http://petstore.swagger.io/v2/swagger.json", url:"http://petstore.swagger.io/v2/swagger.json",
dom_id:"swagger-ui-container" dom_id:"swagger-ui-container"
}); });
window.swaggerUi.load(); swaggerUi.load();
``` ```
##### Parameters ##### Parameters
@@ -108,11 +108,11 @@ swagger-ui supports invocation of all HTTP methods APIs including GET, PUT, POST
Header params are supported through a pluggable mechanism in [swagger-js](https://github.com/swagger-api/swagger-js). You can see the [index.html](https://github.com/swagger-api/swagger-ui/blob/master/dist/index.html) for a sample of how to dynamically set headers: Header params are supported through a pluggable mechanism in [swagger-js](https://github.com/swagger-api/swagger-js). You can see the [index.html](https://github.com/swagger-api/swagger-ui/blob/master/dist/index.html) for a sample of how to dynamically set headers:
```js ```js
// add a new ApiKeyAuthorization when the api-key changes in the ui. // add a new SwaggerClient.ApiKeyAuthorization when the api-key changes in the ui.
$('#input_apiKey').change(function() { $('#input_apiKey').change(function() {
var key = $('#input_apiKey')[0].value; var key = $('#input_apiKey')[0].value;
if(key && key.trim() != "") { if(key && key.trim() != "") {
window.authorizations.add("key", new ApiKeyAuthorization("api_key", key, "header")); swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization("api_key", key, "header"));
} }
}) })
``` ```
@@ -123,15 +123,15 @@ This will add header `api_key` with value `key` on every call to the server. Yo
If you have some header parameters which you need to send with every request, use the headers as below: If you have some header parameters which you need to send with every request, use the headers as below:
```js ```js
window.authorizations.add("key", new ApiKeyAuthorization("Authorization", "XXXX", "header")); swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization("Authorization", "XXXX", "header"));
``` ```
Note! You can pass multiple header params on a single request, just use unique names for them (`key` is used in the above example). Note! You can pass multiple header params on a single request, just use unique names for them (`key` is used in the above example).
### Localisation and translation ### Localization and translation
The localisation files are in the dist/lang directory. The localization files are in the dist/lang directory.
To enable translation you should append next two lines in your swagger's index.html (or another entry point you use) To enable translation you should append next two lines in your Swagger's index.html (or another entry point you use)
```html ```html
<script src='lang/translator.js' type='text/javascript'></script> <script src='lang/translator.js' type='text/javascript'></script>
<script src='lang/en.js' type='text/javascript'></script> <script src='lang/en.js' type='text/javascript'></script>
@@ -220,8 +220,6 @@ Create your own fork of [swagger-api/swagger-ui](https://github.com/swagger-api/
To share your changes, [submit a pull request](https://github.com/swagger-api/swagger-ui/pull/new/master). To share your changes, [submit a pull request](https://github.com/swagger-api/swagger-ui/pull/new/master).
Since the javascript files are compiled from coffeescript, please submit changes in the *.coffee files! We have to reject changes only in the .js files as they will be lost on each build of the ui.
## Change Log ## Change Log
Plsee see [releases](https://github.com/swagger-api/swagger-ui/releases) for change log. Plsee see [releases](https://github.com/swagger-api/swagger-ui/releases) for change log.

13
dist/index.html vendored
View File

@@ -61,14 +61,13 @@
var key = encodeURIComponent($('#input_apiKey')[0].value); var key = encodeURIComponent($('#input_apiKey')[0].value);
log("key: " + key); log("key: " + key);
if(key && key.trim() != "") { if(key && key.trim() != "") {
var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("api_key", key, "query");
window.swaggerUi.api.clientAuthorizations.add("api_key", apiKeyAuth);
log("added key " + key); log("added key " + key);
window.authorizations.add("api_key", new ApiKeyAuthorization("api_key", key, "query"));
} }
} }
$('#input_apiKey').change(function() { $('#input_apiKey').change(addApiKeyAuthorization);
addApiKeyAuthorization();
});
// if you have an apiKey you would like to pre-populate on the page for demonstration purposes... // if you have an apiKey you would like to pre-populate on the page for demonstration purposes...
/* /*
@@ -78,6 +77,12 @@
*/ */
window.swaggerUi.load(); window.swaggerUi.load();
function log() {
if ('console' in window) {
console.log.apply(console, arguments);
}
}
}); });
</script> </script>
</head> </head>

View File

@@ -202,7 +202,7 @@ function initOAuth(opts) {
}); });
} }
function processOAuthCode(data) { window.processOAuthCode = function processOAuthCode(data) {
var params = { var params = {
'client_id': clientId, 'client_id': clientId,
'code': data.code, 'code': data.code,
@@ -225,7 +225,7 @@ function processOAuthCode(data) {
}); });
} }
function onOAuthComplete(token) { window.onOAuthComplete = function onOAuthComplete(token) {
if(token) { if(token) {
if(token.error) { if(token.error) {
var checkbox = $('input[type=checkbox],.secured') var checkbox = $('input[type=checkbox],.secured')

101
dist/swagger-ui.js vendored
View File

@@ -39,7 +39,7 @@ window.SwaggerUi = Backbone.Router.extend({
} }
// Create an empty div which contains the dom_id // 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>') ; $('body').append('<div id="' + this.dom_id + '"></div>') ;
} }
@@ -59,7 +59,7 @@ window.SwaggerUi = Backbone.Router.extend({
// Event handler for when the baseUrl/apiKey is entered by user // Event handler for when the baseUrl/apiKey is entered by user
this.headerView.on('update-swagger-ui', function(data) { this.headerView.on('update-swagger-ui', function(data) {
return this.updateSwaggerUi(data); return that.updateSwaggerUi(data);
}); });
}, },
@@ -118,7 +118,8 @@ window.SwaggerUi = Backbone.Router.extend({
this.mainView = new SwaggerUi.Views.MainView({ this.mainView = new SwaggerUi.Views.MainView({
model: this.api, model: this.api,
el: $('#' + this.dom_id), el: $('#' + this.dom_id),
swaggerOptions: this.options swaggerOptions: this.options,
router: this
}).render(); }).render();
this.showMessage(); this.showMessage();
switch (this.options.docExpansion) { switch (this.options.docExpansion) {
@@ -201,6 +202,39 @@ 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);
}
}
})();
this["Handlebars"] = this["Handlebars"] || {}; this["Handlebars"] = this["Handlebars"] || {};
this["Handlebars"]["templates"] = this["Handlebars"]["templates"] || {}; this["Handlebars"]["templates"] = this["Handlebars"]["templates"] || {};
this["Handlebars"]["templates"]["apikey_button_view"] = Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) { this["Handlebars"]["templates"]["apikey_button_view"] = Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
@@ -317,7 +351,7 @@ if (Function.prototype.bind && console && typeof console.log === "object") {
}, Function.prototype.call); }, Function.prototype.call);
} }
var Docs = { window.Docs = {
shebang: function() { shebang: function() {
@@ -1013,7 +1047,10 @@ SwaggerUi.Views.ApiKeyButton = Backbone.View.extend({ // TODO: append this to gl
'click #apply_api_key' : 'applyApiKey' 'click #apply_api_key' : 'applyApiKey'
}, },
initialize: function(){}, initialize: function(opts){
this.options = opts || {};
this.router = this.options.router;
},
render: function(){ render: function(){
var template = this.template(); var template = this.template();
@@ -1024,18 +1061,18 @@ SwaggerUi.Views.ApiKeyButton = Backbone.View.extend({ // TODO: append this to gl
applyApiKey: function(){ applyApiKey: function(){
var keyAuth = new ApiKeyAuthorization( var keyAuth = new SwaggerClient.ApiKeyAuthorization(
this.model.name, this.model.name,
$('#input_apiKey_entry').val(), $('#input_apiKey_entry').val(),
this.model.in this.model.in
); );
window.authorizations.add(this.model.name, keyAuth); this.router.api.clientAuthorizations.add(this.model.name, keyAuth);
window.swaggerUi.load(); this.router.load();
$('#apikey_container').show(); $('#apikey_container').show();
}, },
toggleApiKeyContainer: function(){ toggleApiKeyContainer: function(){
if ($('#apikey_container').length > 0) { if ($('#apikey_container').length) {
var elem = $('#apikey_container').first(); var elem = $('#apikey_container').first();
@@ -1060,7 +1097,10 @@ SwaggerUi.Views.ApiKeyButton = Backbone.View.extend({ // TODO: append this to gl
SwaggerUi.Views.BasicAuthButton = Backbone.View.extend({ SwaggerUi.Views.BasicAuthButton = Backbone.View.extend({
initialize: function () {}, initialize: function (opts) {
this.options = opts || {};
this.router = this.options.router;
},
render: function(){ render: function(){
var template = this.template(); var template = this.template();
@@ -1077,14 +1117,14 @@ SwaggerUi.Views.BasicAuthButton = Backbone.View.extend({
applyPassword: function(){ applyPassword: function(){
var username = $('.input_username').val(); var username = $('.input_username').val();
var password = $('.input_password').val(); var password = $('.input_password').val();
var basicAuth = new PasswordAuthorization('basic', username, password); var basicAuth = new SwaggerClient.PasswordAuthorization('basic', username, password);
window.authorizations.add(this.model.type, basicAuth); this.router.api.clientAuthorizations.add(this.model.type, basicAuth);
window.swaggerUi.load(); this.router.load();
$('#basic_auth_container').hide(); $('#basic_auth_container').hide();
}, },
togglePasswordContainer: function(){ togglePasswordContainer: function(){
if ($('#basic_auth_container').length > 0) { if ($('#basic_auth_container').length) {
var elem = $('#basic_auth_container').show(); var elem = $('#basic_auth_container').show();
if (elem.is(':visible')){ if (elem.is(':visible')){
elem.slideUp(); elem.slideUp();
@@ -1181,6 +1221,9 @@ SwaggerUi.Views.MainView = Backbone.View.extend({
initialize: function(opts){ initialize: function(opts){
opts = opts || {}; opts = opts || {};
this.router = opts.router;
// set up the UI for input // set up the UI for input
this.model.auths = []; this.model.auths = [];
var key, value; var key, value;
@@ -1221,12 +1264,12 @@ SwaggerUi.Views.MainView = Backbone.View.extend({
var button; var button;
if (auth.type === 'apiKey' && $('#apikey_button').length === 0) { 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); $('.auth_main_container').append(button);
} }
if (auth.type === 'basicAuth' && $('#basic_auth_button').length === 0) { 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); $('.auth_main_container').append(button);
} }
} }
@@ -1264,6 +1307,7 @@ SwaggerUi.Views.MainView = Backbone.View.extend({
resource.id = resource.id.replace(/\s/g, '_'); resource.id = resource.id.replace(/\s/g, '_');
var resourceView = new SwaggerUi.Views.ResourceView({ var resourceView = new SwaggerUi.Views.ResourceView({
model: resource, model: resource,
router: this.router,
tagName: 'li', tagName: 'li',
id: 'resource_' + resource.id, id: 'resource_' + resource.id,
className: 'resource', className: 'resource',
@@ -1293,6 +1337,7 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
initialize: function(opts) { initialize: function(opts) {
opts = opts || {}; opts = opts || {};
this.router = opts.router;
this.auths = opts.auths; this.auths = opts.auths;
this.parentId = this.model.parentId; this.parentId = this.model.parentId;
this.nickname = this.model.nickname; this.nickname = this.model.nickname;
@@ -1444,6 +1489,7 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
if (signatureModel) { if (signatureModel) {
responseSignatureView = new SwaggerUi.Views.SignatureView({ responseSignatureView = new SwaggerUi.Views.SignatureView({
model: signatureModel, model: signatureModel,
router: this.router,
tagName: 'div' tagName: 'div'
}); });
$('.model-signature', $(this.el)).append(responseSignatureView.render().el); $('.model-signature', $(this.el)).append(responseSignatureView.render().el);
@@ -1479,7 +1525,8 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
param.type = type; param.type = type;
} }
responseContentTypeView = new SwaggerUi.Views.ResponseContentTypeView({ responseContentTypeView = new SwaggerUi.Views.ResponseContentTypeView({
model: contentTypeModel model: contentTypeModel,
router: this.router
}); });
$('.response-content-type', $(this.el)).append(responseContentTypeView.render().el); $('.response-content-type', $(this.el)).append(responseContentTypeView.render().el);
ref4 = this.model.parameters; ref4 = this.model.parameters;
@@ -1508,7 +1555,11 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
addStatusCode: function(statusCode) { addStatusCode: function(statusCode) {
// Render status codes // 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); $('.operation-status', $(this.el)).append(statusCodeView.render().el);
}, },
@@ -2021,6 +2072,7 @@ SwaggerUi.Views.ParameterView = Backbone.View.extend({
SwaggerUi.Views.ResourceView = Backbone.View.extend({ SwaggerUi.Views.ResourceView = Backbone.View.extend({
initialize: function(opts) { initialize: function(opts) {
opts = opts || {}; opts = opts || {};
this.router = opts.router;
this.auths = opts.auths; this.auths = opts.auths;
if ('' === this.model.description) { if ('' === this.model.description) {
this.model.description = null; this.model.description = null;
@@ -2068,6 +2120,7 @@ SwaggerUi.Views.ResourceView = Backbone.View.extend({
// Render an operation and add it to operations li // Render an operation and add it to operations li
var operationView = new SwaggerUi.Views.OperationView({ var operationView = new SwaggerUi.Views.OperationView({
model: operation, model: operation,
router: this.router,
tagName: 'li', tagName: 'li',
className: 'endpoint', className: 'endpoint',
swaggerOptions: this.options.swaggerOptions, swaggerOptions: this.options.swaggerOptions,
@@ -2163,19 +2216,19 @@ SwaggerUi.Views.SignatureView = Backbone.View.extend({
'use strict'; 'use strict';
SwaggerUi.Views.StatusCodeView = Backbone.View.extend({ SwaggerUi.Views.StatusCodeView = Backbone.View.extend({
initialize: function () { initialize: function (opts) {
this.options = opts || {};
this.router = this.options.router;
}, },
render: function(){ render: function(){
$(this.el).html(Handlebars.templates.status_code(this.model)); $(this.el).html(Handlebars.templates.status_code(this.model));
// TODO get rid of "swaggerUi" global dependency if (this.router.api.models.hasOwnProperty(this.model.responseModel)) {
if (swaggerUi.api.models.hasOwnProperty(this.model.responseModel)) {
var 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, 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'}); var responseModelView = new SwaggerUi.Views.SignatureView({model: responseModel, tagName: 'div'});

File diff suppressed because one or more lines are too long

View File

@@ -202,7 +202,7 @@ function initOAuth(opts) {
}); });
} }
function processOAuthCode(data) { window.processOAuthCode = function processOAuthCode(data) {
var params = { var params = {
'client_id': clientId, 'client_id': clientId,
'code': data.code, 'code': data.code,
@@ -225,7 +225,7 @@ function processOAuthCode(data) {
}); });
} }
function onOAuthComplete(token) { window.onOAuthComplete = function onOAuthComplete(token) {
if(token) { if(token) {
if(token.error) { if(token.error) {
var checkbox = $('input[type=checkbox],.secured') var checkbox = $('input[type=checkbox],.secured')

View File

@@ -61,14 +61,13 @@
var key = encodeURIComponent($('#input_apiKey')[0].value); var key = encodeURIComponent($('#input_apiKey')[0].value);
log("key: " + key); log("key: " + key);
if(key && key.trim() != "") { if(key && key.trim() != "") {
var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("api_key", key, "query");
window.swaggerUi.api.clientAuthorizations.add("api_key", apiKeyAuth);
log("added key " + key); log("added key " + key);
window.authorizations.add("api_key", new ApiKeyAuthorization("api_key", key, "query"));
} }
} }
$('#input_apiKey').change(function() { $('#input_apiKey').change(addApiKeyAuthorization);
addApiKeyAuthorization();
});
// if you have an apiKey you would like to pre-populate on the page for demonstration purposes... // if you have an apiKey you would like to pre-populate on the page for demonstration purposes...
/* /*
@@ -78,6 +77,12 @@
*/ */
window.swaggerUi.load(); window.swaggerUi.load();
function log() {
if ('console' in window) {
console.log.apply(console, arguments);
}
}
}); });
</script> </script>
</head> </head>

View File

@@ -33,7 +33,7 @@ window.SwaggerUi = Backbone.Router.extend({
} }
// Create an empty div which contains the dom_id // 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>') ; $('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 // Event handler for when the baseUrl/apiKey is entered by user
this.headerView.on('update-swagger-ui', function(data) { 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({ this.mainView = new SwaggerUi.Views.MainView({
model: this.api, model: this.api,
el: $('#' + this.dom_id), el: $('#' + this.dom_id),
swaggerOptions: this.options swaggerOptions: this.options,
router: this
}).render(); }).render();
this.showMessage(); this.showMessage();
switch (this.options.docExpansion) { switch (this.options.docExpansion) {
@@ -195,3 +196,36 @@ 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); }, Function.prototype.call);
} }
var Docs = { window.Docs = {
shebang: function() { shebang: function() {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,19 +1,19 @@
'use strict'; 'use strict';
SwaggerUi.Views.StatusCodeView = Backbone.View.extend({ SwaggerUi.Views.StatusCodeView = Backbone.View.extend({
initialize: function () { initialize: function (opts) {
this.options = opts || {};
this.router = this.options.router;
}, },
render: function(){ render: function(){
$(this.el).html(Handlebars.templates.status_code(this.model)); $(this.el).html(Handlebars.templates.status_code(this.model));
// TODO get rid of "swaggerUi" global dependency if (this.router.api.models.hasOwnProperty(this.model.responseModel)) {
if (swaggerUi.api.models.hasOwnProperty(this.model.responseModel)) {
var 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, 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'}); var responseModelView = new SwaggerUi.Views.SignatureView({model: responseModel, tagName: 'div'});