form.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. const Mn = require('backbone.marionette');
  2. const App = require('../../main');
  3. const AccessListModel = require('../../../models/access-list');
  4. const template = require('./form.ejs');
  5. const ItemView = require('./form/item');
  6. const ClientView = require('./form/client');
  7. require('jquery-serializejson');
  8. const ItemsView = Mn.CollectionView.extend({
  9. childView: ItemView
  10. });
  11. const ClientsView = Mn.CollectionView.extend({
  12. childView: ClientView
  13. });
  14. module.exports = Mn.View.extend({
  15. template: template,
  16. className: 'modal-dialog',
  17. ui: {
  18. items_region: '.items',
  19. clients_region: '.clients',
  20. form: 'form',
  21. buttons: '.modal-footer button',
  22. cancel: 'button.cancel',
  23. save: 'button.save',
  24. access_add: 'button.access_add',
  25. auth_add: 'button.auth_add'
  26. },
  27. regions: {
  28. items_region: '@ui.items_region',
  29. clients_region: '@ui.clients_region'
  30. },
  31. events: {
  32. 'click @ui.save': function (e) {
  33. e.preventDefault();
  34. if (!this.ui.form[0].checkValidity()) {
  35. $('<input type="submit">').hide().appendTo(this.ui.form).click().remove();
  36. return;
  37. }
  38. let view = this;
  39. let form_data = this.ui.form.serializeJSON();
  40. let items_data = [];
  41. let clients_data = [];
  42. form_data.username.map(function (val, idx) {
  43. if (val.trim().length) {
  44. items_data.push({
  45. username: val.trim(),
  46. password: form_data.password[idx]
  47. });
  48. }
  49. });
  50. form_data.address.map(function (val, idx) {
  51. if (val.trim().length) {
  52. clients_data.push({
  53. address: val.trim(),
  54. directive: form_data.directive[idx]
  55. })
  56. }
  57. });
  58. if (!items_data.length && !clients_data.length) {
  59. alert('You must specify at least 1 Authorization or Access rule');
  60. return;
  61. }
  62. let data = {
  63. name: form_data.name,
  64. satisfy_any: !!form_data.satisfy_any,
  65. pass_auth: !!form_data.pass_auth,
  66. items: items_data,
  67. clients: clients_data
  68. };
  69. console.log(data);
  70. let method = App.Api.Nginx.AccessLists.create;
  71. let is_new = true;
  72. if (this.model.get('id')) {
  73. // edit
  74. is_new = false;
  75. method = App.Api.Nginx.AccessLists.update;
  76. data.id = this.model.get('id');
  77. }
  78. this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
  79. method(data)
  80. .then(result => {
  81. view.model.set(result);
  82. App.UI.closeModal(function () {
  83. if (is_new) {
  84. App.Controller.showNginxAccess();
  85. }
  86. });
  87. })
  88. .catch(err => {
  89. alert(err.message);
  90. this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
  91. });
  92. },
  93. 'click @ui.access_add': function (e) {
  94. e.preventDefault();
  95. let clients = this.model.get('clients');
  96. clients.push({});
  97. this.showChildView('clients_region', new ClientsView({
  98. collection: new Backbone.Collection(clients)
  99. }));
  100. },
  101. 'click @ui.auth_add': function (e) {
  102. e.preventDefault();
  103. let items = this.model.get('items');
  104. items.push({});
  105. this.showChildView('items_region', new ItemsView({
  106. collection: new Backbone.Collection(items)
  107. }));
  108. }
  109. },
  110. onRender: function () {
  111. let items = this.model.get('items');
  112. let clients = this.model.get('clients');
  113. // Ensure at least one field is shown initally
  114. if (!items.length) items.push({});
  115. if (!clients.length) clients.push({});
  116. this.showChildView('items_region', new ItemsView({
  117. collection: new Backbone.Collection(items)
  118. }));
  119. this.showChildView('clients_region', new ClientsView({
  120. collection: new Backbone.Collection(clients)
  121. }));
  122. },
  123. initialize: function (options) {
  124. if (typeof options.model === 'undefined' || !options.model) {
  125. this.model = new AccessListModel.Model();
  126. }
  127. }
  128. });