spacedeck_routes.js 8.12 KB
Newer Older
mntmn's avatar
mntmn committed
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
  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([
        {
14
          path: "/spacedeck/spaces/:id",
mntmn's avatar
mntmn committed
15
16
17
18
19
          handler: function(params, on_success) {
            this.load_space(params.id, on_success);
          }.bind(this)
        }
      ]);
20
21
22
      
      this.router.add([
        {
23
          path: "/spacedeck/s/:hash",
24
25
26
27
28
29
30
31
32
33
34
          handler: function(params, on_success) {
            var parts = params.hash.split("-");
            if (path.length > 0) {
              this.load_space(parts.slice(1).join("-"), on_success, null, parts[0]);
            } else {
              // FIXME error handling
              on_success();
            }
          }.bind(this)
        }
      ]);
mntmn's avatar
mntmn committed
35
36
37

      this.router.add([
        {
38
          path: "/spacedeck/confirm/:token",
mntmn's avatar
mntmn committed
39
40
          handler: function(params) {
            if (!this.logged_in) {
41
              this.redirect_to("/spacedeck/login");
mntmn's avatar
mntmn committed
42
43
44
45
46
47
48
49
50
            } else {
              this.confirm_account(params.token);
            }
          }.bind(this)
        }
      ]);

      this.router.add([
        {
51
          path: "/spacedeck/password-confirm/:token",
mntmn's avatar
mntmn committed
52
53
54
55
56
          handler: function(params) {

            console.log(params.token);

            if (this.logged_in) {
57
              this.redirect_to("/spacedeck/spaces");
mntmn's avatar
mntmn committed
58
59
60
61
62
63
64
65
66
67
68
            } else {
              this.reset_token = params.token;
              this.active_view = "password-confirm";
            }

          }.bind(this)
        }
      ]);

      this.router.add([
        {
69
          path: "/spacedeck/password-reset",
mntmn's avatar
mntmn committed
70
71
72
73
74
75
76
77
78
79
80
          handler: function(params, test) {
            if (this.logged_in) {
            } else {
              this.active_view = "password-reset";
            }
          }.bind(this)
        }
      ]);

      this.router.add([
        {
81
          path: "/spacedeck/accept/:membership_id",
mntmn's avatar
mntmn committed
82
83
84
85
86
          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;
87
                location.href = "/spacedeck/spaces/"+m.space._id;
mntmn's avatar
mntmn committed
88
89
              }.bind(this), function(xhr) {
                smoke.alert("Error ("+xhr.status+")", function() {
90
                  this.redirect_to("/spacedeck/spaces");
mntmn's avatar
mntmn committed
91
92
93
                }.bind(this));
              }.bind(this));
            } else {
94
              this.redirect_to("/spacedeck/login");
mntmn's avatar
mntmn committed
95
96
97
98
99
100
101
            }
          }.bind(this)
        }
      ]);

      this.router.add([
        {
102
          path: "/spacedeck/signup",
mntmn's avatar
mntmn committed
103
104
105
106
107
108
109
110
          handler: function(params) {
            var invitation_token = get_query_param("code");

            if (invitation_token) {
              this.invitation_token = invitation_token;
            }
            
            if (this.logged_in) {
111
              this.redirect_to("/spacedeck/spaces");
mntmn's avatar
mntmn committed
112
113
114
115
116
117
118
119
120
121
            } else {
              this.active_view = "signup";
            }

          }.bind(this)
        }
      ]);

      this.router.add([
        {
122
          path: "/spacedeck/login",
mntmn's avatar
mntmn committed
123
124
125
126
127
128
129
130
          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 {
131
                this.redirect_to("/spacedeck/spaces");
mntmn's avatar
mntmn committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
              }
            } else {
              this.active_view = "login";
              token = get_query_param("code");
              if (token) {
                this.login_with_token(token);
              }
            }
          }.bind(this)
        }
      ]);

      this.router.add([
        {
146
          path: "/spacedeck/logout",
mntmn's avatar
mntmn committed
147
148
149
          handler: function(params) {
            if (this.logged_in) {
              this.logout(function(m) {
150
                this.redirect_to("/spacedeck/login");
mntmn's avatar
mntmn committed
151
152
              }.bind(this), function(xhr) { console.error(xhr); });
            } else {
153
              this.redirect_to("/spacedeck/login");
mntmn's avatar
mntmn committed
154
155
156
157
158
159
160
            }
          }.bind(this)
        }
      ]);

      this.router.add([
        {
161
          path: "/spacedeck/spaces",
mntmn's avatar
mntmn committed
162
163
164
          handler: function(params) {
            if (!this.logged_in) {
              window._spacedeck_location_change = true;
165
              location.href = "/spacedeck/login";
mntmn's avatar
mntmn committed
166
167
168
169
170
            } else {

              if (this.logged_in && this.user.home_folder_id) {
                this.load_space(this.user.home_folder_id);
              } else {
171
                location.href = "/spacedeck/";
mntmn's avatar
mntmn committed
172
173
174
175
176
177
178
179
180
              }

            }
          }.bind(this)
        }
      ]);

      this.router.add([
        {
181
          path: "/spacedeck/account",
mntmn's avatar
mntmn committed
182
183
184
          handler: function(params) {
            if (!this.logged_in) {
              window._spacedeck_location_change = true;
185
              location.href = "/spacedeck/";
mntmn's avatar
mntmn committed
186
187
188
189
190
191
192
193
194
195
            } else {
              this.active_view = "account";
            }
          }.bind(this)
        }
      ]);


      this.router.add([
        {
196
          path: "/spacedeck/team",
mntmn's avatar
mntmn committed
197
198
199
          handler: function(params) {
            if (!this.logged_in) {
              window._spacedeck_location_change = true;
200
              location.href = "/spacedeck/";
mntmn's avatar
mntmn committed
201
202
203
204
205
206
207
208
209
210
            } else {
              this.active_view = "team";
              this.load_team();
            }
          }.bind(this)
        }
      ]);

      this.router.add([
        {
211
          path: "/spacedeck/folders/:id",
mntmn's avatar
mntmn committed
212
213
214
215
216
          handler: function(params) {
            this.load_space(params.id, null, function(xhr) {
              // on_error

              console.log("couldn't load folder: "+xhr.status);
217
              this.redirect_to("/spacedeck/spaces", function(){});
mntmn's avatar
mntmn committed
218
219
220
221
222
223
224
225
            }.bind(this));
          }.bind(this)
        }

      ]);

      this.router.add([
        {
226
          path: "/spacedeck/",
mntmn's avatar
mntmn committed
227
          handler: function(params) {
228
            location.href = "/spacedeck/";
mntmn's avatar
mntmn committed
229
230
231
232
233
234
          }.bind(this)
        }
      ]);
      
      this.router.add([
        {
235
          path: "/spacedeck/terms",
mntmn's avatar
mntmn committed
236
          handler: function(params) {
237
            location.href = "/spacedeck/terms";
mntmn's avatar
mntmn committed
238
239
240
241
242
243
          }.bind(this)
        }
      ]);
      
      this.router.add([
        {
244
          path: "/spacedeck/privacy",
mntmn's avatar
mntmn committed
245
          handler: function(params) {
246
            location.href = "/spacedeck/privacy";
mntmn's avatar
mntmn committed
247
248
249
250
251
252
253
254
255
          }.bind(this)
        }
      ]);
    }

    var foundRoute = this.router.recognize(path);
    if (foundRoute) {
      foundRoute[0].handler(foundRoute[0].params, on_success);
    } else {
256
      location.href = "/spacedeck/not_found";
mntmn's avatar
mntmn committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
    }
  },

  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) {
303
    return "/spacedeck/folders/"+space_id;
mntmn's avatar
mntmn committed
304
305
306
  },

  link_to_space: function(space) {
307
    return "/spacedeck/"+space.space_type+"s/"+space._id;
mntmn's avatar
mntmn committed
308
309
  }
}