41b9f0ffa21c291244093367a47588f326dc6423
[oweals/finalsclub.git] / public / javascripts / main2.js
1
2 /*
3
4 This is the core logic for the main page.
5 It implements most page transitions by showing and hiding DIV elements
6 in the page with javascript+jquery
7
8 */
9
10
11 /* Convert a JSON string to an object, or null if unparseable */
12 function j2o(json) { try { return JSON.parse(json); } catch(e) { return null; } }
13
14 /* Convert an object to a JSON string (just easier to type than "JSON.stringify" */
15 function o2j(obj) { return JSON.stringify(obj); }
16
17
18
19 function showHome(cb) {
20         cb("home");
21 }
22
23
24
25 // go to the page that lists the schools
26 function showSchools(response, cb) {
27
28   var path = window.location.pathname;
29
30         ProtoDiv.reset("PROTO_school");
31
32
33       var schools = []
34       if(typeof response == 'object') {
35         schools = response.schools
36       }
37
38       ProtoDiv.replicate("PROTO_school", schools);
39
40       cb("schools");
41
42 }
43
44
45
46 // go to the page that lists the courses for a specific school
47 function showCourses(response, cb) {
48
49   var path = window.location.pathname;
50
51
52         ProtoDiv.reset("PROTO_course");
53
54                 var courses = []
55                 if(typeof response == 'object') {
56                         var school = response.school
57                         $("#school_name").html(school.name);
58                         courses = school.courses
59                 }
60
61                 ProtoDiv.replicate("PROTO_course", courses);
62
63                 cb("courses")
64 }
65
66
67
68
69 // go to the page that lists the lectures for a specific course
70 function showLectures(response, cb) {
71
72   var path = window.location.pathname;
73   
74
75         ProtoDiv.reset("PROTO_lecture");
76         
77                 ProtoDiv.reset("PROTO_lectures_head")
78                 ProtoDiv.reset("PROTO_lectures_instructor")
79                 ProtoDiv.reset("PROTO_lecture")
80
81                 if(typeof response == 'object') {
82
83                         var course = response.course
84                         if(course)
85                                 ProtoDiv.replicate("PROTO_lectures_head", [course])
86
87                         var instructor = response.instructor
88                         if(instructor)
89                                 ProtoDiv.replicate("PROTO_lectures_instructor", [instructor])
90
91                         var lectures = response.lectures
92                         if(lectures)
93                                 ProtoDiv.replicate("PROTO_lecture", lectures);
94
95                 }
96
97                 cb("lectures")
98 }
99
100
101
102 // go to the page that lists the note taking sessions for a specific lecture
103 function showNotes(response, cb) {
104
105   var path = window.location.pathname;
106
107
108         ProtoDiv.reset("PROTO_note");
109         
110                 if(typeof response == 'object') {
111
112                         var course = response.course
113                         //if(course)
114                         //      ProtoDiv.replicate("PROTO_lectures_head", [course])
115
116                         var instructor = response.instructor
117                         //if(instructor)
118                         //      ProtoDiv.replicate("PROTO_lectures_instructor", [instructor])
119
120                         var lecture = response.lecture
121                         //if(lecture)
122                         //      ProtoDiv.replicate("PROTO_lecture", lectures);
123
124                         var notes = response.notes
125                         if(notes)
126                                 ProtoDiv.replicate("PROTO_note", notes);
127
128                 }
129
130                 cb("notes")
131 }
132
133
134 // go to the page that lists the archived subject names
135 function showArchiveSubjects(response, cb) {
136
137   var path = window.location.pathname;
138
139         ProtoDiv.reset("PROTO_archive_subject")
140
141                 var subjects = response.subjects
142
143                 ProtoDiv.replicate("PROTO_archive_subject", subjects)
144
145         cb("archive_subjects")
146 }
147
148
149
150 function showArchiveCourses(response, cb) {
151
152   var path = window.location.pathname;
153
154
155         ProtoDiv.reset("PROTO_archive_course")
156
157                 var courses = response.courses
158
159                 ProtoDiv.replicate("PROTO_archive_course", courses)
160
161         cb("archive_courses")
162 }
163
164
165
166 function showArchiveNotes(response, cb) {
167
168   var path = window.location.pathname;
169
170
171         ProtoDiv.reset("PROTO_archive_note")
172
173                 var notes = response.notes
174                 $.each(notes, function(i, note) {
175                         if(!note.topic)
176                                 note.topic = note._id//note.text.substr(0, 15)+" ..."
177                 })
178
179                 ProtoDiv.replicate("PROTO_archive_note", notes)
180
181         cb("archive_notes")
182 }
183
184
185
186 function showArchiveNote(response, cb) {
187
188   var path = window.location.pathname;
189
190
191         ProtoDiv.reset("PROTO_archive_note_display")
192
193                 var note = response.note
194 note = { text: "Hi <i>Mom!</i>", topic: "21st Century Greetings" }
195                 if(!note.topic)
196                         note.topic = note.text.substr(0, 15)+" ..."
197
198                 ProtoDiv.replicate("PROTO_archive_note_display", note)
199
200         cb("archive_note_display")
201 }
202
203
204
205 // go to the account registration page
206 function showRegister(response, cb) {
207         // xxx clear fields?
208         // xxx change FORM to use AJAX
209         cb("register");
210 }
211
212
213 function showLogin(response, cb) {
214         cb("login");
215 }
216
217
218
219
220 // go to the press articles page
221 function showPress(response, cb) {
222         cb("press");
223 }
224
225
226 // go to the "code of conduct" page
227 function showConduct(response, cb) {
228         cb("conduct");
229 }
230
231
232
233
234 var pageVectors = [
235         { regex: /^\/(index.html)?$/, func: showHome },
236         { regex: /^\/schools/, func: showSchools },
237         { regex: /^\/school\/([a-f0-9]{24})/, func: showCourses },
238         { regex: /^\/course\/([a-f0-9]{24})/, func: showLectures },
239         { regex: /^\/lecture\/([a-f0-9]{24})/, func: showNotes },
240         { regex: /^\/archive\/?$/, func: showArchiveSubjects },
241         { regex: /^\/archive\/subject\/([0-9]+)/, func: showArchiveCourses },
242         { regex: /^\/archive\/course\/([0-9]+)/, func: showArchiveNotes },
243         { regex: /^\/archive\/note\/([0-9]+)/, func: showArchiveNote },
244         { regex: /^\/login/, func: showLogin },
245         { regex: /^\/register/, func: showRegister },
246         { regex: /^\/press/, func: showPress },
247         { regex: /^\/conduct/, func: showConduct },
248 ];
249
250 var testVectors = {
251   schools: showSchools,
252   school: showCourses,
253   course: showLectures,
254   lecture: showNotes,
255   archive: showArchiveSubjects,
256   archivesubject: showArchiveCourses,
257   archivecourse: showArchiveNotes,
258   archivenote: showArchiveNote,
259   login: showLogin,
260   press: showPress,
261   conduct: showConduct
262 }
263
264 /* Do and show the appropriate thing, based on the pages current URL */
265 function showPage(y) {
266
267         var path = document.location.pathname
268
269   var mainSlug = path.match(/((?:[a-z][a-z]+))/) ? path.match(/((?:[a-z][a-z]+))/)[1].toLowerCase() : '';
270   if (mainSlug === 'archive') {
271     var archiveSlugs = path.match(/((?:[a-z][a-z]+))\/((?:[a-z][a-z0-9_]*))/);
272     if (archiveSlugs) {
273       mainSlug = mainSlug + archiveSlugs[2];
274     }
275   }
276
277         $(".page").hide(); //(100);             // hide all pseudo pages
278
279   if (testVectors[mainSlug]) {
280     return $.get(path, { cache: false }, function(response) {
281       if (response.status === 'error') {
282         console.log(response.message)
283         $("#pg_notfound").fadeIn(100);
284         window.scroll(0, 0)
285         return;
286       }
287       testVectors[mainSlug](response, function(pageId) {
288         $("#pg_"+pageId).fadeIn(100);
289         window.scroll(0, y)
290       })
291     });
292   } else if (path === '/') {
293     return showHome(function(pageId) {
294       $("#pg_"+pageId).fadeIn(100);
295       window.scroll(0, y)
296     })
297   }
298
299   $("#pg_notfound").fadeIn(100);
300   window.scroll(0, 0)
301   /*
302         for(var i = 0; i < pageVectors.length; i++) {
303                 var vector = pageVectors[i]
304                 var matches = path.match(vector.regex)
305                 if(matches) {
306                         vector.func(function(pageId) {
307
308                                 $("#pg_"+pageId).fadeIn(100);
309
310                                 window.scroll(0, y)
311
312                         })
313                         break
314                 }
315         }
316   
317
318         if(i == pageVectors.length) {
319         }
320         // scroll to top of page (as if we'd done a real page fetch)
321         /*$('html, body').animate({
322                 scrollTop: $("#topofcontent").offset().top
323         }, 100);*/
324
325 }
326
327
328
329
330 /* Simulates a page load.
331         'path' is something like "/schools", etc.
332         A page fetch doesn't really happen.
333         Based on what path looks like, an appropriate DIV is shown, and action taken
334 */
335 var topQueue = [0]
336 function goPage(path) {
337         var y = 0 + window.pageYOffset
338         topQueue.push(y)
339         history.pushState({}, path, path);
340         showPage(0);
341 }
342
343
344 /* Simulates a "back" browser navigation.  */
345 function goBack(event) {
346         var y = topQueue.pop()
347         showPage( y );
348 }
349
350
351         window.onpopstate = goBack
352
353   $('a[href^=/]').live('click', function(e) {
354     var path = e.target.pathname || '/';
355     var checkNote = path.match(/((?:[a-z][a-z]+))/);
356     if (checkNote && checkNote[1] == 'note') {
357       return true;
358     } else {
359       goPage(path)
360       return false;
361     }
362   })
363 $(document).ready(function() {
364
365         // This code executes after the page has been fully loaded
366
367
368         // xxx older FF browsers don't fire a page load/reload - deal with it somehow.
369         // showPage( 0 );               // needed for some older browsers, redundant for chrome
370
371 })
372
373
374
375
376