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

Skip to content

Commit d2ec3bc

Browse files
committed
auto positioning
1 parent 90ae250 commit d2ec3bc

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

gridstack.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
node.y = parseInt('' + node.y);
6565
node.width = parseInt('' + node.width);
6666
node.height = parseInt('' + node.height);
67+
node.auto_position = node.auto_position || false;
6768

6869
if (node.width > this.width) {
6970
node.width = this.width;
@@ -120,7 +121,24 @@
120121

121122
node._id = ++id_seq;
122123
node._dirty = true;
124+
125+
if (node.auto_position) {
126+
this.nodes = _.sortBy(this.nodes, function (n) { return n.x + n.y * this.width; }, this);
127+
128+
for (var i = 0; ; ++i) {
129+
var x = i % this.width, y = Math.floor(i / this.width);
130+
if (!_.find(this.nodes, function (n) {
131+
return Utils.is_intercepted({x: x, y: y, width: node.width, height: node.height}, n);
132+
})) {
133+
node.x = x;
134+
node.y = y;
135+
break;
136+
}
137+
}
138+
}
139+
123140
this.nodes.push(node);
141+
124142
this._fix_collisions(node);
125143
this._pack_nodes();
126144
this._notify();
@@ -189,7 +207,7 @@
189207
this.grid = new GridStackEngine(this.opts.width, function (nodes) {
190208
_.each(nodes, function (n) {
191209
if (n._id == null) {
192-
n.el.remove();
210+
//n.el.remove();
193211
}
194212
else {
195213
n.el
@@ -229,6 +247,7 @@
229247
min_width: el.attr('data-gs-min-width'),
230248
max_height: el.attr('data-gs-max-height'),
231249
min_height: el.attr('data-gs-min-height'),
250+
auto_position: el.attr('data-gs-auto-position'),
232251
el: el
233252
});
234253
el.data('_gridstack_node', node);
@@ -295,12 +314,13 @@
295314
}
296315
};
297316

298-
GridStack.prototype.add_widget = function (el, x, y, width, height) {
317+
GridStack.prototype.add_widget = function (el, x, y, width, height, auto_position) {
299318
el = $(el);
300319
if (typeof x != 'undefined') el.attr('data-gs-x', x);
301320
if (typeof y != 'undefined') el.attr('data-gs-y', y);
302321
if (typeof width != 'undefined') el.attr('data-gs-width', width);
303322
if (typeof height != 'undefined') el.attr('data-gs-height', height);
323+
if (typeof auto_position != 'undefined') el.attr('data-gs-auto-position', auto_position);
304324
this.container.append(el);
305325
this._prepare_element(el);
306326
this._update_container_height();

0 commit comments

Comments
 (0)