application.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import Ember from 'ember';
  2. export default Ember.Route.extend({
  3. actions: {
  4. setTitle: function(title) {
  5. if (typeof title === 'string') {
  6. title = {title: title};
  7. }
  8. var complete_title = title.title;
  9. if (typeof title.page !== 'undefined') {
  10. complete_title += ' (' + interpolate(gettext('page %(page)s'), {page:title.page}, true) + ')';
  11. }
  12. if (typeof title.parent !== 'undefined') {
  13. complete_title += ' | ' + title.parent;
  14. }
  15. complete_title += ' | ' + this.get('settings.forum_name');
  16. document.title = complete_title;
  17. return false;
  18. },
  19. // Error handlers
  20. error: function(reason) {
  21. if (reason.status === 0) {
  22. this.send('setTitle', gettext('Connection lost'));
  23. return this.intermediateTransitionTo('error-0');
  24. }
  25. if (typeof reason.responseJSON !== 'undefined' && typeof reason.responseJSON.ban !== 'undefined') {
  26. this.send('setTitle', gettext('You are banned'));
  27. return this.intermediateTransitionTo('error-banned', reason.responseJSON.ban);
  28. }
  29. if (reason.status === 403) {
  30. this.send('setTitle', gettext('Page not available'));
  31. var final_error = {status: 403, message: null};
  32. if (typeof reason.responseJSON !== 'undefined' && typeof reason.responseJSON.detail !== 'undefined' && reason.responseJSON.detail !== 'Permission denied') {
  33. final_error.message = reason.responseJSON.detail;
  34. }
  35. return this.intermediateTransitionTo('error-403', final_error);
  36. }
  37. if (reason.status === 404) {
  38. this.send('setTitle', gettext('Page not found'));
  39. return this.intermediateTransitionTo('error-404');
  40. }
  41. this.send('setTitle', gettext('Error'));
  42. return true;
  43. },
  44. toastError: function(reason) {
  45. var errorMessage = gettext('Unknown error has occured.');
  46. if (reason.status === 0) {
  47. errorMessage = gettext('Lost connection with application.');
  48. }
  49. if (reason.status === 403) {
  50. if (typeof reason.responseJSON !== 'undefined' && typeof reason.responseJSON.detail !== 'undefined' && reason.responseJSON.detail !== 'Permission denied') {
  51. errorMessage = reason.responseJSON.detail;
  52. } else {
  53. errorMessage = gettext("You don't have permission to perform this action.");
  54. }
  55. }
  56. if (reason.status === 404) {
  57. errorMessage = gettext('Action link is invalid.');
  58. }
  59. this.get('toast').error(errorMessage);
  60. return false;
  61. },
  62. showBan: function(ban) {
  63. this.send('setTitle', gettext('You are banned'));
  64. this.intermediateTransitionTo('error-banned', ban);
  65. return false;
  66. },
  67. // Auth
  68. openLoginModal: function() {
  69. this.controllerFor("loginModal").send('open');
  70. return false;
  71. },
  72. logOut: function() {
  73. this.get('auth').logout();
  74. Ember.$('#hidden-logout-form').submit();
  75. return false;
  76. }
  77. }
  78. });