form.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. },
  25. regions: {
  26. items_region: '@ui.items_region',
  27. clients_region: '@ui.clients_region'
  28. },
  29. events: {
  30. 'click @ui.save': function (e) {
  31. e.preventDefault();
  32. if (!this.ui.form[0].checkValidity()) {
  33. $('<input type="submit">').hide().appendTo(this.ui.form).click().remove();
  34. return;
  35. }
  36. let view = this;
  37. let form_data = this.ui.form.serializeJSON();
  38. let items_data = [];
  39. let clients_data = [];
  40. form_data.username.map(function (val, idx) {
  41. if (val.trim().length) {
  42. items_data.push({
  43. username: val.trim(),
  44. password: form_data.password[idx]
  45. });
  46. }
  47. });
  48. form_data.address.map(function (val, idx) {
  49. if (val.trim().length) {
  50. clients_data.push({
  51. address: val.trim(),
  52. directive: form_data.directive[idx]
  53. })
  54. }
  55. });
  56. if (!items_data.length && !clients_data.length) {
  57. alert('You must specify at least 1 Authorization or Access rule');
  58. return;
  59. }
  60. let data = {
  61. name: form_data.name,
  62. satisfy_any: !!form_data.satisfy_any,
  63. pass_auth: !!form_data.pass_auth,
  64. items: items_data,
  65. clients: clients_data
  66. };
  67. console.log(data);
  68. let method = App.Api.Nginx.AccessLists.create;
  69. let is_new = true;
  70. if (this.model.get('id')) {
  71. // edit
  72. is_new = false;
  73. method = App.Api.Nginx.AccessLists.update;
  74. data.id = this.model.get('id');
  75. }
  76. this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
  77. method(data)
  78. .then(result => {
  79. view.model.set(result);
  80. App.UI.closeModal(function () {
  81. if (is_new) {
  82. App.Controller.showNginxAccess();
  83. }
  84. });
  85. })
  86. .catch(err => {
  87. alert(err.message);
  88. this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
  89. });
  90. }
  91. },
  92. onRender: function () {
  93. let items = this.model.get('items');
  94. let clients = this.model.get('clients');
  95. // Add empty items to the end of the list. This is cheating but hey I don't have the time to do it right
  96. let items_to_add = 5 - items.length;
  97. if (items_to_add) {
  98. for (let i = 0; i < items_to_add; i++) {
  99. items.push({});
  100. }
  101. }
  102. let clients_to_add = 4 - clients.length;
  103. if (clients_to_add) {
  104. for (let i = 0; i < clients_to_add; i++) {
  105. clients.push({});
  106. }
  107. }
  108. this.showChildView('items_region', new ItemsView({
  109. collection: new Backbone.Collection(items)
  110. }));
  111. this.showChildView('clients_region', new ClientsView({
  112. collection: new Backbone.Collection(clients)
  113. }));
  114. },
  115. initialize: function (options) {
  116. if (typeof options.model === 'undefined' || !options.model) {
  117. this.model = new AccessListModel.Model();
  118. }
  119. }
  120. });