123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- /*
- SpacedeckRoutes
- This module contains functions dealing with Routing and View Switching.
- */
- var SpacedeckRoutes = {
- internal_route: function(path, on_success) {
- if(!this.router) {
- this.router = new RouteRecognizer();
- this.router.add([
- {
- path: "/spaces/:id",
- handler: function(params, on_success) {
- this.load_space(params.id, on_success);
- }.bind(this)
- }
- ]);
- this.router.add([
- {
- path: "/confirm/:token",
- handler: function(params) {
- if (!this.logged_in) {
- this.redirect_to("/login");
- } else {
- this.confirm_account(params.token);
- }
- }.bind(this)
- }
- ]);
- this.router.add([
- {
- path: "/password-confirm/:token",
- handler: function(params) {
- console.log(params.token);
- if (this.logged_in) {
- this.redirect_to("/spaces");
- } else {
- this.reset_token = params.token;
- this.active_view = "password-confirm";
- }
- }.bind(this)
- }
- ]);
- this.router.add([
- {
- path: "/password-reset",
- handler: function(params, test) {
- if (this.logged_in) {
- } else {
- this.active_view = "password-reset";
- }
- }.bind(this)
- }
- ]);
- this.router.add([
- {
- path: "/accept/:membership_id",
- handler: function(params, test) {
- if (this.logged_in) {
- var invitation_token = get_query_param("code");
- accept_invitation(params.membership_id, invitation_token , function(m) {
- window._spacedeck_location_change = true;
- location.href = "/spaces/"+m.space._id;
- }.bind(this), function(xhr) {
- smoke.alert("Error ("+xhr.status+")", function() {
- this.redirect_to("/spaces");
- }.bind(this));
- }.bind(this));
- } else {
- this.redirect_to("/login");
- }
- }.bind(this)
- }
- ]);
- this.router.add([
- {
- path: "/signup",
- handler: function(params) {
- var invitation_token = get_query_param("code");
- if (invitation_token) {
- this.invitation_token = invitation_token;
- }
-
- if (this.logged_in) {
- this.redirect_to("/spaces");
- } else {
- this.active_view = "signup";
- }
- }.bind(this)
- }
- ]);
- this.router.add([
- {
- path: "/login",
- handler: function(params) {
- if (this.logged_in) {
- if(this.invitation_token) {
- accept_invitation(this.accept_invitation, function(m) {
- window._spacedeck_location_change = true;
- location.href = "spaces/"+m.space_id;
- }.bind(this), function(xhr) { console.error(xhr); });
- } else {
- this.redirect_to("/spaces");
- }
- } else {
- this.active_view = "login";
- token = get_query_param("code");
- if (token) {
- this.login_with_token(token);
- }
- }
- }.bind(this)
- }
- ]);
- this.router.add([
- {
- path: "/logout",
- handler: function(params) {
- if (this.logged_in) {
- this.logout(function(m) {
- this.redirect_to("/login");
- }.bind(this), function(xhr) { console.error(xhr); });
- } else {
- this.redirect_to("/login");
- }
- }.bind(this)
- }
- ]);
- this.router.add([
- {
- path: "/spaces",
- handler: function(params) {
- if (!this.logged_in) {
- window._spacedeck_location_change = true;
- location.href = "/login";
- } else {
- if (this.logged_in && this.user.home_folder_id) {
- this.load_space(this.user.home_folder_id);
- } else {
- location.href = "/";
- }
- }
- }.bind(this)
- }
- ]);
- this.router.add([
- {
- path: "/account",
- handler: function(params) {
- if (!this.logged_in) {
- window._spacedeck_location_change = true;
- location.href = "/";
- } else {
- this.active_view = "account";
- }
- }.bind(this)
- }
- ]);
- this.router.add([
- {
- path: "/team",
- handler: function(params) {
- if (!this.logged_in) {
- window._spacedeck_location_change = true;
- location.href = "/";
- } else {
- this.active_view = "team";
- this.load_team();
- }
- }.bind(this)
- }
- ]);
- this.router.add([
- {
- path: "/folders/:id",
- handler: function(params) {
- this.load_space(params.id, null, function(xhr) {
- // on_error
- console.log("couldn't load folder: "+xhr.status);
- this.redirect_to("/spaces", function(){});
- }.bind(this));
- }.bind(this)
- }
- ]);
- this.router.add([
- {
- path: "/",
- handler: function(params) {
- location.href = "/";
- }.bind(this)
- }
- ]);
-
- this.router.add([
- {
- path: "/terms",
- handler: function(params) {
- location.href = "/terms";
- }.bind(this)
- }
- ]);
-
- this.router.add([
- {
- path: "/privacy",
- handler: function(params) {
- location.href = "/privacy";
- }.bind(this)
- }
- ]);
- }
- var foundRoute = this.router.recognize(path);
- if (foundRoute) {
- foundRoute[0].handler(foundRoute[0].params, on_success);
- } else {
- location.href = "/not_found";
- }
- },
- route: function() {
- window.onpopstate = function (event) {
- event.preventDefault();
- this.internal_route(location.pathname);
- }.bind(this);
- $("body").on("click", "a", function(event) {
- // #hash
- if (event.currentTarget.hash && event.currentTarget.hash.length>1) return;
- // external link?
- if (event.currentTarget.host != location.host) return;
- // modifier keys?
- if (event.metaKey || event.ctrlKey || event.shiftKey) return;
- // /t/ path
- if (event.currentTarget.pathname.match(/^\/t\//)) return;
- this.internal_route(event.currentTarget.pathname);
- history.pushState(null, null, event.currentTarget.pathname);
- event.preventDefault();
- }.bind(this));
- this.internal_route(location.pathname);
- },
-
- open_url: function(url) {
- window.open(url,'_blank');
- },
-
- redirect_to: function(path, on_success) {
- if (on_success) {
- this.internal_route(path, on_success);
- history.pushState(null, null, path);
- } else {
- window._spacedeck_location_change = true;
- location.href = path;
- }
- },
- link_to_parent_folder: function(space_id) {
- return "/folders/"+space_id;
- },
- link_to_space: function(space) {
- return "/"+space.space_type+"s/"+space._id;
- }
- }
|