Skip to content

Commit b30655d

Browse files
authored
Merge pull request #1148 from adumesny/develop
optimized `change` callback
2 parents e660480 + 920195a commit b30655d

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

demo/column.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ <h1>setColumn() grid demo</h1>
6262
{x: 2, y: 0, width: 2, height: 1},
6363
{x: 5, y: 1, width: 1, height: 1},
6464
{x: 5, y: 0, width: 2, height: 1},
65+
// {x: 0, y: 0, width: 1, height: 1}, // conflict
6566
{text: ' auto'}, // autoPosition testing
67+
// {x: 4, y: 0, width: 1, height: 1}, // same auto-pos
6668
{x: 5, y: 3, width: 2, height: 1},
6769
{x: 0, y: 4, width: 12, height: 1}
6870
];

demo/two.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
<link rel="stylesheet" href="../dist/gridstack-extra.css"/>
1212

1313
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
14+
<!--
15+
16+
<script src="https://cdn.jsdelivr.net/npm/gridstack@0.6.3/dist/gridstack.js"></script>
17+
<script src="https://cdn.jsdelivr.net/npm/gridstack@0.6.3/dist/jquery-ui.js"></script>
18+
<script src="https://cdn.jsdelivr.net/npm/gridstack@0.6.3/dist/gridstack.jQueryUI.js"></script>
19+
-->
20+
1421
<script src="../dist/jquery-ui.min.js"></script>
1522
<script src="../src/gridstack.js"></script>
1623
<script src="../src/gridstack.jQueryUI.js"></script>

doc/CHANGES.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ Change log
3131

3232
## v0.6.3-dev (upcoming changes)
3333

34-
- fix [#1142](https://github.com/gridstack/gridstack.js/issues/1142) RemoveWidget() doesn't also trigger change events when it should
35-
- del `bower` since dead for a while now (https://snyk.io/blog/bower-is-dead/)
34+
- fix [#1142](https://github.com/gridstack/gridstack.js/issues/1142) add/remove widget will also trigger change events when it should.
35+
- optimized `change` callback to save original x,y,w,h values and only call those that changed [1148](https://github.com/gridstack/gridstack.js/pull/1148)
36+
- delete `bower` since [dead](https://snyk.io/blog/bower-is-dead) for a while now
3637

3738
## v0.6.3 (2020-02-05)
3839

src/gridstack.js

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -473,13 +473,26 @@
473473
this.nodes.forEach(function(n) { delete n._dirty; });
474474
};
475475

476-
GridStackEngine.prototype.getDirtyNodes = function() {
476+
GridStackEngine.prototype.getDirtyNodes = function(verify) {
477+
// compare original X,Y,W,H (or entire node?) instead as _dirty can be a temporary state
478+
if (verify) {
479+
var dirtNodes = [];
480+
this.nodes.forEach(function (n) {
481+
if (n._dirty) {
482+
if (n.y === n._origY && n.x === n._origX && n.width === n._origW && n.height === n._origH) {
483+
delete n._dirty;
484+
} else {
485+
dirtNodes.push(n);
486+
}
487+
}
488+
});
489+
return dirtNodes;
490+
}
491+
477492
return this.nodes.filter(function(n) { return n._dirty; });
478493
};
479494

480495
GridStackEngine.prototype.addNode = function(node, triggerAddEvent) {
481-
var prev = {x: node.x, y: node.y, width: node.width, height: node.height};
482-
483496
node = this._prepareNode(node);
484497

485498
if (node.maxWidth !== undefined) { node.width = Math.min(node.width, node.maxWidth); }
@@ -511,10 +524,6 @@
511524
if (triggerAddEvent) {
512525
this._addedNodes.push(node);
513526
}
514-
// use single equal as they come as string/undefined but end as number....
515-
if (!node._dirty && (prev.x != node.x || prev.y != node.y || prev.width != node.width || prev.height != node.height)) {
516-
node._dirty = true;
517-
}
518527

519528
this._fixCollisions(node);
520529
this._packNodes();
@@ -671,6 +680,11 @@
671680
}
672681
};
673682

683+
/**
684+
* Construct a grid from the given element and options
685+
* @param {GridStackElement} el
686+
* @param {GridstackOptions} opts
687+
*/
674688
var GridStack = function(el, opts) {
675689
var self = this;
676690
var oneColumnMode, _prevColumn, isAutoCellHeight;
@@ -809,6 +823,7 @@
809823
this._prepareElement(item.el);
810824
}, this);
811825
}
826+
this.grid._saveInitial(); // initial start of items
812827

813828
this.setAnimation(this.opts.animate);
814829

@@ -1018,19 +1033,20 @@
10181033

10191034
GridStack.prototype._triggerChangeEvent = function(/*forceTrigger*/) {
10201035
if (this.grid._batchMode) { return; }
1021-
// TODO: compare original X,Y,W,H (or entire node?) instead as _dirty can be a temporary state
1022-
var elements = this.grid.getDirtyNodes();
1036+
var elements = this.grid.getDirtyNodes(true); // verify they really changed
10231037
if (elements && elements.length) {
10241038
this.grid._layoutsNodesChange(elements);
10251039
this.container.trigger('change', [elements]);
1026-
this.grid.cleanNodes(); // clear dirty flags now that we called
10271040
}
1041+
this.grid._saveInitial(); // we called, now reset initial values & dirty flags
10281042
};
10291043

10301044
GridStack.prototype._triggerAddEvent = function() {
10311045
if (this.grid._batchMode) { return; }
10321046
if (this.grid._addedNodes && this.grid._addedNodes.length > 0) {
10331047
this.grid._layoutsNodesChange(this.grid._addedNodes);
1048+
// prevent added nodes from also triggering 'change' event (which is called next)
1049+
this.grid._addedNodes.forEach(function (n) { delete n._dirty; });
10341050
this.container.trigger('added', [this.grid._addedNodes]);
10351051
this.grid._addedNodes = [];
10361052
}
@@ -1450,12 +1466,7 @@
14501466
el = $(el);
14511467
this._writeAttr(el, node);
14521468
this.container.append(el);
1453-
this._prepareElement(el, true);
1454-
this._updateContainerHeight();
1455-
this._triggerAddEvent();
1456-
this._triggerChangeEvent(true); // trigger any other changes
1457-
1458-
return el;
1469+
return this.makeWidget(el);
14591470
};
14601471

14611472
GridStack.prototype.makeWidget = function(el) {
@@ -1697,11 +1708,12 @@
16971708
this.grid._sortNodes();
16981709
var nodes = this.grid.nodes;
16991710
this.grid.nodes = []; // pretend we have no nodes to conflict layout to start with...
1700-
nodes.forEach(function(n) {
1701-
if (!n.noMove && !n.locked) {
1702-
n.autoPosition = true;
1711+
nodes.forEach(function(node) {
1712+
if (!node.noMove && !node.locked) {
1713+
node.autoPosition = true;
17031714
}
1704-
this.grid.addNode(n, false); // 'false' for add event trigger
1715+
this.grid.addNode(node, false); // 'false' for add event trigger...
1716+
node._dirty = true; // force attr update
17051717
}, this);
17061718
this.commit();
17071719
};
@@ -1833,8 +1845,6 @@
18331845
}, this);
18341846
}
18351847
}, this);
1836-
1837-
this._saveInitial(); // reset current value now that we diffed.
18381848
}
18391849

18401850
/**
@@ -1923,9 +1933,6 @@
19231933
}, this);
19241934
this.commit();
19251935
delete this._ignoreLayoutsNodeChange;
1926-
1927-
// save this initial layout so we can see what changed and apply changes to other layouts better (diff)
1928-
this._saveInitial();
19291936
}
19301937

19311938
/** called to save initial position/size */
@@ -1935,6 +1942,7 @@
19351942
n._origY = n.y;
19361943
n._origW = n.width;
19371944
n._origH = n.height;
1945+
delete n._dirty;
19381946
});
19391947
}
19401948

@@ -1992,6 +2000,7 @@
19922000
if (!val) {
19932001
this.grid._packNodes();
19942002
this.grid._notify();
2003+
this._triggerChangeEvent();
19952004
}
19962005
};
19972006

0 commit comments

Comments
 (0)