Skip to content

Commit

Permalink
Replacing .each with for loops and other performance improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
mehrpadin committed Jun 8, 2013
1 parent 8d2d1ad commit 544b5e3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 72 deletions.
17 changes: 10 additions & 7 deletions sftouchscreen.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* sf-Touchscreen v1.2b - Provides touchscreen compatibility for the jQuery Superfish plugin.
* sf-Touchscreen v1.3b - Provides touchscreen compatibility for the jQuery Superfish plugin.
*
* Developer's note:
* Built as a part of the Superfish project for Drupal (http://drupal.org/project/superfish)
Expand All @@ -22,8 +22,10 @@

function activate(menu){
// Select hyperlinks from parent menu items.
menu.find('li > ul').closest('li').children('a').each(function(){
var item = $(this);
// has() cannot be used because it needs jQuery 1.4 or higher.
var links = menu.find('li').children('ul').closest('li').children('a');
for (var a = 0; a < links.length; a++) {
var item = links.eq(a);
// No .toggle() here as it's not possible to reset it.
item.click(function(event){
// Already clicked? proceed to the URL.
Expand All @@ -39,11 +41,11 @@
// Reset everything.
item.removeClass('sf-clicked');
});
});
}
}
// Return original object to support chaining.
return this.each(function(){
var menu = $(this),
for (var b = 0; b < this.length; b++) {
var menu = $(this).eq(b),
mode = options.mode;
// The rest is crystal clear, isn't it? :)
switch (mode){
Expand Down Expand Up @@ -78,6 +80,7 @@
}
break;
}
});
}
return this;
};
})(jQuery);
19 changes: 10 additions & 9 deletions superfish.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* CHANGELOG: http://users.tpg.com.au/j_birch/plugins/superfish/changelog.txt
*/
/*
* This is not the original jQuery Supersubs plugin.
* This is not the original jQuery Superfish plugin.
* Please refer to the README for more information.
*/

Expand Down Expand Up @@ -42,14 +42,15 @@
return this.each(function() {
var s = this.serial = sf.o.length;
var o = $.extend({},sf.defaults,op);
o.$path = $('li.'+o.pathClass,this).slice(0,o.pathLevels).each(function(){
$(this).addClass([o.hoverClass,c.bcClass].join(' '))
.filter('li:has(ul)').removeClass(o.pathClass);
});
o.$path = $('li.'+o.pathClass,this).slice(0,o.pathLevels),
p = o.$path;
for (var l = 0; l < p.length; l++){
p.eq(l).addClass([o.hoverClass,c.bcClass].join(' ')).filter('li:has(ul)').removeClass(o.pathClass);
}
sf.o[s] = sf.op = o;

$('li:has(ul)',this)[($.fn.hoverIntent && !o.disableHI) ? 'hoverIntent' : 'hover'](over,out).each(function() {
if (o.autoArrows) addArrow( $('>a:first-child',this) );
if (o.autoArrows) addArrow( $(this).children('a:first-child') );
})
.not('.'+c.bcClass)
.hideSuperfishUl();
Expand Down Expand Up @@ -89,7 +90,7 @@
pathLevels: 1,
delay: 800,
animation: {opacity:'show'},
speed: 'normal',
speed: 'fast',
autoArrows: true,
dropShadows: true,
disableHI: false, // true disables hoverIntent detection
Expand All @@ -104,15 +105,15 @@
not = (o.retainPath===true) ? o.$path : '';
o.retainPath = false;
var $ul = $(['li.',o.hoverClass].join(''),this).add(this).not(not).removeClass(o.hoverClass)
.find('>ul').addClass('sf-hidden');
.children('ul').addClass('sf-hidden');
o.onHide.call($ul);
return this;
},
showSuperfishUl : function(){
var o = sf.op,
sh = sf.c.shadowClass+'-off',
$ul = this.addClass(o.hoverClass)
.find('>ul.sf-hidden').hide().removeClass('sf-hidden');
.children('ul.sf-hidden').hide().removeClass('sf-hidden');
sf.IE7fix.call($ul);
o.onBeforeShow.call($ul);
$ul.animate(o.animation,o.speed,function(){ sf.IE7fix.call($ul); o.onShow.call($ul); });
Expand Down
90 changes: 42 additions & 48 deletions supersubs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Supersubs v0.2b - jQuery plugin
* Copyright (c) 2008 Joel Birch
* Supersubs v0.3b - jQuery plugin
* Copyright (c) 2013 Joel Birch
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
Expand All @@ -20,75 +20,69 @@
$.fn.supersubs = function(options){
var opts = $.extend({}, $.fn.supersubs.defaults, options);
// return original object to support chaining
return this.each(function() {
// Although this is unnecessary due to the way the module uses these plugins.
for (var a = 0; a < this.length; a++) {
// cache selections
var $$ = $(this);
var $$ = $(this).eq(a),
// support metadata
var o = $.meta ? $.extend({}, opts, $$.data()) : opts;
// get the font size of menu.
// .css('fontSize') returns various results cross-browser, so measure an em dash instead
var fontsize = $('<li id="menu-fontsize">&#8212;</li>').css({
'padding' : 0,
'position' : 'absolute',
'top' : '-99999em',
'width' : 'auto'
}).appendTo($$).width(); //clientWidth is faster, but was incorrect here
// remove em dash
$('#menu-fontsize').remove();

o = $.meta ? $.extend({}, opts, $$.data()) : opts;
// Jump on level if it's a "NavBar"
if ($$.hasClass('sf-navbar')) {
$$ = $('li > ul', $$);
$$ = $$.children('li').children('ul');
}
// cache all ul elements
// cache all ul elements
$ULs = $$.find('ul:not(.sf-megamenu)');
// get the font size of menu.
// .css('fontSize') returns various results cross-browser, so measure an em dash instead
var fontsize = $('<li id="menu-fontsize">&#8212;</li>'),
size = fontsize.attr('style','padding:0;position:absolute;top:-99999em;width:auto;')
.appendTo($$)[0].clientWidth; //clientWidth is faster than width()
// remove em dash
fontsize.remove();

// loop through each ul in menu
$ULs.each(function(i) {
for (var b = 0; b < $ULs.length; b++) {
var
// cache this ul
var $ul = $ULs.eq(i);
$ul = $ULs.eq(b),
// get all (li) children of this ul
var $LIs = $ul.children();
$LIs = $ul.children(),
// get all anchor grand-children
var $As = $LIs.children('a');
$As = $LIs.children('a');
// force content to one line and save current float property
var liFloat = $LIs.css('white-space','nowrap').css('float');
$LIs.css('white-space','nowrap');
// remove width restrictions and floats so elements remain vertically stacked
var emWidth = $ul.add($LIs).add($As).css({
'float' : 'none',
'width' : 'auto'
})
$ul.add($LIs).add($As).css({float:'none',width:'auto'});
// this ul will now be shrink-wrapped to longest li due to position:absolute
// so save its width as ems. Clientwidth is 2 times faster than .width() - thanks Dan Switzer
.end().end()[0].clientWidth / fontsize;
// so save its width as ems.
var emWidth = $ul.get(0).clientWidth / size;
// add more width to ensure lines don't turn over at certain sizes in various browsers
emWidth += o.extraWidth;
// restrict to at least minWidth and at most maxWidth
if (emWidth > o.maxWidth) { emWidth = o.maxWidth; }
else if (emWidth < o.minWidth) { emWidth = o.minWidth; }
if (emWidth > o.maxWidth) {emWidth = o.maxWidth;}
else if (emWidth < o.minWidth) {emWidth = o.minWidth;}
emWidth += 'em';
// set ul to width in ems
$ul.css('width',emWidth);
$ul.css({width:emWidth});
// restore li floats to avoid IE bugs
// set li width to full width of this ul
// revert white-space to normal
$LIs.css({
'float' : liFloat,
'width' : '100%',
'white-space' : 'normal'
})
// update offset position of descendant ul to reflect new width of parent
.each(function(){
var $childUl = $('>ul',this);
var offsetDirection = $childUl.css('left')!==undefined ? 'left' : 'right';
$childUl.css(offsetDirection,emWidth);
});
});
});
$LIs.add($As).css({float:'',width:'',whiteSpace:''});

This comment has been minimized.

Copy link
@yusufhm

yusufhm Jan 20, 2016

@mehrpadin were these CSS values supposed to be empty? When diffing (544b5e3?diff=split), I noticed that these values had changed. It was also causing an issue in one of the projects I'm working on, so I created a pull request for the change I made to make it work: #11

// update offset position of descendant ul to reflect new width of parent.
// set it to 100% in case it isn't already set to this in the CSS
for (var c = 0; c < $LIs.length; c++) {
var $childUl = $LIs.eq(c).children('ul');
var offsetDirection = $childUl.css('left') !== undefined ? 'left' : 'right';
$childUl.css(offsetDirection,'100%');
}
}
}
return this;
};
// expose defaults
$.fn.supersubs.defaults = {
minWidth: 9, // requires em unit.
maxWidth: 25, // requires em unit.
extraWidth: 0 // extra width can ensure lines don't sometimes turn over due to slight browser differences in how they round-off values
minWidth: 12, // requires em unit.
maxWidth: 27, // requires em unit.
extraWidth: 1 // extra width can ensure lines don't sometimes turn over due to slight browser differences in how they round-off values
};
})(jQuery); // plugin code ends
13 changes: 5 additions & 8 deletions supposition.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* http://www.gnu.org/licenses/gpl.html
*/
/*
* This is not the original jQuery Supersubs plugin.
* This is not the original jQuery Supposition plugin.
* Please refer to the README for more information.
*/

Expand All @@ -33,17 +33,15 @@
menuParentLeft = $u.closest('li').offset().left,
totalRight = $w.width() + _offset('x'),
menuRight = $u.offset().left + menuWidth,
exactMenuWidth = (menuRight > (menuParentWidth + menuParentLeft)) ? menuWidth - (menuRight - (menuParentWidth + menuParentLeft)) : menuWidth;
exactMenuWidth = (menuRight > (menuParentWidth + menuParentLeft)) ? menuWidth - (menuRight - (menuParentWidth + menuParentLeft)) : menuWidth;
if ($u.parents('.sf-js-enabled').hasClass('rtl')) {
if (menuParentLeft < exactMenuWidth) {
$u.css('left', menuParentWidth + 'px');
$u.css('right', 'auto');
$u.css({left:menuParentWidth + 'px',right:'auto'});
}
}
else {
if (menuRight > totalRight && menuParentLeft > menuWidth) {
$u.css('right', menuParentWidth + 'px');
$u.css('left', 'auto');
$u.css({right:menuParentWidth + 'px',left:'auto'});
}
}
var windowHeight = $w.height(),
Expand All @@ -54,8 +52,7 @@
baseline = windowHeight + _offset('y');
var expandUp = ((offsetTop + menuHeight > baseline) && (offsetTop > menuHeight));
if (expandUp) {
$u.css('bottom', menuParentHeight + 'px');
$u.css('top', 'auto');
$u.css({bottom:menuParentHeight + 'px',top:'auto'});
}
$u.css('display','none');
});
Expand Down

0 comments on commit 544b5e3

Please sign in to comment.