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); }
24 add: function(name, useAjax, cb) {
25 if (typeof useAjax === 'function') {
36 run: function(name, path) {
38 $('.nav').removeClass('active');
40 checkUser( function() {
42 if (router.routes[name].useAjax) {
49 if (data.status === 'not_found' || (typeof data === 'string')) {
50 return router.run('404');
52 router.routes[name].fn(data, render);
56 router.routes[name].fn(render);
62 function tmpl_render() {
64 $('.username').text(user.name).attr('href', '/profile');
65 $("#login_status").show();
66 $('#login_link').text('Logout').attr('href', '/logout');
67 $('#register_link').hide();
68 $('#sign_up-link').hide();
70 $('.username').text('Guest');
71 $("#login_status").hide();
72 $('#login_link').text('Login').attr('href', '/login');
73 $('#register_link').show();
74 $('#sign_up-link').show();
78 function render(pageId, response) {
81 $('.username').text(user.name).attr('href', '/profile');
82 $("#login_status").show();
83 $('#login_link').text('Logout').attr('href', '/logout');
84 $('#register_link').hide();
85 $('#sign_up-link').hide();
87 $('.username').text('Guest');
88 $("#login_status").hide();
89 $('#login_link').text('Login').attr('href', '/login');
90 $('#register_link').show();
91 $('#sign_up-link').show();
95 if (response instanceof Array) {
96 $.each(response, function() {
97 ProtoDiv.reset("PROTO_" + this.id)
98 ProtoDiv.replicate("PROTO_" + this.id, this.data)
101 ProtoDiv.reset("PROTO_" + response.id)
102 ProtoDiv.replicate("PROTO_" + response.id, response.data)
105 $("#pg_" + pageId).fadeIn(function() {
106 $("#g-footer").fadeIn(); // we don't want the footer jumping up and down
110 function message(type, msg) {
111 ProtoDiv.reset("PROTO_message");
112 ProtoDiv.replicate("PROTO_message", {type: type, msg: msg})
113 $("#messages").fadeIn(100);
116 function checkUser(cb) {
117 $.get('/checkuser', function(data) {
131 router.add('404', false, function() {
132 $("#pg_notfound").fadeIn(100);
136 router.add('home', false, function(cb) {
137 $('#learnsomething').unbind();
138 $('.nav').removeClass('active');
140 $('#signup').click(function(e) {
143 $('#learnsomething').click(function(e) {
144 $.get('/learn/random', function(data) {
145 if (data.status === 'ok') {
150 if ($('#vimeo-screencast').length === 0) {
151 $('.video-wrapper').html('<iframe id="vimeo-screencast" src="http://player.vimeo.com/video/30647271?title=0&byline=0&portrait=0&color=367da9" width="400" height="225" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>');
155 // go to the page that lists the schools
156 router.add('schools', function(data, cb) {
158 $('#school_link').addClass('active');
166 $('#pg_schools').fadeIn(function() {
167 $('#g-footer').fadeIn();
169 $('#schoolTmpl').tmpl( data.schools ).appendTo("#pg_schools #schools");
172 // go to the page that lists the courses for a specific school
173 router.add('school', function(data, cb) {
174 $('#school_link').addClass('active');
175 $('.sub_menu').hide();
176 //$('#new_course').unbind();
177 $('#form_course').hide().unbind();
181 data: data.school.courses
184 $('#pg_courses').fadeIn(function() {
185 $('#g-footer').fadeIn();
187 $("#school_name").html(data.school.name);
189 if (data.school.authorized) {
190 $('.sub_menu').show();
191 var form = $('#form_course');
195 form.submit(function(e) {
198 $.post(window.location.pathname, form.serialize(), function(data) {
199 if (data.status === 'error') {
200 message('error', data.message);
201 } else if (data.status === 'ok') {
203 goPage(window.location.pathname);
204 message('info', data.message);
210 $('#courseTmpl').tmpl( data.school.courses ).appendTo("#pg_courses #main-content");
213 // go to the page that lists the lectures for a specific course
214 router.add('course', function(data, cb) {
215 $('#school_link').addClass('active');
216 $('.sub_menu').hide();
217 $('#new_lecture').unbind();
228 if (data.instructor) {
230 id: 'lectures_instructor',
231 data: data.instructor
238 data: data.lectures.map(function(lecture) {
239 var date = new Date(lecture.date);
240 lecture.date = date.toDateString();
245 cb('lectures', response);
247 if (!data.instructor.email) {
248 $('.instructor_email').hide();
250 $('.instructor_email').show();
253 if (data.course.authorized) {
254 $('.sub_menu').show();
255 var form = $('#form_lecture');
259 form.submit(function(e) {
262 $.post(window.location.pathname, form.serialize(), function(data) {
263 if (data.status === 'error') {
264 message('error', data.message);
265 } else if (data.status === 'ok') {
267 goPage(window.location.pathname);
268 message('info', data.message);
277 // go to the page that lists the note taking sessions for a specific lecture
278 router.add('lecture', function(data, cb) {
279 $('#school_link').addClass('active');
280 $('.sub_menu').hide();
281 $('#new_note').unbind();
282 $('#form_note').hide().unbind();;
293 if (data.instructor) {
295 id: 'notes_instructor',
296 data: data.instructor
307 cb("notes", response);
309 if (!data.instructor.email) {
310 $('.instructor_email').hide();
312 $('.instructor_email').show();
315 if (data.lecture.authorized) {
317 var form = $('#form_note').show();
319 form.submit(function(e) {
322 $.post(window.location.pathname, form.serialize(), function(data) {
323 if (data.status === 'error') {
324 message('error', data.message);
325 } else if (data.status === 'ok') {
327 goPage(window.location.pathname);
328 message('info', data.message);
336 // go to the page that lists the archived subject names
337 router.add('archive', function(data, cb) {
338 $('#archive_link').addClass('active');
341 id: 'archive_subject',
345 cb("archive_subjects", response)
350 router.add('archivesubject', function(data, cb) {
351 $('.nav').removeClass('active');
352 $('#archive_link').addClass('active');
355 id: 'archive_course',
359 cb("archive_courses", response)
364 router.add('archivecourse', function(data, cb) {
365 $('#archive_link').addClass('active');
372 cb("archive_notes", response)
377 router.add('archivenote', function(data, cb) {
378 $('#archive_link').addClass('active');
381 id: 'archive_note_display',
385 cb("archive_note_display", response)
390 // go to the account registration page
391 router.add('register', false, function(cb) {
392 $('#register_link').addClass('active');
393 $('#form_register').submit(function(e) {
398 $.post(window.location.pathname, form.serialize(), function(data) {
399 if (data.status === 'error') {
400 message('error', data.message);
402 } else if (data.status === 'ok') {
404 message('info', data.message);
411 router.add('activate', function(data, cb) {
413 message('info', data.message);
416 router.add('profile', false, function(cb) {
417 $('#profile_link').addClass('active');
418 var form = $('#form_profile');
419 $('input[type=password]','#form_profile').val('');
420 $('#affiliation').attr('value', user.affil);
421 $('#showName').attr('checked', user.showName)
422 form.find('.email').text(user.email);
423 form.find('input[name=name]').val(user.name);
424 form.submit(function(e) {
427 $.post(window.location.pathname, form.serialize(), function(data) {
428 if (data.status === 'error') {
429 message('error', data.message);
431 } else if (data.status === 'ok') {
433 message('info', data.message);
440 router.add('login', false, function(cb) {
441 $('input','#form_login').val('');
442 $('#form_login').submit(function(e) {
447 $.post(window.location.pathname, form.serialize(), function(data) {
448 if (data.status === 'error') {
449 message('error', data.message);
451 } else if (data.status === 'ok') {
453 message('info', 'Successfully logged in');
460 router.add('logout', function(data, cb) {
462 message('info', 'Successfully logged out');
465 router.add('resetpass', false, function(cb) {
466 $('input','#form_resetpass').val('');
467 $('#form_resetpass').submit(function(e) {
472 $.post(window.location.pathname, form.serialize(), function(data) {
473 if (data.status === 'error') {
474 message('error', data.message);
476 } else if (data.status === 'ok') {
478 message('info', data.message);
485 router.add('resetpw', false, function(cb) {
486 $('input','#form_resetpw').val('');
487 $('#form_resetpw').submit(function(e) {
492 $.post(window.location.pathname, form.serialize(), function(data) {
493 if (data.status === 'error') {
494 message('error', data.message);
496 } else if (data.status === 'ok') {
498 message('info', data.message);
505 // go to the press articles page
506 router.add('press', false, function(cb) {
507 $('#press_link').addClass('active');
512 // go to the "code of conduct" page
513 router.add('conduct', false, function(cb) {
519 /* Do and show the appropriate thing, based on the pages current URL */
520 function showPage(y) {
522 $('.page').hide(); //(100);// hide all pseudo pages
523 $('#g-footer').hide();
526 path = document.location.pathname,
527 routes = router.routes,
528 slugs = path.split('/');
532 var mainSlug = slugs[0].toLowerCase() || 'home';
533 console.log(slugs[0].toLowerCase());
535 if (mainSlug === 'archive') {
537 mainSlug = mainSlug + slugs[1];
541 if (routes[mainSlug]) {
542 router.run(mainSlug, path)
551 /* Simulates a page load.
552 'path' is something like "/schools", etc.
553 A page fetch doesn't really happen.
554 Based on what path looks like, an appropriate DIV is shown, and action taken
557 function goPage(path) {
558 if (history.pushState !== undefined) {
559 topQueue.push(window.pageYOffset)
560 history.pushState({}, path, path);
563 document.location = path;
567 /* Simulates a "back" browser navigation. */
569 function goBack(event) {
571 console.timeEnd('pop')
572 showPage( topQueue.pop() );
576 console.time('no-pop')
577 window.onpopstate = goBack
579 $(document).ready(function() {
581 // This code executes after the page has been fully loaded
583 $('body').on('click', 'a[href^=/]', function(e) {
584 var path = e.target.pathname || '/';
585 var checkNote = path.match(/\/([a-zA-Z]+)/);
586 if (checkNote && checkNote[1] == 'note') {
588 } else if (!history.pushState) {
596 // xxx older FF browsers don't fire a page load/reload - deal with it somehow.
597 // I've increased the timeout, we need to avoid calling showPage twice. It causes page flicker.
598 setTimeout(function() {
599 console.timeEnd('no-pop')
601 showPage( 0 ); // needed for some older browsers, redundant for chrome