|
132 | 132 |
|
133 | 133 | this._updateCounter = 0; |
134 | 134 | this._float = this.float; |
| 135 | + |
| 136 | + this._addedNodes = []; |
| 137 | + this._removedNodes = []; |
135 | 138 | }; |
136 | 139 |
|
137 | 140 | GridStackEngine.prototype.batchUpdate = function() { |
|
293 | 296 | return _.filter(this.nodes, function(n) { return n._dirty; }); |
294 | 297 | }; |
295 | 298 |
|
296 | | - GridStackEngine.prototype.addNode = function(node) { |
| 299 | + GridStackEngine.prototype.addNode = function(node, triggerAddEvent) { |
297 | 300 | node = this._prepareNode(node); |
298 | 301 |
|
299 | 302 | if (typeof node.maxWidth != 'undefined') { node.width = Math.min(node.width, node.maxWidth); } |
|
322 | 325 | } |
323 | 326 |
|
324 | 327 | this.nodes.push(node); |
| 328 | + if (typeof triggerAddEvent != 'undefined' && triggerAddEvent) { |
| 329 | + this._addedNodes.push(_.clone(node)); |
| 330 | + } |
325 | 331 |
|
326 | 332 | this._fixCollisions(node); |
327 | 333 | this._packNodes(); |
|
330 | 336 | }; |
331 | 337 |
|
332 | 338 | GridStackEngine.prototype.removeNode = function(node) { |
| 339 | + this._removedNodes.push(_.clone(node)); |
333 | 340 | node._id = null; |
334 | 341 | this.nodes = _.without(this.nodes, node); |
335 | 342 | this._packNodes(); |
|
674 | 681 | } |
675 | 682 | }; |
676 | 683 |
|
| 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 | + |
677 | 698 | GridStack.prototype._initStyles = function() { |
678 | 699 | if (this._stylesId) { |
679 | 700 | Utils.removeStylesheet(this._stylesId); |
|
778 | 799 | this.opts.minWidth; |
779 | 800 | }; |
780 | 801 |
|
781 | | - GridStack.prototype._prepareElement = function(el) { |
| 802 | + GridStack.prototype._prepareElement = function(el, triggerAddEvent) { |
| 803 | + triggerAddEvent = typeof triggerAddEvent != 'undefined' ? triggerAddEvent : false; |
782 | 804 | var self = this; |
783 | 805 | el = $(el); |
784 | 806 |
|
|
798 | 820 | locked: Utils.toBool(el.attr('data-gs-locked')), |
799 | 821 | el: el, |
800 | 822 | id: el.attr('data-gs-id') |
801 | | - }); |
| 823 | + }, triggerAddEvent); |
802 | 824 | el.data('_gridstack_node', node); |
803 | 825 |
|
804 | 826 | var cellWidth; |
|
995 | 1017 | if (typeof maxHeight != 'undefined') { el.attr('data-gs-max-height', maxHeight); } |
996 | 1018 | if (typeof id != 'undefined') { el.attr('data-gs-id', id); } |
997 | 1019 | this.container.append(el); |
998 | | - this._prepareElement(el); |
| 1020 | + this._prepareElement(el, true); |
| 1021 | + this._triggerAddEvent(); |
999 | 1022 | this._updateContainerHeight(); |
1000 | 1023 | this._triggerChangeEvent(true); |
1001 | 1024 |
|
|
1004 | 1027 |
|
1005 | 1028 | GridStack.prototype.makeWidget = function(el) { |
1006 | 1029 | el = $(el); |
1007 | | - this._prepareElement(el); |
| 1030 | + this._prepareElement(el, true); |
| 1031 | + this._triggerAddEvent(); |
1008 | 1032 | this._updateContainerHeight(); |
1009 | 1033 | this._triggerChangeEvent(true); |
1010 | 1034 |
|
|
1033 | 1057 | el.remove(); |
1034 | 1058 | } |
1035 | 1059 | this._triggerChangeEvent(true); |
| 1060 | + this._triggerRemoveEvent(); |
1036 | 1061 | }; |
1037 | 1062 |
|
1038 | 1063 | GridStack.prototype.removeAll = function(detachNode) { |
|
0 commit comments