spacedeck_routes.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. SpacedeckRoutes
  3. This module contains functions dealing with Routing and View Switching.
  4. */
  5. var SpacedeckRoutes = {
  6. internal_route: function(path, on_success) {
  7. if(!this.router) {
  8. this.router = new RouteRecognizer();
  9. this.router.add([
  10. {
  11. path: "/spaces/:id",
  12. handler: function(params, on_success) {
  13. this.load_space(params.id, on_success);
  14. }.bind(this)
  15. }
  16. ]);
  17. this.router.add([
  18. {
  19. path: "/confirm/:token",
  20. handler: function(params) {
  21. if (!this.logged_in) {
  22. this.redirect_to("/login");
  23. } else {
  24. this.confirm_account(params.token);
  25. }
  26. }.bind(this)
  27. }
  28. ]);
  29. this.router.add([
  30. {
  31. path: "/password-confirm/:token",
  32. handler: function(params) {
  33. console.log(params.token);
  34. if (this.logged_in) {
  35. this.redirect_to("/spaces");
  36. } else {
  37. this.reset_token = params.token;
  38. this.active_view = "password-confirm";
  39. }
  40. }.bind(this)
  41. }
  42. ]);
  43. this.router.add([
  44. {
  45. path: "/password-reset",
  46. handler: function(params, test) {
  47. if (this.logged_in) {
  48. } else {
  49. this.active_view = "password-reset";
  50. }
  51. }.bind(this)
  52. }
  53. ]);
  54. this.router.add([
  55. {
  56. path: "/accept/:membership_id",
  57. handler: function(params, test) {
  58. if (this.logged_in) {
  59. var invitation_token = get_query_param("code");
  60. accept_invitation(params.membership_id, invitation_token , function(m) {
  61. window._spacedeck_location_change = true;
  62. location.href = "/spaces/"+m.space._id;
  63. }.bind(this), function(xhr) {
  64. smoke.alert("Error ("+xhr.status+")", function() {
  65. this.redirect_to("/spaces");
  66. }.bind(this));
  67. }.bind(this));
  68. } else {
  69. this.redirect_to("/login");
  70. }
  71. }.bind(this)
  72. }
  73. ]);
  74. this.router.add([
  75. {
  76. path: "/signup",
  77. handler: function(params) {
  78. var invitation_token = get_query_param("code");
  79. if (invitation_token) {
  80. this.invitation_token = invitation_token;
  81. }
  82. if (this.logged_in) {
  83. this.redirect_to("/spaces");
  84. } else {
  85. this.active_view = "signup";
  86. }
  87. }.bind(this)
  88. }
  89. ]);
  90. this.router.add([
  91. {
  92. path: "/login",
  93. handler: function(params) {
  94. if (this.logged_in) {
  95. if(this.invitation_token) {
  96. accept_invitation(this.accept_invitation, function(m) {
  97. window._spacedeck_location_change = true;
  98. location.href = "spaces/"+m.space_id;
  99. }.bind(this), function(xhr) { console.error(xhr); });
  100. } else {
  101. this.redirect_to("/spaces");
  102. }
  103. } else {
  104. this.active_view = "login";
  105. token = get_query_param("code");
  106. if (token) {
  107. this.login_with_token(token);
  108. }
  109. }
  110. }.bind(this)
  111. }
  112. ]);
  113. this.router.add([
  114. {
  115. path: "/logout",
  116. handler: function(params) {
  117. if (this.logged_in) {
  118. this.logout(function(m) {
  119. this.redirect_to("/login");
  120. }.bind(this), function(xhr) { console.error(xhr); });
  121. } else {
  122. this.redirect_to("/login");
  123. }
  124. }.bind(this)
  125. }
  126. ]);
  127. this.router.add([
  128. {
  129. path: "/spaces",
  130. handler: function(params) {
  131. if (!this.logged_in) {
  132. window._spacedeck_location_change = true;
  133. location.href = "/login";
  134. } else {
  135. if (this.logged_in && this.user.home_folder_id) {
  136. this.load_space(this.user.home_folder_id);
  137. } else {
  138. location.href = "/";
  139. }
  140. }
  141. }.bind(this)
  142. }
  143. ]);
  144. this.router.add([
  145. {
  146. path: "/account",
  147. handler: function(params) {
  148. if (!this.logged_in) {
  149. window._spacedeck_location_change = true;
  150. location.href = "/";
  151. } else {
  152. this.active_view = "account";
  153. }
  154. }.bind(this)
  155. }
  156. ]);
  157. this.router.add([
  158. {
  159. path: "/team",
  160. handler: function(params) {
  161. if (!this.logged_in) {
  162. window._spacedeck_location_change = true;
  163. location.href = "/";
  164. } else {
  165. this.active_view = "team";
  166. this.load_team();
  167. }
  168. }.bind(this)
  169. }
  170. ]);
  171. this.router.add([
  172. {
  173. path: "/folders/:id",
  174. handler: function(params) {
  175. this.load_space(params.id, null, function(xhr) {
  176. // on_error
  177. console.log("couldn't load folder: "+xhr.status);
  178. this.redirect_to("/spaces", function(){});
  179. }.bind(this));
  180. }.bind(this)
  181. }
  182. ]);
  183. this.router.add([
  184. {
  185. path: "/",
  186. handler: function(params) {
  187. location.href = "/";
  188. }.bind(this)
  189. }
  190. ]);
  191. this.router.add([
  192. {
  193. path: "/terms",
  194. handler: function(params) {
  195. location.href = "/terms";
  196. }.bind(this)
  197. }
  198. ]);
  199. this.router.add([
  200. {
  201. path: "/privacy",
  202. handler: function(params) {
  203. location.href = "/privacy";
  204. }.bind(this)
  205. }
  206. ]);
  207. }
  208. var foundRoute = this.router.recognize(path);
  209. if (foundRoute) {
  210. foundRoute[0].handler(foundRoute[0].params, on_success);
  211. } else {
  212. location.href = "/not_found";
  213. }
  214. },
  215. route: function() {
  216. window.onpopstate = function (event) {
  217. event.preventDefault();
  218. this.internal_route(location.pathname);
  219. }.bind(this);
  220. $("body").on("click", "a", function(event) {
  221. // #hash
  222. if (event.currentTarget.hash && event.currentTarget.hash.length>1) return;
  223. // external link?
  224. if (event.currentTarget.host != location.host) return;
  225. // modifier keys?
  226. if (event.metaKey || event.ctrlKey || event.shiftKey) return;
  227. // /t/ path
  228. if (event.currentTarget.pathname.match(/^\/t\//)) return;
  229. this.internal_route(event.currentTarget.pathname);
  230. history.pushState(null, null, event.currentTarget.pathname);
  231. event.preventDefault();
  232. }.bind(this));
  233. this.internal_route(location.pathname);
  234. },
  235. open_url: function(url) {
  236. window.open(url,'_blank');
  237. },
  238. redirect_to: function(path, on_success) {
  239. if (on_success) {
  240. this.internal_route(path, on_success);
  241. history.pushState(null, null, path);
  242. } else {
  243. window._spacedeck_location_change = true;
  244. location.href = path;
  245. }
  246. },
  247. link_to_parent_folder: function(space_id) {
  248. return "/folders/"+space_id;
  249. },
  250. link_to_space: function(space) {
  251. return "/"+space.space_type+"s/"+space._id;
  252. }
  253. }