import { Controller } from '@hotwired/stimulus'; /* * The following line makes this controller "lazy": it won't be downloaded until needed * See https://github.com/symfony/stimulus-bridge#lazy-controllers */ /* stimulusFetch: 'lazy' */ export default class extends Controller { H264_CODECS = ['h264', 'h.264', 'x264'] H265_CODECS = ['h265', 'h.265', 'x265', 'hevc'] static values = { title: String, tmdbId: String, imdbId: String }; static targets = ['list'] static outlets = ['loading-icon'] options = [] optionsLoaded = false resultCountEl = null async connect() { this.resultCountEl = document.querySelector('#movie_results_count'); } async listTargetConnected() { this.optionsLoaded = true; this.options = this.element.querySelectorAll('tbody tr'); this.options.forEach((option) => option.querySelector('.download-btn').dataset['title'] = this.titleValue); this.dispatch('optionsLoaded', {detail: {options: this.options}}) this.loadingIconOutlet.toggleIcon(); this.resultCountEl.innerText = this.options.length; } // Keeps compatible with Filter & TV Shows isActive() { return true; } async filter(activeFilter) { let firstIncluded = true; let count = 0; let selectedCount = 0; this.options.forEach((option) => { const optionHeader = document.querySelector(`[data-option-id="${option.dataset['localId']}"]`) const props = { "resolution": option.querySelector('#resolution').textContent.trim(), "codec": option.querySelector('#codec').textContent.trim(), "provider": option.querySelector('#provider').textContent.trim(), "quality": option.dataset['quality'], "languages": JSON.parse(option.dataset['languages']), } let include = true; option.classList.add('r-tablerow'); option.classList.remove('hidden'); optionHeader.classList.add('r-tablerow'); optionHeader.classList.remove('hidden'); option.querySelector('input[type="checkbox"]').checked = false; for (let [key, value] of Object.entries(activeFilter)) { if (value === "" || key === "season") { continue; } if (key === "codec" && value === "h264") { if (!this.H264_CODECS.includes(props[key].toLowerCase())) { include = false; } } else if (key === "codec" && value === "h265") { if (!this.H265_CODECS.includes(props[key].toLowerCase())) { include = false; } } else if (key === "language") { if (!props["languages"].includes(value)) { include = false; } } else if (props[key] !== value) { include = false; } } if (false === include) { option.classList.remove('r-tablerow'); option.classList.add('hidden'); optionHeader.classList.remove('r-tablerow'); optionHeader.classList.add('hidden'); } else if (true === firstIncluded) { count = 1; selectedCount = selectedCount + 1; option.querySelector('input[type="checkbox"]').checked = true; firstIncluded = false; } else { count = count + 1; } }); this.resultCountEl.innerText = count; } }