form.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const Mn = require('backbone.marionette');
  2. const App = require('../../main');
  3. const SslPassthroughModel = require('../../../models/ssl-passthrough-host');
  4. const template = require('./form.ejs');
  5. require('jquery-serializejson');
  6. require('jquery-mask-plugin');
  7. require('selectize');
  8. module.exports = Mn.View.extend({
  9. template: template,
  10. className: 'modal-dialog',
  11. ui: {
  12. form: 'form',
  13. forwarding_host: 'input[name="forwarding_host"]',
  14. type_error: '.forward-type-error',
  15. buttons: '.modal-footer button',
  16. switches: '.custom-switch-input',
  17. cancel: 'button.cancel',
  18. save: 'button.save'
  19. },
  20. events: {
  21. 'change @ui.switches': function () {
  22. this.ui.type_error.hide();
  23. },
  24. 'click @ui.save': function (e) {
  25. e.preventDefault();
  26. if (!this.ui.form[0].checkValidity()) {
  27. $('<input type="submit">').hide().appendTo(this.ui.form).click().remove();
  28. return;
  29. }
  30. let view = this;
  31. let data = this.ui.form.serializeJSON();
  32. // Manipulate
  33. data.incoming_port = parseInt(data.incoming_port, 10);
  34. data.forwarding_port = parseInt(data.forwarding_port, 10);
  35. let method = App.Api.Nginx.SslPassthroughHosts.create;
  36. let is_new = true;
  37. if (this.model.get('id')) {
  38. // edit
  39. is_new = false;
  40. method = App.Api.Nginx.SslPassthroughHosts.update;
  41. data.id = this.model.get('id');
  42. }
  43. this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
  44. method(data)
  45. .then(result => {
  46. view.model.set(result);
  47. App.UI.closeModal(function () {
  48. if (is_new) {
  49. App.Controller.showNginxSslPassthrough();
  50. }
  51. });
  52. })
  53. .catch(err => {
  54. alert(err.message);
  55. this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
  56. });
  57. }
  58. },
  59. initialize: function (options) {
  60. if (typeof options.model === 'undefined' || !options.model) {
  61. this.model = new SslPassthroughModel.Model();
  62. }
  63. }
  64. });