|
28 | 28 |
|
29 | 29 | var id_seq = 0;
|
30 | 30 |
|
31 |
| - var GridStackEngine = function (width, onchange, float) { |
| 31 | + var GridStackEngine = function (width, onchange, float, height, items) { |
32 | 32 | this.width = width;
|
33 | 33 | this.float = float || false;
|
| 34 | + this.height = height || 0; |
34 | 35 |
|
35 |
| - this.nodes = []; |
| 36 | + this.nodes = items || []; |
36 | 37 | this.onchange = onchange || function () {};
|
37 | 38 | };
|
38 | 39 |
|
|
198 | 199 | this._notify(node);
|
199 | 200 | };
|
200 | 201 |
|
| 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 | + |
201 | 219 | GridStackEngine.prototype.move_node = function (node, x, y, width, height, no_pack) {
|
202 | 220 | if (typeof x != 'number') x = node.x;
|
203 | 221 | if (typeof y != 'number') y = node.y;
|
|
255 | 273 | this.container = $(el);
|
256 | 274 |
|
257 | 275 | 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, |
259 | 278 | item_class: 'grid-stack-item',
|
260 | 279 | placeholder_class: 'grid-stack-placeholder',
|
261 | 280 | handle: '.grid-stack-item-content',
|
|
297 | 316 | }
|
298 | 317 | self._styles._max = max_height;
|
299 | 318 | }
|
300 |
| - }, this.opts.float); |
| 319 | + }, this.opts.float, this.opts.height); |
301 | 320 |
|
302 | 321 | if (this.opts.auto) {
|
303 | 322 | this.container.find('.' + this.opts.item_class).each(function (index, el) {
|
|
424 | 443 | drag: function (event, ui) {
|
425 | 444 | var x = Math.round(ui.position.left / cell_width),
|
426 | 445 | 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 | + } |
427 | 449 | self.grid.move_node(node, x, y);
|
428 | 450 | self._update_container_height();
|
429 | 451 | }
|
|
438 | 460 | resize: function (event, ui) {
|
439 | 461 | var width = Math.round(ui.size.width / cell_width),
|
440 | 462 | 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 | + } |
441 | 466 | self.grid.move_node(node, node.x, node.y, width, height);
|
442 | 467 | self._update_container_height();
|
443 | 468 | }
|
|
0 commit comments