Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

143 wiersze
4.2 KiB

  1. import $ from 'jquery';
  2. import {getUrlVars} from "../../_global/scripts/helpers";
  3. class IHKIndex {
  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 = getUrlVars();
  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. let dataSource = this.section.attr('data-letter-content-url');
  63. const letterToLoad = this.letters.eq(index).parent().data("letter");
  64. if (!dataSource) {
  65. dataSource = this.letters.eq(index).attr('href');
  66. }
  67. const dataObj = {
  68. letter: letterToLoad
  69. };
  70. $.ajax({
  71. url: dataSource,
  72. data: dataObj,
  73. type: "GET",
  74. success: (result) => {
  75. outer.html(result);
  76. wrapper.removeClass('loading').addClass('loaded');
  77. $('body').trigger('dynamic-component-loaded');
  78. t.updateHeight();
  79. }
  80. });
  81. }
  82. updateHeight() {
  83. setTimeout(() => {
  84. this.contentWrapper.css('height', this.contentWrapper.children('.current').outerHeight() + 'px');
  85. }, 40)
  86. setTimeout(() => {
  87. this.contentWrapper.removeAttr('style');
  88. }, 500)
  89. if (this.anchorLink != null) {
  90. const anchorLinkElementText = (this.anchorLink[1].length > 10) ? this.anchorLink[1].substr(0, this.anchorLink[1].indexOf('&')) : this.anchorLink[1];
  91. const anchorLinkElement = $("#" + anchorLinkElementText);
  92. anchorLinkElement.css("display", "block");
  93. anchorLinkElement.parent().addClass("open");
  94. this.scrollToTarget(anchorLinkElement);
  95. this.anchorLink = null;
  96. } else {
  97. let target = this.section.offset().top - 100;
  98. if ($(window).width() < 768) {
  99. target = this.section.offset().top - 80;
  100. }
  101. if ($(window).scrollTop() > target) {
  102. $('html, body').animate({'scrollTop': target}, 500, 'easeInOutQuad');
  103. }
  104. }
  105. }
  106. scrollToTarget(anchorLinkElement) {
  107. // const anchorLinkElement = $("#" + anchorLink[1]);
  108. if (anchorLinkElement.length > 0) {
  109. $('html, body').animate({
  110. scrollTop: anchorLinkElement.first().offset().top - 250
  111. }, 200);
  112. }
  113. }
  114. }
  115. export default IHKIndex;
  116. $('body').on('ihk-init dynamic-component-loaded gfi-dynamic-init', function () {
  117. $('.a-z:not(.initiated)').each(function (i) {
  118. new IHKIndex($(this));
  119. });
  120. });