From 09d7a7b142debaefcb91eaa95793ab557448f72c Mon Sep 17 00:00:00 2001 From: doubaniux Date: Sun, 5 Jul 2020 20:52:30 +0800 Subject: [PATCH] add table of contents field to book entity --- books/forms.py | 2 ++ books/models.py | 1 + books/templates/books/detail.html | 15 ++++++++++++--- common/static/js/detail.js | 28 ++++++++++++++-------------- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/books/forms.py b/books/forms.py index 274c8e41..1ed3125d 100644 --- a/books/forms.py +++ b/books/forms.py @@ -40,6 +40,7 @@ class BookForm(forms.ModelForm): 'pages', 'cover', 'brief', + 'contents', 'other_info', ] labels = { @@ -58,6 +59,7 @@ class BookForm(forms.ModelForm): 'pages': _("页数"), 'cover': _("封面"), 'brief': _("简介"), + 'contents': _("目录"), 'other_info': _("其他信息"), } diff --git a/books/models.py b/books/models.py index f3b0f721..1b804de6 100644 --- a/books/models.py +++ b/books/models.py @@ -49,6 +49,7 @@ class Book(Resource): isbn = models.CharField(_("ISBN"), blank=True, null=True, max_length=20, unique=True, db_index=True) # to store previously scrapped data cover = models.ImageField(_("cover picture"), upload_to=book_cover_path, default=DEFAULT_BOOK_IMAGE, blank=True) + contents = models.TextField(blank=True, default="") class Meta: # more info: https://docs.djangoproject.com/en/2.2/ref/models/options/ diff --git a/books/templates/books/detail.html b/books/templates/books/detail.html index c74d8629..76b9fad1 100644 --- a/books/templates/books/detail.html +++ b/books/templates/books/detail.html @@ -105,22 +105,31 @@
-
+
{% trans '简介' %}
{% if book.brief %} -

{{ book.brief | linebreaksbr }}

- {% else %}
{% trans '暂无简介' %}
{% endif %}
+ + {% if book.contents %} +
+
{% trans '目录' %}
+

{{ book.contents | linebreaksbr }}

+ +
+ {% endif %} +
{% trans '这本书的标记' %}
{% if mark_list_more %} diff --git a/common/static/js/detail.js b/common/static/js/detail.js index 7a7251c3..1a79e38f 100644 --- a/common/static/js/detail.js +++ b/common/static/js/detail.js @@ -103,22 +103,22 @@ $(document).ready( function() { // hide long text - let copy = $(".entity-desc__content").clone() - .addClass('entity-desc__content--folded') - .css("visibility", "hidden"); - - $(".entity-desc__content").after(copy); - if ($(".entity-desc__content").height() > copy.height()) { - $(".entity-desc__content").addClass('entity-desc__content--folded'); - $(".entity-desc__unfold-button").removeClass("entity-desc__unfold-button--hidden"); - console.log($(".entity-desc__content").height()) - console.log(copy.height()) - } - copy.remove(); + $(".entity-desc__content").each(function() { + let copy = $(this).clone() + .addClass('entity-desc__content--folded') + .css("visibility", "hidden"); + $(this).after(copy); + if ($(this).height() > copy.height()) { + $(this).addClass('entity-desc__content--folded'); + $(this).siblings(".entity-desc__unfold-button").removeClass("entity-desc__unfold-button--hidden"); + } + copy.remove(); + }); // expand hidden long text $(".entity-desc__unfold-button a").click(function() { - $(".entity-desc__content").removeClass('entity-desc__content--folded'); - $(".entity-desc__unfold-button").remove(); + $(this).parent().siblings(".entity-desc__content").removeClass('entity-desc__content--folded'); + $(this).parent(".entity-desc__unfold-button").remove(); }); + }); \ No newline at end of file