[auth] created auth collection view

This commit is contained in:
bodnia
2016-03-13 02:11:56 +02:00
parent 0f77177455
commit e90a72ad4a
6 changed files with 180 additions and 129 deletions

148
dist/swagger-ui.js vendored
View File

@@ -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);

File diff suppressed because one or more lines are too long

View File

@@ -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,

View File

@@ -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);

View File

@@ -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 () {

View 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;
});
}
});