main.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const Mn = require('backbone.marionette');
  2. const Cache = require('../cache');
  3. const Controller = require('../controller');
  4. const Api = require('../api');
  5. const Helpers = require('../../lib/helpers');
  6. const template = require('./main.ejs');
  7. module.exports = Mn.View.extend({
  8. template: template,
  9. id: 'dashboard',
  10. columns: 0,
  11. stats: {},
  12. ui: {
  13. links: 'a'
  14. },
  15. events: {
  16. 'click @ui.links': function (e) {
  17. e.preventDefault();
  18. Controller.navigate($(e.currentTarget).attr('href'), true);
  19. }
  20. },
  21. templateContext: function () {
  22. let view = this;
  23. return {
  24. getUserName: function () {
  25. return Cache.User.get('nickname') || Cache.User.get('name');
  26. },
  27. getHostStat: function (type) {
  28. if (view.stats && typeof view.stats.hosts !== 'undefined' && typeof view.stats.hosts[type] !== 'undefined') {
  29. return Helpers.niceNumber(view.stats.hosts[type]);
  30. }
  31. return '-';
  32. },
  33. canShow: function (perm) {
  34. return Cache.User.isAdmin() || Cache.User.canView(perm);
  35. },
  36. columns: view.columns
  37. };
  38. },
  39. onRender: function () {
  40. let view = this;
  41. if (typeof view.stats.hosts === 'undefined') {
  42. Api.Reports.getHostStats()
  43. .then(response => {
  44. if (!view.isDestroyed()) {
  45. view.stats.hosts = response;
  46. view.render();
  47. }
  48. })
  49. .catch(err => {
  50. console.log(err);
  51. });
  52. }
  53. },
  54. /**
  55. * @param {Object} [model]
  56. */
  57. preRender: function (model) {
  58. this.columns = 0;
  59. // calculate the available columns based on permissions for the objects
  60. // and store as a variable
  61. //let view = this;
  62. let perms = ['proxy_hosts', 'redirection_hosts', 'streams', 'dead_hosts'];
  63. perms.map(perm => {
  64. this.columns += Cache.User.isAdmin() || Cache.User.canView(perm) ? 1 : 0;
  65. });
  66. // Prevent double rendering on initial calls
  67. if (typeof model !== 'undefined') {
  68. this.render();
  69. }
  70. },
  71. initialize: function () {
  72. this.preRender();
  73. this.listenTo(Cache.User, 'change', this.preRender);
  74. }
  75. });