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(matches, cb) {
25 // go to the page that lists the schools
26 function showSchools(matches, cb) {
28 ProtoDiv.reset("PROTO_school");
30 $.get("/schools", {}, function(response) {
33 if(typeof response == 'object') {
34 schools = response.schools
37 ProtoDiv.replicate("PROTO_school", schools);
47 // go to the page that lists the courses for a specific school
48 function showCourses(matches, cb) {
50 var schoolId = matches[1]
52 ProtoDiv.reset("PROTO_course");
54 $.get("/school/"+schoolId, {}, function(response) {
57 if(typeof response == 'object') {
58 var school = response.school
59 $("#school_name").html(school.name);
60 courses = school.courses
63 ProtoDiv.replicate("PROTO_course", courses);
73 // go to the page that lists the lectures for a specific course
74 function showLectures(matches, cb) {
76 var courseId = matches[1]
78 ProtoDiv.reset("PROTO_lecture");
80 $.get("/course/"+courseId, {}, function(response) {
82 ProtoDiv.reset("PROTO_lectures_head")
83 ProtoDiv.reset("PROTO_lectures_instructor")
84 ProtoDiv.reset("PROTO_lecture")
86 if(typeof response == 'object') {
88 var course = response.course
90 ProtoDiv.replicate("PROTO_lectures_head", [course])
92 var instructor = response.instructor
94 ProtoDiv.replicate("PROTO_lectures_instructor", [instructor])
96 var lectures = response.lectures
98 ProtoDiv.replicate("PROTO_lecture", lectures);
108 // go to the page that lists the note taking sessions for a specific lecture
109 function showNotes(matches, cb) {
111 var lectureId = matches[1]
113 ProtoDiv.reset("PROTO_note");
115 $.get("/lecture/"+lectureId, {}, function(response) {
117 if(typeof response == 'object') {
119 var course = response.course
121 // ProtoDiv.replicate("PROTO_lectures_head", [course])
123 var instructor = response.instructor
125 // ProtoDiv.replicate("PROTO_lectures_instructor", [instructor])
127 var lecture = response.lecture
129 // ProtoDiv.replicate("PROTO_lecture", lectures);
131 var notes = response.notes
133 ProtoDiv.replicate("PROTO_note", notes);
144 var archivedSubjects = []
146 // go to the page that lists the archived subject names
147 function showArchive(matches, cb) {
151 { id: 83, title: "Anthropology" },
152 { id: 44, title: "CORE-Foreign Cultures" },
153 { id: 80, title: "CORE-Historical Study" }
156 ProtoDiv.reset("PROTO_archived_subjects")
157 ProtoDiv.replicate("PROTO_archived_subjects", archivedSubjects)
163 // go to the account registration page
164 function showRegister(matches, cb) {
166 // xxx change FORM to use AJAX
171 function showLogin(matches, cb) {
178 // go to the press articles page
179 function showPress(matches, cb) {
184 // go to the "code of conduct" page
185 function showConduct(matches, cb) {
192 function hideAllPages() {
194 $(".page").fadeOut(100);
200 { regex: /^\/(index.html)?$/, func: showHome },
201 { regex: /^\/schools/, func: showSchools },
202 { regex: /^\/school\/([a-f0-9]{24})/, func: showCourses },
203 { regex: /^\/course\/([a-f0-9]{24})/, func: showLectures },
204 { regex: /^\/lecture\/([a-f0-9]{24})/, func: showNotes },
205 { regex: /^\/login/, func: showLogin },
206 { regex: /^\/register/, func: showRegister },
207 { regex: /^\/press/, func: showPress },
208 { regex: /^\/archive/, func: showArchive },
209 { regex: /^\/conduct/, func: showConduct },
213 /* Do and show the appropriate thing, based on the pages current URL */
214 function showPage(y) {
216 var path = document.location.pathname
218 $(".page").fadeOut(100); // hide all pseudo pages
220 for(var i = 0; i < pageVectors.length; i++) {
221 var vector = pageVectors[i]
222 var matches = path.match(vector.regex)
224 vector.func(matches, function(pageId) {
226 $("#pg_"+pageId).fadeIn(100);
235 if(i == pageVectors.length) {
236 $("#pg_notfound").fadeIn(100);
239 // scroll to top of page (as if we'd done a real page fetch)
240 /*$('html, body').animate({
241 scrollTop: $("#topofcontent").offset().top
249 /* Simulates a page load.
250 'path' is something like "/schools", etc.
251 A page fetch doesn't really happen.
252 Based on what path looks like, an appropriate DIV is shown, and action taken
255 function goPage(path) {
256 var y = 0 + window.pageYOffset
258 var o = {py:(path+"|"+y)}
259 history.pushState(o, path, path);
264 /* Simulates a "back" browser navigation. */
265 function goBack(event) {
266 var y = topQueue.pop()
271 $(document).ready(function() {
273 // This code executes after the page has been fully loaded
275 window.onpopstate = goBack