123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- /*
- This is the core logic for the main page.
- It implements most page transitions by showing and hiding DIV elements
- in the page with javascript+jquery
- */
- /* Convert a JSON string to an object, or null if unparseable */
- function j2o(json) { try { return JSON.parse(json); } catch(e) { return null; } }
- /* Convert an object to a JSON string (just easier to type than "JSON.stringify" */
- function o2j(obj) { return JSON.stringify(obj); }
- function showHome(cb) {
- cb("home");
- }
- // go to the page that lists the schools
- function showSchools(response, cb) {
- var path = window.location.pathname;
- ProtoDiv.reset("PROTO_school");
- var schools = []
- if(typeof response == 'object') {
- schools = response.schools
- }
- ProtoDiv.replicate("PROTO_school", schools);
- cb("schools");
- }
- // go to the page that lists the courses for a specific school
- function showCourses(response, cb) {
- var path = window.location.pathname;
- ProtoDiv.reset("PROTO_course");
- var courses = []
- if(typeof response == 'object') {
- var school = response.school
- $("#school_name").html(school.name);
- courses = school.courses
- }
- ProtoDiv.replicate("PROTO_course", courses);
- cb("courses")
- }
- // go to the page that lists the lectures for a specific course
- function showLectures(response, cb) {
- var path = window.location.pathname;
-
- ProtoDiv.reset("PROTO_lecture");
-
- ProtoDiv.reset("PROTO_lectures_head")
- ProtoDiv.reset("PROTO_lectures_instructor")
- ProtoDiv.reset("PROTO_lecture")
- if(typeof response == 'object') {
- var course = response.course
- if(course)
- ProtoDiv.replicate("PROTO_lectures_head", [course])
- var instructor = response.instructor
- if(instructor)
- ProtoDiv.replicate("PROTO_lectures_instructor", [instructor])
- var lectures = response.lectures
- if(lectures)
- ProtoDiv.replicate("PROTO_lecture", lectures);
- }
- cb("lectures")
- }
- // go to the page that lists the note taking sessions for a specific lecture
- function showNotes(response, cb) {
- var path = window.location.pathname;
- ProtoDiv.reset("PROTO_note");
-
- if(typeof response == 'object') {
- var course = response.course
- //if(course)
- // ProtoDiv.replicate("PROTO_lectures_head", [course])
- var instructor = response.instructor
- //if(instructor)
- // ProtoDiv.replicate("PROTO_lectures_instructor", [instructor])
- var lecture = response.lecture
- //if(lecture)
- // ProtoDiv.replicate("PROTO_lecture", lectures);
- var notes = response.notes
- if(notes)
- ProtoDiv.replicate("PROTO_note", notes);
- }
- cb("notes")
- }
- // go to the page that lists the archived subject names
- function showArchiveSubjects(response, cb) {
- var path = window.location.pathname;
- ProtoDiv.reset("PROTO_archive_subject")
- var subjects = response.subjects
- ProtoDiv.replicate("PROTO_archive_subject", subjects)
- cb("archive_subjects")
- }
- function showArchiveCourses(response, cb) {
- var path = window.location.pathname;
- ProtoDiv.reset("PROTO_archive_course")
- var courses = response.courses
- ProtoDiv.replicate("PROTO_archive_course", courses)
- cb("archive_courses")
- }
- function showArchiveNotes(response, cb) {
- var path = window.location.pathname;
- ProtoDiv.reset("PROTO_archive_note")
- var notes = response.notes
- $.each(notes, function(i, note) {
- if(!note.topic)
- note.topic = note._id//note.text.substr(0, 15)+" ..."
- })
- ProtoDiv.replicate("PROTO_archive_note", notes)
- cb("archive_notes")
- }
- function showArchiveNote(response, cb) {
- var path = window.location.pathname;
- ProtoDiv.reset("PROTO_archive_note_display")
- var note = response.note
- note = { text: "Hi <i>Mom!</i>", topic: "21st Century Greetings" }
- if(!note.topic)
- note.topic = note.text.substr(0, 15)+" ..."
- ProtoDiv.replicate("PROTO_archive_note_display", note)
- cb("archive_note_display")
- }
- // go to the account registration page
- function showRegister(response, cb) {
- // xxx clear fields?
- // xxx change FORM to use AJAX
- cb("register");
- }
- function showLogin(response, cb) {
- cb("login");
- }
- // go to the press articles page
- function showPress(response, cb) {
- cb("press");
- }
- // go to the "code of conduct" page
- function showConduct(response, cb) {
- cb("conduct");
- }
- var pageVectors = [
- { regex: /^\/(index.html)?$/, func: showHome },
- { regex: /^\/schools/, func: showSchools },
- { regex: /^\/school\/([a-f0-9]{24})/, func: showCourses },
- { regex: /^\/course\/([a-f0-9]{24})/, func: showLectures },
- { regex: /^\/lecture\/([a-f0-9]{24})/, func: showNotes },
- { regex: /^\/archive\/?$/, func: showArchiveSubjects },
- { regex: /^\/archive\/subject\/([0-9]+)/, func: showArchiveCourses },
- { regex: /^\/archive\/course\/([0-9]+)/, func: showArchiveNotes },
- { regex: /^\/archive\/note\/([0-9]+)/, func: showArchiveNote },
- { regex: /^\/login/, func: showLogin },
- { regex: /^\/register/, func: showRegister },
- { regex: /^\/press/, func: showPress },
- { regex: /^\/conduct/, func: showConduct },
- ];
- var testVectors = {
- schools: showSchools,
- school: showCourses,
- course: showLectures,
- lecture: showNotes,
- archive: showArchiveSubjects,
- archivesubject: showArchiveCourses,
- archivecourse: showArchiveNotes,
- archivenote: showArchiveNote,
- login: showLogin,
- press: showPress,
- conduct: showConduct
- }
- /* Do and show the appropriate thing, based on the pages current URL */
- function showPage(y) {
- var path = document.location.pathname
- var mainSlug = path.match(/((?:[a-z][a-z]+))/) ? path.match(/((?:[a-z][a-z]+))/)[1].toLowerCase() : '';
- if (mainSlug === 'archive') {
- var archiveSlugs = path.match(/((?:[a-z][a-z]+))\/((?:[a-z][a-z0-9_]*))/);
- if (archiveSlugs) {
- mainSlug = mainSlug + archiveSlugs[2];
- }
- }
- $(".page").hide(); //(100); // hide all pseudo pages
- if (testVectors[mainSlug]) {
- return $.get(path, { cache: false }, function(response) {
- if (response.status === 'error') {
- console.log(response.message)
- $("#pg_notfound").fadeIn(100);
- window.scroll(0, 0)
- return;
- }
- testVectors[mainSlug](response, function(pageId) {
- $("#pg_"+pageId).fadeIn(100);
- window.scroll(0, y)
- })
- });
- } else if (path === '/') {
- return showHome(function(pageId) {
- $("#pg_"+pageId).fadeIn(100);
- window.scroll(0, y)
- })
- }
- $("#pg_notfound").fadeIn(100);
- window.scroll(0, 0)
- /*
- for(var i = 0; i < pageVectors.length; i++) {
- var vector = pageVectors[i]
- var matches = path.match(vector.regex)
- if(matches) {
- vector.func(function(pageId) {
- $("#pg_"+pageId).fadeIn(100);
- window.scroll(0, y)
- })
- break
- }
- }
-
- if(i == pageVectors.length) {
- }
- // scroll to top of page (as if we'd done a real page fetch)
- /*$('html, body').animate({
- scrollTop: $("#topofcontent").offset().top
- }, 100);*/
- }
- /* Simulates a page load.
- 'path' is something like "/schools", etc.
- A page fetch doesn't really happen.
- Based on what path looks like, an appropriate DIV is shown, and action taken
- */
- var topQueue = [0]
- function goPage(path) {
- var y = 0 + window.pageYOffset
- topQueue.push(y)
- history.pushState({}, path, path);
- showPage(0);
- }
- /* Simulates a "back" browser navigation. */
- function goBack(event) {
- var y = topQueue.pop()
- showPage( y );
- }
- window.onpopstate = goBack
- $('a[href^=/]').live('click', function(e) {
- var path = e.target.pathname || '/';
- var checkNote = path.match(/((?:[a-z][a-z]+))/);
- if (checkNote && checkNote[1] == 'note') {
- return true;
- } else {
- goPage(path)
- return false;
- }
- })
- $(document).ready(function() {
- // This code executes after the page has been fully loaded
- // xxx older FF browsers don't fire a page load/reload - deal with it somehow.
- // showPage( 0 ); // needed for some older browsers, redundant for chrome
- })
|