From 809530008db324de32874a0d7df72e2425f43152 Mon Sep 17 00:00:00 2001
From: "vishalsingh.qlo325"
Date: Mon, 26 Aug 2024 19:47:10 +0530
Subject: [PATCH] updated the code to count the words accoirding to their sizes
for the number of character displayed for the room block description
---
.../views/css/WkHotelRoomBlockFront.css | 4 +-
.../views/js/WkHotelRoomBlockFront.js | 67 ++++++++++---------
.../templates/hook/hotelRoomDisplayBlock.tpl | 8 +--
3 files changed, 42 insertions(+), 37 deletions(-)
diff --git a/modules/wkhotelroom/views/css/WkHotelRoomBlockFront.css b/modules/wkhotelroom/views/css/WkHotelRoomBlockFront.css
index 4e8590cfa..a0da52873 100644
--- a/modules/wkhotelroom/views/css/WkHotelRoomBlockFront.css
+++ b/modules/wkhotelroom/views/css/WkHotelRoomBlockFront.css
@@ -19,7 +19,9 @@
.htlRoomTypeDescTextContainer {
overflow: hidden;
- max-height: 65px;
+ display: -webkit-box;
+ -webkit-line-clamp: 3;
+ -webkit-box-orient: vertical;
}
.read-extra-text{
cursor: pointer;
diff --git a/modules/wkhotelroom/views/js/WkHotelRoomBlockFront.js b/modules/wkhotelroom/views/js/WkHotelRoomBlockFront.js
index 4d314099f..b021a40e4 100644
--- a/modules/wkhotelroom/views/js/WkHotelRoomBlockFront.js
+++ b/modules/wkhotelroom/views/js/WkHotelRoomBlockFront.js
@@ -20,7 +20,7 @@
$(document).ready(function() {
applyChangesToDescription();
$(window).on('resize', function() {
- cleanDescriptions();
+ resetDescriptions();
applyChangesToDescription();
});
@@ -35,7 +35,8 @@ $(document).ready(function() {
}
});
});
-function cleanDescriptions() {
+
+function resetDescriptions() {
$(document).find('.htlRoomTypeDescText').each(function() {
$(this).addClass('htlRoomTypeDescTextContainer');
$(this).html($(this).parent().find('.htlRoomTypeDescOriginal').html());
@@ -45,7 +46,7 @@ function cleanDescriptions() {
function applyChangesToDescription() {
$(document).find('.htlRoomTypeDescText').each(function() {
// checking if the display:overFlow has been applied to the description.
- if ($(this).prop('scrollHeight') - $(this).prop('clientHeight') > 15) {
+ if ($(this).prop('scrollHeight') - $(this).prop('clientHeight') > 14) {
// Only show read more and read less text if the text is wrapped.
initToggleOptions($(this)[0]);
}
@@ -54,45 +55,51 @@ function applyChangesToDescription() {
function initToggleOptions(target) {
// Getting the number of displayed characters
- var show_char = getChars(target);
+ var show_char = getVisibleWords(target);
var ellipses = "... ";
var content = $(target).html();
+ show_char = show_char - (readMoreText.length + ellipses.length + 5); // adding extra 5 chars of space
if (content.trim().length > show_char) {
// Dividing the text into two parts so that we are able to differentiate the displayed text from hidden text.
- var a = content.trim().substr(0, show_char);
- var b = content.trim().substr(show_char - content.trim().length);
+ var visibleText = content.trim().substr(0, show_char);
+ var hiddenText = content.trim().substr(show_char - content.trim().length);
// Adding the read more and read less text with the ellipses to the original text.
- var html = a + "" + ellipses + "" + b + "";
+ var html = visibleText + "" + ellipses + "" + hiddenText + "";
$(target).html(html);
}
}
-// Getting the number of characters displayed to the user.
-function getChars(target) {
+function getVisibleWords(target) {
var style = window.getComputedStyle(target, null);
- // Getting the size of the container displayed to the user, this will ignore the overflow: hidden text.
- var line_height = parseInt(style.getPropertyValue("line-height"));
- var font_size = parseInt(style.getPropertyValue("font-size"));
- var height = parseInt(style.getPropertyValue("height"));
var width = parseInt(style.getPropertyValue("width"));
+ var lines = 3; // since we are using line clamp to display only three lines.
+ var text = $(target).text();
+ var words = text.split(/\s+/); // Split text into words based on whitespace
+ var visibleWords = [];
+ var currentLineWidth = 0;
- if(isNaN(line_height)) line_height = font_size * 1.2;
- // Counting the number of the lines displayed to the user by the display:overFlow property, since this can vary depending the screen size.
- var lines = Math.ceil(height / line_height) + 1;
- // Counting the number of the characters in the lines
- var displayedCharacters = $(target).text().split('').slice(0, lines * (width / parseInt(font_size))).join('').length;
+ for (var i = 0; i < words.length; i++) {
+ var word = words[i];
+ var wordWidth = getTextWidth(word, style);
- if ($(document).width() > 991) {
- displayedCharacters += 25;
- } else if ($(document).width() > 768) {
- displayedCharacters += 35;
- } else if ($(document).width() > 570) {
- displayedCharacters += 20;
- } else if ($(document).width() > 444) {
- displayedCharacters += 10;
- } else {
- displayedCharacters += 5;
+ if (currentLineWidth + wordWidth <= width) {
+ visibleWords.push(word);
+ currentLineWidth += wordWidth + getTextWidth(' ', style); // Add space width
+ } else {
+ lines--;
+ if (lines <= 0) break;
+
+ visibleWords.push(word);
+ currentLineWidth = wordWidth + getTextWidth(' ', style); // Start a new line with the current word
+ }
}
- return displayedCharacters;
-}
\ No newline at end of file
+ return visibleWords.join(' ').length;
+}
+
+function getTextWidth(text, style) {
+ var canvas = document.createElement('canvas');
+ var context = canvas.getContext('2d');
+ context.font = `${style.getPropertyValue("font-weight")} ${style.getPropertyValue("font-size")} ${style.getPropertyValue("font-family")}`;
+ return context.measureText(text).width;
+}
diff --git a/modules/wkhotelroom/views/templates/hook/hotelRoomDisplayBlock.tpl b/modules/wkhotelroom/views/templates/hook/hotelRoomDisplayBlock.tpl
index 0daebcc80..7911888c8 100644
--- a/modules/wkhotelroom/views/templates/hook/hotelRoomDisplayBlock.tpl
+++ b/modules/wkhotelroom/views/templates/hook/hotelRoomDisplayBlock.tpl
@@ -58,12 +58,8 @@
{/if}
-
- {$roomDisplay.description|escape:'html':'UTF-8'}
-
-
- {$roomDisplay.description|escape:'html':'UTF-8'}
-
+ {$roomDisplay.description|escape:'html':'UTF-8'}
+ {$roomDisplay.description|escape:'html':'UTF-8'}