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
36 ProtoDiv.replicate("PROTO_school", schools);
46 // go to the page that lists the courses for a specific school
48 function goCourses(schoolName, schoolId, a) {
49 ProtoDiv.reset("PROTO_course");
51 $.get("/school/"+schoolId, {}, function(response) {
53 if(typeof response == 'object') {
54 var school = response.school
55 $("#school_name").html(school.name);
56 courses = school.courses
58 ProtoDiv.replicate("PROTO_course", courses);
59 goPage("courses", "/courses/"+schoolId)
66 // go to the page that lists the lectures for a specific course
68 function goLectures(courseId) {
69 ProtoDiv.reset("PROTO_lecture");
71 $.get("/course/"+courseId, {}, function(response) {
77 { _id: 1, name: "lecture 1" },
78 { _id: 2, name: "lecture 2" },
83 if(typeof response == 'object') {
84 var course = response.course
85 $("#course_name").html(course.name);
86 lectures = course.lectures
88 ProtoDiv.replicate("PROTO_lecture", lectures);
96 // go to the page that lists the lectures for a specific course
98 function goPads(lectureId) {
99 ProtoDiv.reset("PROTO_pad");
101 $.get("/lecture/"+lectureId, {}, function(response) {
107 { _id: 1, name: "pad 1" },
108 { _id: 2, name: "pad 2" },
113 if(typeof response == 'object') {
114 var lecture = response.lecture
115 $("#lecture_name").html(lecture.name);
118 ProtoDiv.replicate("PROTO_pad", lectures);
126 var archivedSubjects = []
128 // go to the page that lists the archived subject names
129 function goArchivedSubjects() {
134 { id: 83, title: "Anthropology" },
135 { id: 44, title: "CORE-Foreign Cultures" },
136 { id: 80, title: "CORE-Historical Study" }
139 ProtoDiv.reset("PROTO_archived_subjects");
140 ProtoDiv.replicate("PROTO_archived_subjects", archivedSubjects);
147 // go to the account registration page
148 function goRegister() {
151 // xxx change FORM to use AJAX
156 // go to the press articles page
163 // go to the "code of conduct" page
164 function goConduct() {
172 function hideAllPages() {
174 $(".page").fadeOut(100);
180 { regex: /^\/(index.html)?$/, func: showHome },
181 { regex: /^\/schools/, func: showSchools },
185 /* Do and show the appropriate thing, based on the pages current URL */
186 function showPage() {
188 var path = document.location.pathname
190 $(".page").fadeOut(100); // hide all pseudo pages
192 for(var i = 0; i < pageVectors.length; i++) {
193 var vector = pageVectors[i]
194 var matches = path.match(vector.regex)
196 vector.func(matches, function(pageId) {
198 $("#pg_"+pageId).fadeIn(100);
199 $('html, body').animate({ scrollTop: 0 }, 100);
206 // scroll to top of page (as if we'd done a real page fetch)
207 //$('html, body').animate({ scrollTop: 0 }, 100);
208 /*$('html, body').animate({
209 scrollTop: $("#topofcontent").offset().top
217 /* Simulates a page load.
218 'path' is something like "/schools", etc.
219 A page fetch doesn't really happen.
220 Based on what path looks like, an appropriate DIV is shown, and action taken
222 function goPage(path) {
223 history.pushState({prev:path}, path, path);
228 /* Simulates a "back" browser navigation.
230 function goBack(event) {
231 var state = event.state; alert("pop: "+o2j(state));
239 history.replaceState(null, "", "/index.html");
244 //alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
245 //history.replaceState(null, "", state.prev);
246 showPage(state.prev);
247 // showPage(state.prev);
248 //alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
254 $(document).ready(function() {
256 // This executes after the page has been fully loaded
258 window.onpopstate = goBack