Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * jQuery list slice v1.0
- *
- * Slices a list ('ul', 'ol') and shows a more/less link to either show more
- * or less 'li's respectively.
- *
- * @USAGE:
- * For a list like the following:
- *
- * <ul id="sample-list">
- * <li>Item 1</li>
- * <li>Item 2</li>
- * <li>Item 3</li>
- * </ul>
- *
- * Initiate the sliceList as follows:
- *
- * $('ul.sample-list').listSlice({
- * default_items: 2, // Set Any other options here
- * });
- */
- (function($) {
- $.fn.listSlice = function(options) {
- // Merge or Override user defined options
- options = $.extend({}, $.fn.listSlice.options, options);
- var entity = $(this);
- /**
- * Slices the initiating list to show the number of default_items
- * and append a more link to the list.
- */
- function sliceList(){
- entity.find('li').addClass('listSliceItem');
- // Make sure we do not count items in ignore_list
- ignore_list = options.ignore_list.split(',');
- $.each(ignore_list, function() {
- var class_name = '.' + $.trim(this);
- var id_name = '#' + $.trim(this);
- var obj = entity.find(class_name);
- obj.removeClass('listSliceItem');
- if (!(obj.is('li'))) {
- obj.closest('li').removeClass('listSliceItem');
- }
- var obj = entity.find(id_name);
- obj.removeClass('listSliceItem');
- if (!(obj.is('li'))) {
- obj.closest('li').removeClass('listSliceItem');
- }
- });
- $.each(entity, function() {
- var current_entity = $(this);
- var should_apply = true;
- if((current_entity.find('li.listSliceItem').length) <= (
- options.default_items)) {
- should_apply = false;
- }
- // Make sure we apply more/less only to lists that have
- // enough 'li' elements.
- if(should_apply) {
- current_entity.find('li.listSliceItem' +
- ':gt(' + (options.default_items - 1).toString() +
- ')').hide();
- current_entity.append('<' + options.link_container +
- ' class="' + options.link_container_class + '">' +
- '<a href="#!" class="listSliceMore ' +
- options.link_class + '">' + options.more_text + '</a>');
- }
- });
- }
- /**
- * uses the slideToggle method to toggle between showing more or less
- * list items in the initiated list.
- */
- function toggleMoreLess(btn){
- var dad = btn.parent().parent();
- dad.find('li.listSliceItem' +
- ':gt(' + (options.default_items - 1).toString() +
- ')').slideToggle(options.animation_time);
- btn.text(btn.text() == options.more_text ? options.less_text : options.more_text);
- }
- /**
- * Initiate the sliceList method and more link click method.
- */
- sliceList();
- $('a.listSliceMore').click( function() {
- toggleMoreLess($(this));
- return false; // Cancel Default Anchor Action.
- // This prevents appending '#!' to the end of the url
- });
- }
- // Default options
- $.fn.listSlice.options = {
- // Default number of items to be displayed (Accepts Integer only).
- default_items: 5,
- // More Anchor link's label when hiding items (Accepts Strings only).
- more_text: 'More',
- // More Anchor link's label when showing all items (Accepts Strings only).
- less_text: 'Less',
- // Class names to be applied to the More link (Accepts Strings only).
- link_class: 'more link',
- // Class names to be applied to the More link's container (Accepts Strings only).
- link_container_class: 'more',
- // An element that wraps the more link. (Accepts Strings only)
- link_container: 'li',
- // Amount of time in miliseconds the show/hide animation should run.(Accepts Integer and Strings)
- animation_time: 500,
- // Ignore 'li' items to be counted as the part of list which have following classes
- // or id's. A comma separated list of classes or id's or both. (Accepts Strings Only)
- ignore_list: 'ignore, no-include, all',
- }
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement