main.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. const Mn = require('backbone.marionette');
  2. const App = require('../main');
  3. const AuditLogModel = require('../../models/audit-log');
  4. const ListView = require('./list/main');
  5. const template = require('./main.ejs');
  6. const ErrorView = require('../error/main');
  7. const EmptyView = require('../empty/main');
  8. module.exports = Mn.View.extend({
  9. id: 'audit-log',
  10. template: template,
  11. ui: {
  12. list_region: '.list-region',
  13. dimmer: '.dimmer',
  14. search: '.search-form',
  15. query: 'input[name="source-query"]'
  16. },
  17. fetch: App.Api.AuditLog.getAll,
  18. showData: function(response) {
  19. this.showChildView('list_region', new ListView({
  20. collection: new AuditLogModel.Collection(response)
  21. }));
  22. },
  23. showError: function(err) {
  24. this.showChildView('list_region', new ErrorView({
  25. code: err.code,
  26. message: err.message,
  27. retry: function () {
  28. App.Controller.showAuditLog();
  29. }
  30. }));
  31. console.error(err);
  32. },
  33. showEmpty: function() {
  34. this.showChildView('list_region', new EmptyView({
  35. title: App.i18n('audit-log', 'empty'),
  36. subtitle: App.i18n('audit-log', 'empty-subtitle')
  37. }));
  38. },
  39. regions: {
  40. list_region: '@ui.list_region'
  41. },
  42. events: {
  43. 'submit @ui.search': function (e) {
  44. e.preventDefault();
  45. let query = this.ui.query.val();
  46. this.fetch(['user'], query)
  47. .then(response => this.showData(response))
  48. .catch(err => {
  49. this.showError(err);
  50. });
  51. }
  52. },
  53. onRender: function () {
  54. let view = this;
  55. view.fetch(['user'])
  56. .then(response => {
  57. if (!view.isDestroyed() && response && response.length) {
  58. view.showData(response);
  59. } else {
  60. view.showEmpty();
  61. }
  62. })
  63. .catch(err => {
  64. view.showError(err);
  65. })
  66. .then(() => {
  67. view.ui.dimmer.removeClass('active');
  68. });
  69. }
  70. });