|
64 | 64 | node.y = parseInt('' + node.y);
|
65 | 65 | node.width = parseInt('' + node.width);
|
66 | 66 | node.height = parseInt('' + node.height);
|
| 67 | + node.auto_position = node.auto_position || false; |
67 | 68 |
|
68 | 69 | if (node.width > this.width) {
|
69 | 70 | node.width = this.width;
|
|
120 | 121 |
|
121 | 122 | node._id = ++id_seq;
|
122 | 123 | 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 | + |
123 | 140 | this.nodes.push(node);
|
| 141 | + |
124 | 142 | this._fix_collisions(node);
|
125 | 143 | this._pack_nodes();
|
126 | 144 | this._notify();
|
|
189 | 207 | this.grid = new GridStackEngine(this.opts.width, function (nodes) {
|
190 | 208 | _.each(nodes, function (n) {
|
191 | 209 | if (n._id == null) {
|
192 |
| - n.el.remove(); |
| 210 | + //n.el.remove(); |
193 | 211 | }
|
194 | 212 | else {
|
195 | 213 | n.el
|
|
229 | 247 | min_width: el.attr('data-gs-min-width'),
|
230 | 248 | max_height: el.attr('data-gs-max-height'),
|
231 | 249 | min_height: el.attr('data-gs-min-height'),
|
| 250 | + auto_position: el.attr('data-gs-auto-position'), |
232 | 251 | el: el
|
233 | 252 | });
|
234 | 253 | el.data('_gridstack_node', node);
|
|
295 | 314 | }
|
296 | 315 | };
|
297 | 316 |
|
298 |
| - GridStack.prototype.add_widget = function (el, x, y, width, height) { |
| 317 | + GridStack.prototype.add_widget = function (el, x, y, width, height, auto_position) { |
299 | 318 | el = $(el);
|
300 | 319 | if (typeof x != 'undefined') el.attr('data-gs-x', x);
|
301 | 320 | if (typeof y != 'undefined') el.attr('data-gs-y', y);
|
302 | 321 | if (typeof width != 'undefined') el.attr('data-gs-width', width);
|
303 | 322 | if (typeof height != 'undefined') el.attr('data-gs-height', height);
|
| 323 | + if (typeof auto_position != 'undefined') el.attr('data-gs-auto-position', auto_position); |
304 | 324 | this.container.append(el);
|
305 | 325 | this._prepare_element(el);
|
306 | 326 | this._update_container_height();
|
|
0 commit comments