main.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. const 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. const 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. const perms = ['proxy_hosts', 'redirection_hosts', 'streams', 'dead_hosts'];
  62. perms.map(perm => {
  63. this.columns += Cache.User.isAdmin() || Cache.User.canView(perm) ? 1 : 0;
  64. });
  65. // Prevent double rendering on initial calls
  66. if (typeof model !== 'undefined') {
  67. this.render();
  68. }
  69. },
  70. initialize: function () {
  71. this.preRender();
  72. this.listenTo(Cache.User, 'change', this.preRender);
  73. }
  74. });