Skip to content

Commit 6bb47dc

Browse files
author
floyd_hawkes
committed
fix(gridster): fixing resize limits when in fixed width mode
feature(gridster): added fix_to_content function to fit widget dimensions to a given width and height feature(gridster): added resize_widget_dimensions function refactor(gridster): moved code to set number of columns in grid to helper function
1 parent 14e08d9 commit 6bb47dc

7 files changed

+336
-81
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.2 - 2015-02-23
1+
/*! gridster.js - v0.6.2 - 2015-03-06
22
* http://gridster.net/
33
* Copyright (c) 2015 decksterteam; Licensed */
44

dist/jquery.gridster.js

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

@@ -1174,7 +1174,7 @@
11741174
* @return {HTMLElement} Returns the jQuery wrapped HTMLElement representing.
11751175
* the widget that was just created.
11761176
*/
1177-
fn.add_widget = function(html, size_x, size_y, col, row, max_size, min_size) {
1177+
fn.add_widget = function(html, size_x, size_y, col, row, max_size, min_size, callback) {
11781178
var pos;
11791179
size_x || (size_x = 1);
11801180
size_y || (size_y = 1);
@@ -1218,7 +1218,7 @@
12181218

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

1221-
return $w.fadeIn();
1221+
return $w.fadeIn({complete: function () { if(callback) callback.call(this); }});
12221222
};
12231223

12241224

@@ -1348,7 +1348,7 @@
13481348
* representing the widget.
13491349
* @param {Number} size_x The number of cols that will occupy the widget.
13501350
* @param {Number} size_y The number of rows that will occupy the widget.
1351-
* @param {Function} [callback] Function executed when the widget is removed.
1351+
* @param {Function} [callback] Function executed when the widget is expanded.
13521352
* @return {HTMLElement} Returns $widget.
13531353
*/
13541354
fn.expand_widget = function($widget, size_x, size_y, callback) {
@@ -1392,7 +1392,7 @@
13921392
* @method expand_widget
13931393
* @param {HTMLElement} $widget The jQuery wrapped HTMLElement
13941394
* representing the widget.
1395-
* @param {Function} [callback] Function executed when the widget is removed.
1395+
* @param {Function} [callback] Function executed when the widget is collapsed.
13961396
* @return {HTMLElement} Returns $widget.
13971397
*/
13981398
fn.collapse_widget = function($widget, callback) {
@@ -1421,6 +1421,44 @@
14211421
return $widget;
14221422
};
14231423

1424+
/**
1425+
* Fit the size of a widget to its content (best guess)
1426+
*
1427+
* @method fit_to_content
1428+
* @param $widget {HTMLElement} $widget The jQuery wrapped HTMLElement
1429+
* @param max_cols {Number} max number of columns a widget can take up
1430+
* @param max_rows {Number} max number of rows a widget can take up
1431+
* @param {Function} [callback] Function executed when the widget is fit to content.
1432+
* @return {HTMLElement} Returns $widget.
1433+
*/
1434+
fn.fit_to_content = function($widget, max_cols, max_rows, callback) {
1435+
var wgd = $widget.coords().grid;
1436+
var width = this.$wrapper.width();
1437+
var height = this.$wrapper.height();
1438+
var col_size = this.options.widget_base_dimensions[0] + (2 * this.options.widget_margins[0]);
1439+
var row_size = this.options.widget_base_dimensions[1] + (2 * this.options.widget_margins[1]);
1440+
var best_cols = Math.ceil((width + (2 * this.options.widget_margins[0])) / col_size);
1441+
var best_rows = Math.ceil((height + (2 * this.options.widget_margins[1])) / row_size);
1442+
1443+
var new_grid_data = {
1444+
col: wgd.col,
1445+
row: wgd.row,
1446+
size_x: Math.min(max_cols, best_cols),
1447+
size_y: Math.min(max_rows, best_rows)
1448+
};
1449+
1450+
this.mutate_widget_in_gridmap($widget, wgd, new_grid_data);
1451+
1452+
this.set_dom_grid_height();
1453+
this.set_dom_grid_width();
1454+
1455+
if (callback) {
1456+
callback.call(this, new_grid_data.size_x, new_grid_data.size_y);
1457+
}
1458+
1459+
return $widget;
1460+
};
1461+
14241462
/**
14251463
* Mutate widget dimensions and position in the grid map.
14261464
*
@@ -2308,9 +2346,10 @@
23082346
var size_x = Math.max(1, this.resize_initial_sizex + inc_units_x);
23092347
var size_y = Math.max(1, this.resize_initial_sizey + inc_units_y);
23102348

2311-
var max_cols = (this.container_width / this.min_widget_width) -
2312-
this.resize_initial_col + 1;
2313-
var limit_width = (max_cols * this.min_widget_width) + (this.is_responsive() ? (max_cols - 1) * margin_x : -(margin_x * 2));
2349+
// Max number of cols this widget can be in width
2350+
var max_cols = Math.floor((this.container_width / this.min_widget_width) - this.resize_initial_col + 1);
2351+
2352+
var limit_width = (max_cols * this.min_widget_width) + ((max_cols - 1) * margin_x);
23142353

23152354
size_x = Math.max(Math.min(size_x, max_size_x), min_size_x);
23162355
size_x = Math.min(max_cols, size_x);
@@ -4073,6 +4112,36 @@
40734112
};
40744113

40754114

4115+
/**
4116+
* Resize dimensions of widgets in grid based on given options
4117+
*
4118+
* @method resize_widget_dimensions
4119+
* @param options
4120+
* @returns {Gridster}
4121+
*/
4122+
fn.resize_widget_dimensions = function(options) {
4123+
if (options.widget_margins) {
4124+
this.options.widget_margins = options.widget_margins;
4125+
}
4126+
4127+
if (options.widget_base_dimensions) {
4128+
this.options.widget_base_dimensions = options.widget_base_dimensions;
4129+
}
4130+
4131+
this.$widgets.each($.proxy(function(i, widget) {
4132+
var $widget = $(widget);
4133+
this.resize_widget($widget);
4134+
}, this));
4135+
4136+
this.generate_grid_and_stylesheet();
4137+
this.get_widgets_from_DOM();
4138+
this.set_dom_grid_height();
4139+
this.set_dom_grid_width();
4140+
4141+
return this;
4142+
};
4143+
4144+
40764145
/**
40774146
* Get all widgets in the DOM and register them.
40784147
*
@@ -4099,6 +4168,39 @@
40994168
};
41004169

41014170

4171+
/**
4172+
* Helper function used to set the current number of columns in the grid
4173+
*
4174+
* @param wrapper
4175+
*/
4176+
fn.set_num_columns = function (wrapper) {
4177+
4178+
var max_cols = this.options.max_cols;
4179+
4180+
var cols = Math.floor(wrapper / (this.min_widget_width + this.options.widget_margins[0])) +
4181+
this.options.extra_cols;
4182+
4183+
var actual_cols = this.$widgets.map(function() {
4184+
return $(this).attr('data-col');
4185+
}).get();
4186+
4187+
//needed to pass tests with phantomjs
4188+
actual_cols.length || (actual_cols = [0]);
4189+
4190+
var min_cols = Math.max.apply(Math, actual_cols);
4191+
4192+
this.cols = Math.max(min_cols, cols, this.options.min_cols);
4193+
4194+
if (max_cols !== Infinity && max_cols >= min_cols && max_cols < this.cols) {
4195+
this.cols = max_cols;
4196+
}
4197+
4198+
if (this.drag_api) {
4199+
this.drag_api.set_limits((this.cols * this.min_widget_width) + ((this.cols + 1) * this.options.widget_margins[0]));
4200+
}
4201+
};
4202+
4203+
41024204
/**
41034205
* Calculate columns and rows to be set based on the configuration
41044206
* parameters, grid dimensions, etc ...
@@ -4108,25 +4210,8 @@
41084210
*/
41094211
fn.generate_grid_and_stylesheet = function() {
41104212
var aw = this.$wrapper.width();
4111-
var max_cols = this.options.max_cols;
4112-
4113-
var cols = Math.floor(aw / this.min_widget_width) +
4114-
this.options.extra_cols;
4115-
4116-
var actual_cols = this.$widgets.map(function() {
4117-
return $(this).attr('data-col');
4118-
}).get();
4119-
4120-
//needed to pass tests with phantomjs
4121-
actual_cols.length || (actual_cols = [0]);
41224213

4123-
var min_cols = Math.max.apply(Math, actual_cols);
4124-
4125-
this.cols = Math.max(min_cols, cols, this.options.min_cols);
4126-
4127-
if (max_cols !== Infinity && max_cols >= min_cols && max_cols < this.cols) {
4128-
this.cols = max_cols;
4129-
}
4214+
this.set_num_columns(aw);
41304215

41314216
// get all rows that could be occupied by the current widgets
41324217
var max_rows = this.options.extra_rows;

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)