Guest User

Untitled

a guest
May 23rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. export default class TabSwitcher {
  2. constructor(info) {
  3. this.params = {
  4. buttonSwitch: info.buttonSwitch || true,
  5. tabs: info.tabs,
  6. buttons: info.buttons,
  7. };
  8. }
  9.  
  10. init() {
  11. const { buttonSwitch, tabs, buttons } = this.params;
  12.  
  13. for (let i = 0; i < buttons.length; i += 1) {
  14. const currBtn = buttons[i];
  15. const currTab = tabs[i];
  16. const { dataset } = currTab;
  17. let currTabID = 0;
  18.  
  19. for (let x = 0; x < dataset.length; x += 1) {
  20. currTabID = dataset[x];
  21. }
  22.  
  23. currBtn.addEventListener('click', (e) => {
  24. const { currentTarget } = e;
  25. const targetData = currentTarget.dataset;
  26. const activeItems = [];
  27.  
  28. let tabID = 0;
  29. for (let x = 0; x < targetData.length; x += 1) {
  30. tabID = targetData[x];
  31. }
  32.  
  33. for (let x = 0; x < tabs.length; x += 1) {
  34. const activeTab = tabs[x].classList.contains('active');
  35. const activeButton = buttons[x].classList.contains('active');
  36. if (activeTab) activeItems.push(tabs[x]);
  37. if (activeButton) activeItems.push(buttons[x]);
  38. }
  39.  
  40. for (let x = 0; x < activeItems.length; x += 1) {
  41. const item = activeItems[x];
  42. const itemData = item.dataset;
  43.  
  44. let isButton = false;
  45. let dataAttr = 0;
  46.  
  47. for (let y = 0; y < itemData.length; y += 1) {
  48. dataAttr = itemData[y];
  49. if (dataAttr.match(/Btn/)) isButton = true;
  50. }
  51.  
  52. const isButtonChange = isButton && buttonSwitch;
  53. if (isButtonChange || !isButton) item.classList.remove('active');
  54. }
  55.  
  56. if (currTabID === tabID) currTab.classList.add('active');
  57. if (buttonSwitch) currentTarget.classList.add('active');
  58. });
  59. }
  60. }
  61. }
Add Comment
Please, Sign In to add comment