spacedeck_routes.js 8.16 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([
        {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
14
          path: ENV.prefix+"/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([
        {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
23
          path: ENV.prefix+"/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([
        {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
38
          path: ENV.prefix+"/confirm/:token",
mntmn's avatar
mntmn committed
39
40
          handler: function(params) {
            if (!this.logged_in) {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
41
              this.redirect_to(ENV.prefix+"/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([
        {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
51
          path: ENV.prefix+"/password-confirm/:token",
mntmn's avatar
mntmn committed
52
53
54
55
56
          handler: function(params) {

            console.log(params.token);

            if (this.logged_in) {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
57
              this.redirect_to(ENV.prefix+"/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([
        {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
69
          path: ENV.prefix+"/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([
        {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
81
          path: ENV.prefix+"/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;
Wolfgang Knopki's avatar
Wolfgang Knopki committed
87
                location.href = ENV.prefix+"/spaces/"+m.space._id;
mntmn's avatar
mntmn committed
88
89
              }.bind(this), function(xhr) {
                smoke.alert("Error ("+xhr.status+")", function() {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
90
                  this.redirect_to(ENV.prefix+"/spaces");
mntmn's avatar
mntmn committed
91
92
93
                }.bind(this));
              }.bind(this));
            } else {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
94
              this.redirect_to(ENV.prefix+"/login");
mntmn's avatar
mntmn committed
95
96
97
98
99
100
101
            }
          }.bind(this)
        }
      ]);

      this.router.add([
        {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
102
          path: ENV.prefix+"/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) {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
111
              this.redirect_to(ENV.prefix+"/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([
        {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
122
          path: ENV.prefix+"/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 {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
131
                this.redirect_to(ENV.prefix+"/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([
        {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
146
          path: ENV.prefix+"/logout",
mntmn's avatar
mntmn committed
147
148
149
          handler: function(params) {
            if (this.logged_in) {
              this.logout(function(m) {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
150
                this.redirect_to(ENV.prefix+"/login");
mntmn's avatar
mntmn committed
151
152
              }.bind(this), function(xhr) { console.error(xhr); });
            } else {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
153
              this.redirect_to(ENV.prefix+"/login");
mntmn's avatar
mntmn committed
154
155
156
157
158
159
160
            }
          }.bind(this)
        }
      ]);

      this.router.add([
        {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
161
          path: ENV.prefix+"/spaces",
mntmn's avatar
mntmn committed
162
163
164
          handler: function(params) {
            if (!this.logged_in) {
              window._spacedeck_location_change = true;
Wolfgang Knopki's avatar
Wolfgang Knopki committed
165
              location.href = ENV.prefix+"/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 {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
171
                location.href = ENV.prefix+"/";
mntmn's avatar
mntmn committed
172
173
174
175
176
177
178
179
180
              }

            }
          }.bind(this)
        }
      ]);

      this.router.add([
        {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
181
          path: ENV.prefix+"/account",
mntmn's avatar
mntmn committed
182
183
184
          handler: function(params) {
            if (!this.logged_in) {
              window._spacedeck_location_change = true;
Wolfgang Knopki's avatar
Wolfgang Knopki committed
185
              location.href = ENV.prefix+"/";
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([
        {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
196
          path: ENV.prefix+"/team",
mntmn's avatar
mntmn committed
197
198
199
          handler: function(params) {
            if (!this.logged_in) {
              window._spacedeck_location_change = true;
Wolfgang Knopki's avatar
Wolfgang Knopki committed
200
              location.href = ENV.prefix+"/";
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([
        {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
211
          path: ENV.prefix+"/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);
Wolfgang Knopki's avatar
Wolfgang Knopki committed
217
              this.redirect_to(ENV.prefix+"/spaces", function(){});
mntmn's avatar
mntmn committed
218
219
220
221
222
223
224
225
            }.bind(this));
          }.bind(this)
        }

      ]);

      this.router.add([
        {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
226
          path: ENV.prefix+"/",
mntmn's avatar
mntmn committed
227
          handler: function(params) {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
228
            location.href = ENV.prefix+"/";
mntmn's avatar
mntmn committed
229
230
231
232
233
234
          }.bind(this)
        }
      ]);
      
      this.router.add([
        {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
235
          path: ENV.prefix+"/terms",
mntmn's avatar
mntmn committed
236
          handler: function(params) {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
237
            location.href = ENV.prefix+"/terms";
mntmn's avatar
mntmn committed
238
239
240
241
242
243
          }.bind(this)
        }
      ]);
      
      this.router.add([
        {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
244
          path: ENV.prefix+"/privacy",
mntmn's avatar
mntmn committed
245
          handler: function(params) {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
246
            location.href = ENV.prefix+"/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 {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
256
      location.href = ENV.prefix+"/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) {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
303
    return ENV.prefix+"/folders/"+space_id;
mntmn's avatar
mntmn committed
304
305
306
  },

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