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
75 function goLectures(courseId) {
76 ProtoDiv.reset("PROTO_lecture");
78 $.get("/course/"+courseId, {}, function(response) {
84 { _id: 1, name: "lecture 1" },
85 { _id: 2, name: "lecture 2" },
90 if(typeof response == 'object') {
91 var course = response.course
92 $("#course_name").html(course.name);
93 lectures = course.lectures
95 ProtoDiv.replicate("PROTO_lecture", lectures);
103 // go to the page that lists the lectures for a specific course
105 function goPads(lectureId) {
106 ProtoDiv.reset("PROTO_pad");
108 $.get("/lecture/"+lectureId, {}, function(response) {
114 { _id: 1, name: "pad 1" },
115 { _id: 2, name: "pad 2" },
120 if(typeof response == 'object') {
121 var lecture = response.lecture
122 $("#lecture_name").html(lecture.name);
125 ProtoDiv.replicate("PROTO_pad", lectures);
133 var archivedSubjects = []
135 // go to the page that lists the archived subject names
136 function showArchive(matches, cb) {
140 { id: 83, title: "Anthropology" },
141 { id: 44, title: "CORE-Foreign Cultures" },
142 { id: 80, title: "CORE-Historical Study" }
145 ProtoDiv.reset("PROTO_archived_subjects")
146 ProtoDiv.replicate("PROTO_archived_subjects", archivedSubjects)
152 // go to the account registration page
153 function showRegister(matches, cb) {
155 // xxx change FORM to use AJAX
160 function showLogin(matches, cb) {
167 // go to the press articles page
168 function showPress(matches, cb) {
173 // go to the "code of conduct" page
174 function showConduct(matches, cb) {
181 function hideAllPages() {
183 $(".page").fadeOut(100);
189 { regex: /^\/(index.html)?$/, func: showHome },
190 { regex: /^\/schools/, func: showSchools },
191 { regex: /^\/school\/([a-f0-9]{24})/, func: showCourses },
192 { regex: /^\/login/, func: showLogin },
193 { regex: /^\/register/, func: showRegister },
194 { regex: /^\/press/, func: showPress },
195 { regex: /^\/archive/, func: showArchive },
196 { regex: /^\/conduct/, func: showConduct },
200 /* Do and show the appropriate thing, based on the pages current URL */
201 function showPage() {
203 var path = document.location.pathname
205 $(".page").fadeOut(100); // hide all pseudo pages
207 for(var i = 0; i < pageVectors.length; i++) {
208 var vector = pageVectors[i]
209 var matches = path.match(vector.regex)
211 vector.func(matches, function(pageId) {
213 $("#pg_"+pageId).fadeIn(100);
214 $('html, body').animate({ scrollTop: 0 }, 100);
221 if(i == pageVectors.length) {
222 $("#pg_notfound").fadeIn(100);
223 $('html, body').animate({ scrollTop: 0 }, 100);
225 // scroll to top of page (as if we'd done a real page fetch)
226 //$('html, body').animate({ scrollTop: 0 }, 100);
227 /*$('html, body').animate({
228 scrollTop: $("#topofcontent").offset().top
236 /* Simulates a page load.
237 'path' is something like "/schools", etc.
238 A page fetch doesn't really happen.
239 Based on what path looks like, an appropriate DIV is shown, and action taken
241 function goPage(path) {
242 history.pushState({prev:path}, path, path);
247 /* Simulates a "back" browser navigation.
249 function goBack(event) {
250 var state = event.state; //alert("pop: "+o2j(state));
258 history.replaceState(null, "", "/index.html");
263 //alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
264 //history.replaceState(null, "", state.prev);
265 showPage(state.prev);
266 // showPage(state.prev);
267 //alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
273 $(document).ready(function() {
275 // This executes after the page has been fully loaded
277 window.onpopstate = goBack