form.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. satify_any: !!form_data.satify_any,
  63. items: items_data,
  64. clients: clients_data
  65. };
  66. console.log(data);
  67. let method = App.Api.Nginx.AccessLists.create;
  68. let is_new = true;
  69. if (this.model.get('id')) {
  70. // edit
  71. is_new = false;
  72. method = App.Api.Nginx.AccessLists.update;
  73. data.id = this.model.get('id');
  74. }
  75. this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
  76. method(data)
  77. .then(result => {
  78. view.model.set(result);
  79. App.UI.closeModal(function () {
  80. if (is_new) {
  81. App.Controller.showNginxAccess();
  82. }
  83. });
  84. })
  85. .catch(err => {
  86. alert(err.message);
  87. this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
  88. });
  89. }
  90. },
  91. onRender: function () {
  92. let items = this.model.get('items');
  93. let clients = this.model.get('clients');
  94. // Add empty items to the end of the list. This is cheating but hey I don't have the time to do it right
  95. let items_to_add = 5 - items.length;
  96. if (items_to_add) {
  97. for (let i = 0; i < items_to_add; i++) {
  98. items.push({});
  99. }
  100. }
  101. let clients_to_add = 5 - clients.length;
  102. if (clients_to_add) {
  103. for (let i = 0; i < clients_to_add; i++) {
  104. clients.push({});
  105. }
  106. }
  107. this.showChildView('items_region', new ItemsView({
  108. collection: new Backbone.Collection(items)
  109. }));
  110. this.showChildView('clients_region', new ClientsView({
  111. collection: new Backbone.Collection(clients)
  112. }));
  113. },
  114. initialize: function (options) {
  115. if (typeof options.model === 'undefined' || !options.model) {
  116. this.model = new AccessListModel.Model();
  117. }
  118. }
  119. });