main.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. const Mn = require('backbone.marionette');
  3. const StreamModel = require('../../../models/stream');
  4. const Api = require('../../api');
  5. const Cache = require('../../cache');
  6. const Controller = require('../../controller');
  7. const ListView = require('./list/main');
  8. const ErrorView = require('../../error/main');
  9. const template = require('./main.ejs');
  10. const EmptyView = require('../../empty/main');
  11. module.exports = Mn.View.extend({
  12. id: 'nginx-streams',
  13. template: template,
  14. ui: {
  15. list_region: '.list-region',
  16. add: '.add-item',
  17. dimmer: '.dimmer'
  18. },
  19. regions: {
  20. list_region: '@ui.list_region'
  21. },
  22. events: {
  23. 'click @ui.add': function (e) {
  24. e.preventDefault();
  25. Controller.showNginxStreamForm();
  26. }
  27. },
  28. templateContext: {
  29. showAddButton: Cache.User.canManage('streams')
  30. },
  31. onRender: function () {
  32. let view = this;
  33. Api.Nginx.RedirectionHosts.getAll()
  34. .then(response => {
  35. if (!view.isDestroyed()) {
  36. if (response && response.length) {
  37. view.showChildView('list_region', new ListView({
  38. collection: new StreamModel.Collection(response)
  39. }));
  40. } else {
  41. let manage = Cache.User.canManage('streams');
  42. view.showChildView('list_region', new EmptyView({
  43. title: 'There are no Streams',
  44. subtitle: manage ? 'Why don\'t you create one?' : 'And you don\'t have permission to create one.',
  45. link: manage ? 'Add Stream' : null,
  46. btn_color: 'blue',
  47. action: function () {
  48. Controller.showNginxStreamForm();
  49. }
  50. }));
  51. }
  52. }
  53. })
  54. .catch(err => {
  55. view.showChildView('list_region', new ErrorView({
  56. code: err.code,
  57. message: err.message,
  58. retry: function () {
  59. Controller.showNginxStream();
  60. }
  61. }));
  62. console.error(err);
  63. })
  64. .then(() => {
  65. view.ui.dimmer.removeClass('active');
  66. });
  67. }
  68. });