main.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const Mn = require('backbone.marionette');
  2. const App = require('../../main');
  3. const StreamModel = require('../../../models/stream');
  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-stream',
  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.Streams.getAll,
  20. showData: function(response) {
  21. this.showChildView('list_region', new ListView({
  22. collection: new StreamModel.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.showNginxStream();
  31. }
  32. }));
  33. console.error(err);
  34. },
  35. showEmpty: function() {
  36. let manage = App.Cache.User.canManage('streams');
  37. this.showChildView('list_region', new EmptyView({
  38. title: App.i18n('streams', 'empty'),
  39. subtitle: App.i18n('all-hosts', 'empty-subtitle', {manage: manage}),
  40. link: manage ? App.i18n('streams', 'add') : null,
  41. btn_color: 'blue',
  42. permission: 'streams',
  43. action: function () {
  44. App.Controller.showNginxStreamForm();
  45. }
  46. }));
  47. },
  48. regions: {
  49. list_region: '@ui.list_region'
  50. },
  51. events: {
  52. 'click @ui.add': function (e) {
  53. e.preventDefault();
  54. App.Controller.showNginxStreamForm();
  55. },
  56. 'click @ui.help': function (e) {
  57. e.preventDefault();
  58. App.Controller.showHelp(App.i18n('streams', 'help-title'), App.i18n('streams', 'help-content'));
  59. },
  60. 'submit @ui.search': function (e) {
  61. e.preventDefault();
  62. let query = this.ui.query.val();
  63. this.fetch(['owner'], query)
  64. .then(response => this.showData(response))
  65. .catch(err => {
  66. this.showError(err);
  67. });
  68. }
  69. },
  70. templateContext: {
  71. showAddButton: App.Cache.User.canManage('streams')
  72. },
  73. onRender: function () {
  74. let view = this;
  75. view.fetch(['owner', 'certificate'])
  76. .then(response => {
  77. if (!view.isDestroyed()) {
  78. if (response && response.length) {
  79. view.showData(response);
  80. } else {
  81. view.showEmpty();
  82. }
  83. }
  84. })
  85. .catch(err => {
  86. view.showError(err);
  87. })
  88. .then(() => {
  89. view.ui.dimmer.removeClass('active');
  90. });
  91. }
  92. });