From 1343472ca0da73630431a6061d8121cc36f64f37 Mon Sep 17 00:00:00 2001 From: mb863 Date: Mon, 24 Jan 2011 21:21:09 +0000 Subject: [PATCH] NIHVIVO-1315: Added jQuery HTML Truncator for for showing "more"/"less" content with core:overview data property --- productMods/js/individual/individualUtils.js | 8 ++ .../js/jquery_plugins/jquery.truncator.js | 98 +++++++++++++++++++ .../freemarker/body/individual/individual.ftl | 6 +- 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 productMods/js/individual/individualUtils.js create mode 100644 productMods/js/jquery_plugins/jquery.truncator.js diff --git a/productMods/js/individual/individualUtils.js b/productMods/js/individual/individualUtils.js new file mode 100644 index 00000000..29b0b582 --- /dev/null +++ b/productMods/js/individual/individualUtils.js @@ -0,0 +1,8 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +$(document).ready(function(){ + + // "more"/"less" HTML truncator for showing more or less content in data property core:overview + $('.individual-overview').truncate({max_length: 500}); + +}); \ No newline at end of file diff --git a/productMods/js/jquery_plugins/jquery.truncator.js b/productMods/js/jquery_plugins/jquery.truncator.js new file mode 100644 index 00000000..e68fa6f1 --- /dev/null +++ b/productMods/js/jquery_plugins/jquery.truncator.js @@ -0,0 +1,98 @@ +// HTML Truncator for jQuery +// by Henrik Nyh 2008-02-28. +// Free to modify and redistribute with credit. + +(function($) { + + var trailing_whitespace = true; + + $.fn.truncate = function(options) { + + var opts = $.extend({}, $.fn.truncate.defaults, options); + + $(this).each(function() { + + var content_length = $.trim(squeeze($(this).text())).length; + if (content_length <= opts.max_length) + return; // bail early if not overlong + + var actual_max_length = opts.max_length - opts.more.length - 3; // 3 for " ()" + var truncated_node = recursivelyTruncate(this, actual_max_length); + var full_node = $(this).hide(); + + truncated_node.insertAfter(full_node); + + findNodeForMore(truncated_node).append(' ('+opts.more+')'); + findNodeForLess(full_node).append(' ('+opts.less+')'); + + truncated_node.find('a:last').click(function() { + truncated_node.hide(); full_node.show(); return false; + }); + full_node.find('a:last').click(function() { + truncated_node.show(); full_node.hide(); return false; + }); + + }); + } + + // Note that the " (…more)" bit counts towards the max length – so a max + // length of 10 would truncate "1234567890" to "12 (…more)". + $.fn.truncate.defaults = { + max_length: 100, + more: '+', + less: '-' + }; + + function recursivelyTruncate(node, max_length) { + return (node.nodeType == 3) ? truncateText(node, max_length) : truncateNode(node, max_length); + } + + function truncateNode(node, max_length) { + var node = $(node); + var new_node = node.clone().empty(); + var truncatedChild; + node.contents().each(function() { + var remaining_length = max_length - new_node.text().length; + if (remaining_length == 0) return; // breaks the loop + truncatedChild = recursivelyTruncate(this, remaining_length); + if (truncatedChild) new_node.append(truncatedChild); + }); + return new_node; + } + + function truncateText(node, max_length) { + var text = squeeze(node.data); + if (trailing_whitespace) // remove initial whitespace if last text + text = text.replace(/^ /, ''); // node had trailing whitespace. + trailing_whitespace = !!text.match(/ $/); + var text = text.slice(0, max_length); + // Ensure HTML entities are encoded + // http://debuggable.com/posts/encode-html-entities-with-jquery:480f4dd6-13cc-4ce9-8071-4710cbdd56cb + text = $('
').text(text).html(); + return text; + } + + // Collapses a sequence of whitespace into a single space. + function squeeze(string) { + return string.replace(/\s+/g, ' '); + } + + // Finds the last, innermost block-level element + function findNodeForMore(node) { + var $node = $(node); + var last_child = $node.children(":last"); + if (!last_child) return node; + var display = last_child.css('display'); + if (!display || display=='inline') return $node; + return findNodeForMore(last_child); + }; + + // Finds the last child if it's a p; otherwise the parent + function findNodeForLess(node) { + var $node = $(node); + var last_child = $node.children(":last"); + if (last_child && last_child.is('p')) return last_child; + return node; + }; + +})(jQuery); diff --git a/productMods/templates/freemarker/body/individual/individual.ftl b/productMods/templates/freemarker/body/individual/individual.ftl index 896d4280..f7523600 100644 --- a/productMods/templates/freemarker/body/individual/individual.ftl +++ b/productMods/templates/freemarker/body/individual/individual.ftl @@ -12,4 +12,8 @@ -<#include "individual-vitro.ftl"> \ No newline at end of file +<#include "individual-vitro.ftl"> + +${headScripts.add("/js/jquery_plugins/jquery.truncator.js")} + +${scripts.add("/js/individual/individualUtils.js")} \ No newline at end of file