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

Skip to content

Commit 9928b3a

Browse files
committed
onchange event
1 parent ab84e78 commit 9928b3a

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

gridstack.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
if (!can_be_moved) {
5252
break;
5353
}
54+
n._dirty = n.y != new_y;
5455
n.y = new_y;
5556
}
5657
}, this);
@@ -96,7 +97,17 @@
9697
};
9798

9899
GridStackEngine.prototype._notify = function () {
99-
this.onchange(this.nodes);
100+
var deleted_nodes = Array.prototype.slice.call(arguments, 1).concat(this.get_dirty_nodes());
101+
deleted_nodes = deleted_nodes.concat(this.get_dirty_nodes());
102+
this.onchange(deleted_nodes);
103+
};
104+
105+
GridStackEngine.prototype.clean_nodes = function () {
106+
_.each(this.nodes, function (n) {n._dirty = false });
107+
};
108+
109+
GridStackEngine.prototype.get_dirty_nodes = function () {
110+
return _.filter(this.nodes, function (n) { return n._dirty; });
100111
};
101112

102113
GridStackEngine.prototype.add_node = function(node) {
@@ -108,6 +119,7 @@
108119
if (typeof node.min_height != 'undefined') node.height = Math.max(node.height, node.min_height);
109120

110121
node._id = ++id_seq;
122+
node._dirty = true;
111123
this.nodes.push(node);
112124
this._fix_collisions(node);
113125
this._pack_nodes();
@@ -138,6 +150,7 @@
138150
}
139151

140152
var moving = node.x != x;
153+
node._dirty = true;
141154

142155
node.x = x;
143156
node.y = y;
@@ -175,11 +188,16 @@
175188

176189
this.grid = new GridStackEngine(this.opts.width, function (nodes) {
177190
_.each(nodes, function (n) {
178-
n.el
179-
.attr('data-gs-x', n.x)
180-
.attr('data-gs-y', n.y)
181-
.attr('data-gs-width', n.width)
182-
.attr('data-gs-height', n.height);
191+
if (n._id == null) {
192+
n.el.remove();
193+
}
194+
else {
195+
n.el
196+
.attr('data-gs-x', n.x)
197+
.attr('data-gs-y', n.y)
198+
.attr('data-gs-width', n.width)
199+
.attr('data-gs-height', n.height);
200+
}
183201
});
184202
});
185203

@@ -219,6 +237,7 @@
219237

220238
var on_start_moving = function (event, ui) {
221239
var o = $(this);
240+
self.grid.clean_nodes();
222241
cell_width = Math.ceil(o.outerWidth() / o.attr('data-gs-width'));
223242
self.placeholder
224243
.attr('data-gs-x', o.attr('data-gs-x'))
@@ -240,6 +259,7 @@
240259
.attr('data-gs-height', node.height)
241260
.removeAttr('style');
242261
self._update_container_height();
262+
self.container.trigger('change', [self.grid.get_dirty_nodes()]);
243263
};
244264

245265
el.draggable({

0 commit comments

Comments
 (0)