class FARALLON_DOUBAN { ver: string; type: any; finished: boolean; paged: number; genre_list: Array; subjects: Array; genre: Array; baseAPI: string = "https://node.wpista.com/v1/outer/"; token: string; constructor(config: any) { this.ver = "1.0.1"; this.type = "movie"; this.finished = false; this.paged = 1; this.genre_list = []; this.genre = []; this.subjects = []; this.token = config.token; this._create(); } on(t: any, e: any, n: any) { var a = document.querySelectorAll(e); a.forEach((item) => { item.addEventListener(t, n); }); } _fetchGenres() { document.querySelector(".db--genres")!.innerHTML = ""; fetch( this.baseAPI + "genres?token=" + this.token + "&type=" + this.type ) .then((response) => response.json()) .then((t) => { // @ts-ignore if (t.data.length) { this.genre_list = t.data; this._renderGenre(); } }); } _handleGenreClick() { this.on("click", ".db--genreItem", (t: any) => { const self = t.currentTarget as HTMLElement; if (self.classList.contains("is-active")) { const index = this.genre.indexOf(self.innerText); self.classList.remove("is-active"); this.genre.splice(index, 1); this.paged = 1; this.finished = false; this.subjects = []; this._fetchData(); return; } document.querySelector(".db--list")!.innerHTML = ""; document.querySelector(".lds-ripple")!.classList.remove("u-hide"); self.classList.add("is-active"); this.genre.push(self.innerText); this.paged = 1; this.finished = false; this.subjects = []; this._fetchData(); return; }); } _renderGenre() { document.querySelector(".db--genres")!.innerHTML = this.genre_list .map((item: any) => { return `${item.name}`; }) .join(""); this._handleGenreClick(); } _fetchData() { fetch( this.baseAPI + "faves?token=" + this.token + "&type=" + this.type + "&paged=" + this.paged + "&genre=" + JSON.stringify(this.genre) ) .then((response) => response.json()) .then((t: any) => { if (t.data.length) { if ( document .querySelector(".db--list")! .classList.contains("db--list__card") ) { this.subjects = [...this.subjects, ...t.data]; this._randerDateTemplate(); } else { this.subjects = [...this.subjects, ...t.data]; this._randerListTemplate(); } document .querySelector(".lds-ripple")! .classList.add("u-hide"); } else { this.finished = true; document .querySelector(".lds-ripple")! .classList.add("u-hide"); } }); } _randerDateTemplate() { const result = this.subjects.reduce((result, item) => { const date = new Date(item.create_time); const year = date.getFullYear(); const month = date.getMonth() + 1; const key = `${year}-${month.toString().padStart(2, "0")}`; if (Object.prototype.hasOwnProperty.call(result, key)) { result[key].push(item); } else { result[key] = [item]; } return result; }, {}); let html = ``; for (let key in result) { const date = key.split("-"); html += `
${date[1]}
${date[0]}
`; html += result[key] .map((movie: any) => { return `
${ movie.is_top250 ? 'Top 250' : "" }
${ movie.douban_score > 0 ? '' + movie.douban_score : "" }${ movie.year > 0 ? " · " + movie.year : "" }
`; }) .join(""); html += `
`; } document.querySelector(".db--list")!.innerHTML = html; } _randerListTemplate() { document.querySelector(".db--list")!.innerHTML = this.subjects .map((item: any) => { return `
${ item.is_top250 ? 'Top 250' : "" }
${ item.create_time }
${ item.douban_score > 0 ? '' + item.douban_score : "" }${ item.year > 0 ? " · " + item.year : "" }
`; }) .join(""); } _handleScroll() { window.addEventListener("scroll", () => { var t = window.scrollY || window.pageYOffset; const moreElement = document.querySelector( ".block-more" ) as HTMLElement; if ( moreElement.offsetTop + -window.innerHeight < t && document .querySelector(".lds-ripple")! .classList.contains("u-hide") && !this.finished ) { document .querySelector(".lds-ripple")! .classList.remove("u-hide"); this.paged++; this._fetchData(); } }); } _handleNavClick() { this.on("click", ".db--navItem", (t: any) => { if (t.currentTarget.classList.contains("current")) return; this.genre = []; this.type = t.currentTarget.dataset.type; if (this.type != "book") { this._fetchGenres(); document .querySelector(".db--genres") ?.classList.remove("u-hide"); } else { document.querySelector(".db--genres")!.classList.add("u-hide"); } document.querySelector(".db--list")!.innerHTML = ""; document.querySelector(".lds-ripple")!.classList.remove("u-hide"); document .querySelector(".db--navItem.current")! .classList.remove("current"); const self = t.target; self.classList.add("current"); this.paged = 1; this.finished = false; this.subjects = []; this._fetchData(); }); } _create() { if (document.querySelector(".db--container")) { const container = document.querySelector( ".db--container" ) as HTMLElement; if (container.dataset.token) { this.token = container.dataset.token; } else { return; } const currentNavItem = document.querySelector( ".db--navItem.current" ); if (currentNavItem instanceof HTMLElement) { this.type = currentNavItem.dataset.type; } const currentType = document.querySelector( ".db--list" ) as HTMLElement; if (currentType.dataset.type) this.type = currentType.dataset.type; if (this.type == "movie") { document .querySelector(".db--genres")! .classList.remove("u-hide"); } this._fetchGenres(); this._fetchData(); this._handleScroll(); this._handleNavClick(); } if (document.querySelector(".js-db")) { document.querySelectorAll(".js-db").forEach((item: any) => { const db = item; const id = db.dataset.id; const type = db.dataset.type; const nodeParent = db.parentNode as HTMLElement; fetch( // @ts-ignore this.baseAPI + `${type}/${id}?token=${this.token}` ).then((response) => { response.json().then((t) => { if (t.data) { const data = t.data; const node = document.createElement("div"); node.classList.add("doulist-item"); node.innerHTML = `
${data.douban_score}
${data.card_subtitle}
`; nodeParent.replaceWith(node); } }); }); }); } if (document.querySelector(".db--collection")) { document .querySelectorAll(".db--collection") .forEach((item: any) => { this._fetchCollection(item); }); } } _fetchCollection(item: any) { const type = item.dataset.style ? item.dataset.style : "card"; fetch( // @ts-ignore obvInit.api + "v1/movies?type=" + item.dataset.type + "&paged=1&genre=&start_time=" + item.dataset.start + "&end_time=" + item.dataset.end ) .then((response) => response.json()) .then((t: any) => { if (t.length) { if (type == "card") { item.innerHTML += t .map((movie: any) => { return `
Marked ${ movie.create_time }
${ movie.douban_score }
${ movie.remark || movie.card_subtitle }
`; }) .join(""); } else { const result = t.reduce((result: any, item: any) => { if ( Object.prototype.hasOwnProperty.call( result, item.create_time ) ) { result[item.create_time].push(item); } else { result[item.create_time] = [item]; } return result; }, {}); let html = ``; for (let key in result) { html += `
${key}
`; html += result[key] .map((movie: any) => { return `
${ movie.douban_score }
${movie.remark || movie.card_subtitle}
`; }) .join(""); html += `
`; } item.innerHTML = html; } } }); } } new FARALLON_DOUBAN({ // @ts-ignore token: window.WPD_TOKEN, });