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); }
21 add: function(name, useAjax, cb) {
22 if (typeof useAjax === 'function') {
31 run: function(name, path) {
32 checkUser(function() {
33 if (router.routes[name].useAjax) {
34 $.get(path, {cache: false}, function(data) {
35 if (data.status === 'not_found' || (typeof data === 'string')) {
36 return router.run('404');
38 router.routes[name].fn(data, render);
41 router.routes[name].fn(render);
47 function render(pageId, response) {
49 ProtoDiv.inject("PROTO_login_info", user)
50 $("#login_status").show();
51 $('#login_link').text('Logout').attr('href', '/logout');
52 $('#register_link').hide();
53 $('#profile_link').show();
55 ProtoDiv.reset("PROTO_login_info");
56 $("#login_status").hide();
57 $('#login_link').text('Login').attr('href', '/login');
58 $('#register_link').show();
59 $('#profile_link').hide();
62 if (response instanceof Array) {
63 $.each(response, function() {
64 console.log(this.id, this.data)
65 ProtoDiv.reset("PROTO_" + this.id)
66 ProtoDiv.replicate("PROTO_" + this.id, this.data)
69 ProtoDiv.reset("PROTO_" + response.id)
70 ProtoDiv.replicate("PROTO_" + response.id, response.data)
73 $("#pg_" + pageId).fadeIn(100);
76 function message(type, msg) {
77 ProtoDiv.reset("PROTO_message");
78 ProtoDiv.replicate("PROTO_message", {type: type, msg: msg})
79 $("#messages").fadeIn(100);
82 function checkUser(cb) {
83 $.get('/checkuser', function(data) {
96 router.add('404', false, function() {
97 $("#pg_notfound").fadeIn(100);
101 router.add('home', false, function(cb) {
102 $('#learnsomething').unbind();
104 $('#learnsomething').click(function(e) {
105 $.get('/learn/random', function(data) {
106 if (data.status === 'ok') {
111 if ($('#vimeo-screencast').length === 0) {
112 $('#screencast').html('<iframe id="vimeo-screencast" src="http://player.vimeo.com/video/30647271?title=0&byline=0&portrait=0" width="350" height="250" frameborder="0" webkitallowfullscreen="webkitAllowFullScreen" allowfullscreen="allowFullScreen"></iframe>');
118 // go to the page that lists the schools
119 router.add('schools', function(data, cb) {
126 cb("schools", response);
131 // go to the page that lists the courses for a specific school
132 router.add('school', function(data, cb) {
133 $('#new_course').hide().unbind();
134 $('#form_course').hide().unbind();
137 data: data.school.courses
140 $("#school_name").html(data.school.name);
142 if (data.school.authorized) {
143 $('#new_course').show();
144 $('#new_course').click(function(e) {
147 var form = $('#form_course');
151 form.submit(function(e) {
154 $.post(window.location.pathname, form.serialize(), function(data) {
155 if (data.status === 'error') {
156 message('error', data.message);
157 } else if (data.status === 'ok') {
159 goPage(window.location.pathname);
160 message('info', data.message);
166 cb("courses", response)
172 // go to the page that lists the lectures for a specific course
173 router.add('course', function(data, cb) {
174 $('#new_lecture').hide().unbind();
175 $('#form_lecture').hide().unbind();;
186 if (data.instructor) {
188 id: 'lectures_instructor',
189 data: data.instructor
200 if (data.course.authorized) {
201 $('#new_lecture').show();
202 $('#new_lecture').click(function(e) {
205 var form = $('#form_lecture');
209 form.submit(function(e) {
212 $.post(window.location.pathname, form.serialize(), function(data) {
213 if (data.status === 'error') {
214 message('error', data.message);
215 } else if (data.status === 'ok') {
217 goPage(window.location.pathname);
218 message('info', data.message);
225 cb('lectures', response);
230 // go to the page that lists the note taking sessions for a specific lecture
231 router.add('lecture', function(data, cb) {
232 $('#new_note').hide().unbind();
233 $('#form_note').hide().unbind();;
244 if (data.lecture.authorized) {
245 $('#new_note').show();
246 $('#new_note').click(function(e) {
249 var form = $('#form_note');
253 form.submit(function(e) {
256 $.post(window.location.pathname, form.serialize(), function(data) {
257 if (data.status === 'error') {
258 message('error', data.message);
259 } else if (data.status === 'ok') {
261 goPage(window.location.pathname);
262 message('info', data.message);
268 cb("notes", response);
272 // go to the page that lists the archived subject names
273 router.add('archive', function(data, cb) {
276 id: 'archive_subject',
280 cb("archive_subjects", response)
285 router.add('archivesubject', function(data, cb) {
288 id: 'archive_course',
292 cb("archive_courses", response)
297 router.add('archivecourse', function(data, cb) {
304 cb("archive_notes", response)
309 router.add('archivenote', function(data, cb) {
312 id: 'archive_note_display',
316 cb("archive_note_display", response)
321 // go to the account registration page
322 router.add('register', false, function(cb) {
323 $('#form_register').submit(function(e) {
326 console.log('register')
329 $.post(window.location.pathname, form.serialize(), function(data) {
330 if (data.status === 'error') {
331 message('error', data.message);
333 } else if (data.status === 'ok') {
335 message('info', data.message);
342 router.add('activate', function(data, cb) {
344 message('info', data.message);
347 router.add('profile', false, function(cb) {
348 var form = $('#form_profile');
349 $('input[type=password]','#form_profile').val('');
350 $('#affiliation').attr('value', user.affil);
351 $('#showName').attr('checked', user.showName)
352 form.find('.email').text(user.email);
353 form.find('input[name=name]').val(user.name);
354 form.submit(function(e) {
358 $.post(window.location.pathname, form.serialize(), function(data) {
359 if (data.status === 'error') {
360 message('error', data.message);
362 } else if (data.status === 'ok') {
364 message('info', data.message);
371 router.add('login', false, function(cb) {
372 $('input','#form_login').val('');
373 $('#form_login').submit(function(e) {
378 $.post(window.location.pathname, form.serialize(), function(data) {
379 if (data.status === 'error') {
380 message('error', data.message);
382 } else if (data.status === 'ok') {
384 message('info', 'Successfully logged in');
391 router.add('logout', function(data, cb) {
393 message('info', 'Successfully logged out');
396 router.add('resetpass', false, function(cb) {
397 $('input','#form_resetpass').val('');
398 $('#form_resetpass').submit(function(e) {
403 $.post(window.location.pathname, form.serialize(), function(data) {
404 if (data.status === 'error') {
405 message('error', data.message);
407 } else if (data.status === 'ok') {
409 message('info', data.message);
416 router.add('resetpw', false, function(cb) {
417 $('input','#form_resetpw').val('');
418 $('#form_resetpw').submit(function(e) {
423 $.post(window.location.pathname, form.serialize(), function(data) {
424 if (data.status === 'error') {
425 message('error', data.message);
427 } else if (data.status === 'ok') {
429 message('info', data.message);
436 // go to the press articles page
437 router.add('press', false, function(cb) {
442 // go to the "code of conduct" page
443 router.add('conduct', false, function(cb) {
449 /* Do and show the appropriate thing, based on the pages current URL */
450 function showPage(y) {
452 $(".page").hide(); //(100); // hide all pseudo pages
454 var path = document.location.pathname
455 var routes = router.routes;
457 var slugs = path.split('/');
461 mainSlug = slugs[0].toLowerCase() || 'home';
463 if (mainSlug === 'archive') {
465 mainSlug = mainSlug + slugs[1];
469 if (routes[mainSlug]) {
470 router.run(mainSlug, path)
480 /* Simulates a page load.
481 'path' is something like "/schools", etc.
482 A page fetch doesn't really happen.
483 Based on what path looks like, an appropriate DIV is shown, and action taken
486 function goPage(path) {
487 var y = 0 + window.pageYOffset
489 history.pushState({}, path, path);
494 /* Simulates a "back" browser navigation. */
495 function goBack(event) {
496 var y = topQueue.pop()
501 window.onpopstate = goBack
503 $(document).ready(function() {
505 // This code executes after the page has been fully loaded
507 $('body').on('click', 'a[href^=/]', function(e) {
508 var path = e.target.pathname || '/';
509 var checkNote = path.match(/\/([a-zA-Z]+)/);
510 if (checkNote && checkNote[1] == 'note') {
512 } else if (!history.pushState) {
520 // xxx older FF browsers don't fire a page load/reload - deal with it somehow.
521 // showPage( 0 ); // needed for some older browsers, redundant for chrome