Cache $(window) and $(this) selectors for performance

This commit is contained in:
Eric Mann
2014-12-17 10:29:57 -08:00
parent 7026f0f608
commit 4d6abc862a
8 changed files with 76 additions and 59 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
/*! gridster.js - v0.5.6 - 2014-09-25
/*! gridster.js - v0.5.6 - 2014-12-17
* http://gridster.net/
* Copyright (c) 2014 ducksboard; Licensed MIT */
+25 -18
View File
@@ -1,4 +1,4 @@
/*! gridster.js - v0.5.6 - 2014-09-25
/*! gridster.js - v0.5.6 - 2014-12-17
* http://gridster.net/
* Copyright (c) 2014 ducksboard; Licensed MIT */
@@ -520,7 +520,7 @@
this.disabled = false;
this.events();
$(window).bind(this.nsEvent('resize'),
$window.bind(this.nsEvent('resize'),
throttle($.proxy(this.calculate_dimensions, this), 200));
};
@@ -578,9 +578,9 @@
var diff_y = Math.round(mouse_actual_pos.top - this.mouse_init_pos.top);
var left = Math.round(this.el_init_offset.left +
diff_x - this.baseX + $(window).scrollLeft() - this.win_offset_x);
diff_x - this.baseX + $window.scrollLeft() - this.win_offset_x);
var top = Math.round(this.el_init_offset.top +
diff_y - this.baseY + $(window).scrollTop() - this.win_offset_y);
diff_y - this.baseY + $window.scrollTop() - this.win_offset_y);
if (this.options.limit) {
if (left > this.player_max_left) {
@@ -598,8 +598,8 @@
pointer: {
left: mouse_actual_pos.left,
top: mouse_actual_pos.top,
diff_left: diff_x + ($(window).scrollLeft() - this.win_offset_x),
diff_top: diff_y + ($(window).scrollTop() - this.win_offset_y)
diff_left: diff_x + ($window.scrollLeft() - this.win_offset_x),
diff_top: diff_y + ($window.scrollTop() - this.win_offset_y)
}
};
};
@@ -747,8 +747,8 @@
this.helper = false;
}
this.win_offset_y = $(window).scrollTop();
this.win_offset_x = $(window).scrollLeft();
this.win_offset_y = $window.scrollTop();
this.win_offset_x = $window.scrollLeft();
this.scroll_offset_y = 0;
this.scroll_offset_x = 0;
this.el_init_offset = this.$player.offset();
@@ -827,7 +827,7 @@
this.$container.off(this.ns);
this.$document.off(this.ns);
$(window).off(this.ns);
$window.off(this.ns);
$.removeData(this.$container, 'drag');
};
@@ -864,7 +864,8 @@
}(this, function($, Draggable, Collision) {
var defaults = {
var $window = $( window ),
defaults = {
namespace: '',
widget_selector: 'li',
widget_margins: [10, 10],
@@ -1100,7 +1101,7 @@
this.draggable();
this.options.resize.enabled && this.resizable();
$(window).bind('resize.gridster', throttle(
$window.bind('resize.gridster', throttle(
$.proxy(this.recalculate_faux_grid, this), 200));
};
@@ -1471,10 +1472,11 @@
});
$nexts.not($exclude).each($.proxy(function(i, w) {
var wgd = $(w).coords().grid;
var $w = $( w ),
wgd = $w.coords().grid;
if ( !(wgd.row <= (row + size_y - 1))) { return; }
var diff = (row + size_y) - wgd.row;
this.move_widget_down($(w), diff);
this.move_widget_down($w, diff);
}, this));
this.set_dom_grid_height();
@@ -3861,7 +3863,7 @@
*/
fn.recalculate_faux_grid = function() {
var aw = this.$wrapper.width();
this.baseX = ($(window).width() - aw) / 2;
this.baseX = ($window.width() - aw) / 2;
this.baseY = this.$wrapper.offset().top;
$.each(this.faux_grid, $.proxy(function(i, coords) {
@@ -3938,7 +3940,7 @@
this.rows = Math.max(max_rows, this.options.min_rows);
this.baseX = ($(window).width() - aw) / 2;
this.baseX = ($window.width() - aw) / 2;
this.baseY = this.$wrapper.offset().top;
if (this.options.autogenerate_stylesheet) {
@@ -3959,12 +3961,16 @@
this.$el.removeData('gridster');
// remove bound callback on window resize
$(window).unbind('.gridster');
$window.unbind('.gridster');
if (this.drag_api) {
this.drag_api.destroy();
}
if (this.resize_api) {
this.resize_api.destroy();
}
this.remove_style_tags();
remove && this.$el.remove();
@@ -3976,8 +3982,9 @@
//jQuery adapter
$.fn.gridster = function(options) {
return this.each(function() {
if (! $(this).data('gridster')) {
$(this).data('gridster', new Gridster( this, options ));
var $this = $( this );
if (! $this.data('gridster')) {
$this.data('gridster', new Gridster( this, options ));
}
});
};
+1 -1
View File
@@ -1,2 +1,2 @@
/*! gridster.js - v0.5.6 - 2014-09-25 - * http://gridster.net/ - Copyright (c) 2014 ducksboard; Licensed MIT */
/*! gridster.js - v0.5.6 - 2014-12-17 - * http://gridster.net/ - Copyright (c) 2014 ducksboard; Licensed MIT */
.gridster{position:relative}.gridster>*{margin:0 auto;-webkit-transition:height .4s,width .4s;-moz-transition:height .4s,width .4s;-o-transition:height .4s,width .4s;-ms-transition:height .4s,width .4s;transition:height .4s,width .4s}.gridster .gs-w{z-index:2;position:absolute}.ready .gs-w:not(.preview-holder){-webkit-transition:opacity .3s,left .3s,top .3s;-moz-transition:opacity .3s,left .3s,top .3s;-o-transition:opacity .3s,left .3s,top .3s;transition:opacity .3s,left .3s,top .3s}.ready .gs-w:not(.preview-holder),.ready .resize-preview-holder{-webkit-transition:opacity .3s,left .3s,top .3s,width .3s,height .3s;-moz-transition:opacity .3s,left .3s,top .3s,width .3s,height .3s;-o-transition:opacity .3s,left .3s,top .3s,width .3s,height .3s;transition:opacity .3s,left .3s,top .3s,width .3s,height .3s}.gridster .preview-holder{z-index:1;position:absolute;background-color:#fff;border-color:#fff;opacity:.3}.gridster .player-revert{z-index:10!important;-webkit-transition:left .3s,top .3s!important;-moz-transition:left .3s,top .3s!important;-o-transition:left .3s,top .3s!important;transition:left .3s,top .3s!important}.gridster .dragging,.gridster .resizing{z-index:10!important;-webkit-transition:all 0s!important;-moz-transition:all 0s!important;-o-transition:all 0s!important;transition:all 0s!important}.gs-resize-handle{position:absolute;z-index:1}.gs-resize-handle-both{width:20px;height:20px;bottom:-8px;right:-8px;background-image:url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/Pg08IS0tIEdlbmVyYXRvcjogQWRvYmUgRmlyZXdvcmtzIENTNiwgRXhwb3J0IFNWRyBFeHRlbnNpb24gYnkgQWFyb24gQmVhbGwgKGh0dHA6Ly9maXJld29ya3MuYWJlYWxsLmNvbSkgLiBWZXJzaW9uOiAwLjYuMSAgLS0+DTwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+DTxzdmcgaWQ9IlVudGl0bGVkLVBhZ2UlMjAxIiB2aWV3Qm94PSIwIDAgNiA2IiBzdHlsZT0iYmFja2dyb3VuZC1jb2xvcjojZmZmZmZmMDAiIHZlcnNpb249IjEuMSINCXhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHhtbDpzcGFjZT0icHJlc2VydmUiDQl4PSIwcHgiIHk9IjBweCIgd2lkdGg9IjZweCIgaGVpZ2h0PSI2cHgiDT4NCTxnIG9wYWNpdHk9IjAuMzAyIj4NCQk8cGF0aCBkPSJNIDYgNiBMIDAgNiBMIDAgNC4yIEwgNCA0LjIgTCA0LjIgNC4yIEwgNC4yIDAgTCA2IDAgTCA2IDYgTCA2IDYgWiIgZmlsbD0iIzAwMDAwMCIvPg0JPC9nPg08L3N2Zz4=);background-position:top left;background-repeat:no-repeat;cursor:se-resize;z-index:20}.gs-resize-handle-x{top:0;bottom:13px;right:-5px;width:10px;cursor:e-resize}.gs-resize-handle-y{left:0;right:13px;bottom:-5px;height:10px;cursor:s-resize}.gs-w:hover .gs-resize-handle,.resizing .gs-resize-handle{opacity:1}.gs-resize-handle,.gs-w.dragging .gs-resize-handle{opacity:0}.gs-resize-disabled .gs-resize-handle{display:none!important}[data-max-sizex="1"] .gs-resize-handle-x,[data-max-sizey="1"] .gs-resize-handle-y,[data-max-sizey="1"][data-max-sizex="1"] .gs-resize-handle{display:none!important}
+2 -2
View File
File diff suppressed because one or more lines are too long
+25 -18
View File
@@ -1,4 +1,4 @@
/*! gridster.js - v0.5.6 - 2014-09-25
/*! gridster.js - v0.5.6 - 2014-12-17
* http://gridster.net/
* Copyright (c) 2014 ducksboard; Licensed MIT */
@@ -520,7 +520,7 @@
this.disabled = false;
this.events();
$(window).bind(this.nsEvent('resize'),
$window.bind(this.nsEvent('resize'),
throttle($.proxy(this.calculate_dimensions, this), 200));
};
@@ -578,9 +578,9 @@
var diff_y = Math.round(mouse_actual_pos.top - this.mouse_init_pos.top);
var left = Math.round(this.el_init_offset.left +
diff_x - this.baseX + $(window).scrollLeft() - this.win_offset_x);
diff_x - this.baseX + $window.scrollLeft() - this.win_offset_x);
var top = Math.round(this.el_init_offset.top +
diff_y - this.baseY + $(window).scrollTop() - this.win_offset_y);
diff_y - this.baseY + $window.scrollTop() - this.win_offset_y);
if (this.options.limit) {
if (left > this.player_max_left) {
@@ -598,8 +598,8 @@
pointer: {
left: mouse_actual_pos.left,
top: mouse_actual_pos.top,
diff_left: diff_x + ($(window).scrollLeft() - this.win_offset_x),
diff_top: diff_y + ($(window).scrollTop() - this.win_offset_y)
diff_left: diff_x + ($window.scrollLeft() - this.win_offset_x),
diff_top: diff_y + ($window.scrollTop() - this.win_offset_y)
}
};
};
@@ -747,8 +747,8 @@
this.helper = false;
}
this.win_offset_y = $(window).scrollTop();
this.win_offset_x = $(window).scrollLeft();
this.win_offset_y = $window.scrollTop();
this.win_offset_x = $window.scrollLeft();
this.scroll_offset_y = 0;
this.scroll_offset_x = 0;
this.el_init_offset = this.$player.offset();
@@ -827,7 +827,7 @@
this.$container.off(this.ns);
this.$document.off(this.ns);
$(window).off(this.ns);
$window.off(this.ns);
$.removeData(this.$container, 'drag');
};
@@ -864,7 +864,8 @@
}(this, function($, Draggable, Collision) {
var defaults = {
var $window = $( window ),
defaults = {
namespace: '',
widget_selector: 'li',
widget_margins: [10, 10],
@@ -1100,7 +1101,7 @@
this.draggable();
this.options.resize.enabled && this.resizable();
$(window).bind('resize.gridster', throttle(
$window.bind('resize.gridster', throttle(
$.proxy(this.recalculate_faux_grid, this), 200));
};
@@ -1471,10 +1472,11 @@
});
$nexts.not($exclude).each($.proxy(function(i, w) {
var wgd = $(w).coords().grid;
var $w = $( w ),
wgd = $w.coords().grid;
if ( !(wgd.row <= (row + size_y - 1))) { return; }
var diff = (row + size_y) - wgd.row;
this.move_widget_down($(w), diff);
this.move_widget_down($w, diff);
}, this));
this.set_dom_grid_height();
@@ -3861,7 +3863,7 @@
*/
fn.recalculate_faux_grid = function() {
var aw = this.$wrapper.width();
this.baseX = ($(window).width() - aw) / 2;
this.baseX = ($window.width() - aw) / 2;
this.baseY = this.$wrapper.offset().top;
$.each(this.faux_grid, $.proxy(function(i, coords) {
@@ -3938,7 +3940,7 @@
this.rows = Math.max(max_rows, this.options.min_rows);
this.baseX = ($(window).width() - aw) / 2;
this.baseX = ($window.width() - aw) / 2;
this.baseY = this.$wrapper.offset().top;
if (this.options.autogenerate_stylesheet) {
@@ -3959,12 +3961,16 @@
this.$el.removeData('gridster');
// remove bound callback on window resize
$(window).unbind('.gridster');
$window.unbind('.gridster');
if (this.drag_api) {
this.drag_api.destroy();
}
if (this.resize_api) {
this.resize_api.destroy();
}
this.remove_style_tags();
remove && this.$el.remove();
@@ -3976,8 +3982,9 @@
//jQuery adapter
$.fn.gridster = function(options) {
return this.each(function() {
if (! $(this).data('gridster')) {
$(this).data('gridster', new Gridster( this, options ));
var $this = $( this );
if (! $this.data('gridster')) {
$this.data('gridster', new Gridster( this, options ));
}
});
};
File diff suppressed because one or more lines are too long
+8 -8
View File
@@ -98,7 +98,7 @@
this.disabled = false;
this.events();
$(window).bind(this.nsEvent('resize'),
$window.bind(this.nsEvent('resize'),
throttle($.proxy(this.calculate_dimensions, this), 200));
};
@@ -156,9 +156,9 @@
var diff_y = Math.round(mouse_actual_pos.top - this.mouse_init_pos.top);
var left = Math.round(this.el_init_offset.left +
diff_x - this.baseX + $(window).scrollLeft() - this.win_offset_x);
diff_x - this.baseX + $window.scrollLeft() - this.win_offset_x);
var top = Math.round(this.el_init_offset.top +
diff_y - this.baseY + $(window).scrollTop() - this.win_offset_y);
diff_y - this.baseY + $window.scrollTop() - this.win_offset_y);
if (this.options.limit) {
if (left > this.player_max_left) {
@@ -176,8 +176,8 @@
pointer: {
left: mouse_actual_pos.left,
top: mouse_actual_pos.top,
diff_left: diff_x + ($(window).scrollLeft() - this.win_offset_x),
diff_top: diff_y + ($(window).scrollTop() - this.win_offset_y)
diff_left: diff_x + ($window.scrollLeft() - this.win_offset_x),
diff_top: diff_y + ($window.scrollTop() - this.win_offset_y)
}
};
};
@@ -325,8 +325,8 @@
this.helper = false;
}
this.win_offset_y = $(window).scrollTop();
this.win_offset_x = $(window).scrollLeft();
this.win_offset_y = $window.scrollTop();
this.win_offset_x = $window.scrollLeft();
this.scroll_offset_y = 0;
this.scroll_offset_x = 0;
this.el_init_offset = this.$player.offset();
@@ -405,7 +405,7 @@
this.$container.off(this.ns);
this.$document.off(this.ns);
$(window).off(this.ns);
$window.off(this.ns);
$.removeData(this.$container, 'drag');
};
+12 -9
View File
@@ -17,7 +17,8 @@
}(this, function($, Draggable, Collision) {
var defaults = {
var $window = $( window ),
defaults = {
namespace: '',
widget_selector: 'li',
widget_margins: [10, 10],
@@ -253,7 +254,7 @@
this.draggable();
this.options.resize.enabled && this.resizable();
$(window).bind('resize.gridster', throttle(
$window.bind('resize.gridster', throttle(
$.proxy(this.recalculate_faux_grid, this), 200));
};
@@ -624,10 +625,11 @@
});
$nexts.not($exclude).each($.proxy(function(i, w) {
var wgd = $(w).coords().grid;
var $w = $( w ),
wgd = $w.coords().grid;
if ( !(wgd.row <= (row + size_y - 1))) { return; }
var diff = (row + size_y) - wgd.row;
this.move_widget_down($(w), diff);
this.move_widget_down($w, diff);
}, this));
this.set_dom_grid_height();
@@ -3014,7 +3016,7 @@
*/
fn.recalculate_faux_grid = function() {
var aw = this.$wrapper.width();
this.baseX = ($(window).width() - aw) / 2;
this.baseX = ($window.width() - aw) / 2;
this.baseY = this.$wrapper.offset().top;
$.each(this.faux_grid, $.proxy(function(i, coords) {
@@ -3091,7 +3093,7 @@
this.rows = Math.max(max_rows, this.options.min_rows);
this.baseX = ($(window).width() - aw) / 2;
this.baseX = ($window.width() - aw) / 2;
this.baseY = this.$wrapper.offset().top;
if (this.options.autogenerate_stylesheet) {
@@ -3112,7 +3114,7 @@
this.$el.removeData('gridster');
// remove bound callback on window resize
$(window).unbind('.gridster');
$window.unbind('.gridster');
if (this.drag_api) {
this.drag_api.destroy();
@@ -3133,8 +3135,9 @@
//jQuery adapter
$.fn.gridster = function(options) {
return this.each(function() {
if (! $(this).data('gridster')) {
$(this).data('gridster', new Gridster( this, options ));
var $this = $( this );
if (! $this.data('gridster')) {
$this.data('gridster', new Gridster( this, options ));
}
});
};