Skip to content

Commit ff51187

Browse files
Daniel Mattesvieron
Daniel Mattes
authored andcommitted
feat(resize): add option to set min_size of a widget
1 parent 0641aa8 commit ff51187

File tree

1 file changed

+50
-8
lines changed

1 file changed

+50
-8
lines changed

src/jquery.gridster.js

+50-8
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@
101101
* @param {Array} [options.resize.max_size] Limit widget dimensions
102102
* when resizing. Array values should be integers:
103103
* `[max_cols_occupied, max_rows_occupied]`
104+
* @param {Array} [options.resize.min_size] Limit widget dimensions
105+
* when resizing. Array values should be integers:
106+
* `[min_cols_occupied, min_rows_occupied]`
104107
* @param {Function} [options.resize.start] Function executed
105108
* when resizing starts.
106109
* @param {Function} [otions.resize.resize] Function executed
@@ -211,10 +214,11 @@
211214
* @param {Number} [col] The column the widget should start in.
212215
* @param {Number} [row] The row the widget should start in.
213216
* @param {Array} [max_size] max_size Maximun size (in units) for width and height.
217+
* @param {Array} [min_size] min_size Minimum size (in units) for width and height.
214218
* @return {HTMLElement} Returns the jQuery wrapped HTMLElement representing.
215219
* the widget that was just created.
216220
*/
217-
fn.add_widget = function(html, size_x, size_y, col, row, max_size) {
221+
fn.add_widget = function(html, size_x, size_y, col, row, max_size, min_size) {
218222
var pos;
219223
size_x || (size_x = 1);
220224
size_y || (size_y = 1);
@@ -248,12 +252,39 @@
248252
this.set_widget_max_size($w, max_size);
249253
}
250254

255+
if (min_size) {
256+
this.set_widget_min_size($w, min_size);
257+
}
258+
251259
this.set_dom_grid_height();
252260

253261
return $w.fadeIn();
254262
};
255263

256264

265+
/**
266+
* Change widget size limits.
267+
*
268+
* @method set_widget_min_size
269+
* @param {HTMLElement|Number} $widget The jQuery wrapped HTMLElement
270+
* representing the widget or an index representing the desired widget.
271+
* @param {Array} min_size Minimum size (in units) for width and height.
272+
* @return {HTMLElement} Returns instance of gridster Class.
273+
*/
274+
fn.set_widget_min_size = function($widget, min_size) {
275+
$widget = typeof $widget === 'number' ?
276+
this.$widgets.eq($widget) : $widget;
277+
278+
if (!$widget.length) { return this; }
279+
280+
var wgd = $widget.data('coords').grid;
281+
wgd.min_size_x = min_size[0];
282+
wgd.min_size_y = min_size[1];
283+
284+
return this;
285+
};
286+
287+
257288
/**
258289
* Change widget size limits.
259290
*
@@ -681,6 +712,8 @@
681712
'size_y': parseInt($el.attr('data-sizey'), 10),
682713
'max_size_x': parseInt($el.attr('data-max-sizex'), 10) || false,
683714
'max_size_y': parseInt($el.attr('data-max-sizey'), 10) || false,
715+
'min_size_x': parseInt($el.attr('data-min-sizex'), 10) || false,
716+
'min_size_y': parseInt($el.attr('data-min-sizey'), 10) || false,
684717
'el': $el
685718
};
686719

@@ -1061,6 +1094,12 @@
10611094
this.options.max_cols - this.resize_initial_col + 1);
10621095
this.resize_max_size_y = this.resize_wgd.max_size_y ||
10631096
this.options.resize.max_size[1];
1097+
1098+
this.resize_min_size_x = (this.resize_wgd.min_size_x ||
1099+
this.options.resize.min_size[0] || 1);
1100+
this.resize_min_size_y = (this.resize_wgd.min_size_y ||
1101+
this.options.resize.min_size[1] || 1);
1102+
10641103
this.resize_initial_last_col = this.get_highest_occupied_cell().col;
10651104

10661105
this.resize_dir = {
@@ -1153,14 +1192,17 @@
11531192
var size_x = Math.max(1, this.resize_initial_sizex + inc_units_x);
11541193
var size_y = Math.max(1, this.resize_initial_sizey + inc_units_y);
11551194

1156-
size_x = Math.min(size_x, this.resize_max_size_x);
1195+
size_x = Math.max(Math.min(size_x, this.resize_max_size_x), this.resize_min_size_x);
11571196
max_width = (this.resize_max_size_x * wbd_x) +
11581197
((size_x - 1) * this.options.widget_margins[0] * 2);
1198+
min_width = (this.resize_min_size_x * wbd_x) +
1199+
((size_x - 1) * this.options.widget_margins[0] * 2);
11591200

1160-
size_y = Math.min(size_y, this.resize_max_size_y);
1201+
size_y = Math.max(Math.min(size_y, this.resize_max_size_y), this.resize_min_size_y);
11611202
max_height = (this.resize_max_size_y * wbd_y) +
11621203
((size_y - 1) * this.options.widget_margins[1] * 2);
1163-
1204+
min_height = (this.resize_min_size_y * wbd_y) +
1205+
((size_y - 1) * this.options.widget_margins[1] * 2);
11641206

11651207
if (this.resize_dir.right) {
11661208
size_y = this.resize_initial_sizey;
@@ -1183,10 +1225,10 @@
11831225

11841226

11851227
var css_props = {};
1186-
!this.resize_dir.bottom && (css_props.width = Math.min(
1187-
this.resize_initial_width + rel_x, max_width));
1188-
!this.resize_dir.right && (css_props.height = Math.min(
1189-
this.resize_initial_height + rel_y, max_height));
1228+
!this.resize_dir.bottom && (css_props.width = Math.max(Math.min(
1229+
this.resize_initial_width + rel_x, max_width), min_width));
1230+
!this.resize_dir.right && (css_props.height = Math.max(Math.min(
1231+
this.resize_initial_height + rel_y, max_height), min_height));
11901232

11911233
this.$resized_widget.css(css_props);
11921234

0 commit comments

Comments
 (0)