You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

131 lines
3.5 KiB

  1. /**
  2. * Sets up the behaviour of direct menu links
  3. *
  4. * @author Jana Deutschlaender <deutschlaender@cosmocode.de>
  5. */
  6. (function($) {
  7. var $body,
  8. /**
  9. * Register the click handler for the direct links
  10. * should scroll to the page area whether there is a fixed magic matcher bar or not
  11. *
  12. * @param $directMenu
  13. */
  14. scrollingForDirectNav = function($directMenu) {
  15. $body = $('body');
  16. checkAnchorsOnLoad($directMenu);
  17. registerClickForDirectLinks($directMenu);
  18. },
  19. /**
  20. * register click event listener for direct links
  21. * @param $menu
  22. */
  23. registerClickForDirectLinks = function($menu) {
  24. $menu.find('a').on('click', function (e) {
  25. e.stopPropagation();
  26. var target = $(this).attr('href');
  27. tasksBeforeScrolling(target);
  28. scrollToTarget(target);
  29. });
  30. },
  31. /**
  32. * scroll to / set focus to target of direct link if value of location hash equals direct link
  33. * @param $menu
  34. */
  35. checkAnchorsOnLoad = function($menu) {
  36. var hash = window.location.hash;
  37. if (hash) {
  38. $menu.find('a').each(function() {
  39. var target = $(this).attr('href');
  40. if(hash === target) {
  41. tasksBeforeScrolling(target);
  42. scrollToTarget(target);
  43. setFocusOnLoad(target);
  44. }
  45. });
  46. }
  47. },
  48. /**
  49. * todos that needs to be done before the scrolling can start
  50. * @param target
  51. */
  52. tasksBeforeScrolling = function(target) {
  53. switch (target) {
  54. case '#qsearch__in':
  55. showSearchField(target);
  56. break;
  57. case '#dokuwiki__usertools':
  58. $(target).find('li:first-child').find('a').focus();
  59. break;
  60. }
  61. },
  62. /**
  63. * set focus on target or first link found in target
  64. * @param target
  65. */
  66. setFocusOnLoad = function(target) {
  67. var $target = $(target);
  68. switch (target) {
  69. case '#qsearch__in':
  70. case '#spr__toggle-content':
  71. $target.focus();
  72. break;
  73. case '#dokuwiki__usertools':
  74. break;
  75. default:
  76. $target.attr('tabindex',0);
  77. $target.focus();
  78. }
  79. },
  80. /**
  81. * trigger content toggle link to make the search field visible otherwise it neither be used for scrolling nor
  82. * for focus setting
  83. * @param target
  84. */
  85. showSearchField = function(target) {
  86. if($body.hasClass('wide-content')) {
  87. $('#spr__toggle-content').trigger('click');
  88. }
  89. },
  90. /**
  91. * scrolls to the target with an offset of 60px
  92. * @param target
  93. */
  94. scrollToTarget = function(target) {
  95. // scroll to each target
  96. $(target).velocity('scroll', {
  97. duration: 400,
  98. offset: -60,
  99. easing: 'ease-in-out'
  100. });
  101. };
  102. $(function(){
  103. var $directMenu = $('#spr__direct');
  104. if (!$directMenu.length) return;
  105. scrollingForDirectNav($directMenu);
  106. });
  107. })(jQuery);