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
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; } }
14 /* Convert an object to a JSON string (just easier to type than "JSON.stringify" */
15 function o2j(obj) { return JSON.stringify(obj); }
19 function showHome(cb) {
25 // go to the page that lists the schools
26 function showSchools(response, cb) {
28 var path = window.location.pathname;
30 ProtoDiv.reset("PROTO_school");
34 if(typeof response == 'object') {
35 schools = response.schools
38 ProtoDiv.replicate("PROTO_school", schools);
46 // go to the page that lists the courses for a specific school
47 function showCourses(response, cb) {
49 var path = window.location.pathname;
52 ProtoDiv.reset("PROTO_course");
55 if(typeof response == 'object') {
56 var school = response.school
57 $("#school_name").html(school.name);
58 courses = school.courses
61 ProtoDiv.replicate("PROTO_course", courses);
69 // go to the page that lists the lectures for a specific course
70 function showLectures(response, cb) {
72 var path = window.location.pathname;
75 ProtoDiv.reset("PROTO_lecture");
77 ProtoDiv.reset("PROTO_lectures_head")
78 ProtoDiv.reset("PROTO_lectures_instructor")
79 ProtoDiv.reset("PROTO_lecture")
81 if(typeof response == 'object') {
83 var course = response.course
85 ProtoDiv.replicate("PROTO_lectures_head", [course])
87 var instructor = response.instructor
89 ProtoDiv.replicate("PROTO_lectures_instructor", [instructor])
91 var lectures = response.lectures
93 ProtoDiv.replicate("PROTO_lecture", lectures);
102 // go to the page that lists the note taking sessions for a specific lecture
103 function showNotes(response, cb) {
105 var path = window.location.pathname;
108 ProtoDiv.reset("PROTO_note");
110 if(typeof response == 'object') {
112 var course = response.course
114 // ProtoDiv.replicate("PROTO_lectures_head", [course])
116 var instructor = response.instructor
118 // ProtoDiv.replicate("PROTO_lectures_instructor", [instructor])
120 var lecture = response.lecture
122 // ProtoDiv.replicate("PROTO_lecture", lectures);
124 var notes = response.notes
126 ProtoDiv.replicate("PROTO_note", notes);
134 // go to the page that lists the archived subject names
135 function showArchiveSubjects(response, cb) {
137 var path = window.location.pathname;
139 ProtoDiv.reset("PROTO_archive_subject")
141 var subjects = response.subjects
143 ProtoDiv.replicate("PROTO_archive_subject", subjects)
145 cb("archive_subjects")
150 function showArchiveCourses(response, cb) {
152 var path = window.location.pathname;
155 ProtoDiv.reset("PROTO_archive_course")
157 var courses = response.courses
159 ProtoDiv.replicate("PROTO_archive_course", courses)
161 cb("archive_courses")
166 function showArchiveNotes(response, cb) {
168 var path = window.location.pathname;
171 ProtoDiv.reset("PROTO_archive_note")
173 var notes = response.notes
174 $.each(notes, function(i, note) {
176 note.topic = note._id//note.text.substr(0, 15)+" ..."
179 ProtoDiv.replicate("PROTO_archive_note", notes)
186 function showArchiveNote(response, cb) {
188 var path = window.location.pathname;
191 ProtoDiv.reset("PROTO_archive_note_display")
193 var note = response.note
194 note = { text: "Hi <i>Mom!</i>", topic: "21st Century Greetings" }
196 note.topic = note.text.substr(0, 15)+" ..."
198 ProtoDiv.replicate("PROTO_archive_note_display", note)
200 cb("archive_note_display")
205 // go to the account registration page
206 function showRegister(response, cb) {
208 // xxx change FORM to use AJAX
213 function showLogin(response, cb) {
220 // go to the press articles page
221 function showPress(response, cb) {
226 // go to the "code of conduct" page
227 function showConduct(response, cb) {
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 },
251 schools: showSchools,
253 course: showLectures,
255 archive: showArchiveSubjects,
256 archivesubject: showArchiveCourses,
257 archivecourse: showArchiveNotes,
258 archivenote: showArchiveNote,
264 /* Do and show the appropriate thing, based on the pages current URL */
265 function showPage(y) {
267 var path = document.location.pathname
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_]*))/);
273 mainSlug = mainSlug + archiveSlugs[2];
277 $(".page").hide(); //(100); // hide all pseudo pages
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);
287 testVectors[mainSlug](response, function(pageId) {
288 $("#pg_"+pageId).fadeIn(100);
292 } else if (path === '/') {
293 return showHome(function(pageId) {
294 $("#pg_"+pageId).fadeIn(100);
299 $("#pg_notfound").fadeIn(100);
302 for(var i = 0; i < pageVectors.length; i++) {
303 var vector = pageVectors[i]
304 var matches = path.match(vector.regex)
306 vector.func(function(pageId) {
308 $("#pg_"+pageId).fadeIn(100);
318 if(i == pageVectors.length) {
320 // scroll to top of page (as if we'd done a real page fetch)
321 /*$('html, body').animate({
322 scrollTop: $("#topofcontent").offset().top
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
336 function goPage(path) {
337 var y = 0 + window.pageYOffset
339 history.pushState({}, path, path);
344 /* Simulates a "back" browser navigation. */
345 function goBack(event) {
346 var y = topQueue.pop()
351 window.onpopstate = goBack
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') {
363 $(document).ready(function() {
365 // This code executes after the page has been fully loaded
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