Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5d4f33

Browse files
flexibly resizing tiles to fit within max grid height
1 parent daa1c3c commit f5d4f33

File tree

4 files changed

+199
-18
lines changed

4 files changed

+199
-18
lines changed

dist/gridstack.js

Lines changed: 117 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@
6969

7070
var id_seq = 0;
7171

72-
var GridStackEngine = function(width, onchange, float_mode, height, items) {
72+
var GridStackEngine = function(width, onchange, float_mode, height, items, make_room) {
7373
this.width = width;
7474
this.float = float_mode || false;
7575
this.height = height || 0;
76+
this.make_room = make_room || false;
7677

7778
this.nodes = items || [];
7879
this.onchange = onchange || function() {};
@@ -112,8 +113,13 @@
112113
return;
113114
}
114115

115-
this.move_node(collision_node, collision_node.x, node.y + node.height,
116+
var node_moved = this.move_node(collision_node, collision_node.x, node.y + node.height,
116117
collision_node.width, collision_node.height, true);
118+
119+
if (!node_moved) {
120+
this.move_node(collision_node, collision_node.x, node.y + node.height,
121+
collision_node.width, +collision_node.min_height);
122+
}
117123
}
118124
};
119125

@@ -305,16 +311,17 @@
305311
});
306312
}
307313

308-
if (this.height) {
314+
if (this.height && !this.make_room) {
309315
res &= clone.get_grid_height() <= this.height;
310316
}
311317

312318
return res;
313319
};
314320

315321
GridStackEngine.prototype.can_be_placed_with_respect_to_height = function(node) {
316-
if (!this.height)
322+
if (!this.height || this.make_room) {
317323
return true;
324+
}
318325

319326
var clone = this.clone();
320327
clone.add_node(node);
@@ -333,7 +340,7 @@
333340
if (typeof node.min_height != 'undefined') height = Math.max(height, node.min_height);
334341

335342
if (node.x == x && node.y == y && node.width == width && node.height == height) {
336-
return node;
343+
return false;
337344
}
338345

339346
var resizing = node.width != width;
@@ -358,6 +365,64 @@
358365
return _.reduce(this.nodes, function(memo, n) { return Math.max(memo, n.y + n.height); }, 0);
359366
};
360367

368+
GridStackEngine.prototype.grid_is_too_tall = function() {
369+
return this.get_grid_height() > this.height;
370+
};
371+
372+
GridStackEngine.prototype.get_nodes_by_column = function() {
373+
var columns = [];
374+
375+
var nodes_by_column = this.nodes
376+
.forEach(function(node) {
377+
for (var i = 0; i < node.width; i++) {
378+
var col = node.x + i;
379+
columns[col] = columns[col] || [];
380+
columns[col].push(node);
381+
}
382+
});
383+
384+
return columns;
385+
};
386+
387+
GridStackEngine.prototype.column_overflows = function(column) {
388+
var column_height = _(column).map(function(node) {
389+
return node.y + node.height;
390+
}).max();
391+
return column_height > this.height;
392+
};
393+
394+
GridStackEngine.prototype.can_shrink_column = function(column) {
395+
return _(column).any(function(node) {
396+
var min_height = parseInt(node.min_height, 10)
397+
return min_height && node.height > min_height;
398+
});
399+
};
400+
401+
GridStackEngine.prototype.fit_to_height = function() {
402+
var self = this,
403+
everything_fits = true;
404+
405+
if (this.grid_is_too_tall()) {
406+
var columns = this.get_nodes_by_column();
407+
columns.forEach(function(column) {
408+
while (self.column_overflows(column) && self.can_shrink_column(column)) {
409+
column.forEach(function(node) {
410+
self.move_node(
411+
node,
412+
node.x,
413+
node.y,
414+
node.width,
415+
node.height-3)
416+
});
417+
}
418+
});
419+
420+
everything_fits = !this.grid_is_too_tall();
421+
}
422+
423+
return everything_fits;
424+
};
425+
361426
GridStackEngine.prototype.begin_update = function(node) {
362427
_.each(this.nodes, function(n) {
363428
n._orig_y = n.y;
@@ -384,7 +449,7 @@
384449
} else {
385450
return $.extend({}, node);
386451
}
387-
}));
452+
}), this.make_room);
388453

389454
clone.target_node = cloned_node;
390455

@@ -516,11 +581,15 @@
516581
_class: 'grid-stack-' + (Math.random() * 10000).toFixed(0),
517582
animate: Boolean(this.container.attr('data-gs-animate')) || false,
518583
always_show_resize_handle: false,
519-
static_class: 'grid-stack-static'
584+
static_class: 'grid-stack-static',
585+
y_fit_increment: 1,
586+
can_expand_x: true,
587+
make_room_on_drag: false
520588
};
521589

522-
opts = _.defaults(opts, defaults);
523590

591+
592+
opts = _.defaults(opts, defaults);
524593
opts.is_nested = this.container.closest('.' + opts.item_class).size() > 0;
525594

526595
opts.resizable = _.defaults(opts.resizable || {}, {
@@ -535,6 +604,7 @@
535604
});
536605

537606
return opts;
607+
538608
};
539609

540610
GridStack.prototype._trigger_change_event = function(forceTrigger) {
@@ -650,6 +720,7 @@
650720
var cell_width, cell_height;
651721

652722
var on_start_moving = function(event, ui) {
723+
self.grid.make_room = self.opts.make_room_on_drag;
653724
self.container.append(self.placeholder);
654725
var o = $(this);
655726
self.grid.clean_nodes();
@@ -669,6 +740,8 @@
669740
};
670741

671742
var on_end_moving = function(event, ui) {
743+
self.grid.fit_to_height();
744+
self.grid.make_room = false;
672745
self.placeholder.detach();
673746
var o = $(this);
674747
node.el = o;
@@ -971,6 +1044,42 @@
9711044
return this.grid.is_area_empty(x, y, width, height);
9721045
};
9731046

1047+
GridStack.prototype.is_area_empty_and_will_it_fit = function(x, y, width, height, auto_position) {
1048+
return (auto_position || this.is_area_empty(x, y, width, height)) &&
1049+
this.will_it_fit(x, y, width, height, auto_position);
1050+
};
1051+
1052+
GridStack.prototype.try_moving_tile_y = function(x, y, width, height, rows) {
1053+
var increment_y = 0,
1054+
max_y = rows - height,
1055+
fitting_y = y,
1056+
fitted_previously = false;
1057+
1058+
while (increment_y <= max_y) {
1059+
var it_will_fit = this.is_area_empty_and_will_it_fit(x, increment_y, width, height, false);
1060+
if (it_will_fit) {
1061+
if (!fitted_previously) {
1062+
fitting_y = increment_y;
1063+
fitted_previously = true;
1064+
}
1065+
} else {
1066+
fitted_previously = false;
1067+
}
1068+
increment_y += this.opts.y_fit_increment;
1069+
}
1070+
1071+
return fitting_y;
1072+
};
1073+
1074+
GridStack.prototype.try_shrinking_tile_height = function(x, y, width, height, min_height, auto_position) {
1075+
for (var h = height-1; h >= min_height; h -= this.opts.y_fit_increment) {
1076+
if (this.is_area_empty_and_will_it_fit(x, y, width, h, auto_position)) {
1077+
return h;
1078+
}
1079+
}
1080+
return false;
1081+
};
1082+
9741083
GridStack.prototype.set_static = function(static_value) {
9751084
this.opts.static_grid = (static_value === true);
9761085
this._set_static_class();

0 commit comments

Comments
 (0)