2018-08-15 23:08:56 -04:00
|
|
|
const Mn = require('backbone.marionette');
|
|
|
|
const App = require('../../main');
|
|
|
|
const AccessListModel = require('../../../models/access-list');
|
|
|
|
const template = require('./form.ejs');
|
2018-08-18 03:16:23 -04:00
|
|
|
const ItemView = require('./form/item');
|
2020-04-10 20:33:14 -04:00
|
|
|
const ClientView = require('./form/client');
|
2018-07-24 02:56:39 -04:00
|
|
|
|
|
|
|
require('jquery-serializejson');
|
2018-08-18 03:16:23 -04:00
|
|
|
|
|
|
|
const ItemsView = Mn.CollectionView.extend({
|
|
|
|
childView: ItemView
|
|
|
|
});
|
2018-07-24 02:56:39 -04:00
|
|
|
|
2020-04-10 20:33:14 -04:00
|
|
|
const ClientsView = Mn.CollectionView.extend({
|
|
|
|
childView: ClientView
|
|
|
|
});
|
|
|
|
|
2018-07-24 02:56:39 -04:00
|
|
|
module.exports = Mn.View.extend({
|
|
|
|
template: template,
|
|
|
|
className: 'modal-dialog',
|
|
|
|
|
|
|
|
ui: {
|
2020-04-10 20:33:14 -04:00
|
|
|
items_region: '.items',
|
|
|
|
clients_region: '.clients',
|
|
|
|
form: 'form',
|
|
|
|
buttons: '.modal-footer button',
|
|
|
|
cancel: 'button.cancel',
|
2021-03-25 14:59:07 -04:00
|
|
|
save: 'button.save',
|
|
|
|
access_add: 'button.access_add',
|
|
|
|
auth_add: 'button.auth_add'
|
2018-08-18 03:16:23 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
regions: {
|
2020-04-10 20:33:14 -04:00
|
|
|
items_region: '@ui.items_region',
|
|
|
|
clients_region: '@ui.clients_region'
|
2018-07-24 02:56:39 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
events: {
|
|
|
|
'click @ui.save': function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (!this.ui.form[0].checkValidity()) {
|
|
|
|
$('<input type="submit">').hide().appendTo(this.ui.form).click().remove();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-10 20:33:14 -04:00
|
|
|
let view = this;
|
|
|
|
let form_data = this.ui.form.serializeJSON();
|
|
|
|
let items_data = [];
|
|
|
|
let clients_data = [];
|
2018-07-24 02:56:39 -04:00
|
|
|
|
2018-08-18 03:16:23 -04:00
|
|
|
form_data.username.map(function (val, idx) {
|
|
|
|
if (val.trim().length) {
|
|
|
|
items_data.push({
|
|
|
|
username: val.trim(),
|
|
|
|
password: form_data.password[idx]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-10 20:33:14 -04:00
|
|
|
form_data.address.map(function (val, idx) {
|
|
|
|
if (val.trim().length) {
|
|
|
|
clients_data.push({
|
|
|
|
address: val.trim(),
|
|
|
|
directive: form_data.directive[idx]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-11 03:26:54 -04:00
|
|
|
if (!items_data.length && !clients_data.length) {
|
|
|
|
alert('You must specify at least 1 Authorization or Access rule');
|
2018-08-18 03:16:23 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = {
|
2020-04-11 03:26:54 -04:00
|
|
|
name: form_data.name,
|
2020-04-21 21:11:20 -04:00
|
|
|
satisfy_any: !!form_data.satisfy_any,
|
2020-10-14 05:17:25 -04:00
|
|
|
pass_auth: !!form_data.pass_auth,
|
2020-04-11 03:26:54 -04:00
|
|
|
items: items_data,
|
|
|
|
clients: clients_data
|
2018-08-18 03:16:23 -04:00
|
|
|
};
|
2018-07-24 02:56:39 -04:00
|
|
|
|
2020-04-11 03:26:54 -04:00
|
|
|
console.log(data);
|
|
|
|
|
2018-08-15 23:08:56 -04:00
|
|
|
let method = App.Api.Nginx.AccessLists.create;
|
|
|
|
let is_new = true;
|
2018-07-24 02:56:39 -04:00
|
|
|
|
|
|
|
if (this.model.get('id')) {
|
|
|
|
// edit
|
|
|
|
is_new = false;
|
2018-08-15 23:08:56 -04:00
|
|
|
method = App.Api.Nginx.AccessLists.update;
|
2018-07-24 02:56:39 -04:00
|
|
|
data.id = this.model.get('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
|
|
|
|
method(data)
|
|
|
|
.then(result => {
|
|
|
|
view.model.set(result);
|
|
|
|
|
|
|
|
App.UI.closeModal(function () {
|
|
|
|
if (is_new) {
|
2018-08-15 23:08:56 -04:00
|
|
|
App.Controller.showNginxAccess();
|
2018-07-24 02:56:39 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
alert(err.message);
|
|
|
|
this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
|
|
|
|
});
|
2021-03-25 14:59:07 -04:00
|
|
|
},
|
|
|
|
'click @ui.access_add': function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
let clients = this.model.get('clients');
|
|
|
|
clients.push({});
|
|
|
|
this.showChildView('clients_region', new ClientsView({
|
|
|
|
collection: new Backbone.Collection(clients)
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
'click @ui.auth_add': function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
let items = this.model.get('items');
|
|
|
|
items.push({});
|
|
|
|
this.showChildView('items_region', new ItemsView({
|
|
|
|
collection: new Backbone.Collection(items)
|
|
|
|
}));
|
2018-07-24 02:56:39 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-08-18 03:16:23 -04:00
|
|
|
onRender: function () {
|
|
|
|
let items = this.model.get('items');
|
2020-04-10 20:33:14 -04:00
|
|
|
let clients = this.model.get('clients');
|
2018-08-18 03:16:23 -04:00
|
|
|
|
2021-03-25 14:59:07 -04:00
|
|
|
// Ensure at least one field is shown initally
|
|
|
|
if (!items.length) items.push({});
|
|
|
|
if (!clients.length) clients.push({});
|
2020-04-10 20:33:14 -04:00
|
|
|
|
2018-08-18 03:16:23 -04:00
|
|
|
this.showChildView('items_region', new ItemsView({
|
|
|
|
collection: new Backbone.Collection(items)
|
|
|
|
}));
|
2020-04-10 20:33:14 -04:00
|
|
|
|
|
|
|
this.showChildView('clients_region', new ClientsView({
|
|
|
|
collection: new Backbone.Collection(clients)
|
|
|
|
}));
|
2018-08-18 03:16:23 -04:00
|
|
|
},
|
|
|
|
|
2018-07-24 02:56:39 -04:00
|
|
|
initialize: function (options) {
|
|
|
|
if (typeof options.model === 'undefined' || !options.model) {
|
2018-08-15 23:08:56 -04:00
|
|
|
this.model = new AccessListModel.Model();
|
2018-07-24 02:56:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|