main.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. This is the core logic for the main page.
  3. It implements most page transitions by showing and hiding DIV elements
  4. in the page with javascript+jquery
  5. */
  6. /* Convert a JSON string to an object, or null if unparseable */
  7. function j2o(json) { try { return JSON.parse(json); } catch(e) { return null; } }
  8. /* Convert an object to a JSON string (just easier to type than "JSON.stringify" */
  9. function o2j(obj) { return JSON.stringify(obj); }
  10. function showHome(matches, cb) {
  11. cb("home");
  12. }
  13. // go to the page that lists the schools
  14. function showSchools(matches, cb) {
  15. ProtoDiv.reset("PROTO_school");
  16. $.get("/schools", { cache: false }, function(response) {
  17. var schools = [];
  18. if(typeof response == 'object') {
  19. schools = response.schools;
  20. }
  21. ProtoDiv.replicate("PROTO_school", schools);
  22. cb("schools");
  23. });
  24. }
  25. // go to the page that lists the courses for a specific school
  26. function showCourses(matches, cb) {
  27. var schoolId = matches[1];
  28. ProtoDiv.reset("PROTO_course");
  29. $.get("/school/"+schoolId, { cache: false }, function(response) {
  30. var courses = [];
  31. if(typeof response == 'object') {
  32. var school = response.school;
  33. $("#school_name").html(school.name);
  34. courses = school.courses;
  35. }
  36. ProtoDiv.replicate("PROTO_course", courses);
  37. cb("courses");
  38. });
  39. }
  40. // go to the page that lists the lectures for a specific course
  41. function showLectures(matches, cb) {
  42. var courseId = matches[1];
  43. ProtoDiv.reset("PROTO_lecture");
  44. $.get("/course/"+courseId, { cache: false }, function(response) {
  45. ProtoDiv.reset("PROTO_lectures_head");
  46. ProtoDiv.reset("PROTO_lectures_instructor");
  47. ProtoDiv.reset("PROTO_lecture");
  48. if(typeof response == 'object') {
  49. var course = response.course;
  50. if(course)
  51. ProtoDiv.replicate("PROTO_lectures_head", [course]);
  52. var instructor = response.instructor;
  53. if(instructor)
  54. ProtoDiv.replicate("PROTO_lectures_instructor", [instructor]);
  55. var lectures = response.lectures;
  56. if(lectures)
  57. ProtoDiv.replicate("PROTO_lecture", lectures);
  58. }
  59. cb("lectures");
  60. });
  61. }
  62. // go to the page that lists the note taking sessions for a specific lecture
  63. function showNotes(matches, cb) {
  64. var lectureId = matches[1];
  65. ProtoDiv.reset("PROTO_note");
  66. $.get("/lecture/"+lectureId, { cache: false }, function(response) {
  67. if(typeof response == 'object') {
  68. var course = response.course;
  69. //if(course)
  70. // ProtoDiv.replicate("PROTO_lectures_head", [course])
  71. var instructor = response.instructor;
  72. //if(instructor)
  73. // ProtoDiv.replicate("PROTO_lectures_instructor", [instructor])
  74. var lecture = response.lecture;
  75. //if(lecture)
  76. // ProtoDiv.replicate("PROTO_lecture", lectures);
  77. var notes = response.notes;
  78. if(notes)
  79. ProtoDiv.replicate("PROTO_note", notes);
  80. }
  81. cb("notes");
  82. });
  83. }
  84. // go to the page that lists the archived subject names
  85. function showArchiveSubjects(matches, cb) {
  86. ProtoDiv.reset("PROTO_archive_subject");
  87. $.get("/archive", { cache: false }, function(response) {
  88. var subjects = response.subjects;
  89. ProtoDiv.replicate("PROTO_archive_subject", subjects);
  90. })
  91. cb("archive_subjects");
  92. }
  93. function showArchiveCourses(matches, cb) {
  94. var subjectId = parseInt(matches[1]);
  95. ProtoDiv.reset("PROTO_archive_course");
  96. $.get("/archive/subject/"+subjectId, { cache: false }, function(response) {
  97. var courses = response.courses;
  98. ProtoDiv.replicate("PROTO_archive_course", courses);
  99. })
  100. cb("archive_courses");
  101. }
  102. function showArchiveNotes(matches, cb) {
  103. var courseId = parseInt(matches[1]);
  104. ProtoDiv.reset("PROTO_archive_note");
  105. $.get("/archive/course/"+courseId, { cache: false }, function(response) {
  106. var notes = response.notes;
  107. $.each(notes, function(i, note) {
  108. if(!note.topic)
  109. note.topic = note.text.substr(0, 15)+" ...";
  110. })
  111. ProtoDiv.replicate("PROTO_archive_note", notes);
  112. })
  113. cb("archive_notes");
  114. }
  115. function showArchiveNote(matches, cb) {
  116. var noteId = matches[1];
  117. ProtoDiv.reset("PROTO_archive_note_display");
  118. $.get("/archive/note/"+noteId, { cache: false }, function(response) {
  119. var note = response.note;
  120. //note = { text: "Hi <i>Mom!</i>", topic: "21st Century Greetings" }
  121. // note.text = note.text || ""
  122. if(!note.topic)
  123. note.topic = note.text.substr(0, 15)+" ...";
  124. // if(note.err) {
  125. // note.topic = note.message
  126. // note.err = note.err.stack
  127. // }
  128. //
  129. ProtoDiv.replicate("PROTO_archive_note_display", note);
  130. })
  131. cb("archive_note_display");
  132. }
  133. // go to the account registration page
  134. function showRegister(matches, cb) {
  135. // xxx clear fields?
  136. // xxx change FORM to use AJAX
  137. cb("register");
  138. }
  139. function showLogin(matches, cb) {
  140. cb("login");
  141. }
  142. // go to the press articles page
  143. function showPress(matches, cb) {
  144. cb("press");
  145. }
  146. // go to the "code of conduct" page
  147. function showConduct(matches, cb) {
  148. cb("conduct");
  149. }
  150. var pageVectors = [
  151. { regex: /^\/(index.html)?$/, func: showHome },
  152. { regex: /^\/schools/, func: showSchools },
  153. { regex: /^\/school\/([a-f0-9]{24})/, func: showCourses },
  154. { regex: /^\/course\/([a-f0-9]{24})/, func: showLectures },
  155. { regex: /^\/lecture\/([a-f0-9]{24})/, func: showNotes },
  156. { regex: /^\/archive\/?$/, func: showArchiveSubjects },
  157. { regex: /^\/archive\/subject\/([0-9]+)/, func: showArchiveCourses },
  158. { regex: /^\/archive\/course\/([0-9]+)/, func: showArchiveNotes },
  159. { regex: /^\/archive\/note\/([a-f0-9]{24})/, func: showArchiveNote },
  160. { regex: /^\/login/, func: showLogin },
  161. { regex: /^\/register/, func: showRegister },
  162. { regex: /^\/press/, func: showPress },
  163. { regex: /^\/conduct/, func: showConduct }
  164. ];
  165. /* Do and show the appropriate thing, based on the pages current URL */
  166. function showPage(y) {
  167. var path = document.location.pathname;
  168. $(".page").hide(); //(100); // hide all pseudo pages
  169. for(var i = 0; i < pageVectors.length; i++) {
  170. var vector = pageVectors[i];
  171. var matches = path.match(vector.regex);
  172. if(matches) {
  173. vector.func(matches, function(pageId) {
  174. $("#pg_"+pageId).fadeIn(100);
  175. window.scroll(0, y);
  176. })
  177. break;
  178. }
  179. }
  180. if(i == pageVectors.length) {
  181. $("#pg_notfound").fadeIn(100);
  182. window.scroll(0, 0);
  183. }
  184. // scroll to top of page (as if we'd done a real page fetch)
  185. /*$('html, body').animate({
  186. scrollTop: $("#topofcontent").offset().top
  187. }, 100);*/
  188. }
  189. /* Simulates a page load.
  190. 'path' is something like "/schools", etc.
  191. A page fetch doesn't really happen.
  192. Based on what path looks like, an appropriate DIV is shown, and action taken
  193. */
  194. var topQueue = [0];
  195. function goPage(path) {
  196. if(history.pushState !== undefined) {
  197. topQueue.push(window.pageYOffset);
  198. history.pushState({}, path, path);
  199. showPage(0);
  200. }
  201. else {
  202. document.location = path;
  203. }
  204. }
  205. /* Simulates a "back" browser navigation. */
  206. var popped = false;
  207. function goBack(event) {
  208. popped = true;
  209. showPage( topQueue.pop() );
  210. }
  211. $(document).ready(function() {
  212. // This code executes after the page has been fully loaded
  213. $(".proto").css("display", "none"); // make all the prototypes invisible
  214. //$("body").get(0).onunload = function() { } // fires when leaving the page proper
  215. ProtoDiv.each = function(e) { $(e).show() };
  216. window.onpopstate = goBack;
  217. /*setTimeout(function() {
  218. if(!popped)
  219. showPage(0)
  220. }, 2000);*/
  221. // xxx older FF browsers don't fire a page load/reload - deal with it somehow.
  222. // showPage( 0 ); // needed for some older browsers, redundant for chrome
  223. })