main-jacob.js 7.7 KB

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