navbar.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. $(function() {
  2. $('#suggestions').hide();
  3. $('#nav-toggle').click(function() {
  4. $('#nav-toggle').toggleClass('is-active');
  5. $('#nav-menu').toggleClass('is-active');
  6. });
  7. $('#search_button').click(function () {
  8. var query = $('#search_field').val().trim();
  9. if (query.length == 0)
  10. return;
  11. document.location.href = '/search?q=' + query;
  12. });
  13. $('#search_field').on('keyup', function (event) {
  14. if (event.keyCode == 13) {
  15. $('#search_button').click();
  16. return;
  17. }
  18. var query = $('#search_field').val();
  19. query = query.trim();
  20. if (query.length == 0) {
  21. $('#suggestions').hide();
  22. return;
  23. }
  24. suggest('#suggestions', '#suggestions_content', '#search_field', query);
  25. });
  26. if (window.my_user_id >= 0) {
  27. $('#navbar_user_link').attr('href', '/user/' + String(window.my_user_id));
  28. api('user', {user_id: window.my_user_id}, function (data) {
  29. $('#navbar_avatar').attr('src', data.avatar128);
  30. var username = data.username;
  31. if (username.length > 10)
  32. username = username.slice(0, 10) + '...';
  33. $('#navbar_username').text(username);
  34. });
  35. $('#logout_button').click(function () {
  36. api('logout', {}, function () {
  37. popup('primary', 'Successfully logged out!');
  38. setTimeout(function () {
  39. document.location.href = '/';
  40. }, 1500);
  41. });
  42. });
  43. }
  44. });