main.js 5.4 KB

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