169 lines
5.7 KiB
JavaScript
169 lines
5.7 KiB
JavaScript
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,
|
|
season: String,
|
|
episode: String,
|
|
active: Boolean,
|
|
};
|
|
|
|
static targets = ['list', 'count', 'episodeSelector']
|
|
static outlets = ['loading-icon']
|
|
|
|
options = []
|
|
optionsLoaded = false
|
|
|
|
async connect() {
|
|
await this.setOptions();
|
|
}
|
|
|
|
async setOptions() {
|
|
if (this.optionsLoaded === false) {
|
|
this.optionsLoaded = true;
|
|
await fetch(`/torrentio/tvshows/${this.tmdbIdValue}/${this.imdbIdValue}/${this.seasonValue}/${this.episodeValue}`)
|
|
.then(res => res.text())
|
|
.then(response => {
|
|
this.element.innerHTML = response;
|
|
this.options = this.element.querySelectorAll('tbody tr');
|
|
if (this.options.length > 0) {
|
|
this.options.forEach((option) => option.querySelector('.download-btn').dataset['title'] = this.titleValue);
|
|
this.options[0].querySelector('input[type="checkbox"]').checked = true;
|
|
} else {
|
|
this.episodeSelectorTarget.disabled = true;
|
|
}
|
|
this.loadingIconOutlet.increaseCount();
|
|
});
|
|
}
|
|
}
|
|
|
|
//
|
|
// async clearCache() {
|
|
// await fetch(`/torrentio/tvshows/clear/${this.tmdbIdValue}/${this.imdbIdValue}/${this.seasonValue}/${this.episodeValue}`)
|
|
// .then(res => res.text())
|
|
// .then(response => {});
|
|
// }
|
|
|
|
async setActive() {
|
|
this.activeValue = true;
|
|
// this.element.classList.remove('hidden');
|
|
if (false === this.optionsLoaded) {
|
|
await this.setOptions();
|
|
}
|
|
}
|
|
|
|
async setInActive() {
|
|
this.activeValue = false;
|
|
// if (true === this.hasEpisodeSelectorTarget()) {
|
|
this.episodeSelectorTarget.checked = false;
|
|
// }
|
|
// this.element.classList.add('hidden');
|
|
}
|
|
|
|
isActive() {
|
|
return this.activeValue;
|
|
}
|
|
|
|
isSelected() {
|
|
return this.episodeSelectorTarget.checked;
|
|
}
|
|
|
|
selectEpisodeForDownload() {
|
|
if (true === this.isActive() && this.episodeSelectorTarget.disabled === false) {
|
|
this.episodeSelectorTarget.checked = !this.episodeSelectorTarget.checked;
|
|
}
|
|
}
|
|
|
|
toggleList() {
|
|
this.listTarget.classList.toggle('hidden');
|
|
}
|
|
|
|
download() {
|
|
this.options.forEach(option => {
|
|
const optionSelector = option.querySelector('input[type="checkbox"]');
|
|
if (true === optionSelector.checked) {
|
|
const downloadBtn = option.querySelector('button.download-btn');
|
|
const downloadBtnController = this.application.getControllerForElementAndIdentifier(downloadBtn, 'download-button');
|
|
downloadBtnController.download();
|
|
optionSelector.checked = false;
|
|
this.episodeSelectorTarget.checked = false;
|
|
}
|
|
})
|
|
}
|
|
|
|
async filter(activeFilter, currentSeason, newSeason) {
|
|
if (currentSeason !== activeFilter['season']) {
|
|
if (this.seasonValue === newSeason) {
|
|
await this.setActive();
|
|
} else {
|
|
await this.setInActive();
|
|
}
|
|
}
|
|
|
|
if (false === this.isActive()) {
|
|
return;
|
|
}
|
|
|
|
let firstIncluded = true;
|
|
let count = 0;
|
|
let selectedCount = 0;
|
|
|
|
this.options.forEach((option) => {
|
|
const props = {
|
|
"resolution": option.querySelector('#resolution').textContent.trim(),
|
|
"codec": option.querySelector('#codec').textContent.trim(),
|
|
"provider": option.querySelector('#provider').textContent.trim(),
|
|
"languages": JSON.parse(option.dataset['languages']),
|
|
}
|
|
|
|
let include = true;
|
|
option.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.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.countTarget.innerText = count;
|
|
});
|
|
}
|
|
}
|