main.js 2.4 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. 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. Api.Reports.getHostStats()
  42. .then(response => {
  43. if (!view.isDestroyed()) {
  44. view.stats.hosts = response;
  45. view.render();
  46. }
  47. })
  48. .catch(err => {
  49. console.log(err);
  50. });
  51. },
  52. /**
  53. * @param {Object} [model]
  54. */
  55. preRender: function (model) {
  56. this.columns = 0;
  57. // calculate the available columns based on permissions for the objects
  58. // and store as a variable
  59. //let view = this;
  60. let perms = ['proxy_hosts', 'redirection_hosts', 'streams', 'dead_hosts'];
  61. perms.map(perm => {
  62. this.columns += Cache.User.isAdmin() || Cache.User.canView(perm) ? 1 : 0;
  63. });
  64. // Prevent double rendering on initial calls
  65. if (typeof model !== 'undefined') {
  66. this.render();
  67. }
  68. },
  69. initialize: function () {
  70. this.preRender();
  71. this.listenTo(Cache.User, 'change', this.preRender);
  72. }
  73. });