Skip to content

Commit 195295b

Browse files
committed
fix: fixes and improvements in widget-resizing. Closes #32
1 parent 290e648 commit 195295b

File tree

1 file changed

+49
-36
lines changed

1 file changed

+49
-36
lines changed

src/jquery.gridster.js

+49-36
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
min_cols: 1,
1818
max_cols: null,
1919
min_rows: 15,
20-
max_size_x: 6,
20+
max_size_x: false,
2121
autogenerate_stylesheet: true,
2222
avoid_overlapped_widgets: true,
2323
serialize_params: function($w, wgd) {
@@ -191,7 +191,7 @@
191191

192192

193193
/**
194-
* Change the size of a widget.
194+
* Change the size of a widget. Width is limited to the current grid width.
195195
*
196196
* @method resize_widget
197197
* @param {HTMLElement} $widget The jQuery wrapped HTMLElement
@@ -210,28 +210,46 @@
210210
size_x = this.cols;
211211
}
212212

213-
var old_cells_occupied = this.get_cells_occupied(wgd);
214-
var old_size_x = wgd.size_x;
215213
var old_size_y = wgd.size_y;
216214
var old_col = wgd.col;
217215
var new_col = old_col;
218-
var wider = size_x > old_size_x;
219-
var taller = size_y > old_size_y;
220216

221217
if (old_col + size_x - 1 > this.cols) {
222218
var diff = old_col + (size_x - 1) - this.cols;
223219
var c = old_col - diff;
224220
new_col = Math.max(1, c);
225221
}
226222

223+
if (size_y > old_size_y) {
224+
this.add_faux_rows(Math.max(size_y - old_size_y, 0));
225+
}
226+
227227
var new_grid_data = {
228228
col: new_col,
229229
row: wgd.row,
230230
size_x: size_x,
231231
size_y: size_y
232232
};
233233

234-
var new_cells_occupied = this.get_cells_occupied(new_grid_data);
234+
this.mutate_widget_in_gridmap($widget, wgd, new_grid_data);
235+
236+
this.set_dom_grid_height();
237+
238+
if (callback) {
239+
callback.call(this, new_grid_data.size_x, new_grid_data.size_y);
240+
}
241+
242+
return $widget;
243+
};
244+
245+
246+
fn.mutate_widget_in_gridmap = function($widget, wgd, new_wgd) {
247+
248+
var old_size_x = wgd.size_x;
249+
var old_size_y = wgd.size_y;
250+
251+
var old_cells_occupied = this.get_cells_occupied(wgd);
252+
var new_cells_occupied = this.get_cells_occupied(new_wgd);
235253

236254
var empty_cols = [];
237255
$.each(old_cells_occupied.cols, function(i, col) {
@@ -265,48 +283,44 @@
265283

266284
if (occupied_cols.length) {
267285
var cols_to_empty = [
268-
new_col, wgd.row, size_x, Math.min(old_size_y, size_y), $widget
286+
new_wgd.col, new_wgd.row, new_wgd.size_x, Math.min(old_size_y, new_wgd.size_y), $widget
269287
];
270288
this.empty_cells.apply(this, cols_to_empty);
271289
}
272290

273291
if (occupied_rows.length) {
274-
var rows_to_empty = [new_col, wgd.row, size_x, size_y, $widget];
292+
var rows_to_empty = [new_wgd.col, new_wgd.row, new_wgd.size_x, new_wgd.size_y, $widget];
275293
this.empty_cells.apply(this, rows_to_empty);
276294
}
277295

278-
wgd.col = new_col;
279-
wgd.size_x = size_x;
280-
wgd.size_y = size_y;
281-
this.add_to_gridmap(new_grid_data, $widget);
296+
// not the same that wgd = new_wgd;
297+
wgd.col = new_wgd.col;
298+
wgd.row = new_wgd.row;
299+
wgd.size_x = new_wgd.size_x;
300+
wgd.size_y = new_wgd.size_y;
301+
302+
this.add_to_gridmap(new_wgd, $widget);
282303

283304
//update coords instance attributes
284305
$widget.data('coords').update({
285-
width: (size_x * this.options.widget_base_dimensions[0] +
286-
((size_x - 1) * this.options.widget_margins[0]) * 2),
287-
height: (size_y * this.options.widget_base_dimensions[1] +
288-
((size_y - 1) * this.options.widget_margins[1]) * 2)
306+
width: (new_wgd.size_x * this.options.widget_base_dimensions[0] +
307+
((new_wgd.size_x - 1) * this.options.widget_margins[0]) * 2),
308+
height: (new_wgd.size_y * this.options.widget_base_dimensions[1] +
309+
((new_wgd.size_y - 1) * this.options.widget_margins[1]) * 2)
289310
});
290311

291-
if (size_y > old_size_y) {
292-
this.add_faux_rows(size_y - old_size_y);
293-
}
294-
295-
if (size_x > old_size_x) {
296-
this.add_faux_cols(size_x - old_size_x);
297-
}
298-
299312
$widget.attr({
300-
'data-col': new_col,
301-
'data-sizex': size_x,
302-
'data-sizey': size_y
313+
'data-col': new_wgd.col,
314+
'data-row': new_wgd.row,
315+
'data-sizex': new_wgd.size_x,
316+
'data-sizey': new_wgd.size_y
303317
});
304318

305319
if (empty_cols.length) {
306320
var cols_to_remove_holes = [
307-
empty_cols[0], wgd.row,
321+
empty_cols[0], new_wgd.row,
308322
empty_cols.length,
309-
Math.min(old_size_y, size_y),
323+
Math.min(old_size_y, new_wgd.size_y),
310324
$widget
311325
];
312326

@@ -315,18 +329,17 @@
315329

316330
if (empty_rows.length) {
317331
var rows_to_remove_holes = [
318-
new_col, wgd.row, size_x, size_y, $widget
332+
new_wgd.col, new_wgd.row, new_wgd.size_x, new_wgd.size_y, $widget
319333
];
320334
this.remove_empty_cells.apply(this, rows_to_remove_holes);
321335
}
322336

323-
if (callback) {
324-
callback.call(this, size_x, size_y);
325-
}
337+
this.move_widget_up($widget);
326338

327-
return $widget;
339+
return this;
328340
};
329341

342+
330343
/**
331344
* Move down widgets in cells represented by the arguments col, row, size_x,
332345
* size_y
@@ -2274,7 +2287,7 @@
22742287
*/
22752288
fn.generate_stylesheet = function(opts) {
22762289
var styles = '';
2277-
var max_size_x = this.options.max_size_x;
2290+
var max_size_x = this.options.max_size_x || this.cols;
22782291
var max_rows = 0;
22792292
var max_cols = 0;
22802293
var i;

0 commit comments

Comments
 (0)