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

Skip to content

Commit 8d8f3a9

Browse files
committed
add float option
1 parent 305dd8e commit 8d8f3a9

File tree

2 files changed

+61
-23
lines changed

2 files changed

+61
-23
lines changed

dist/gridstack.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/gridstack.js

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
var id_seq = 0;
1010

11-
var GridStackEngine = function (width, onchange) {
11+
var GridStackEngine = function (width, onchange, float) {
1212
this.width = width;
13+
this.float = float || false;
1314

1415
this.nodes = [];
1516
this.onchange = onchange || function () {};
@@ -38,28 +39,47 @@
3839
GridStackEngine.prototype._pack_nodes = function () {
3940
this._sort_nodes();
4041

41-
_.each(this.nodes, function (n, i) {
42-
while (n.y > 0) {
43-
var new_y = n.y - 1;
44-
var can_be_moved = i == 0;
45-
46-
if (i > 0) {
47-
var collision_node = _.chain(this.nodes)
48-
.first(i)
49-
.find(function (bn) {
50-
return Utils.is_intercepted({x: n.x, y: new_y, width: n.width, height: n.height}, bn);
51-
})
52-
.value();
53-
can_be_moved = typeof collision_node == 'undefined';
42+
if (this.float) {
43+
_.each(this.nodes, function (n, i) {
44+
if (n._updating || typeof n._orig_y == 'undefined' || n.y == n._orig_y)
45+
return;
46+
47+
var collision_node = _.chain(this.nodes)
48+
.find(function (bn) {
49+
return n != bn && Utils.is_intercepted({x: n.x, y: n._orig_y, width: n.width, height: n.height}, bn);
50+
})
51+
.value();
52+
53+
if (!collision_node) {
54+
n._dirty = true;
55+
n.y = n._orig_y;
5456
}
57+
}, this);
58+
}
59+
else {
60+
_.each(this.nodes, function (n, i) {
61+
while (n.y > 0) {
62+
var new_y = n.y - 1;
63+
var can_be_moved = i == 0;
64+
65+
if (i > 0) {
66+
var collision_node = _.chain(this.nodes)
67+
.first(i)
68+
.find(function (bn) {
69+
return Utils.is_intercepted({x: n.x, y: new_y, width: n.width, height: n.height}, bn);
70+
})
71+
.value();
72+
can_be_moved = typeof collision_node == 'undefined';
73+
}
5574

56-
if (!can_be_moved) {
57-
break;
75+
if (!can_be_moved) {
76+
break;
77+
}
78+
n._dirty = n.y != new_y;
79+
n.y = new_y;
5880
}
59-
n._dirty = n.y != new_y;
60-
n.y = new_y;
61-
}
62-
}, this);
81+
}, this);
82+
}
6383
};
6484

6585
GridStackEngine.prototype._prepare_node = function (node, moving) {
@@ -194,6 +214,20 @@
194214
return _.reduce(this.nodes, function (memo, n) { return Math.max(memo, n.y + n.height); }, 0);
195215
};
196216

217+
GridStackEngine.prototype.begin_update = function (node) {
218+
_.each(this.nodes, function (n) {
219+
n._orig_y = n.y;
220+
});
221+
node._updating = true;
222+
};
223+
224+
GridStackEngine.prototype.end_update = function () {
225+
var n = _.find(this.nodes, function (n) { return n._updating; });
226+
if (n) {
227+
n._updating = false;
228+
}
229+
};
230+
197231
var GridStack = function (el, opts) {
198232
var self = this, one_column_mode;
199233

@@ -207,7 +241,8 @@
207241
cell_height: 60,
208242
vertical_margin: 20,
209243
auto: true,
210-
min_width: 768
244+
min_width: 768,
245+
float: false
211246
});
212247

213248
this.grid = new GridStackEngine(this.opts.width, function (nodes) {
@@ -223,7 +258,7 @@
223258
.attr('data-gs-height', n.height);
224259
}
225260
});
226-
});
261+
}, this.opts.float);
227262

228263
if (this.opts.auto) {
229264
this.container.find('.' + this.opts.item_class).each(function (index, el) {
@@ -299,6 +334,7 @@
299334
var on_start_moving = function (event, ui) {
300335
var o = $(this);
301336
self.grid.clean_nodes();
337+
self.grid.begin_update(node);
302338
cell_width = Math.ceil(o.outerWidth() / o.attr('data-gs-width'));
303339
self.placeholder
304340
.attr('data-gs-x', o.attr('data-gs-x'))
@@ -322,6 +358,8 @@
322358
self._update_container_height();
323359
self.container.trigger('change', [self.grid.get_dirty_nodes()]);
324360

361+
self.grid.end_update();
362+
325363
self.grid._sort_nodes();
326364
_.each(self.grid.nodes, function (node) {
327365
node.el.detach();

0 commit comments

Comments
 (0)