Advertisement
Guest User

Untitled

a guest
May 10th, 2013
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * jQuery list slice v1.0
  3.  *
  4.  * Slices a list ('ul', 'ol') and shows a more/less link to either show more
  5.  * or less 'li's respectively.
  6.  *
  7.  * @USAGE:
  8.  *      For a list like the following:
  9.  *
  10.  *      <ul id="sample-list">
  11.  *          <li>Item 1</li>
  12.  *          <li>Item 2</li>
  13.  *          <li>Item 3</li>
  14.  *      </ul>
  15.  *
  16.  *      Initiate the sliceList as follows:
  17.  *
  18.  *      $('ul.sample-list').listSlice({
  19.  *          default_items: 2, // Set Any other options here
  20.  *      });
  21.  */
  22.  
  23. (function($) {
  24.     $.fn.listSlice = function(options) {
  25.         // Merge or Override user defined options
  26.         options = $.extend({}, $.fn.listSlice.options, options);
  27.  
  28.         var entity = $(this);
  29.  
  30.         /**
  31.          * Slices the initiating list to show the number of default_items
  32.          * and append a more link to the list.
  33.          */
  34.         function sliceList(){
  35.  
  36.             entity.find('li').addClass('listSliceItem');
  37.  
  38.             // Make sure we do not count items in ignore_list
  39.             ignore_list = options.ignore_list.split(',');
  40.             $.each(ignore_list, function() {
  41.                 var class_name = '.' + $.trim(this);
  42.                 var id_name = '#' + $.trim(this);
  43.  
  44.                 var obj = entity.find(class_name);
  45.                 obj.removeClass('listSliceItem');
  46.                 if (!(obj.is('li'))) {
  47.                     obj.closest('li').removeClass('listSliceItem');
  48.                 }
  49.  
  50.                 var obj = entity.find(id_name);
  51.                 obj.removeClass('listSliceItem');
  52.                 if (!(obj.is('li'))) {
  53.                     obj.closest('li').removeClass('listSliceItem');
  54.                 }
  55.             });
  56.  
  57.             $.each(entity, function() {
  58.                 var current_entity = $(this);
  59.                 var should_apply = true;
  60.                 if((current_entity.find('li.listSliceItem').length) <= (
  61.                         options.default_items)) {
  62.                     should_apply = false;
  63.                 }
  64.  
  65.                 // Make sure we apply more/less only to lists that have
  66.                 // enough 'li' elements.
  67.                 if(should_apply) {
  68.                 current_entity.find('li.listSliceItem' +
  69.                             ':gt(' + (options.default_items - 1).toString() +
  70.                             ')').hide();
  71.                 current_entity.append('<' + options.link_container +
  72.                                 ' class="' + options.link_container_class + '">' +
  73.                                 '<a href="#!" class="listSliceMore ' +
  74.                                 options.link_class + '">' + options.more_text + '</a>');
  75.                 }
  76.             });
  77.  
  78.         }
  79.  
  80.         /**
  81.          * uses the slideToggle method to toggle between showing more or less
  82.          * list items in the initiated list.
  83.          */
  84.         function toggleMoreLess(btn){
  85.             var dad = btn.parent().parent();
  86.             dad.find('li.listSliceItem' +
  87.                      ':gt(' + (options.default_items - 1).toString() +
  88.                      ')').slideToggle(options.animation_time);
  89.             btn.text(btn.text() == options.more_text ? options.less_text : options.more_text);
  90.         }
  91.  
  92.         /**
  93.          * Initiate the sliceList method and more link click method.
  94.          */
  95.         sliceList();
  96.         $('a.listSliceMore').click( function() {
  97.             toggleMoreLess($(this));
  98.             return false; // Cancel Default Anchor Action.
  99.                           // This prevents appending '#!' to the end of the url
  100.         });
  101.     }
  102.  
  103.     // Default options
  104.     $.fn.listSlice.options = {
  105.  
  106.         // Default number of items to be displayed (Accepts Integer only).
  107.         default_items: 5,
  108.  
  109.         // More Anchor link's label when hiding items (Accepts Strings only).
  110.         more_text: 'More',
  111.  
  112.         // More Anchor link's label when showing all items (Accepts Strings only).
  113.         less_text: 'Less',
  114.  
  115.         // Class names to be applied to the More link (Accepts Strings only).
  116.         link_class: 'more link',
  117.  
  118.         // Class names to be applied to the More link's container (Accepts Strings only).
  119.         link_container_class: 'more',
  120.  
  121.         // An element that wraps the more link. (Accepts Strings only)
  122.         link_container: 'li',
  123.  
  124.         // Amount of time in miliseconds the show/hide animation should run.(Accepts Integer and Strings)
  125.         animation_time: 500,
  126.  
  127.         // Ignore 'li' items to be counted as the part of list which have following classes
  128.         // or id's. A comma separated list of classes or id's or both. (Accepts Strings Only)
  129.         ignore_list: 'ignore, no-include, all',
  130.     }
  131.  
  132. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement