Skip to content

Commit 86053f8

Browse files
author
floyd_hawkes
committed
fix(gridster): fixed bugs in centering_widgets (widgets were getting smushed when being resized) and fixed bug with min_width
1 parent 16a7a65 commit 86053f8

8 files changed

+158
-113
lines changed

dist/jquery.gridster.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! gridster.js - v0.6.4 - 2015-03-19
1+
/*! gridster.js - v0.6.4 - 2015-04-06
22
* http://gridster.net/
33
* Copyright (c) 2015 decksterteam; Licensed */
44

dist/jquery.gridster.js

+51-36
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! gridster.js - v0.6.4 - 2015-03-19
1+
/*! gridster.js - v0.6.4 - 2015-04-06
22
* http://gridster.net/
33
* Copyright (c) 2015 decksterteam; Licensed */
44

@@ -845,7 +845,7 @@
845845
};
846846

847847
//jQuery adapter
848-
$.fn.drag = function ( options ) {
848+
$.fn.gridDraggable = function ( options ) {
849849
return new Draggable(this, options);
850850
};
851851

@@ -1106,6 +1106,12 @@
11061106
this.draggable();
11071107
this.options.resize.enabled && this.resizable();
11081108

1109+
if (this.options.center_widgets) {
1110+
setTimeout($.proxy(function () {
1111+
this.center_widgets();
1112+
}, this), 0);
1113+
}
1114+
11091115
$(window).bind('resize.gridster', throttle(
11101116
$.proxy(this.recalculate_faux_grid, this), 200));
11111117
};
@@ -1222,6 +1228,12 @@
12221228

12231229
this.drag_api.set_limits((this.cols * this.min_widget_width) + ((this.cols + 1) * this.options.widget_margins[0]));
12241230

1231+
if (this.options.center_widgets) {
1232+
setTimeout($.proxy(function () {
1233+
this.center_widgets();
1234+
}, this), 0);
1235+
}
1236+
12251237
return $w.fadeIn({complete: function () { if(callback) callback.call(this); }});
12261238
};
12271239

@@ -1297,23 +1309,32 @@
12971309
* @param {Number} size_x The number of columns that will occupy the widget.
12981310
* By default <code>size_x</code> is limited to the space available from
12991311
* the column where the widget begins, until the last column to the right.
1312+
* @param {Boolean} [reposition] Set to false to not move the widget to
1313+
* the left if there is insufficient space on the right.
13001314
* @param {Number} size_y The number of rows that will occupy the widget.
13011315
* @param {Function} [callback] Function executed when the widget is removed.
13021316
* @return {HTMLElement} Returns $widget.
13031317
*/
1304-
fn.resize_widget = function($widget, size_x, size_y, callback) {
1318+
fn.resize_widget = function($widget, size_x, size_y, reposition, callback) {
13051319
var wgd = $widget.coords().grid;
13061320
var col = wgd.col;
13071321
var max_cols = this.options.max_cols;
1322+
reposition !== false && (reposition = true);
13081323
var old_size_y = wgd.size_y;
13091324
var old_col = wgd.col;
13101325
var new_col = old_col;
13111326

13121327
size_x || (size_x = wgd.size_x);
13131328
size_y || (size_y = wgd.size_y);
13141329

1315-
if (max_cols !== Infinity) {
1316-
size_x = Math.min(size_x, max_cols - col + 1);
1330+
//if (max_cols !== Infinity) {
1331+
// size_x = Math.min(size_x, max_cols - col + 1);
1332+
//}
1333+
1334+
if (reposition && old_col + size_x - 1 > this.cols) {
1335+
var diff = old_col + (size_x - 1) - this.cols;
1336+
var c = old_col - diff;
1337+
new_col = Math.max(1, c);
13171338
}
13181339

13191340
if (size_y > old_size_y) {
@@ -1470,15 +1491,16 @@
14701491
* @method center_widgets
14711492
*/
14721493
fn.center_widgets = debounce(function () {
1473-
var window_width = $(window).width();
1494+
var wrapper_width = this.$wrapper.width();
14741495
var col_size = this.options.widget_base_dimensions[0] + (2 * this.options.widget_margins[0]);
1475-
var col_count = Math.floor(Math.max(Math.floor(window_width / col_size), this.min_col_count) / 2) * 2;
1496+
var col_count = Math.floor(Math.max(Math.floor(wrapper_width / col_size), this.min_col_count) / 2) * 2;
14761497

14771498
this.options.min_cols = col_count;
14781499
this.options.max_cols = col_count;
14791500
this.options.extra_cols = 0;
14801501
this.options.max_size_x = col_count;
14811502
this.set_dom_grid_width(col_count);
1503+
this.cols = col_count;
14821504

14831505
var col_dif = (col_count - this.prev_col_count) / 2;
14841506

@@ -1515,28 +1537,30 @@
15151537

15161538

15171539
fn.get_min_col = function () {
1518-
var min_col = this.min_col_count;
1519-
this.$widgets.each($.proxy(function(i, widget) {
1520-
var $widget = $(widget);
1521-
var value = parseInt($widget.attr("data-col"));
1522-
min_col = Math.min(min_col, value);
1523-
}, this));
1524-
return min_col;
1540+
return Math.min.apply(Math, this.$widgets.map($.proxy(function (key, widget) {
1541+
return this.get_cells_occupied($(widget).coords().grid).cols;
1542+
}, this)).get());
15251543
};
15261544

15271545

15281546
fn.shift_cols = function (col_dif) {
1529-
this.$widgets.each($.proxy(function(i, widget) {
1530-
var $widget = $(widget);
1547+
var widgets_coords = this.$widgets.map($.proxy(function(i, widget) {
1548+
var $w = $(widget);
1549+
return this.dom_to_coords($w);
1550+
}, this));
1551+
widgets_coords = Gridster.sort_by_row_and_col_asc(widgets_coords);
1552+
1553+
widgets_coords.each($.proxy(function(i, widget) {
1554+
var $widget = $(widget.el);
15311555
var wgd = $widget.coords().grid;
1532-
var value = parseInt($widget.attr("data-col"));
1556+
var col = parseInt($widget.attr("data-col"));
1557+
15331558
var new_grid_data = {
1534-
col: Math.round(value + col_dif),
1559+
col: col + col_dif,
15351560
row: wgd.row,
15361561
size_x: wgd.size_x,
15371562
size_y: wgd.size_y
15381563
};
1539-
15401564
setTimeout($.proxy(function () {
15411565
this.mutate_widget_in_gridmap($widget, wgd, new_grid_data);
15421566
}, this), 0);
@@ -1925,12 +1949,6 @@
19251949

19261950
this.options.resize.enabled && this.add_resize_handle($el);
19271951

1928-
if (this.options.center_widgets) {
1929-
setTimeout($.proxy(function () {
1930-
this.center_widgets();
1931-
}, this), 0);
1932-
}
1933-
19341952
return posChanged;
19351953
};
19361954

@@ -2066,7 +2084,7 @@
20662084
}, 60)
20672085
});
20682086

2069-
this.drag_api = this.$el.drag(draggable_options);
2087+
this.drag_api = this.$el.gridDraggable(draggable_options);
20702088
return this;
20712089
};
20722090

@@ -2078,7 +2096,7 @@
20782096
* @return {Class} Returns instance of gridster Class.
20792097
*/
20802098
fn.resizable = function() {
2081-
this.resize_api = this.$el.drag({
2099+
this.resize_api = this.$el.gridDraggable({
20822100
items: '.' + this.options.resize.handle_class,
20832101
offset_left: this.options.widget_margins[0],
20842102
container_width: this.container_width,
@@ -2480,7 +2498,7 @@
24802498
if (size_x !== this.resize_last_sizex ||
24812499
size_y !== this.resize_last_sizey) {
24822500

2483-
this.resize_widget(this.$resized_widget, size_x, size_y);
2501+
this.resize_widget(this.$resized_widget, size_x, size_y, false);
24842502
this.set_dom_grid_width(this.cols);
24852503

24862504
this.$resize_preview_holder.css({
@@ -3974,16 +3992,13 @@
39743992
opts.widget_base_dimensions ||
39753993
(opts.widget_base_dimensions = this.options.widget_base_dimensions);
39763994

3995+
opts.widget_margins || (opts.widget_margins = this.options.widget_margins);
39773996

39783997
if(this.is_responsive()) {
3979-
opts.widget_base_dimensions = [this.get_responsive_col_width(), opts.widget_base_dimensions[1]];
3998+
opts.widget_base_dimensions = [this.get_responsive_col_width(), opts.widget_base_dimensions[1]];
3999+
this.toggle_collapsed_grid(full_width, opts);
39804000
}
39814001

3982-
opts.widget_margins || (opts.widget_margins = this.options.widget_margins);
3983-
3984-
3985-
this.toggle_collapsed_grid(full_width, opts);
3986-
39874002
// don't duplicate stylesheets for the same configuration
39884003
var serialized_opts = $.param(opts);
39894004
if ($.inArray(serialized_opts, Gridster.generated_stylesheets) >= 0) {
@@ -4268,11 +4283,11 @@
42684283
*
42694284
* @param wrapper
42704285
*/
4271-
fn.set_num_columns = function (wrapper) {
4286+
fn.set_num_columns = function (wrapper_width) {
42724287

42734288
var max_cols = this.options.max_cols;
42744289

4275-
var cols = Math.floor(wrapper / (this.min_widget_width + this.options.widget_margins[0])) +
4290+
var cols = Math.floor(wrapper_width / (this.min_widget_width + this.options.widget_margins[0])) +
42764291
this.options.extra_cols;
42774292

42784293
var actual_cols = this.$widgets.map(function() {

dist/jquery.gridster.min.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/jquery.gridster.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)