Download app/javascript/controllers/language_tabs_controller.js from denise-oneill-inbox-ru/Rails-8-API-Image-Processing: direct link, hf CLI and curl.
- Browser
- Download file 1.72 kB
-
https://huggingface.co/spaces/denise-oneill-inbox-ru/Rails-8-API-Image-Processing/resolve/main/app/javascript/controllers/language_tabs_controller.js
- Command line
-
hf download hf://spaces/denise-oneill-inbox-ru/Rails-8-API-Image-Processing/app/javascript/controllers/language_tabs_controller.js
-
curl -L -o language_tabs_controller.js https://huggingface.co/spaces/denise-oneill-inbox-ru/Rails-8-API-Image-Processing/resolve/main/app/javascript/controllers/language_tabs_controller.js
1.72 kB
| import { Controller } from "@hotwired/stimulus" | |
| export default class extends Controller { | |
| static targets = ["tab", "panel"] | |
| static values = { active: { type: String, default: "en" } } | |
| connect() { | |
| this.activate(this.activeValue) | |
| } | |
| switch(event) { | |
| const locale = event.currentTarget.dataset.locale | |
| this.activeValue = locale | |
| this.activate(locale) | |
| } | |
| navigate(event) { | |
| const currentIndex = this.tabTargets.indexOf(event.currentTarget) | |
| const targetIndex = { | |
| ArrowRight: (currentIndex + 1) % this.tabTargets.length, | |
| ArrowLeft: (currentIndex - 1 + this.tabTargets.length) % this.tabTargets.length, | |
| Home: 0, | |
| End: this.tabTargets.length - 1 | |
| }[event.key] | |
| if (targetIndex === undefined) return | |
| event.preventDefault() | |
| const tab = this.tabTargets[targetIndex] | |
| this.activeValue = tab.dataset.locale | |
| this.activate(this.activeValue) | |
| tab.focus() | |
| } | |
| activate(locale) { | |
| // Update tabs | |
| this.tabTargets.forEach(tab => { | |
| const isActive = tab.dataset.locale === locale | |
| if (isActive) { | |
| tab.classList.add("border-indigo-500", "text-indigo-600") | |
| tab.classList.remove("border-transparent", "text-gray-500") | |
| } else { | |
| tab.classList.remove("border-indigo-500", "text-indigo-600") | |
| tab.classList.add("border-transparent", "text-gray-500") | |
| } | |
| tab.setAttribute("aria-selected", isActive) | |
| tab.setAttribute("tabindex", isActive ? "0" : "-1") | |
| }) | |
| // Update panels | |
| this.panelTargets.forEach(panel => { | |
| if (panel.dataset.locale === locale) { | |
| panel.classList.remove("hidden") | |
| } else { | |
| panel.classList.add("hidden") | |
| } | |
| }) | |
| } | |
| } | |