JAVASCRIPT   34

expand

Guest on 20th August 2022 12:51:33 AM

  1. /* ---------------------------------------------
  2. expandAll v.1.3.8.2
  3. http://www.adipalaz.com/experiments/jquery/expand.html
  4. Requires: jQuery v1.3+
  5. Copyright (c)  Adriana Palazova
  6. Dual licensed under the MIT (http://www.adipalaz.com/docs/mit-license.txt) and GPL (http://www.adipalaz.com/docs/gpl-license.txt) licenses
  7. ------------------------------------------------ */
  8. (function($) {
  9. $.fn.expandAll = function(options) {
  10.     var o = $.extend({}, $.fn.expandAll.defaults, options);  
  11.    
  12.     return this.each(function(index) {
  13.         var $$ = $(this), $referent, $sw, $cllps, $tr, container, toggleTxt, toggleClass;
  14.                
  15.         // --- functions:
  16.        (o.switchPosition == 'before') ? ($.fn.findSibling = $.fn.prev, $.fn.insrt = $.fn.before) : ($.fn.findSibling = $.fn.next, $.fn.insrt = $.fn.after);
  17.                    
  18.         // --- var container
  19.         if (this.id.length) { container = '#' + this.id;
  20.         } else if (this.className.length) { container = this.tagName.toLowerCase() + '.' + this.className.split(' ').join('.');
  21.         } else { container = this.tagName.toLowerCase();}
  22.        
  23.         // --- var $referent
  24.         if (o.ref && $$.find(o.ref).length) {
  25.           (o.switchPosition == 'before') ? $referent = $$.find("'" + o.ref + ":first'") : $referent = $$.find("'" + o.ref + ":last'");
  26.         } else { return; }
  27.        
  28.         // end the script if the length of the collapsible element isn't long enough.  
  29.         if (o.cllpsLength && $$.closest(container).find(o.cllpsEl).text().length < o.cllpsLength) {$$.closest(container).find(o.cllpsEl).addClass('dont_touch'); return;}
  30.    
  31.         // --- if expandAll() claims initial state = hidden:
  32.         (o.initTxt == 'show') ? (toggleTxt = o.expTxt, toggleClass='') : (toggleTxt = o.cllpsTxt, toggleClass='open');
  33.         if (o.state == 'hidden') {
  34.           $$.find(o.cllpsEl + ':not(.shown, .dont_touch)').hide().findSibling().find('> a.open').removeClass('open').data('state', 0);
  35.         } else {
  36.           $$.find(o.cllpsEl).show().findSibling().find('> a').addClass('open').data('state', 1);
  37.         }
  38.        
  39.         (o.oneSwitch) ? ($referent.insrt('<p class="switch"><a href="#" class="' + toggleClass + '">' + toggleTxt + '</a></p>')) :
  40.           ($referent.insrt('<p class="switch"><a href="#" class="">' + o.expTxt + '</a>&nbsp;|&nbsp;<a href="#" class="open">' + o.cllpsTxt + '</a></p>'));
  41.  
  42.         // --- var $sw, $cllps, $tr :
  43.         $sw = $referent.findSibling('p').find('a');
  44.         $cllps = $$.closest(container).find(o.cllpsEl).not('.dont_touch');
  45.         $tr = (o.trigger) ? $$.closest(container).find(o.trigger + ' > a') : $$.closest(container).find('.expand > a');
  46.                
  47.         if (o.child) {
  48.           $$.find(o.cllpsEl + '.shown').show().findSibling().find('> a').addClass('open').text(o.cllpsTxt);
  49.           window.$vrbls = { kt1 : o.expTxt, kt2 : o.cllpsTxt };
  50.         }
  51.  
  52.         var scrollElem;
  53.         (typeof scrollableElement == 'function') ? (scrollElem = scrollableElement('html', 'body')) : (scrollElem = 'html, body');
  54.        
  55.         $sw.click(function() {
  56.             var $switch = $(this),
  57.                 $c = $switch.closest(container).find(o.cllpsEl +':first'),
  58.                 cOffset = $c.offset().top - o.offset;
  59.             if (o.parent) {
  60.               var $swChildren = $switch.parent().nextAll().children('p.switch').find('a');
  61.                   kidTxt1 = $vrbls.kt1, kidTxt2 = $vrbls.kt2,
  62.                   kidTxt = ($switch.text() == o.expTxt) ? kidTxt2 : kidTxt1;
  63.               $swChildren.text(kidTxt);
  64.               if ($switch.text() == o.expTxt) {$swChildren.addClass('open');} else {$swChildren.removeClass('open');}
  65.             }
  66.             if ($switch.text() == o.expTxt) {
  67.               if (o.oneSwitch) {$switch.text(o.cllpsTxt).attr('class', 'open');}
  68.               $tr.addClass('open').data('state', 1);
  69.               $cllps[o.showMethod](o.speed);
  70.             } else {
  71.               if (o.oneSwitch) {$switch.text(o.expTxt).attr('class', '');}
  72.               $tr.removeClass('open').data('state', 0);
  73.               if (o.speed == 0 || o.instantHide) {$cllps.hide();} else {$cllps[o.hideMethod](o.speed);}
  74.               if (o.scroll && cOffset < $(window).scrollTop()) {$(scrollElem).animate({scrollTop: cOffset},600);}
  75.           }
  76.           return false;
  77.         });
  78.         /* -----------------------------------------------
  79.         To save file size, feel free to remove the following code if you don't use the option: 'localLinks: true'
  80.         -------------------------------------------------- */
  81.         if (o.localLinks) {
  82.           var localLink = $(container).find(o.localLinks);
  83.           if (localLink.length) {
  84.             // based on http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links:
  85.             $(localLink).click(function() {
  86.               var $target = $(this.hash);
  87.               $target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');
  88.               if ($target.length) {
  89.                 var tOffset = $target.offset().top;
  90.                 $(scrollElem).animate({scrollTop: tOffset},600);
  91.                 return false;
  92.               }
  93.             });
  94.           }
  95.         }
  96.         /* -----------------------------------------------
  97.         Feel free to remove the following function if you don't use the options: 'localLinks: true' or 'scroll: true'
  98.         -------------------------------------------------- */
  99.         //http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links:
  100.         function scrollableElement(els) {
  101.           for (var i = 0, argLength = arguments.length; i < argLength; i++) {
  102.             var el = arguments[i],
  103.                 $scrollElement = $(el);
  104.             if ($scrollElement.scrollTop() > 0) {
  105.               return el;
  106.             } else {
  107.               $scrollElement.scrollTop(1);
  108.               var isScrollable = $scrollElement.scrollTop() > 0;
  109.               $scrollElement.scrollTop(0);
  110.               if (isScrollable) {
  111.                 return el;
  112.               }
  113.             }
  114.           };
  115.           return [];
  116.         };
  117.       /* --- end of the optional code --- */
  118. });};
  119. $.fn.expandAll.defaults = {
  120.         state : 'hidden', // If 'hidden', the collapsible elements are hidden by default, else they are expanded by default
  121.         initTxt : 'show', // 'show' - if the initial text of the switch is for expanding, 'hide' - if the initial text of the switch is for collapsing
  122.         expTxt : '[Expand All]', // the text of the switch for expanding
  123.         cllpsTxt : '[Collapse All]', // the text of the switch for collapsing
  124.         oneSwitch : true, // true or false - whether both [Expand All] and [Collapse All] are shown, or they swap
  125.         ref : '.expand', // the switch 'Expand All/Collapse All' is inserted in regards to the element specified by 'ref'
  126.         switchPosition: 'before', //'before' or 'after' - specifies the position of the switch 'Expand All/Collapse All' - before or after the collapsible element
  127.         scroll : false, // false or true. If true, the switch 'Expand All/Collapse All' will be dinamically repositioned to remain in view when the collapsible element closes
  128.         offset : 20,
  129.         showMethod : 'slideDown', // 'show', 'slideDown', 'fadeIn', or custom
  130.         hideMethod : 'slideUp', // 'hide', 'slideUp', 'fadeOut', or custom
  131.         speed : 600, // the speed of the animation in m.s. or 'slow', 'normal', 'fast'
  132.         cllpsEl : '.collapse', // the collapsible element
  133.         trigger : '.expand', // if expandAll() is used in conjunction with toggle() - the elements that contain the trigger of the toggle effect on the individual collapsible sections
  134.         localLinks : null, // null or the selector of the same-page links to which we will apply a smooth-scroll function, e.g. 'a.to_top'
  135.         parent : false, // true, false
  136.         child : false, // true, false
  137.         cllpsLength : null, //null, {Number}. If {Number} (e.g. cllpsLength: 200) - if the number of characters inside the "collapsible element" is less than the given {Number}, the element will be visible all the time
  138.         instantHide : false // {true} fixes hiding content inside hidden elements
  139. };
  140.  
  141. /* ---------------------------------------------
  142. Toggler v.1.0
  143. http://www.adipalaz.com/experiments/jquery/expand.html
  144. Requires: jQuery v1.3+
  145. Copyright (c) 2009 Adriana Palazova
  146. Dual licensed under the MIT (http://www.adipalaz.com/docs/mit-license.txt) and GPL (http://www.adipalaz.com/docs/gpl-license.txt) licenses
  147. ------------------------------------------------ */
  148. $.fn.toggler = function(options) {
  149.     var o = $.extend({}, $.fn.toggler.defaults, options);
  150.    
  151.     var $this = $(this);
  152.     $this.wrapInner('<a style="display:block" href="#" title="Expand/Collapse" />');
  153.     if (o.initShow) {$(o.initShow).addClass('shown');}
  154.     $this.next(o.cllpsEl + ':not(.shown)').hide();
  155.     return this.each(function() {
  156.       var container;
  157.       (o.container) ? container = o.container : container = 'html';
  158.       if ($this.next('div.shown').length) { $this.closest(container).find('.shown').show().prev().find('a').addClass('open'); }
  159.       $(this).click(function() {
  160.         $(this).find('a').toggleClass('open').end().next(o.cllpsEl)[o.method](o.speed);
  161.         return false;
  162.     });
  163. });};
  164. $.fn.toggler.defaults = {
  165.      cllpsEl : 'div.collapse',
  166.      method : 'slideToggle',
  167.      speed : 'slow',
  168.      container : '', //the common container of all groups with collapsible content (optional)
  169.      initShow : '.shown' //the initially expanded sections (optional)
  170. };
  171. /* ---------------------------------------------
  172. Feel free to remove any of the following functions if you don't need it.
  173. ------------------------------------------------ */
  174. //credit: http://jquery.malsup.com/fadetest.html
  175. $.fn.fadeToggle = function(speed, callback) {
  176.     return this.animate({opacity: 'toggle'}, speed, function() {
  177.     if (jQuery.browser.msie) { this.style.removeAttribute('filter'); }
  178.     if (jQuery.isFunction(callback)) { callback(); }
  179.   });
  180. };
  181. $.fn.slideFadeToggle = function(speed, easing, callback) {
  182.     return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, function() {
  183.     if (jQuery.browser.msie) { this.style.removeAttribute('filter'); }
  184.     if (jQuery.isFunction(callback)) { callback(); }
  185.   });
  186. };
  187. $.fn.slideFadeDown = function(speed, callback) {
  188.   return this.animate({opacity: 'show', height: 'show'}, speed, function() {
  189.     if (jQuery.browser.msie) { this.style.removeAttribute('filter'); }
  190.     if (jQuery.isFunction(callback)) { callback(); }
  191.   });
  192. };
  193. $.fn.slideFadeUp = function(speed, callback) {
  194.   return this.animate({opacity: 'hide', height: 'hide'}, speed, function() {
  195.     if (jQuery.browser.msie) { this.style.removeAttribute('filter'); }
  196.     if (jQuery.isFunction(callback)) { callback(); }
  197.   });
  198. };
  199. /* --- end of the optional code --- */
  200. })(jQuery);

Raw Paste


Login or Register to edit or fork this paste. It's free.