Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

94 строки
2.6 KiB

  1. import $ from 'jquery';
  2. class IHKContact {
  3. constructor(wrapper) {
  4. this.wrapper = wrapper;
  5. this.wrapper = wrapper.addClass('initiated');
  6. this.contactsWrapper = wrapper.find('.contacts');
  7. this.contacts = this.contactsWrapper.find('.contact-person');
  8. this.current = 0;
  9. this.tabStyle = wrapper.data('tab-style');
  10. this.TABS_CLOSED = "closed";
  11. this.TABS_OPEN = "open";
  12. this.initTabs();
  13. this.initSizeObserver();
  14. }
  15. initTabs() {
  16. if (this.tabStyle === this.TABS_OPEN || this.contacts.length < 2) {
  17. this.contacts.addClass('active');
  18. return;
  19. }
  20. this.tabs = $('<ul />').addClass('tab-buttons').insertBefore(this.contactsWrapper);
  21. this.contacts.each((i, el) => {
  22. const c = $(el);
  23. const a = $('<a href="' + c.attr('id') + '" />').attr('data-index', i).text(c.data('contact-tab'));
  24. a.addClass('btn').addClass('btn-small');
  25. if (!c.data('contact-tab')) {
  26. a.text( i < 9 ? '0' + (i + 1) : i + 1 );
  27. }
  28. $('<li />').append(a).appendTo(this.tabs);
  29. });
  30. const tabAnchors = this.tabs.find('a');
  31. tabAnchors.on('click', (e) => {
  32. e.preventDefault();
  33. const btn = $(e.currentTarget);
  34. const index = parseInt(btn.attr('data-index'));
  35. this.contacts.eq(index).addClass('active').siblings('.active').removeClass('active');
  36. btn.parent('li').addClass('active').siblings('.active').removeClass('active');
  37. this.changeHeight(this.current, index);
  38. this.current = index;
  39. });
  40. if(this.tabStyle !== this.TABS_CLOSED){
  41. tabAnchors.first().trigger('click');
  42. }
  43. }
  44. changeHeight(prev, next) {
  45. if (prev === next) {
  46. return;
  47. }
  48. this.contactsWrapper.css('height', this.contacts.eq(prev).outerHeight() + 'px');
  49. setTimeout(() => {
  50. this.contactsWrapper.css('height', this.contacts.eq(next).outerHeight() + 'px');
  51. }, 40)
  52. setTimeout(() => {
  53. this.contactsWrapper.removeAttr('style').trigger('resize');
  54. }, 500)
  55. }
  56. initSizeObserver() {
  57. const observer = new ResizeObserver((entries) => {
  58. entries.map((entry) => {
  59. const w = entry.borderBoxSize[0].inlineSize;
  60. const wrapper = this.wrapper.get(0);
  61. if (w < 500) {
  62. wrapper.dataset.size = 'sm';
  63. } else if (w < 1000) {
  64. wrapper.dataset.size = 'md';
  65. } else {
  66. wrapper.dataset.size = 'lg';
  67. }
  68. })
  69. })
  70. observer.observe(this.wrapper.get(0));
  71. }
  72. }
  73. export default IHKContact;
  74. $('body').on('ihk-init dynamic-component-loaded gfi-dynamic-init', function () {
  75. $('.contact-wrapper:not(.initiated)').each(function () {
  76. new IHKContact($(this));
  77. });
  78. });