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

Skip to content

Commit 1e88ca6

Browse files
committed
add height option
1 parent 5fae8cc commit 1e88ca6

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/gridstack.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828

2929
var id_seq = 0;
3030

31-
var GridStackEngine = function (width, onchange, float) {
31+
var GridStackEngine = function (width, onchange, float, height, items) {
3232
this.width = width;
3333
this.float = float || false;
34+
this.height = height || 0;
3435

35-
this.nodes = [];
36+
this.nodes = items || [];
3637
this.onchange = onchange || function () {};
3738
};
3839

@@ -198,6 +199,23 @@
198199
this._notify(node);
199200
};
200201

202+
GridStackEngine.prototype.can_move_node = function (node, x, y, width, height) {
203+
if (!this.height)
204+
return true;
205+
206+
var cloned_node;
207+
var clone = new GridStackEngine(
208+
this.width,
209+
null,
210+
this.float,
211+
0,
212+
_.map(this.nodes, function (n) { if (n == node) { cloned_node = $.extend({}, n); return cloned_node; } return $.extend({}, n) }));
213+
214+
clone.move_node(cloned_node, x, y, width, height);
215+
216+
return clone.get_grid_height() <= this.height;
217+
};
218+
201219
GridStackEngine.prototype.move_node = function (node, x, y, width, height, no_pack) {
202220
if (typeof x != 'number') x = node.x;
203221
if (typeof y != 'number') y = node.y;
@@ -255,7 +273,8 @@
255273
this.container = $(el);
256274

257275
this.opts = _.defaults(opts || {}, {
258-
width: this.container.attr('data-gs-width') || 12,
276+
width: parseInt(this.container.attr('data-gs-width')) || 12,
277+
height: parseInt(this.container.attr('data-gs-height')) || 0,
259278
item_class: 'grid-stack-item',
260279
placeholder_class: 'grid-stack-placeholder',
261280
handle: '.grid-stack-item-content',
@@ -297,7 +316,7 @@
297316
}
298317
self._styles._max = max_height;
299318
}
300-
}, this.opts.float);
319+
}, this.opts.float, this.opts.height);
301320

302321
if (this.opts.auto) {
303322
this.container.find('.' + this.opts.item_class).each(function (index, el) {
@@ -424,6 +443,9 @@
424443
drag: function (event, ui) {
425444
var x = Math.round(ui.position.left / cell_width),
426445
y = Math.floor(ui.position.top / cell_height);
446+
if (self.opts.height && !self.grid.can_move_node(node, x, y, node.width, node.height)) {
447+
return;
448+
}
427449
self.grid.move_node(node, x, y);
428450
self._update_container_height();
429451
}
@@ -438,6 +460,9 @@
438460
resize: function (event, ui) {
439461
var width = Math.round(ui.size.width / cell_width),
440462
height = Math.round(ui.size.height / cell_height);
463+
if (self.opts.height && !self.grid.can_move_node(node, node.x, node.y, width, height)) {
464+
return;
465+
}
441466
self.grid.move_node(node, node.x, node.y, width, height);
442467
self._update_container_height();
443468
}

0 commit comments

Comments
 (0)