From 0a83a24ab2a3daf945c6012e80d284304a73c38b Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Mon, 16 Apr 2018 11:07:46 +0200 Subject: [PATCH 1/1] book abstracts - frontend --- .../templates/catalogue/book_short.html | 5 + src/wolnelektury/settings/static.py | 1 + src/wolnelektury/static/js/base.js | 8 + .../static/js/contrib/jquery.shorten.js | 150 ++++++++++++++++++ .../static/scss/main/book_box.scss | 15 +- 5 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 src/wolnelektury/static/js/contrib/jquery.shorten.js diff --git a/src/catalogue/templates/catalogue/book_short.html b/src/catalogue/templates/catalogue/book_short.html index a238904ab..bf3a4b260 100644 --- a/src/catalogue/templates/catalogue/book_short.html +++ b/src/catalogue/templates/catalogue/book_short.html @@ -94,6 +94,11 @@ {% endif %} {% endwith %} + {% if book.abstract %} +
+ {{ book.abstract|safe }} +
+ {% endif %} {% book_shelf_tags book.pk %} diff --git a/src/wolnelektury/settings/static.py b/src/wolnelektury/settings/static.py index 17ac2bc95..8be119efb 100644 --- a/src/wolnelektury/settings/static.py +++ b/src/wolnelektury/settings/static.py @@ -93,6 +93,7 @@ PIPELINE = { 'js/contrib/jquery.countdown-de.js', 'js/contrib/jquery.countdown-uk.js', 'js/contrib/jquery.countdown-es.js', 'js/contrib/jquery.countdown-lt.js', 'js/contrib/jquery.countdown-ru.js', 'js/contrib/jquery.countdown-fr.js', + 'js/contrib/jquery.shorten.js', 'js/contrib/jquery-ui-1.8.16.custom.min.js', diff --git a/src/wolnelektury/static/js/base.js b/src/wolnelektury/static/js/base.js index 1cb62a485..bdb5fbee7 100644 --- a/src/wolnelektury/static/js/base.js +++ b/src/wolnelektury/static/js/base.js @@ -57,6 +57,14 @@ }) }); + $('.more').each(function () { + $(this).shorten({ + showChars: 150, + moreText: "więcej", + lessText: "mniej" + }); + }); + (function() { diff --git a/src/wolnelektury/static/js/contrib/jquery.shorten.js b/src/wolnelektury/static/js/contrib/jquery.shorten.js new file mode 100644 index 000000000..f8cede4d2 --- /dev/null +++ b/src/wolnelektury/static/js/contrib/jquery.shorten.js @@ -0,0 +1,150 @@ +/* + * jQuery Shorten plugin 1.1.0 + * + * Copyright (c) 2014 Viral Patel + * http://viralpatel.net + * + * Licensed under the MIT license: + * http://www.opensource.org/licenses/mit-license.php + */ + +/* +** updated by Jeff Richardson +** Updated to use strict, +** IE 7 has a "bug" It is returning undefined when trying to reference string characters in this format +** content[i]. IE 7 allows content.charAt(i) This works fine in all modern browsers. +** I've also added brackets where they weren't added just for readability (mostly for me). +*/ + +(function($) { + $.fn.shorten = function(settings) { + "use strict"; + + var config = { + showChars: 100, + minHideChars: 10, + ellipsesText: "...", + moreText: "more", + lessText: "less", + onLess: function() {}, + onMore: function() {}, + errMsg: null, + force: false + }; + + if (settings) { + $.extend(config, settings); + } + + if ($(this).data('jquery.shorten') && !config.force) { + return false; + } + $(this).data('jquery.shorten', true); + + $(document).off("click", '.morelink'); + + $(document).on({ + click: function() { + + var $this = $(this); + if ($this.hasClass('less')) { + $this.removeClass('less'); + $this.html(config.moreText); + $this.parent().prev().hide(); + $this.parent().prev().prev().show(); + config.onLess(); + } else { + $this.addClass('less'); + $this.html(config.lessText); + $this.parent().prev().show(); + $this.parent().prev().prev().hide(); + config.onMore(); + } + return false; + } + }, '.morelink'); + + return this.each(function() { + var $this = $(this); + + var content = $this.html(); + var contentlen = $this.text().length; + if (contentlen > config.showChars + config.minHideChars) { + var c = content.substr(0, config.showChars); + if (c.indexOf('<') >= 0) // If there's HTML don't want to cut it + { + var inTag = false; // I'm in a tag? + var bag = ''; // Put the characters to be shown here + var countChars = 0; // Current bag size + var openTags = []; // Stack for opened tags, so I can close them later + var tagName = null; + + for (var i = 0, r = 0; r <= config.showChars; i++) { + if (content[i] == '<' && !inTag) { + inTag = true; + + // This could be "tag" or "/tag" + tagName = content.substring(i + 1, content.indexOf('>', i)); + + // If its a closing tag + if (tagName[0] == '/') { + + + if (tagName != '/' + openTags[0]) { + config.errMsg = 'ERROR en HTML: the top of the stack should be the tag that closes'; + } else { + openTags.shift(); // Pops the last tag from the open tag stack (the tag is closed in the retult HTML!) + } + + } else { + // There are some nasty tags that don't have a close tag like
+ if (tagName.toLowerCase() != 'br') { + openTags.unshift(tagName); // Add to start the name of the tag that opens + } + } + } + if (inTag && content[i] == '>') { + inTag = false; + } + + if (inTag) { bag += content.charAt(i); } // Add tag name chars to the result + else { + r++; + if (countChars <= config.showChars) { + bag += content.charAt(i); // Fix to ie 7 not allowing you to reference string characters using the [] + countChars++; + } else // Now I have the characters needed + { + if (openTags.length > 0) // I have unclosed tags + { + //console.log('They were open tags'); + //console.log(openTags); + for (j = 0; j < openTags.length; j++) { + //console.log('Cierro tag ' + openTags[j]); + bag += ''; // Close all tags that were opened + + // You could shift the tag from the stack to check if you end with an empty stack, that means you have closed all open tags + } + break; + } + } + } + } + c = $('
').html(bag + '' + config.ellipsesText + '').html(); + }else{ + c+=config.ellipsesText; + } + + var html = '
' + c + + '
' + content + + '
' + config.moreText + ''; + + $this.html(html); + $this.find(".allcontent").hide(); // Hide all text + $('.shortcontent p:last', $this).css('margin-bottom', 0); //Remove bottom margin on last paragraph as it's likely shortened + } + }); + + }; + +})(jQuery); diff --git a/src/wolnelektury/static/scss/main/book_box.scss b/src/wolnelektury/static/scss/main/book_box.scss index 726bb5a21..1554b6a84 100755 --- a/src/wolnelektury/static/scss/main/book_box.scss +++ b/src/wolnelektury/static/scss/main/book_box.scss @@ -297,7 +297,8 @@ .book-box-head, .tags, - .book-box-tools { + .book-box-tools, + .abstract { @include min-screen($S_BOOK_SHORT_FULL) { margin-left: 154px; } @@ -372,6 +373,18 @@ } } + .abstract { + @include size(font-size, 16px); + line-height: 1.2em; + @include size(margin-bottom, 18px); + @include size(margin-top, 18px); + + p.paragraph { + @include size(margin-bottom, 9px); + @include size(margin-top, 9px); + } + } + } .book-box-tools { -- 2.20.1