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 =>