2018-07-08 22:21:03 -04:00
|
|
|
'use strict';
|
|
|
|
|
2018-07-16 03:00:52 -04:00
|
|
|
const _ = require('underscore');
|
2018-07-08 22:21:03 -04:00
|
|
|
const Mn = require('backbone.marionette');
|
|
|
|
const App = require('../../main');
|
|
|
|
const ProxyHostModel = require('../../../models/proxy-host');
|
2018-07-18 02:55:09 -04:00
|
|
|
const template = require('./form.ejs');
|
2018-07-08 22:21:03 -04:00
|
|
|
|
|
|
|
require('jquery-serializejson');
|
|
|
|
require('jquery-mask-plugin');
|
2018-07-17 18:35:49 -04:00
|
|
|
require('selectize');
|
2018-07-08 22:21:03 -04:00
|
|
|
|
|
|
|
module.exports = Mn.View.extend({
|
|
|
|
template: template,
|
|
|
|
className: 'modal-dialog',
|
|
|
|
|
|
|
|
ui: {
|
2018-07-16 03:00:52 -04:00
|
|
|
form: 'form',
|
2018-07-17 18:35:49 -04:00
|
|
|
domain_names: 'input[name="domain_names"]',
|
2018-07-16 03:00:52 -04:00
|
|
|
forward_ip: 'input[name="forward_ip"]',
|
|
|
|
buttons: '.modal-footer button',
|
|
|
|
cancel: 'button.cancel',
|
|
|
|
save: 'button.save',
|
|
|
|
ssl_enabled: 'input[name="ssl_enabled"]',
|
|
|
|
ssl_options: '#ssl-options input',
|
|
|
|
ssl_provider: 'input[name="ssl_provider"]',
|
|
|
|
|
|
|
|
// SSL hiding and showing
|
|
|
|
all_ssl: '.letsencrypt-ssl, .other-ssl',
|
|
|
|
letsencrypt_ssl: '.letsencrypt-ssl',
|
|
|
|
other_ssl: '.other-ssl'
|
2018-07-08 22:21:03 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
events: {
|
2018-07-16 03:00:52 -04:00
|
|
|
'change @ui.ssl_enabled': function () {
|
|
|
|
let enabled = this.ui.ssl_enabled.prop('checked');
|
|
|
|
this.ui.ssl_options.not(this.ui.ssl_enabled).prop('disabled', !enabled).parents('.form-group').css('opacity', enabled ? 1 : 0.5);
|
|
|
|
this.ui.ssl_provider.trigger('change');
|
|
|
|
},
|
|
|
|
|
|
|
|
'change @ui.ssl_provider': function () {
|
|
|
|
let enabled = this.ui.ssl_enabled.prop('checked');
|
|
|
|
let provider = this.ui.ssl_provider.filter(':checked').val();
|
|
|
|
this.ui.all_ssl.hide().find('input').prop('disabled', true);
|
|
|
|
this.ui[provider + '_ssl'].show().find('input').prop('disabled', !enabled);
|
|
|
|
},
|
2018-07-08 22:21:03 -04:00
|
|
|
|
|
|
|
'click @ui.save': function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
2018-07-16 03:00:52 -04:00
|
|
|
if (!this.ui.form[0].checkValidity()) {
|
|
|
|
$('<input type="submit">').hide().appendTo(this.ui.form).click().remove();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-08 22:21:03 -04:00
|
|
|
let view = this;
|
|
|
|
let data = this.ui.form.serializeJSON();
|
|
|
|
|
|
|
|
// Manipulate
|
2018-07-16 03:00:52 -04:00
|
|
|
data.forward_port = parseInt(data.forward_port, 10);
|
|
|
|
_.map(data, function (item, idx) {
|
|
|
|
if (typeof item === 'string' && item === '1') {
|
|
|
|
item = true;
|
|
|
|
} else if (typeof item === 'object' && item !== null) {
|
|
|
|
_.map(item, function (item2, idx2) {
|
|
|
|
if (typeof item2 === 'string' && item2 === '1') {
|
|
|
|
item[idx2] = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
data[idx] = item;
|
|
|
|
});
|
2018-07-08 22:21:03 -04:00
|
|
|
|
2018-07-17 18:35:49 -04:00
|
|
|
if (typeof data.domain_names === 'string' && data.domain_names) {
|
|
|
|
data.domain_names = data.domain_names.split(',');
|
|
|
|
}
|
|
|
|
|
2018-07-16 03:00:52 -04:00
|
|
|
// Process
|
2018-07-08 22:21:03 -04:00
|
|
|
this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
|
2018-07-18 02:55:09 -04:00
|
|
|
let method = App.Api.Nginx.ProxyHosts.create;
|
2018-07-08 22:21:03 -04:00
|
|
|
|
|
|
|
if (this.model.get('id')) {
|
|
|
|
// edit
|
2018-07-18 02:55:09 -04:00
|
|
|
method = App.Api.Nginx.ProxyHosts.update;
|
2018-07-08 22:21:03 -04:00
|
|
|
data.id = this.model.get('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
method(data)
|
|
|
|
.then(result => {
|
|
|
|
view.model.set(result);
|
|
|
|
App.UI.closeModal(function () {
|
2018-07-18 02:55:09 -04:00
|
|
|
if (method === App.Api.Nginx.ProxyHosts.create) {
|
|
|
|
App.Controller.showNginxProxy();
|
2018-07-08 22:21:03 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2018-07-16 03:00:52 -04:00
|
|
|
alert(err.message);
|
2018-07-08 22:21:03 -04:00
|
|
|
this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-07-16 03:00:52 -04:00
|
|
|
templateContext: {
|
|
|
|
getLetsencryptEmail: function () {
|
2018-07-18 02:55:09 -04:00
|
|
|
return typeof this.meta.letsencrypt_email !== 'undefined' ? this.meta.letsencrypt_email : App.Cache.User.get('email');
|
2018-07-16 03:00:52 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
getLetsencryptAgree: function () {
|
|
|
|
return typeof this.meta.letsencrypt_agree !== 'undefined' ? this.meta.letsencrypt_agree : false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-07-08 22:21:03 -04:00
|
|
|
onRender: function () {
|
|
|
|
this.ui.forward_ip.mask('099.099.099.099', {
|
|
|
|
clearIfNotMatch: true,
|
|
|
|
placeholder: '000.000.000.000'
|
|
|
|
});
|
2018-07-16 03:00:52 -04:00
|
|
|
|
|
|
|
this.ui.ssl_enabled.trigger('change');
|
|
|
|
this.ui.ssl_provider.trigger('change');
|
|
|
|
|
2018-07-17 18:35:49 -04:00
|
|
|
this.ui.domain_names.selectize({
|
|
|
|
delimiter: ',',
|
|
|
|
persist: false,
|
|
|
|
maxOptions: 15,
|
|
|
|
create: function (input) {
|
|
|
|
return {
|
|
|
|
value: input,
|
|
|
|
text: input
|
|
|
|
};
|
|
|
|
},
|
|
|
|
createFilter: /^(?:\*\.)?(?:[^.*]+\.?)+[^.]$/
|
|
|
|
});
|
2018-07-08 22:21:03 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function (options) {
|
|
|
|
if (typeof options.model === 'undefined' || !options.model) {
|
|
|
|
this.model = new ProxyHostModel.Model();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|