main.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const Mn = require('backbone.marionette');
  2. const App = require('../../main');
  3. const CertificateModel = require('../../../models/certificate');
  4. const ListView = require('./list/main');
  5. const ErrorView = require('../../error/main');
  6. const EmptyView = require('../../empty/main');
  7. const template = require('./main.ejs');
  8. module.exports = Mn.View.extend({
  9. id: 'nginx-certificates',
  10. template: template,
  11. ui: {
  12. list_region: '.list-region',
  13. add: '.add-item',
  14. help: '.help',
  15. dimmer: '.dimmer',
  16. search: '.search-form',
  17. query: 'input[name="source-query"]'
  18. },
  19. fetch: App.Api.Nginx.Certificates.getAll,
  20. showData: function(response) {
  21. this.showChildView('list_region', new ListView({
  22. collection: new CertificateModel.Collection(response)
  23. }));
  24. },
  25. showError: function(err) {
  26. this.showChildView('list_region', new ErrorView({
  27. code: err.code,
  28. message: err.message,
  29. retry: function () {
  30. App.Controller.showNginxCertificates();
  31. }
  32. }));
  33. console.error(err);
  34. },
  35. showEmpty: function() {
  36. let manage = App.Cache.User.canManage('certificates');
  37. this.showChildView('list_region', new EmptyView({
  38. title: App.i18n('certificates', 'empty'),
  39. subtitle: App.i18n('all-hosts', 'empty-subtitle', {manage: manage}),
  40. link: manage ? App.i18n('certificates', 'add') : null,
  41. btn_color: 'pink',
  42. permission: 'certificates',
  43. action: function () {
  44. App.Controller.showNginxCertificateForm();
  45. }
  46. }));
  47. },
  48. regions: {
  49. list_region: '@ui.list_region'
  50. },
  51. events: {
  52. 'click @ui.add': function (e) {
  53. e.preventDefault();
  54. let model = new CertificateModel.Model({provider: $(e.currentTarget).data('cert')});
  55. App.Controller.showNginxCertificateForm(model);
  56. },
  57. 'click @ui.help': function (e) {
  58. e.preventDefault();
  59. App.Controller.showHelp(App.i18n('certificates', 'help-title'), App.i18n('certificates', 'help-content'));
  60. },
  61. 'submit @ui.search': function (e) {
  62. e.preventDefault();
  63. let query = this.ui.query.val();
  64. this.fetch(['owner'], query)
  65. .then(response => this.showData(response))
  66. .catch(err => {
  67. this.showError(err);
  68. });
  69. }
  70. },
  71. templateContext: {
  72. showAddButton: App.Cache.User.canManage('certificates')
  73. },
  74. onRender: function () {
  75. let view = this;
  76. view.fetch(['owner'])
  77. .then(response => {
  78. if (!view.isDestroyed()) {
  79. if (response && response.length) {
  80. view.showData(response);
  81. } else {
  82. view.showEmpty();
  83. }
  84. }
  85. })
  86. .catch(err => {
  87. view.showError(err);
  88. })
  89. .then(() => {
  90. view.ui.dimmer.removeClass('active');
  91. });
  92. }
  93. });