Skip to content

Commit 2abfbb3

Browse files
committed
#54 - Added added and removed events.
1 parent 3f37425 commit 2abfbb3

File tree

6 files changed

+116
-41
lines changed

6 files changed

+116
-41
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ Changes
471471
- add `detachGrid` parameter to `destroy` method ([#216](https://github.com/troolee/gridstack.js/issues/216))
472472
- add `useOffset` parameter to `getCellFromPixel` method ([#237](https://github.com/troolee/gridstack.js/issues/237))
473473
- add `minWidth`, `maxWidth`, `minHeight`, `maxHeight`, `id` parameters to `addWidget` ([#188](https://github.com/troolee/gridstack.js/issues/188))
474+
- add `added` and `removed` events for when a widget is added or removed, respectively. ([#54](https://github.com/troolee/gridstack.js/issues/54))
474475

475476
#### v0.2.4 (2016-02-15)
476477

dist/gridstack.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@
132132

133133
this._updateCounter = 0;
134134
this._float = this.float;
135+
136+
this._addedNodes = [];
137+
this._removedNodes = [];
135138
};
136139

137140
GridStackEngine.prototype.batchUpdate = function() {
@@ -293,7 +296,7 @@
293296
return _.filter(this.nodes, function(n) { return n._dirty; });
294297
};
295298

296-
GridStackEngine.prototype.addNode = function(node) {
299+
GridStackEngine.prototype.addNode = function(node, triggerAddEvent) {
297300
node = this._prepareNode(node);
298301

299302
if (typeof node.maxWidth != 'undefined') { node.width = Math.min(node.width, node.maxWidth); }
@@ -322,6 +325,9 @@
322325
}
323326

324327
this.nodes.push(node);
328+
if (typeof triggerAddEvent != 'undefined' && triggerAddEvent) {
329+
this._addedNodes.push(_.clone(node));
330+
}
325331

326332
this._fixCollisions(node);
327333
this._packNodes();
@@ -330,6 +336,7 @@
330336
};
331337

332338
GridStackEngine.prototype.removeNode = function(node) {
339+
this._removedNodes.push(_.clone(node));
333340
node._id = null;
334341
this.nodes = _.without(this.nodes, node);
335342
this._packNodes();
@@ -674,6 +681,20 @@
674681
}
675682
};
676683

684+
GridStack.prototype._triggerAddEvent = function() {
685+
if (this.grid._addedNodes && this.grid._addedNodes.length > 0) {
686+
this.container.trigger('added', [_.map(this.grid._addedNodes, _.clone)]);
687+
this.grid._addedNodes = [];
688+
}
689+
};
690+
691+
GridStack.prototype._triggerRemoveEvent = function() {
692+
if (this.grid._removedNodes && this.grid._removedNodes.length > 0) {
693+
this.container.trigger('removed', [_.map(this.grid._removedNodes, _.clone)]);
694+
this.grid._removedNodes = [];
695+
}
696+
};
697+
677698
GridStack.prototype._initStyles = function() {
678699
if (this._stylesId) {
679700
Utils.removeStylesheet(this._stylesId);
@@ -778,7 +799,8 @@
778799
this.opts.minWidth;
779800
};
780801

781-
GridStack.prototype._prepareElement = function(el) {
802+
GridStack.prototype._prepareElement = function(el, triggerAddEvent) {
803+
triggerAddEvent = typeof triggerAddEvent != 'undefined' ? triggerAddEvent : false;
782804
var self = this;
783805
el = $(el);
784806

@@ -798,7 +820,7 @@
798820
locked: Utils.toBool(el.attr('data-gs-locked')),
799821
el: el,
800822
id: el.attr('data-gs-id')
801-
});
823+
}, triggerAddEvent);
802824
el.data('_gridstack_node', node);
803825

804826
var cellWidth;
@@ -995,7 +1017,8 @@
9951017
if (typeof maxHeight != 'undefined') { el.attr('data-gs-max-height', maxHeight); }
9961018
if (typeof id != 'undefined') { el.attr('data-gs-id', id); }
9971019
this.container.append(el);
998-
this._prepareElement(el);
1020+
this._prepareElement(el, true);
1021+
this._triggerAddEvent();
9991022
this._updateContainerHeight();
10001023
this._triggerChangeEvent(true);
10011024

@@ -1004,7 +1027,8 @@
10041027

10051028
GridStack.prototype.makeWidget = function(el) {
10061029
el = $(el);
1007-
this._prepareElement(el);
1030+
this._prepareElement(el, true);
1031+
this._triggerAddEvent();
10081032
this._updateContainerHeight();
10091033
this._triggerChangeEvent(true);
10101034

@@ -1033,6 +1057,7 @@
10331057
el.remove();
10341058
}
10351059
this._triggerChangeEvent(true);
1060+
this._triggerRemoveEvent();
10361061
};
10371062

10381063
GridStack.prototype.removeAll = function(detachNode) {

0 commit comments

Comments
 (0)