|
69 | 69 |
|
70 | 70 | var id_seq = 0;
|
71 | 71 |
|
72 |
| - var GridStackEngine = function(width, onchange, float_mode, height, items) { |
| 72 | + var GridStackEngine = function(width, onchange, float_mode, height, items, make_room) { |
73 | 73 | this.width = width;
|
74 | 74 | this.float = float_mode || false;
|
75 | 75 | this.height = height || 0;
|
| 76 | + this.make_room = make_room || false; |
76 | 77 |
|
77 | 78 | this.nodes = items || [];
|
78 | 79 | this.onchange = onchange || function() {};
|
|
112 | 113 | return;
|
113 | 114 | }
|
114 | 115 |
|
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, |
116 | 117 | 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 | + } |
117 | 123 | }
|
118 | 124 | };
|
119 | 125 |
|
|
305 | 311 | });
|
306 | 312 | }
|
307 | 313 |
|
308 |
| - if (this.height) { |
| 314 | + if (this.height && !this.make_room) { |
309 | 315 | res &= clone.get_grid_height() <= this.height;
|
310 | 316 | }
|
311 | 317 |
|
312 | 318 | return res;
|
313 | 319 | };
|
314 | 320 |
|
315 | 321 | GridStackEngine.prototype.can_be_placed_with_respect_to_height = function(node) {
|
316 |
| - if (!this.height) |
| 322 | + if (!this.height || this.make_room) { |
317 | 323 | return true;
|
| 324 | + } |
318 | 325 |
|
319 | 326 | var clone = this.clone();
|
320 | 327 | clone.add_node(node);
|
|
333 | 340 | if (typeof node.min_height != 'undefined') height = Math.max(height, node.min_height);
|
334 | 341 |
|
335 | 342 | if (node.x == x && node.y == y && node.width == width && node.height == height) {
|
336 |
| - return node; |
| 343 | + return false; |
337 | 344 | }
|
338 | 345 |
|
339 | 346 | var resizing = node.width != width;
|
|
358 | 365 | return _.reduce(this.nodes, function(memo, n) { return Math.max(memo, n.y + n.height); }, 0);
|
359 | 366 | };
|
360 | 367 |
|
| 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 | + |
361 | 426 | GridStackEngine.prototype.begin_update = function(node) {
|
362 | 427 | _.each(this.nodes, function(n) {
|
363 | 428 | n._orig_y = n.y;
|
|
384 | 449 | } else {
|
385 | 450 | return $.extend({}, node);
|
386 | 451 | }
|
387 |
| - })); |
| 452 | + }), this.make_room); |
388 | 453 |
|
389 | 454 | clone.target_node = cloned_node;
|
390 | 455 |
|
|
516 | 581 | _class: 'grid-stack-' + (Math.random() * 10000).toFixed(0),
|
517 | 582 | animate: Boolean(this.container.attr('data-gs-animate')) || false,
|
518 | 583 | 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 |
520 | 588 | };
|
521 | 589 |
|
522 |
| - opts = _.defaults(opts, defaults); |
523 | 590 |
|
| 591 | + |
| 592 | + opts = _.defaults(opts, defaults); |
524 | 593 | opts.is_nested = this.container.closest('.' + opts.item_class).size() > 0;
|
525 | 594 |
|
526 | 595 | opts.resizable = _.defaults(opts.resizable || {}, {
|
|
535 | 604 | });
|
536 | 605 |
|
537 | 606 | return opts;
|
| 607 | + |
538 | 608 | };
|
539 | 609 |
|
540 | 610 | GridStack.prototype._trigger_change_event = function(forceTrigger) {
|
|
650 | 720 | var cell_width, cell_height;
|
651 | 721 |
|
652 | 722 | var on_start_moving = function(event, ui) {
|
| 723 | + self.grid.make_room = self.opts.make_room_on_drag; |
653 | 724 | self.container.append(self.placeholder);
|
654 | 725 | var o = $(this);
|
655 | 726 | self.grid.clean_nodes();
|
|
669 | 740 | };
|
670 | 741 |
|
671 | 742 | var on_end_moving = function(event, ui) {
|
| 743 | + self.grid.fit_to_height(); |
| 744 | + self.grid.make_room = false; |
672 | 745 | self.placeholder.detach();
|
673 | 746 | var o = $(this);
|
674 | 747 | node.el = o;
|
|
971 | 1044 | return this.grid.is_area_empty(x, y, width, height);
|
972 | 1045 | };
|
973 | 1046 |
|
| 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 | + |
974 | 1083 | GridStack.prototype.set_static = function(static_value) {
|
975 | 1084 | this.opts.static_grid = (static_value === true);
|
976 | 1085 | this._set_static_class();
|
|
0 commit comments