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

Skip to content

Commit ec6e6f5

Browse files
committed
add batch_update/commit methods
1 parent 8747785 commit ec6e6f5

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ Inspired by [gridster.js](http://gridster.net). Built with love.
2626
- [onresizestop(event, ui)](#onresizestopevent-ui)
2727
- [API](#api)
2828
- [add_widget(el, x, y, width, height, auto_position)](#add_widgetel-x-y-width-height-auto_position)
29+
- [batch_update()](#batch_update)
2930
- [cell_height()](#cell_height)
3031
- [cell_height(val)](#cell_heightval)
3132
- [cell_width()](#cell_width)
33+
- [commit()](#commit)
3234
- [disable()](#disable)
3335
- [enable()](#enable)
3436
- [get_cell_from_pixel(position)](#get_cell_from_pixelposition)
@@ -218,6 +220,10 @@ var grid = $('.grid-stack').data('gridstack');
218220
grid.add_widget(el, 0, 0, 3, 2, true);
219221
```
220222

223+
### batch_update()
224+
225+
Initailizes batch updates. You will see no changes until `commit` method is called.
226+
221227
### cell_height()
222228

223229
Gets current cell height.
@@ -235,6 +241,10 @@ grid.cell_height(grid.cell_width() * 1.2);
235241

236242
Gets current cell width.
237243

244+
### commit()
245+
246+
Finishes batch updates. Updates DOM nodes. You must call it after `batch_update`.
247+
238248
### disable()
239249

240250
Disables widgets moving/resizing. This is a shortcut for:
@@ -616,6 +626,7 @@ Changes
616626

617627
#### v0.2.3 (development version)
618628

629+
- add `batch_update`/`commit` methods
619630
- add `update` method
620631
- allow to override `resizable`/`draggable` options
621632
- add `disable`/`enable` methods

src/gridstack.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,23 @@
6767

6868
this.nodes = items || [];
6969
this.onchange = onchange || function () {};
70+
71+
this._update_counter = 0;
72+
this._float = this.float;
73+
};
74+
75+
GridStackEngine.prototype.batch_update = function () {
76+
this._update_counter = 1;
77+
this.float = true;
78+
};
79+
80+
GridStackEngine.prototype.commit = function () {
81+
this._update_counter = 0;
82+
if (this._update_counter == 0) {
83+
this.float = this._float;
84+
this._pack_nodes();
85+
this._notify();
86+
}
7087
};
7188

7289
GridStackEngine.prototype._fix_collisions = function (node) {
@@ -188,6 +205,9 @@
188205
};
189206

190207
GridStackEngine.prototype._notify = function () {
208+
if (this._update_counter) {
209+
return;
210+
}
191211
var deleted_nodes = Array.prototype.slice.call(arguments, 1).concat(this.get_dirty_nodes());
192212
deleted_nodes = deleted_nodes.concat(this.get_dirty_nodes());
193213
this.onchange(deleted_nodes);
@@ -502,6 +522,9 @@
502522
};
503523

504524
GridStack.prototype._update_container_height = function () {
525+
if (this.grid._update_counter) {
526+
return;
527+
}
505528
this.container.height(this.grid.get_grid_height() * (this.opts.cell_height + this.opts.vertical_margin) - this.opts.vertical_margin);
506529
};
507530

@@ -789,6 +812,15 @@
789812
return {x: Math.floor(relativeLeft / column_width), y: Math.floor(relativeTop / row_height)};
790813
};
791814

815+
GridStack.prototype.batch_update = function () {
816+
this.grid.batch_update();
817+
};
818+
819+
GridStack.prototype.commit = function () {
820+
this.grid.commit();
821+
this._update_container_height()
822+
};
823+
792824
scope.GridStackUI = GridStack;
793825

794826
scope.GridStackUI.Utils = Utils;

0 commit comments

Comments
 (0)