Last active
September 8, 2017 19:13
-
-
Save brzrkr/9415a1de25324509b413 to your computer and use it in GitHub Desktop.
Modification of https://github.com/micjamking/succinct to allow the plugin to show the longer versions of text on click, not quite as customizable in practice.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2013 Mike King (@micjamking) | |
* | |
* jQuery Succinct plugin | |
* Version 1.0.1 (July 2013) | |
* | |
* Licensed under the MIT License | |
*/ | |
/*global jQuery*/ | |
(function($){ | |
'use strict'; | |
$.fn.succinct = function(options){ | |
var defaults = { | |
size: 240, | |
omission: '...', | |
ignore: true | |
}, | |
options = $.extend(defaults, options); | |
return this.each(function(i, el){ | |
var textDefault, | |
textTruncated, | |
elements = $(this), | |
regex = /[!-\/:-@\[-`{-~]$/; | |
var untruncate = function(e) { | |
var link = $(e.target); | |
var parent = link.parent(); | |
// replace truncated text with untruncated text | |
parent.html(parent.data('default')); | |
// create the show less link | |
link = document.createElement('a'); | |
link.className = 'show_less'; | |
link.href = "#"; | |
link.rel = i; | |
link.innerHTML = " — show less"; | |
// add it to the end of the long text, and attach truncate event | |
parent.append(link); | |
link.onclick = truncate; | |
return false; | |
} | |
var truncate = function(){ | |
elements.each(function(){ | |
// remove any previous links, so they aren't truncated again | |
$(this).find('.show_less, .show_more').remove(); | |
// retrieve the current text and copy it for retrieval in untruncate | |
textDefault = $(this).html(); | |
$(this).data('default', textDefault); | |
if (textDefault.length > options.size) { | |
textTruncated = $.trim(textDefault). | |
substring(0, options.size). | |
split(' '). | |
slice(0, -1). | |
join(' '); | |
if (options.ignore) { | |
textTruncated = textTruncated.replace( regex , '' ); | |
} | |
// create the link to show more of the text | |
var link = document.createElement('a'); | |
link.className = 'show_more'; | |
link.href = "#"; | |
link.rel = i; | |
link.innerHTML = "read more"; | |
// replace element with truncated text | |
$(this).html(textTruncated + '... '); | |
// add link and attach untruncate event | |
$(this).append(link); | |
link.onclick = untruncate; | |
} | |
}); | |
return false; | |
}; | |
var init = function() { | |
truncate(); | |
}; | |
init(); | |
}); | |
}; | |
})(jQuery); |
Hi brzrkr, thanks for the modification! It helps a lot!
I noticed one thing:
When there are multiple truncates on the page, when you click on the second or more "read more", only the last truncate will have "read more" button, and the former "read more"s will disappear..
This is because the default
data of the every truncated item is reseted to be $(this).html();
every time when a truncate happens( line 58,59), and the already truncated item only have limited text in html, so it will not trigger the truncate.
Solved the problem with replacing line 59 with textDefault = $(this).data('default') || $(this).html();
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thx Nic !