From 0c424c2637a6b20b09f054839714ca1a70168412 Mon Sep 17 00:00:00 2001 From: d Date: Tue, 16 Aug 2016 14:36:02 -0400 Subject: [PATCH] Always call notify when removing a widget. Only remove the actual element if detachNode is not false. Update tests to reflect these changes. --- spec/gridstack-engine-spec.js | 24 +++++++++++++++++------- src/gridstack.js | 17 +++++++++-------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/spec/gridstack-engine-spec.js b/spec/gridstack-engine-spec.js index 4bb3b0e..db52d2f 100644 --- a/spec/gridstack-engine-spec.js +++ b/spec/gridstack-engine-spec.js @@ -176,21 +176,31 @@ describe('gridstack engine', function() { expect(spy.callback).toHaveBeenCalledWith([ engine.nodes[0], engine.nodes[1] - ]); + ], true); }); - it('should by called with extra passed dirty nodes', function() { - var n1 = {idx: -1}, - n2 = {idx: -2}; + it('should by called with extra passed node to be removed', function() { + var n1 = {idx: -1}; - engine._notify(n1, n2); + engine._notify(n1); expect(spy.callback).toHaveBeenCalledWith([ n1, - n2, engine.nodes[0], engine.nodes[1] - ]); + ], true); + }); + + it('should by called with extra passed node to be removed and should maintain false parameter', function() { + var n1 = {idx: -1}; + + engine._notify(n1, false); + + expect(spy.callback).toHaveBeenCalledWith([ + n1, + engine.nodes[0], + engine.nodes[1] + ], false); }); }); diff --git a/src/gridstack.js b/src/gridstack.js index 6fa9612..9ed863a 100644 --- a/src/gridstack.js +++ b/src/gridstack.js @@ -281,12 +281,14 @@ }; GridStackEngine.prototype._notify = function() { + var args = Array.prototype.slice.call(arguments, 0); + args[0] = typeof args[0] === 'undefined' ? [] : [args[0]]; + args[1] = typeof args[1] === 'undefined' ? true : args[1]; if (this._updateCounter) { return; } - var deletedNodes = Array.prototype.slice.call(arguments, 0); - deletedNodes = deletedNodes.concat(this.getDirtyNodes()); - this.onchange(deletedNodes); + var deletedNodes = args[0].concat(this.getDirtyNodes()); + this.onchange(deletedNodes, args[1]); }; GridStackEngine.prototype.cleanNodes = function() { @@ -345,9 +347,7 @@ node._id = null; this.nodes = _.without(this.nodes, node); this._packNodes(); - if (detachNode) { - this._notify(node); - } + this._notify(node, detachNode); }; GridStackEngine.prototype.canMoveNode = function(node, x, y, width, height) { @@ -578,10 +578,11 @@ this._initStyles(); - this.grid = new GridStackEngine(this.opts.width, function(nodes) { + this.grid = new GridStackEngine(this.opts.width, function(nodes, detachNode) { + detachNode = typeof detachNode === 'undefined' ? true : detachNode; var maxHeight = 0; _.each(nodes, function(n) { - if (n._id === null) { + if (detachNode && n._id === null) { if (n.el) { n.el.remove(); }