main_old.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. $('#form_register').submit(function(e) {
  138. e.preventDefault();
  139. $.post(window.location.pathname, $(this).serialize(), function(data) {
  140. console.log(data) // Will give you results, either an data.status == error or ok
  141. })
  142. })
  143. cb("register");
  144. }
  145. function showLogin(matches, cb) {
  146. $('#form_login').submit(function(e) {
  147. e.preventDefault();
  148. $.post(window.location.pathname, $(this).serialize(), function(data) {
  149. console.log(data) // Will give you results, either an data.status == error or ok
  150. })
  151. })
  152. cb("login");
  153. }
  154. // go to the press articles page
  155. function showPress(matches, cb) {
  156. cb("press");
  157. }
  158. // go to the "code of conduct" page
  159. function showConduct(matches, cb) {
  160. cb("conduct");
  161. }
  162. var pageVectors = [
  163. { regex: /^\/(index.html)?$/, func: showHome },
  164. { regex: /^\/schools/, func: showSchools },
  165. { regex: /^\/school\/([a-f0-9]{24})/, func: showCourses },
  166. { regex: /^\/course\/([a-f0-9]{24})/, func: showLectures },
  167. { regex: /^\/lecture\/([a-f0-9]{24})/, func: showNotes },
  168. { regex: /^\/archive\/?$/, func: showArchiveSubjects },
  169. { regex: /^\/archive\/subject\/([0-9]+)/, func: showArchiveCourses },
  170. { regex: /^\/archive\/course\/([0-9]+)/, func: showArchiveNotes },
  171. { regex: /^\/archive\/note\/([a-f0-9]{24})/, func: showArchiveNote },
  172. { regex: /^\/login/, func: showLogin },
  173. { regex: /^\/register/, func: showRegister },
  174. { regex: /^\/press/, func: showPress },
  175. { regex: /^\/conduct/, func: showConduct }
  176. ];
  177. /* Do and show the appropriate thing, based on the pages current URL */
  178. function showPage(y) {
  179. var path = document.location.pathname;
  180. $(".page").hide(); //(100); // hide all pseudo pages
  181. for(var i = 0; i < pageVectors.length; i++) {
  182. var vector = pageVectors[i];
  183. var matches = path.match(vector.regex);
  184. if(matches) {
  185. vector.func(matches, function(pageId) {
  186. $("#pg_"+pageId).fadeIn(100);
  187. window.scroll(0, y);
  188. })
  189. break;
  190. }
  191. }
  192. if(i == pageVectors.length) {
  193. $("#pg_notfound").fadeIn(100);
  194. window.scroll(0, 0);
  195. }
  196. // scroll to top of page (as if we'd done a real page fetch)
  197. /*$('html, body').animate({
  198. scrollTop: $("#topofcontent").offset().top
  199. }, 100);*/
  200. }
  201. /* Simulates a page load.
  202. 'path' is something like "/schools", etc.
  203. A page fetch doesn't really happen.
  204. Based on what path looks like, an appropriate DIV is shown, and action taken
  205. */
  206. var topQueue = [0];
  207. function goPage(path) {
  208. if(history.pushState !== undefined) {
  209. topQueue.push(window.pageYOffset);
  210. history.pushState({}, path, path);
  211. showPage(0);
  212. }
  213. else {
  214. document.location = path;
  215. }
  216. }
  217. /* Simulates a "back" browser navigation. */
  218. var popped = false;
  219. function goBack(event) {
  220. popped = true;
  221. showPage( topQueue.pop() );
  222. }
  223. $(document).ready(function() {
  224. // This code executes after the page has been fully loaded
  225. $(".proto").css("display", "none"); // make all the prototypes invisible
  226. //$("body").get(0).onunload = function() { } // fires when leaving the page proper
  227. ProtoDiv.each = function(e) { $(e).show() };
  228. window.onpopstate = goBack;
  229. /*setTimeout(function() {
  230. if(!popped)
  231. showPage(0)
  232. }, 2000);*/
  233. // xxx older FF browsers don't fire a page load/reload - deal with it somehow.
  234. // showPage( 0 ); // needed for some older browsers, redundant for chrome
  235. })