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.
 
 
 
 

139 line
4.2 KiB

  1. import {SearchUtils} from "../utils/SearchUtils";
  2. const $ = require('jquery');
  3. class Index {
  4. constructor(section) {
  5. this.anchorLink = window.location.href.match(/#(.*)/);
  6. this.section = section.addClass('initiated');
  7. this.letterWrapper = section.find('.letters');
  8. this.letters = this.letterWrapper.find('a');
  9. this.contentWrapper = $('<div class="letter-content" />').insertAfter(this.letterWrapper);
  10. this.initContentWrappers();
  11. this.handleClicks();
  12. this.initHeightCheck();
  13. }
  14. initContentWrappers() {
  15. this.letters.each((i, el) => {
  16. const letter = $(el).attr('data-index', i);
  17. const wrapper = $('<div class="a-z-content-wrapper" />').attr('data-source', letter.attr('href'));
  18. wrapper.appendTo(this.contentWrapper);
  19. })
  20. this.contents = this.contentWrapper.children();
  21. }
  22. handleClicks() {
  23. const urlParamsObj = SearchUtils.getAllUrlParams();
  24. const letterInUrlParams = urlParamsObj.letter;
  25. const letterElementFromParams = this.section.find("[data-letter='" + letterInUrlParams + "']");
  26. this.letters.on('click', (e) => {
  27. e.preventDefault();
  28. const a = $(e.currentTarget);
  29. const index = parseInt(a.attr('data-index'));
  30. a.parent('li').addClass('active').siblings('.active').removeClass('active');
  31. this.contentWrapper.css('height', this.contentWrapper.outerHeight() + 'px');
  32. this.contents.eq(index).addClass('current').siblings('.current').removeClass('current');
  33. if (!this.contents.eq(index).hasClass('loading') && !this.contents.eq(index).hasClass('loaded')) {
  34. this.loadContent(index);
  35. } else {
  36. this.updateHeight();
  37. }
  38. });
  39. if (letterInUrlParams && letterElementFromParams.length > 0) {
  40. letterElementFromParams.find("a").trigger("click");
  41. } else {
  42. this.letters.first().trigger("click");
  43. }
  44. }
  45. initHeightCheck() {
  46. $(window).on('resize', (e) => {
  47. this.handleResize();
  48. })
  49. this.handleResize();
  50. }
  51. handleResize() {
  52. if (this.letterWrapper.height() > 60) {
  53. this.letterWrapper.addClass('not-sticky');
  54. } else {
  55. this.letterWrapper.removeClass('not-sticky');
  56. }
  57. }
  58. loadContent(index) {
  59. const t = this;
  60. const wrapper = this.contents.eq(index).addClass('loading');
  61. const outer = $('<div class="outer" />').appendTo(wrapper);
  62. const dataSource = this.section.attr('data-letter-content-url');
  63. const letterToLoad = this.letters.eq(index).parent().data("letter");
  64. const dataObj = {
  65. letter: letterToLoad
  66. };
  67. $.ajax({
  68. url: dataSource,
  69. data: dataObj,
  70. type: "GET",
  71. success: (result) => {
  72. outer.html(result);
  73. wrapper.removeClass('loading').addClass('loaded');
  74. $('body').trigger('dynamic-component-loaded');
  75. t.updateHeight();
  76. }
  77. });
  78. }
  79. updateHeight() {
  80. setTimeout(() => {
  81. this.contentWrapper.css('height', this.contentWrapper.children('.current').outerHeight() + 'px');
  82. }, 40)
  83. setTimeout(() => {
  84. this.contentWrapper.removeAttr('style');
  85. }, 500)
  86. if (this.anchorLink != null) {
  87. const anchorLinkElementText = (this.anchorLink[1].length > 10) ? this.anchorLink[1].substr(0, this.anchorLink[1].indexOf('&')) : this.anchorLink[1];
  88. const anchorLinkElement = $("#" + anchorLinkElementText);
  89. $("#" + anchorLinkElementText).css("display", "block");
  90. anchorLinkElement.parent().addClass("open");
  91. this.scrollToTarget(anchorLinkElement);
  92. this.anchorLink = null;
  93. } else {
  94. let target = this.section.offset().top - 100;
  95. if ($(window).width() < 768) {
  96. target = this.section.offset().top - 80;
  97. }
  98. if ($(window).scrollTop() > target) {
  99. $('html, body').animate({'scrollTop': target}, 500, 'easeInOutQuad');
  100. }
  101. }
  102. }
  103. scrollToTarget(anchorLinkElement) {
  104. // const anchorLinkElement = $("#" + anchorLink[1]);
  105. if (anchorLinkElement.length > 0) {
  106. $('html, body').animate({
  107. scrollTop: anchorLinkElement.first().offset().top - 250
  108. }, 200);
  109. }
  110. }
  111. }
  112. $('body').on('ihk-init dynamic-component-loaded gfi-dynamic-init', function () {
  113. $('.a-z:not(.initiated)').each(function (i) {
  114. new Index($(this));
  115. });
  116. });