Put authorizations into popup

This commit is contained in:
Anna Bodnia
2016-02-23 20:56:09 +02:00
parent 40d3161385
commit 9207be7d4b
12 changed files with 100 additions and 214 deletions

View File

@@ -2,125 +2,51 @@
SwaggerUi.Views.AuthView = Backbone.View.extend({
events: {
'click .auth_submit_button': 'authorizeClick',
'click .auth_logout__button': 'logoutClick'
'click .authorize__btn': 'authorizeBtnClick'
},
tpls: {
main: Handlebars.templates.auth_view
popup: Handlebars.templates.popup,
authBtn: Handlebars.templates.auth_button
},
selectors: {
innerEl: '.auth_inner'
},
initialize: function(opts) {
this.options = opts || {};
opts.data = opts.data || {};
this.router = this.options.router;
this.collection = new Backbone.Collection();
this.collection.add(this.parseData(opts.data));
this.$el.html(this.tpls.main({isLogout: this.isAuthorizedCollection()}));
this.$innerEl = this.$(this.selectors.innerEl);
},
initialize: function(){},
render: function () {
this.renderAuths();
if (!this.$innerEl.html()) {
this.$el.html('');
}
this.$el.html(this.tpls.authBtn());
return this;
},
authorizeClick: function (e) {
authorizeBtnClick: function (e) {
var authsModel;
e.preventDefault();
if (this.isValidCollection()) {
this.authorize();
}
},
authsModel = {title: 'Please authorize', content: this.renderAuths()};
parseData: function (data) {
var authz = window.swaggerUi.api.clientAuthorizations.authz;
return _.map(data, function (auth, name) {
var isBasic = authz.basic && auth.type === 'basic';
if (authz[name] || isBasic) {
_.extend(auth, {
isLogout: true,
value: isBasic ? '' : authz[name].value,
valid: true
});
}
return auth;
});
this.popup = new SwaggerUi.Views.PopupView({model: authsModel});
this.popup.render();
},
renderAuths: function () {
this.collection.each(function (auth) {
this.renderOneAuth(auth);
}, this);
},
var name, authEl;
var el = $('<div>');
renderOneAuth: function (authModel) {
var authEl;
var type = authModel.get('type');
//todo refactor, copy-pasted from MainView.js
for (name in this.model) {
auth = this.model[name];
if (type === 'apiKey') {
authEl = new SwaggerUi.Views.ApiKeyAuthView({model: authModel, router: this.router}).render().el;
this.$innerEl.append(authEl);
} else if (type === 'basic' && this.$innerEl.find('.basic_auth_container').length === 0) {
authEl = new SwaggerUi.Views.BasicAuthView({model: authModel, router: this.router}).render().el;
this.$innerEl.append(authEl);
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);
}
}
},
isValidCollection: function () {
return this.collection.length === this.collection.where({ valid: true }).length;
},
authorize: function () {
this.collection.forEach(function (auth) {
var keyAuth, basicAuth;
var type = auth.get('type');
if (type === 'apiKey') {
keyAuth = new SwaggerClient.ApiKeyAuthorization(
auth.get('name'),
auth.get('value'),
auth.get('in')
);
this.router.api.clientAuthorizations.add(auth.get('name'), keyAuth);
} else if (type === 'basic') {
basicAuth = new SwaggerClient.PasswordAuthorization(auth.get('username'), auth.get('password'));
this.router.api.clientAuthorizations.add(auth.get('type'), basicAuth);
}
}, this);
this.router.load();
},
isAuthorizedCollection: function () {
return this.collection.length === this.collection.where({ isLogout: true }).length;
},
logoutClick: function (e) {
e.preventDefault();
this.collection.forEach(function (auth) {
var name = auth.get('name');
window.swaggerUi.api.clientAuthorizations.remove(name);
});
this.router.load();
return el.html();
}
});