main.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. const _ = require('underscore');
  2. const Backbone = require('backbone');
  3. const Mn = require('../lib/marionette');
  4. const Cache = require('./cache');
  5. const Controller = require('./controller');
  6. const Router = require('./router');
  7. const Api = require('./api');
  8. const Tokens = require('./tokens');
  9. const UI = require('./ui/main');
  10. const i18n = require('./i18n');
  11. const App = Mn.Application.extend({
  12. Cache: Cache,
  13. Api: Api,
  14. UI: null,
  15. i18n: i18n,
  16. Controller: Controller,
  17. region: {
  18. el: '#app',
  19. replaceElement: true
  20. },
  21. onStart: function (app, options) {
  22. console.log(i18n('main', 'welcome'));
  23. // Check if token is coming through
  24. if (this.getParam('token')) {
  25. Tokens.addToken(this.getParam('token'));
  26. }
  27. // Check if we are still logged in by refreshing the token
  28. Api.status()
  29. .then(result => {
  30. Cache.version = [result.version.major, result.version.minor, result.version.revision].join('.');
  31. })
  32. .then(Api.Tokens.refresh)
  33. .then(this.bootstrap)
  34. .then(() => {
  35. console.info(i18n('main', 'logged-in', Cache.User.attributes));
  36. this.bootstrapTimer();
  37. this.refreshTokenTimer();
  38. this.UI = new UI();
  39. this.UI.on('render', () => {
  40. new Router(options);
  41. Backbone.history.start({pushState: true});
  42. // Ask the admin use to change their details
  43. if (Cache.User.get('email') === '[email protected]') {
  44. Controller.showUserForm(Cache.User);
  45. }
  46. });
  47. this.getRegion().show(this.UI);
  48. })
  49. .catch(err => {
  50. console.warn('Not logged in:', err.message);
  51. Controller.showLogin();
  52. });
  53. },
  54. History: {
  55. replace: function (data) {
  56. window.history.replaceState(_.extend(window.history.state || {}, data), document.title);
  57. },
  58. get: function (attr) {
  59. return window.history.state ? window.history.state[attr] : undefined;
  60. }
  61. },
  62. getParam: function (name) {
  63. name = name.replace(/[\[\]]/g, '\\$&');
  64. let url = window.location.href;
  65. let regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
  66. let results = regex.exec(url);
  67. if (!results) {
  68. return null;
  69. }
  70. if (!results[2]) {
  71. return '';
  72. }
  73. return decodeURIComponent(results[2].replace(/\+/g, ' '));
  74. },
  75. /**
  76. * Get user and other base info to start prime the cache and the application
  77. *
  78. * @returns {Promise}
  79. */
  80. bootstrap: function () {
  81. return Api.Users.getById('me', ['permissions'])
  82. .then(response => {
  83. Cache.User.set(response);
  84. Tokens.setCurrentName(response.nickname || response.name);
  85. });
  86. },
  87. /**
  88. * Bootstraps the user from time to time
  89. */
  90. bootstrapTimer: function () {
  91. setTimeout(() => {
  92. Api.status()
  93. .then(result => {
  94. let version = [result.version.major, result.version.minor, result.version.revision].join('.');
  95. if (version !== Cache.version) {
  96. document.location.reload();
  97. }
  98. })
  99. .then(this.bootstrap)
  100. .then(() => {
  101. this.bootstrapTimer();
  102. })
  103. .catch(err => {
  104. if (err.message !== 'timeout' && err.code && err.code !== 400) {
  105. console.log(err);
  106. console.error(err.message);
  107. console.info('Not logged in?');
  108. Controller.showLogin();
  109. } else {
  110. this.bootstrapTimer();
  111. }
  112. });
  113. }, 30 * 1000); // 30 seconds
  114. },
  115. refreshTokenTimer: function () {
  116. setTimeout(() => {
  117. return Api.Tokens.refresh()
  118. .then(this.bootstrap)
  119. .then(() => {
  120. this.refreshTokenTimer();
  121. })
  122. .catch(err => {
  123. if (err.message !== 'timeout' && err.code && err.code !== 400) {
  124. console.log(err);
  125. console.error(err.message);
  126. console.info('Not logged in?');
  127. Controller.showLogin();
  128. } else {
  129. this.refreshTokenTimer();
  130. }
  131. });
  132. }, 10 * 60 * 1000);
  133. }
  134. });
  135. const app = new App();
  136. module.exports = app;