diff --git a/src/main/html/css/print.css b/src/main/html/css/print.css index a0a320de..f0d90689 100644 --- a/src/main/html/css/print.css +++ b/src/main/html/css/print.css @@ -1181,6 +1181,10 @@ .swagger-section .oauth_submit { text-align: center; } +.swagger-section .authorize__btn:hover { + text-decoration: underline; + cursor: pointer; +} .swagger-section .auth_container .basic_auth__title { color: #547f00; font-size: 1.2em; diff --git a/src/main/html/css/screen.css b/src/main/html/css/screen.css index b568a8bb..3235151e 100644 --- a/src/main/html/css/screen.css +++ b/src/main/html/css/screen.css @@ -1181,6 +1181,10 @@ .swagger-section .oauth_submit { text-align: center; } +.swagger-section .authorize__btn:hover { + text-decoration: underline; + cursor: pointer; +} .swagger-section .auth_container .basic_auth__title { color: #547f00; font-size: 1.2em; diff --git a/src/main/html/index.html b/src/main/html/index.html index d95471a1..7c5f45e9 100644 --- a/src/main/html/index.html +++ b/src/main/html/index.html @@ -36,7 +36,7 @@ if (url && url.length > 1) { url = decodeURIComponent(url[1]); } else { - url = "http://petstore.swagger.io/v2/swagger.json"; + url = "http://localhost:3001/swagger.json"; } hljs.configure({ diff --git a/src/main/javascript/view/ApiKeyButton.js b/src/main/javascript/view/ApiKeyButton.js index 1820ab6f..7d5cce66 100644 --- a/src/main/javascript/view/ApiKeyButton.js +++ b/src/main/javascript/view/ApiKeyButton.js @@ -14,7 +14,7 @@ SwaggerUi.Views.ApiKeyButton = Backbone.View.extend({ // TODO: append this to gl }, render: function(){ - $(this.el).html(this.template(this.model)); + this.$el.html(this.template(this.model)); return this; }, diff --git a/src/main/javascript/view/AuthView.js b/src/main/javascript/view/AuthView.js new file mode 100644 index 00000000..9433c8ae --- /dev/null +++ b/src/main/javascript/view/AuthView.js @@ -0,0 +1,52 @@ +'use strict'; + +SwaggerUi.Views.AuthView = Backbone.View.extend({ + events: { + 'click .authorize__btn': 'authorizeBtnClick' + }, + + tpls: { + popup: Handlebars.templates.popup, + authBtn: Handlebars.templates.auth_button + }, + + initialize: function(){}, + + render: function () { + this.$el.html(this.tpls.authBtn()); + + return this; + }, + + authorizeBtnClick: function (e) { + var authsModel; + e.preventDefault(); + + authsModel = {title: 'Please authorize', content: this.renderAuths()}; + + this.popup = new SwaggerUi.Views.PopupView({model: authsModel}); + this.popup.render(); + }, + + renderAuths: function () { + var name, authEl; + var el = $('
'); + + //todo refactor, copy-pasted from MainView.js + for (name in this.model) { + auth = this.model[name]; + + if (auth.type === 'apiKey') { + authEl = new SwaggerUi.Views.ApiKeyButton({model: auth, router: this.router}).render().el; + el.append(authEl); + } + + if (auth.type === 'basic' && el.find('.basic_auth_container').length === 0) { + authEl = new SwaggerUi.Views.BasicAuthButton({model: auth, router: this.router}).render().el; + el.append(authEl); + } + } + + return el.html(); + } +}); diff --git a/src/main/javascript/view/MainView.js b/src/main/javascript/view/MainView.js index 9d3c748f..7eb8aae2 100644 --- a/src/main/javascript/view/MainView.js +++ b/src/main/javascript/view/MainView.js @@ -89,18 +89,9 @@ SwaggerUi.Views.MainView = Backbone.View.extend({ $(this.el).html(Handlebars.templates.main(this.model)); this.model.securityDefinitions = this.model.securityDefinitions || {}; - for (name in this.model.securityDefinitions) { - auth = this.model.securityDefinitions[name]; - - if (auth.type === 'apiKey') { - authEl = new SwaggerUi.Views.ApiKeyButton({model: auth, router: this.router}).render().el; - $('.auth_main_container').append(authEl); - } - - if (auth.type === 'basic' && $('.auth_main_container .basic_auth_container').length === 0) { - authEl = new SwaggerUi.Views.BasicAuthButton({model: auth, router: this.router}).render().el; - $('.auth_main_container').append(authEl); - } + if (this.model.securityDefinitions) { + this.authView = new SwaggerUi.Views.AuthView({model: this.model.securityDefinitions}); + this.$('.authorize-wrapper').append(this.authView.render().el); } // Render each resource diff --git a/src/main/javascript/view/OperationView.js b/src/main/javascript/view/OperationView.js index dcf8c6c7..e89805de 100644 --- a/src/main/javascript/view/OperationView.js +++ b/src/main/javascript/view/OperationView.js @@ -32,21 +32,21 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({ }, selectText: function(event) { - var doc = document, - text = event.target.firstChild, - range, - selection; - if (doc.body.createTextRange) { - range = document.body.createTextRange(); - range.moveToElementText(text); - range.select(); - } else if (window.getSelection) { - selection = window.getSelection(); - range = document.createRange(); - range.selectNodeContents(text); - selection.removeAllRanges(); - selection.addRange(range); - } + var doc = document, + text = event.target.firstChild, + range, + selection; + if (doc.body.createTextRange) { + range = document.body.createTextRange(); + range.moveToElementText(text); + range.select(); + } else if (window.getSelection) { + selection = window.getSelection(); + range = document.createRange(); + range.selectNodeContents(text); + selection.removeAllRanges(); + selection.addRange(range); + } }, mouseEnter: function(e) { @@ -91,9 +91,54 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({ this.model.isReadOnly = true; } this.model.description = this.model.description || this.model.notes; - - this.handleAuth(); - + this.model.oauth = null; + modelAuths = this.model.authorizations || this.model.security; + if (modelAuths) { + if (Array.isArray(modelAuths)) { + for (l = 0, len = modelAuths.length; l < len; l++) { + auths = modelAuths[l]; + for (key in auths) { + for (a in this.auths) { + auth = this.auths[a]; + if (key === auth.name) { + if (auth.type === 'oauth2') { + this.model.oauth = {}; + this.model.oauth.scopes = []; + ref1 = auth.value.scopes; + for (k in ref1) { + v = ref1[k]; + scopeIndex = auths[key].indexOf(k); + if (scopeIndex >= 0) { + o = { + scope: k, + description: v + }; + this.model.oauth.scopes.push(o); + } + } + } + } + } + } + } + } else { + for (k in modelAuths) { + v = modelAuths[k]; + if (k === 'oauth2') { + if (this.model.oauth === null) { + this.model.oauth = {}; + } + if (this.model.oauth.scopes === void 0) { + this.model.oauth.scopes = []; + } + for (m = 0, len1 = v.length; m < len1; m++) { + o = v[m]; + this.model.oauth.scopes.push(o); + } + } + } + } + } if (typeof this.model.responses !== 'undefined') { this.model.responseMessages = []; ref2 = this.model.responses; @@ -447,7 +492,7 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({ // wraps a jquery response as a shred response wrap: function(data) { - var h, headerArray, headers, i, l, len, o; + var h, headerArray, headers, i, l, len, o; headers = {}; headerArray = data.getAllResponseHeaders().split('\r'); for (l = 0, len = headerArray.length; l < len; l++) { @@ -624,7 +669,7 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({ code = $('').text('no content'); pre = $('
').append(code);
 
-    // JSON
+      // JSON
     } else if (contentType === 'application/json' || /\+json$/.test(contentType)) {
       var json = null;
       try {
@@ -635,35 +680,35 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
       code = $('').text(json);
       pre = $('
').append(code);
 
-    // XML
+      // XML
     } else if (contentType === 'application/xml' || /\+xml$/.test(contentType)) {
       code = $('').text(this.formatXml(content));
       pre = $('
').append(code);
 
-    // HTML
+      // HTML
     } else if (contentType === 'text/html') {
       code = $('').html(_.escape(content));
       pre = $('
').append(code);
 
-    // Plain Text
+      // Plain Text
     } else if (/text\/plain/.test(contentType)) {
       code = $('').text(content);
       pre = $('
').append(code);
 
 
-    // Image
+      // Image
     } else if (/^image\//.test(contentType)) {
       pre = $('').attr('src', url);
 
-    // Audio
+      // Audio
     } else if (/^audio\//.test(contentType) && supportsAudioPlayback(contentType)) {
       pre = $('