application.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. },
  18. // Error handlers
  19. error: function(reason) {
  20. if (reason.status === 0) {
  21. this.send('setTitle', gettext('Connection lost'));
  22. return this.intermediateTransitionTo('error-0');
  23. }
  24. if (typeof reason.responseJSON !== 'undefined' && typeof reason.responseJSON.ban !== 'undefined') {
  25. this.send('setTitle', gettext('You are banned'));
  26. return this.intermediateTransitionTo('error-banned', reason.responseJSON.ban);
  27. }
  28. if (reason.status === 403) {
  29. this.send('setTitle', gettext('Page not available'));
  30. var final_error = {status: 403, message: null};
  31. if (typeof reason.responseJSON !== 'undefined' && typeof reason.responseJSON.detail !== 'undefined' && reason.responseJSON.detail !== 'Permission denied') {
  32. final_error.message = reason.responseJSON.detail;
  33. }
  34. return this.intermediateTransitionTo('error-403', final_error);
  35. }
  36. if (reason.status === 404) {
  37. this.send('setTitle', gettext('Page not found'));
  38. return this.intermediateTransitionTo('error-404');
  39. }
  40. this.send('setTitle', gettext('Error'));
  41. return true;
  42. },
  43. toastError: function(reason) {
  44. var errorMessage = gettext('Unknown error has occured.');
  45. if (reason.status === 0) {
  46. errorMessage = gettext('Lost connection with application.');
  47. }
  48. if (reason.status === 403) {
  49. if (typeof reason.responseJSON !== 'undefined' && typeof reason.responseJSON.detail !== 'undefined' && reason.responseJSON.detail !== 'Permission denied') {
  50. errorMessage = reason.responseJSON.detail;
  51. } else {
  52. errorMessage = gettext("You don't have permission to perform this action.");
  53. }
  54. }
  55. if (reason.status === 404) {
  56. errorMessage = gettext('Action link is invalid.');
  57. }
  58. this.get('toast').error(errorMessage);
  59. },
  60. showBan: function(ban) {
  61. this.send('setTitle', gettext('You are banned'));
  62. this.intermediateTransitionTo('error-banned', ban);
  63. },
  64. // Auth
  65. openLoginModal: function() {
  66. this.controllerFor("loginModal").send('open');
  67. },
  68. logOut: function() {
  69. this.get('auth').logout();
  70. Ember.$('#hidden-logout-form').submit();
  71. }
  72. }
  73. });