Sha256: aeb7bea872e8ddf3e12552859b6df61872cc3c5681ee672ac3bc30df677e0b42

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

import { Controller } from "@hotwired/stimulus";

export default class extends Controller {
  static targets = ["link", "content"]
  static values = { refreshInterval: 10000 }
  index = 0;

  connect() {
    this.showInitialTab();
    this.loopThroughTabs();
  }

  showInitialTab() {
    const initialTab = this.linkTargets[0];
    if (initialTab) {
      this.showTab(initialTab.dataset.target);
    }
  }

  loopThroughTabs() {
    this.showTab(this.linkTargets[this.index].dataset.target);

    this.interval = setInterval(() => {
      this.index = (this.index + 1) % this.linkTargets.length;
      this.showTab(this.linkTargets[this.index].dataset.target);
    }, this.refreshIntervalValue);
  }

  showTab(targetId) {
    this.resetTabs();
    const activeTab = this.linkTargets.find(link => link.dataset.target === targetId);
    const activeContent = this.contentTargets.find(content => content.dataset.target === targetId);

    if (activeTab && activeContent) {
      activeTab.classList.add("text-xl", "text-gray-100", 'bg-indigo-500', 'transition', 'duration-300', 'ease-in-out');

      activeContent.classList.remove('absolute', 'top-0', 'left-full', 'w-0', 'h-0');
      activeContent.classList.add('relative');
    }
  }

  resetTabs() {
    this.linkTargets.forEach(link => {
      link.classList.add('transition', 'duration-300', 'ease-in-out');
      link.classList.remove("text-xl", "text-gray-100", 'bg-indigo-500');
    });
    this.contentTargets.forEach(content => {
      content.classList.add('absolute', 'top-0', 'left-full', 'w-0', 'h-0');
      content.classList.remove('relative');
    });
  }


  changeTab(event) {
    event.preventDefault();
    const targetId = event.target.dataset.target;
    const newIndex = this.linkTargets.findIndex(link => link.dataset.target === targetId);
    if (newIndex >= 0) {
      this.index = newIndex;
    }
    this.showTab(targetId);

    clearInterval(this.interval);
    this.loopThroughTabs();
  }

  disconnect() {
    clearInterval(this.interval);
  }

}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mission_control-servers-0.2.1 app/javascript/mission_control/servers/controllers/tabs_controller.js