diff --git a/.jshintrc b/.jshintrc
index e5afedf9..694473e8 100644
--- a/.jshintrc
+++ b/.jshintrc
@@ -18,18 +18,20 @@
"trailing": true,
"smarttabs": true,
"globals": {
- "Backbone": false,
+
+ // Libraries
"_": false,
"$": false,
+ "Backbone": false,
+ "Handlebars": false,
"jQuery": false,
"marked": false,
- "Docs": false,
"SwaggerClient": false,
- "Handlebars": false,
- "ApiKeyAuthorization": false,
- "PasswordAuthorization": false,
"hljs": false,
"SwaggerUi": false,
- "swaggerUi": false // TODO: remove me
+
+ // Global object
+ // TODO: remove these
+ "Docs": false
}
}
\ No newline at end of file
diff --git a/README.md b/README.md
index cfc92841..2c2e9f33 100644
--- a/README.md
+++ b/README.md
@@ -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:
```javascript
- window.swaggerUi = new SwaggerUi({
+ var swaggerUi = new SwaggerUi({
url:"http://petstore.swagger.io/v2/swagger.json",
dom_id:"swagger-ui-container"
});
- window.swaggerUi.load();
+ swaggerUi.load();
```
##### 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:
```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() {
var key = $('#input_apiKey')[0].value;
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:
```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).
-### Localisation and translation
-The localisation files are in the dist/lang directory.
+### Localization and translation
+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
@@ -141,12 +141,12 @@ The first line script is a translator and the second one is your language lexeme
If you wish to append support for new language you just need to create lang/your_lang.js and fill it like it's done in existing files.
To append new lexemex for translation you shoul do two things:
-1. Add lexeme into the language file.
+1. Add lexeme into the language file.
Example of new line: "new sentence":"translation of new sentence".
2. Mark this lexeme in source html with attribute data-sw-translate.
- Example of changed source:
+ Example of changed source:
```html
- new sentence
+ new sentence
or
```
.
@@ -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).
-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
Plsee see [releases](https://github.com/swagger-api/swagger-ui/releases) for change log.
diff --git a/dist/index.html b/dist/index.html
index bf585c96..daff2810 100644
--- a/dist/index.html
+++ b/dist/index.html
@@ -61,14 +61,13 @@
var key = encodeURIComponent($('#input_apiKey')[0].value);
log("key: " + key);
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);
- window.authorizations.add("api_key", new ApiKeyAuthorization("api_key", key, "query"));
}
}
- $('#input_apiKey').change(function() {
- addApiKeyAuthorization();
- });
+ $('#input_apiKey').change(addApiKeyAuthorization);
// if you have an apiKey you would like to pre-populate on the page for demonstration purposes...
/*
@@ -78,6 +77,12 @@
*/
window.swaggerUi.load();
+
+ function log() {
+ if ('console' in window) {
+ console.log.apply(console, arguments);
+ }
+ }
});
diff --git a/dist/lib/swagger-oauth.js b/dist/lib/swagger-oauth.js
index 72e649a6..01fae27f 100644
--- a/dist/lib/swagger-oauth.js
+++ b/dist/lib/swagger-oauth.js
@@ -202,7 +202,7 @@ function initOAuth(opts) {
});
}
-function processOAuthCode(data) {
+window.processOAuthCode = function processOAuthCode(data) {
var params = {
'client_id': clientId,
'code': data.code,
@@ -225,7 +225,7 @@ function processOAuthCode(data) {
});
}
-function onOAuthComplete(token) {
+window.onOAuthComplete = function onOAuthComplete(token) {
if(token) {
if(token.error) {
var checkbox = $('input[type=checkbox],.secured')
diff --git a/dist/swagger-ui.js b/dist/swagger-ui.js
index d16532b2..5678f451 100644
--- a/dist/swagger-ui.js
+++ b/dist/swagger-ui.js
@@ -39,7 +39,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('
') ;
}
@@ -59,7 +59,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);
});
},
@@ -118,7 +118,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) {
@@ -200,7 +201,40 @@ 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"]["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) {
@@ -317,7 +351,7 @@ if (Function.prototype.bind && console && typeof console.log === "object") {
}, Function.prototype.call);
}
-var Docs = {
+window.Docs = {
shebang: function() {
@@ -1013,7 +1047,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();
@@ -1024,18 +1061,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();
@@ -1060,7 +1097,10 @@ SwaggerUi.Views.ApiKeyButton = Backbone.View.extend({ // TODO: append this to gl
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();
@@ -1077,14 +1117,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();
@@ -1181,6 +1221,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;
@@ -1221,12 +1264,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);
}
}
@@ -1264,6 +1307,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',
@@ -1293,6 +1337,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;
@@ -1444,6 +1489,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);
@@ -1479,7 +1525,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;
@@ -1508,7 +1555,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);
},
@@ -2021,6 +2072,7 @@ SwaggerUi.Views.ParameterView = Backbone.View.extend({
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;
@@ -2068,6 +2120,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,
@@ -2163,19 +2216,19 @@ SwaggerUi.Views.SignatureView = Backbone.View.extend({
'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'});
diff --git a/dist/swagger-ui.min.js b/dist/swagger-ui.min.js
index f4499c38..a8b7dcc8 100644
--- a/dist/swagger-ui.min.js
+++ b/dist/swagger-ui.min.js
@@ -1,2 +1,2 @@
-(function(){"use strict";window.SwaggerUi=Backbone.Router.extend({dom_id:"swagger_ui",options:null,api:null,headerView:null,mainView:null,initialize:function(e){e=e||{},e.dom_id&&(this.dom_id=e.dom_id,delete e.dom_id),e.supportedSubmitMethods||(e.supportedSubmitMethods=["get","put","post","delete","head","options","patch"]),$("#"+this.dom_id)||$("body").append(''),this.options=e,marked.setOptions({gfm:!0});var n=this;this.options.success=function(){return n.render()},this.options.progress=function(e){return n.showMessage(e)},this.options.failure=function(e){return n.onLoadFailure(e)},this.headerView=new SwaggerUi.Views.HeaderView({el:$("#header")}),this.headerView.on("update-swagger-ui",function(e){return this.updateSwaggerUi(e)})},setOption:function(e,n){this.options[e]=n},getOption:function(e){return this.options[e]},updateSwaggerUi:function(e){this.options.url=e.url,this.load()},load:function(){this.mainView&&this.mainView.clear();var e=this.options.url;e&&0!==e.indexOf("http")&&(e=this.buildUrl(window.location.href.toString(),e)),this.options.url=e,this.headerView.update(e),this.api=new SwaggerClient(this.options)},collapseAll:function(){e.collapseEndpointListForResource("")},listAll:function(){e.collapseOperationsForResource("")},expandAll:function(){e.expandOperationsForResource("")},render:function(){switch(this.showMessage("Finished Loading Resource Information. Rendering Swagger UI..."),this.mainView=new SwaggerUi.Views.MainView({model:this.api,el:$("#"+this.dom_id),swaggerOptions:this.options}).render(),this.showMessage(),this.options.docExpansion){case"full":this.expandAll();break;case"list":this.listAll()}this.renderGFM(),this.options.onComplete&&this.options.onComplete(this.api,this),setTimeout(e.shebang.bind(this),100)},buildUrl:function(e,n){if(0===n.indexOf("/")){var a=e.split("/");return e=a[0]+"//"+a[2],e+n}var t=e.length;return e.indexOf("?")>-1&&(t=Math.min(t,e.indexOf("?"))),e.indexOf("#")>-1&&(t=Math.min(t,e.indexOf("#"))),e=e.substring(0,t),-1!==e.indexOf("/",e.length-1)?e+n:e+"/"+n},showMessage:function(e){void 0===e&&(e=""),$("#message-bar").removeClass("message-fail"),$("#message-bar").addClass("message-success"),$("#message-bar").html(e)},onLoadFailure:function(e){void 0===e&&(e=""),$("#message-bar").removeClass("message-success"),$("#message-bar").addClass("message-fail");var n=$("#message-bar").html(e);return this.options.onFailure&&this.options.onFailure(e),n},renderGFM:function(){$(".markdown").each(function(){$(this).html(marked($(this).html()))})}}),window.SwaggerUi.Views={},this.Handlebars=this.Handlebars||{},this.Handlebars.templates=this.Handlebars.templates||{},this.Handlebars.templates.apikey_button_view=Handlebars.template({compiler:[6,">= 2.0.0-beta.1"],main:function(e,n,a,t){var i,s="function",l=n.helperMissing,r=this.escapeExpression;return"\n