From 65587efb0c080e54a22b5dc08bb9f45fbe653615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=AA=E5=AD=90?= Date: Tue, 4 Jun 2024 18:16:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- page-douban.php | 57 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/page-douban.php b/page-douban.php index e808381..a2c34d4 100644 --- a/page-douban.php +++ b/page-douban.php @@ -98,15 +98,64 @@ class APIHandler { .then(response => response.json()) .then(data => { if (data.data.length) { - this.subjects = [...this.subjects, ...data.data]; - this._renderListTemplate(); - document.querySelector(".lds-ripple").classList.add("u-hide"); - } else { + if (document.querySelector(".db--list").classList.contains("db--list__card")) + { + this.subjects = [...this.subjects, ...data.data]; + this._randerDateTemplate(); + } else { + this.subjects = [...this.subjects, ...data.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(subject => { + return `
${ + subject.is_top250 + ? 'Top 250' + : "" + }
${ + subject.douban_score > 0 + ? '' + + subject.douban_score + : "" + }${ + subject.year > 0 ? " ยท " + subject.year : "" + }
`; + }) + .join(""); + html += `
`; + } + document.querySelector(".db--list").innerHTML = html; + } _renderListTemplate() { document.querySelector(".db--list").innerHTML = this.subjects.map(subject =>