Tasty Text Trimmer by Drew McLellan
In most cases, when designing a user interface it’s best to make a decision about how data is best displayed and stick with it. Failing to make a decision ultimately leads to too many user options, which in turn can be taxing on the poor old user.
Under some circumstances, however, it’s good to give the user freedom in customising their workspace. One good example of this is the ‘Article Length’ tool in Apple’s Safari RSS reader. Sliding a slider left of right dynamically changes the length of each article shown. It’s that kind of awesomey magic stuff that’s enough to keep you from sleeping. Let’s build one.
The Setup
Let’s take a page that has lots of long text items, a bit like a news page or like Safari’s RSS items view. If we were to attach a class name to each element we wanted to resize, that would give us something to hook onto from the JavaScript.
Example 1: The basic page.
As you can see, I’ve wrapped my items in a DIV and added a class name of chunk to them. It’s these chunks that we’ll be finding with the JavaScript. Speaking of which …
Our Core Functions
There are two main tasks that need performing in our script. The first is to find the chunks we’re going to be resizing and store their original contents away somewhere safe. We’ll need this so that if we trim the text down we’ll know what it was if the user decides they want it back again. We’ll call this loadChunks.
var loadChunks = function(){var everything = document.getElementsByTagName('*');var i, l;chunks= [];for (i=0, l=everything.length; i<l; i++){if (everything[i].className.indexOf(chunkClass) > -1){chunks.push({ref: everything[i],original: everything[i].innerHTML});}}};- Source: /code/tasty-text-trimmer/loadChunks.txt
The variable chunks is stored outside of this function so that we can access it from our next core function, which is doTrim.
var doTrim = function(interval) {if (!chunks) loadChunks();var i, l;for (i=0, l=chunks.length; i<l; i++){var a = chunks[i].original.split(' ');a = a.slice(0, interval);chunks[i].ref.innerHTML= a.join(' ');}};- Source: /code/tasty-text-trimmer/doTrim.txt
The first thing that needs to be done is to call loadChunks if the chunks variable isn’t set. This should only happen the first time doTrim is called, as from that point the chunks will be loaded.
Then all we do is loop through the chunks and trim them. The trimming itself (lines 6-8) is very simple. We split the text into an array of words (line 6), then select only a portion from the beginning of the array up until the number we want (line 7). Finally the words are glued back together (line 8).
In essense, that’s it, but it leaves us needing to know how to get the number into this function in the first place, and how that number is generated by the user. Let’s look at the latter case first.
The YUI Slider Widget
There are lots of JavaScript libraries available at the moment. A fair few of those are really good. I use the Yahoo! User Interface Library professionally, but have only recently played with their pre-build slider widget. Turns out, it’s pretty good and perfect for this task.
Once you have the library files linked in (check the docs linked above) it’s fairly straightforward to create yourself a slider.
slider = YAHOO.widget.Slider.getHorizSlider("sliderbg", "sliderthumb", 0, 100, 5);slider.setValue(50);slider.subscribe("change", doTrim);- Source: /code/tasty-text-trimmer/slider.txt
All that’s needed then is some CSS to make the slider look like a slider, and of course a few bits of HTML. We’ll see those later.
See It Working!
Rather than spell out all the nuts and bolts of implementing this fairly simple script, let’s just look at in it action and then pick on some interesting bits I’ve added.
Example 2: Try the Tasty Text Trimmer.
At the top of the JavaScript file I’ve added a small number of settings.
var chunkClass= 'chunk';var minValue= 10;var maxValue= 100;var multiplier= 5;- Source: /code/tasty-text-trimmer/settings.txt
Obvious candidates for configuration are the class name used to find the chunks, and also the some minimum and maximum values. The minValue is the fewest number of words we wish to display when the slider is all the way down. The maxValue is the length of the slider, in this case 100.
Moving the slider makes a call to our doTrim function with the current value of the slider. For a slider 100 pixels long, this is going to be in the range of 0-100. That might be okay for some things, but for longer items of text you’ll want to allow for displaying more than 100 words. I’ve accounted for this by adding in a multiplier – in my code I’m multiplying the value by 5, so a slider value of 50 shows 250 words. You’ll probably want to tailor the multiplier to the type of content you’re using.
Keeping it Accessible
This effect isn’t something we can really achieve without JavaScript, but even so we must make sure that this functionality has no adverse impact on the page when JavaScript isn’t available. This is achieved by adding the slider markup to the page from within the insertSliderHTML function.
var insertSliderHTML = function(){var s = '<a id="slider-less" href="#less"><img src="icon_min.gif" width="10" height="10" alt="Less text" class="first" /></a>';s +=' <div id="sliderbg"><div id="sliderthumb"><img src="sliderthumbimg.gif" /></div></div>';s +=' <a id="slider-more" href="#more"><img src="icon_max.gif" width="10" height="10" alt="More text" /></a>';document.getElementById('slider').innerHTML = s;}- Source: /code/tasty-text-trimmer/insertSliderHTML.txt
The other important factor to consider is that a slider can be tricky to use unless you have good eyesight and pretty well controlled motor skills. Therefore we should provide a method of changing the value by the keyboard.
I’ve done this by making the icons at either end of the slider links so they can be tabbed to. Clicking on either icon fires the appropriate JavaScript function to show more or less of the text.
In Conclusion
The upshot of all this is that without JavaScript the page just shows all the text as it normally would. With JavaScript we have a slider for trimming the text excepts that can be controlled with the mouse or with a keyboard.
If you’re like me and have just scrolled to the bottom to find the working demo, here it is again:
Trimmer for Christmas? Don’t say I never give you anything!
Your comments
Great first article! Looking forward to the next 23 ;) Nice design too….
Hi Drew,
Nice trick, works lovely and smooth – I was surprised how smooth in FF2 on Windows.
I tried to set “position: fixed” on the #slider element so that it would float above the text as you scrolled down (I gave it a background to make it stand out as well), but that must break the slider bit, because it stopped sliding.
Anyway, I’m sure with a bit more tweaking I could get it to work – cool none the less, and congrats on the first 24ways article – I look forward to the rest!
(btw, came here from seeing the link in your Twitter feed)
drew
ps: Textile formatting isn’t working very well – I tried to wrap the “position: fixed” in @s to make it into a “code” block, and it didn’t work.
Excellent! I’m glad you’re doing this again for 2006.
Neat little trick. This would be cool for blog or news archives.
I’m looking forward to this years 24 ways. Great series.
Wow, am I ever glad I left this site in the feedreader all year. Great article.
Great first article, I’m looking forward for more ;-)
The aligned design is very nice as well…
It could be better if it dinamically adds a “Read more” link that expand the current text.
I did something similar with the RSS and Atom feeds for Tamworth and Lichfield College a couple of months ago. It uses the Yahoo library, but I’m definitely not as familiar with it as Drew is. The idea was to take the user interface of the Safari RSS handling, but keep the stylised branding of the college. You’ll have to view it in IE6, as Firefox2, IE7 and Safari override the defined XSLT and show their (new) found RSS handling capabilities, which is a shame. Check out the Atom feed.
Great to see the 24ways back and love the updated features (microformats, improved about the author etc.)
Like the first feature. May try it on my blog when I do the next redesign.
David
Drew, it is a cool UI trick but do you think that non-geek real world users would actually respond well to it? To what level do users actually think about page customization or do they just come in, get what they need, and get out again? Maybe this would be good for a Web site that you go to over and over again.
Justin – that’s right. It’s just a tool, and like all tools you need to weigh up when and where it’s appropriate to use.
This is exactly what I’ve been stalling the development of for K2. W00t!
That’s an awesome interface, I’ve never seen a slider with the trim text functionality.
Ultimately useful I would think!
Nice idea ,cool thingyee!
Nice idea, but your example is broken (“YAHOO is not defined”).
Really great !Thanks!
This function looks really sweet! It’s a good thing it’s christmas again so we can get all these very nice tips :)
I do however wonder what the real advantage of this would be over having a div-tag with the css-property overflow set to hidden and which height is adjusted to hide different portions of the article? Maybe not the height should be adjusted but rather the padding or something. Wouldn’t such a solution also work? It would need the javascript controls but wouldn’t need as much javascript as this. Only curious :)
Really good article. Have to try this for sure! Allready have couple of ideas where to implement this one.
Hey, Drew, congratulations on the first 24ways Article. And a very interesting one it is, I might add!
Keep up the good work, can’t wait for all of the other articles :)
Commenting is closed for this article.
24 ways is an edgeofmyseat.com production.
Edited by Drew McLellan and Brian Suda. Possible only with the help of our wonderful authors.Grab our RSS feed or follow us on Twitter for updates. Hosted by Memset.