[auth] created auth collection view
This commit is contained in:
148
dist/swagger-ui.js
vendored
148
dist/swagger-ui.js
vendored
@@ -19242,7 +19242,7 @@ SwaggerUi.Models.ApiKeyAuthModel = Backbone.Model.extend({
|
||||
SwaggerUi.Views.ApiKeyAuthView = Backbone.View.extend({ // TODO: append this to global SwaggerUi
|
||||
|
||||
events: {
|
||||
'change .auth_input': 'apiKeyChange'
|
||||
'change .input_apiKey_entry': 'apiKeyChange'
|
||||
},
|
||||
|
||||
template: Handlebars.templates.apikey_auth,
|
||||
@@ -19365,7 +19365,15 @@ SwaggerUi.Collections.AuthsCollection = Backbone.Collection.extend({
|
||||
},
|
||||
|
||||
isValid: function () {
|
||||
return this.length === this.where({ valid: true }).length;
|
||||
var valid = true;
|
||||
|
||||
this.models.forEach(function(model) {
|
||||
if (!model.validate()) {
|
||||
valid = false;
|
||||
}
|
||||
});
|
||||
|
||||
return valid;
|
||||
},
|
||||
|
||||
isAuthorized: function () {
|
||||
@@ -19378,6 +19386,74 @@ SwaggerUi.Collections.AuthsCollection = Backbone.Collection.extend({
|
||||
});
|
||||
'use strict';
|
||||
|
||||
SwaggerUi.Views.AuthsCollectionView = Backbone.View.extend({
|
||||
|
||||
initialize: function(opts) {
|
||||
this.options = opts || {};
|
||||
this.options.data = this.options.data || {};
|
||||
this.router = this.options.router;
|
||||
|
||||
this.collection = new SwaggerUi.Collections.AuthsCollection();
|
||||
this.collection.add(this.parseData(opts.data));
|
||||
|
||||
this.$innerEl = $('<div>');
|
||||
},
|
||||
|
||||
render: function () {
|
||||
this.collection.each(function (auth) {
|
||||
this.renderOneAuth(auth);
|
||||
}, this);
|
||||
|
||||
this.$el.html(this.$innerEl.html() ? this.$innerEl : '');
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
renderOneAuth: function (authModel) {
|
||||
var authEl;
|
||||
var type = authModel.get('type');
|
||||
|
||||
//todo refactor move view name into var and call new with it.
|
||||
if (type === 'apiKey') {
|
||||
authEl = new SwaggerUi.Views.ApiKeyAuthView({model: authModel, router: this.router}).render().el;
|
||||
} else if (type === 'basic' && this.$innerEl.find('.basic_auth_container').length === 0) {
|
||||
authEl = new SwaggerUi.Views.BasicAuthView({model: authModel, router: this.router}).render().el;
|
||||
} else if (type === 'oauth2') {
|
||||
authEl = new SwaggerUi.Views.Oauth2View({model: authModel, router: this.router}).render().el;
|
||||
}
|
||||
|
||||
this.$innerEl.append(authEl);
|
||||
},
|
||||
|
||||
//todo move into collection
|
||||
parseData: function (data) {
|
||||
var authz = Object.assign({}, window.swaggerUi.api.clientAuthorizations.authz);
|
||||
|
||||
return _.map(data, function (auth, name) {
|
||||
var isBasic = authz.basic && auth.type === 'basic';
|
||||
|
||||
_.extend(auth, {
|
||||
title: name
|
||||
});
|
||||
|
||||
if (authz[name] || isBasic) {
|
||||
_.extend(auth, {
|
||||
isLogout: true,
|
||||
value: isBasic ? undefined : authz[name].value,
|
||||
username: isBasic ? authz.basic.username : undefined,
|
||||
password: isBasic ? authz.basic.password : undefined,
|
||||
valid: true
|
||||
});
|
||||
}
|
||||
|
||||
return auth;
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
'use strict';
|
||||
|
||||
/* global OAuthSchemeKeys */
|
||||
/* global redirect_uri */
|
||||
/* global clientId */
|
||||
@@ -19400,7 +19476,8 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||
},
|
||||
|
||||
selectors: {
|
||||
innerEl: '.auth_inner'
|
||||
innerEl: '.auth_inner',
|
||||
authBtn: '.auth_submit__button'
|
||||
},
|
||||
|
||||
initialize: function(opts) {
|
||||
@@ -19408,22 +19485,17 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||
opts.data = opts.data || {};
|
||||
this.router = this.options.router;
|
||||
|
||||
this.collection = new SwaggerUi.Collections.AuthsCollection();
|
||||
this.collection.add(this.parseData(opts.data));
|
||||
this.authsCollectionView = new SwaggerUi.Views.AuthsCollectionView({data: opts.data});
|
||||
|
||||
this.$el.html(this.tpls.main({
|
||||
isLogout: this.collection.isAuthorized(),
|
||||
isAuthorized: this.collection.isPartiallyAuthorized()
|
||||
isLogout: this.authsCollectionView.collection.isAuthorized(),
|
||||
isAuthorized: this.authsCollectionView.collection.isPartiallyAuthorized()
|
||||
}));
|
||||
this.$innerEl = this.$(this.selectors.innerEl);
|
||||
},
|
||||
|
||||
render: function () {
|
||||
this.renderAuths();
|
||||
|
||||
if (!this.$innerEl.html()) {
|
||||
this.$el.html('');
|
||||
}
|
||||
this.$innerEl.html(this.authsCollectionView.render().el);
|
||||
|
||||
return this;
|
||||
},
|
||||
@@ -19432,59 +19504,13 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
if (this.collection.isValid()) {
|
||||
if (this.authsCollectionView.collection.isValid()) {
|
||||
this.authorize();
|
||||
}
|
||||
},
|
||||
|
||||
parseData: function (data) {
|
||||
var authz = Object.assign({}, window.swaggerUi.api.clientAuthorizations.authz);
|
||||
|
||||
return _.map(data, function (auth, name) {
|
||||
var isBasic = authz.basic && auth.type === 'basic';
|
||||
|
||||
_.extend(auth, {
|
||||
title: name
|
||||
});
|
||||
|
||||
if (authz[name] || isBasic) {
|
||||
_.extend(auth, {
|
||||
isLogout: true,
|
||||
value: isBasic ? undefined : authz[name].value,
|
||||
username: isBasic ? authz.basic.username : undefined,
|
||||
password: isBasic ? authz.basic.password : undefined,
|
||||
valid: true
|
||||
});
|
||||
}
|
||||
|
||||
return auth;
|
||||
});
|
||||
},
|
||||
|
||||
renderAuths: function () {
|
||||
this.collection.each(function (auth) {
|
||||
this.renderOneAuth(auth);
|
||||
}, this);
|
||||
},
|
||||
|
||||
renderOneAuth: function (authModel) {
|
||||
var authEl;
|
||||
var type = authModel.get('type');
|
||||
|
||||
//todo refactor move view name into var and call new with it.
|
||||
if (type === 'apiKey') {
|
||||
authEl = new SwaggerUi.Views.ApiKeyAuthView({model: authModel, router: this.router}).render().el;
|
||||
} else if (type === 'basic' && this.$innerEl.find('.basic_auth_container').length === 0) {
|
||||
authEl = new SwaggerUi.Views.BasicAuthView({model: authModel, router: this.router}).render().el;
|
||||
} else if (type === 'oauth2') {
|
||||
authEl = new SwaggerUi.Views.Oauth2View({model: authModel, router: this.router}).render().el;
|
||||
}
|
||||
|
||||
this.$innerEl.append(authEl);
|
||||
},
|
||||
|
||||
authorize: function () {
|
||||
this.collection.forEach(function (auth) {
|
||||
this.authsCollectionView.collection.forEach(function (auth) {
|
||||
var keyAuth, basicAuth;
|
||||
var type = auth.get('type');
|
||||
|
||||
@@ -19510,7 +19536,7 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||
logoutClick: function (e) {
|
||||
e.preventDefault();
|
||||
|
||||
this.collection.forEach(function (auth) {
|
||||
this.authsCollectionView.collection.forEach(function (auth) {
|
||||
var name = auth.get('type') === 'basic' ? 'basic' : auth.get('title');
|
||||
|
||||
window.swaggerUi.api.clientAuthorizations.remove(name);
|
||||
|
||||
14
dist/swagger-ui.min.js
vendored
14
dist/swagger-ui.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -3,7 +3,7 @@
|
||||
SwaggerUi.Views.ApiKeyAuthView = Backbone.View.extend({ // TODO: append this to global SwaggerUi
|
||||
|
||||
events: {
|
||||
'change .auth_input': 'apiKeyChange'
|
||||
'change .input_apiKey_entry': 'apiKeyChange'
|
||||
},
|
||||
|
||||
template: Handlebars.templates.apikey_auth,
|
||||
|
||||
@@ -22,7 +22,8 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||
},
|
||||
|
||||
selectors: {
|
||||
innerEl: '.auth_inner'
|
||||
innerEl: '.auth_inner',
|
||||
authBtn: '.auth_submit__button'
|
||||
},
|
||||
|
||||
initialize: function(opts) {
|
||||
@@ -30,22 +31,17 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||
opts.data = opts.data || {};
|
||||
this.router = this.options.router;
|
||||
|
||||
this.collection = new SwaggerUi.Collections.AuthsCollection();
|
||||
this.collection.add(this.parseData(opts.data));
|
||||
this.authsCollectionView = new SwaggerUi.Views.AuthsCollectionView({data: opts.data});
|
||||
|
||||
this.$el.html(this.tpls.main({
|
||||
isLogout: this.collection.isAuthorized(),
|
||||
isAuthorized: this.collection.isPartiallyAuthorized()
|
||||
isLogout: this.authsCollectionView.collection.isAuthorized(),
|
||||
isAuthorized: this.authsCollectionView.collection.isPartiallyAuthorized()
|
||||
}));
|
||||
this.$innerEl = this.$(this.selectors.innerEl);
|
||||
},
|
||||
|
||||
render: function () {
|
||||
this.renderAuths();
|
||||
|
||||
if (!this.$innerEl.html()) {
|
||||
this.$el.html('');
|
||||
}
|
||||
this.$innerEl.html(this.authsCollectionView.render().el);
|
||||
|
||||
return this;
|
||||
},
|
||||
@@ -54,59 +50,13 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
if (this.collection.isValid()) {
|
||||
if (this.authsCollectionView.collection.isValid()) {
|
||||
this.authorize();
|
||||
}
|
||||
},
|
||||
|
||||
parseData: function (data) {
|
||||
var authz = Object.assign({}, window.swaggerUi.api.clientAuthorizations.authz);
|
||||
|
||||
return _.map(data, function (auth, name) {
|
||||
var isBasic = authz.basic && auth.type === 'basic';
|
||||
|
||||
_.extend(auth, {
|
||||
title: name
|
||||
});
|
||||
|
||||
if (authz[name] || isBasic) {
|
||||
_.extend(auth, {
|
||||
isLogout: true,
|
||||
value: isBasic ? undefined : authz[name].value,
|
||||
username: isBasic ? authz.basic.username : undefined,
|
||||
password: isBasic ? authz.basic.password : undefined,
|
||||
valid: true
|
||||
});
|
||||
}
|
||||
|
||||
return auth;
|
||||
});
|
||||
},
|
||||
|
||||
renderAuths: function () {
|
||||
this.collection.each(function (auth) {
|
||||
this.renderOneAuth(auth);
|
||||
}, this);
|
||||
},
|
||||
|
||||
renderOneAuth: function (authModel) {
|
||||
var authEl;
|
||||
var type = authModel.get('type');
|
||||
|
||||
//todo refactor move view name into var and call new with it.
|
||||
if (type === 'apiKey') {
|
||||
authEl = new SwaggerUi.Views.ApiKeyAuthView({model: authModel, router: this.router}).render().el;
|
||||
} else if (type === 'basic' && this.$innerEl.find('.basic_auth_container').length === 0) {
|
||||
authEl = new SwaggerUi.Views.BasicAuthView({model: authModel, router: this.router}).render().el;
|
||||
} else if (type === 'oauth2') {
|
||||
authEl = new SwaggerUi.Views.Oauth2View({model: authModel, router: this.router}).render().el;
|
||||
}
|
||||
|
||||
this.$innerEl.append(authEl);
|
||||
},
|
||||
|
||||
authorize: function () {
|
||||
this.collection.forEach(function (auth) {
|
||||
this.authsCollectionView.collection.forEach(function (auth) {
|
||||
var keyAuth, basicAuth;
|
||||
var type = auth.get('type');
|
||||
|
||||
@@ -132,7 +82,7 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||
logoutClick: function (e) {
|
||||
e.preventDefault();
|
||||
|
||||
this.collection.forEach(function (auth) {
|
||||
this.authsCollectionView.collection.forEach(function (auth) {
|
||||
var name = auth.get('type') === 'basic' ? 'basic' : auth.get('title');
|
||||
|
||||
window.swaggerUi.api.clientAuthorizations.remove(name);
|
||||
|
||||
@@ -38,7 +38,15 @@ SwaggerUi.Collections.AuthsCollection = Backbone.Collection.extend({
|
||||
},
|
||||
|
||||
isValid: function () {
|
||||
return this.length === this.where({ valid: true }).length;
|
||||
var valid = true;
|
||||
|
||||
this.models.forEach(function(model) {
|
||||
if (!model.validate()) {
|
||||
valid = false;
|
||||
}
|
||||
});
|
||||
|
||||
return valid;
|
||||
},
|
||||
|
||||
isAuthorized: function () {
|
||||
|
||||
67
src/main/javascript/view/AuthsCollectionView.js
Normal file
67
src/main/javascript/view/AuthsCollectionView.js
Normal file
@@ -0,0 +1,67 @@
|
||||
'use strict';
|
||||
|
||||
SwaggerUi.Views.AuthsCollectionView = Backbone.View.extend({
|
||||
|
||||
initialize: function(opts) {
|
||||
this.options = opts || {};
|
||||
this.options.data = this.options.data || {};
|
||||
this.router = this.options.router;
|
||||
|
||||
this.collection = new SwaggerUi.Collections.AuthsCollection();
|
||||
this.collection.add(this.parseData(opts.data));
|
||||
|
||||
this.$innerEl = $('<div>');
|
||||
},
|
||||
|
||||
render: function () {
|
||||
this.collection.each(function (auth) {
|
||||
this.renderOneAuth(auth);
|
||||
}, this);
|
||||
|
||||
this.$el.html(this.$innerEl.html() ? this.$innerEl : '');
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
renderOneAuth: function (authModel) {
|
||||
var authEl;
|
||||
var type = authModel.get('type');
|
||||
|
||||
//todo refactor move view name into var and call new with it.
|
||||
if (type === 'apiKey') {
|
||||
authEl = new SwaggerUi.Views.ApiKeyAuthView({model: authModel, router: this.router}).render().el;
|
||||
} else if (type === 'basic' && this.$innerEl.find('.basic_auth_container').length === 0) {
|
||||
authEl = new SwaggerUi.Views.BasicAuthView({model: authModel, router: this.router}).render().el;
|
||||
} else if (type === 'oauth2') {
|
||||
authEl = new SwaggerUi.Views.Oauth2View({model: authModel, router: this.router}).render().el;
|
||||
}
|
||||
|
||||
this.$innerEl.append(authEl);
|
||||
},
|
||||
|
||||
//todo move into collection
|
||||
parseData: function (data) {
|
||||
var authz = Object.assign({}, window.swaggerUi.api.clientAuthorizations.authz);
|
||||
|
||||
return _.map(data, function (auth, name) {
|
||||
var isBasic = authz.basic && auth.type === 'basic';
|
||||
|
||||
_.extend(auth, {
|
||||
title: name
|
||||
});
|
||||
|
||||
if (authz[name] || isBasic) {
|
||||
_.extend(auth, {
|
||||
isLogout: true,
|
||||
value: isBasic ? undefined : authz[name].value,
|
||||
username: isBasic ? authz.basic.username : undefined,
|
||||
password: isBasic ? authz.basic.password : undefined,
|
||||
valid: true
|
||||
});
|
||||
}
|
||||
|
||||
return auth;
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user