Skip to content

Commit bf28a78

Browse files
author
Alain Dumesny
authored
Merge pull request #1096 from adumesny/develop
fixed callbacks types
2 parents 8989dfa + 4f6ae38 commit bf28a78

File tree

12 files changed

+178
-84
lines changed

12 files changed

+178
-84
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ Using gridstack.js with jQuery UI
6363

6464
```html
6565
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gridstack@0.5.5/dist/gridstack.min.css" />
66-
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/gridstack@0.5.5/dist/gridstack.all.js"></script>
66+
<script src="https://cdn.jsdelivr.net/npm/gridstack@0.5.5/dist/gridstack.all.js"></script>
6767
```
6868

6969
* Using CDN (debug):
7070

7171
```html
7272
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gridstack@0.5.5/dist/gridstack.css" />
73-
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/gridstack@0.5.5/dist/gridstack.js"></script>
74-
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/gridstack@0.5.5/dist/jquery-ui.js"></script>
75-
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/gridstack@0.5.5/dist/gridstack.jQueryUI.js"></script>
73+
<script src="https://cdn.jsdelivr.net/npm/gridstack@0.5.5/dist/gridstack.js"></script>
74+
<script src="https://cdn.jsdelivr.net/npm/gridstack@0.5.5/dist/jquery-ui.js"></script>
75+
<script src="https://cdn.jsdelivr.net/npm/gridstack@0.5.5/dist/gridstack.jQueryUI.js"></script>
7676
```
7777

7878
* or local:

demo/advance.html

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ <h1>Advanced Demo</h1>
5353
</div>
5454
</div>
5555
<div class="col-sm-12 col-md-10">
56-
<div class="grid-stack" id="advanced-grid" data-gs-width="12" data-gs-animate="yes">
56+
<div class="grid-stack" id="advanced-grid" data-gs-animate="yes">
5757
<div class="grid-stack-item" data-gs-x="0" data-gs-y="0" data-gs-width="4" data-gs-height="2">
5858
<div class="grid-stack-item-content">1</div>
5959
</div>
@@ -105,7 +105,19 @@ <h1>Advanced Demo</h1>
105105

106106
<script type="text/javascript">
107107
$(function () {
108-
$('#advanced-grid').gridstack({
108+
var $grid = $('#advanced-grid');
109+
110+
$grid.on('added', function(e, items) { log(' added ', items) });
111+
$grid.on('removed', function(e, items) { log(' removed ', items) });
112+
$grid.on('change', function(e, items) { log(' change ', items) });
113+
function log(type, items) {
114+
if (!items) { return; }
115+
var str = '';
116+
for (let i=0; i<items.length && items[i]; i++) { str += ' (x,y)=' + items[i].x + ',' + items[i].y; }
117+
console.log(type + items.length + ' items.' + str );
118+
}
119+
120+
$grid.gridstack({
109121
alwaysShowResizeHandle: /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
110122
navigator.userAgent
111123
),
@@ -116,6 +128,7 @@ <h1>Advanced Demo</h1>
116128
removeTimeout: 100,
117129
acceptWidgets: '.newWidget'
118130
});
131+
119132
$('.newWidget').draggable({
120133
revert: 'invalid',
121134
scroll: false,

demo/responsive.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,13 @@ <h1>Responsive grid demo</h1>
8585
this.grid = $('.grid-stack').data('gridstack');
8686

8787
this.loadGrid = function () {
88+
this.grid.batchUpdate();
8889
this.grid.removeAll();
8990
var items = GridStackUI.Utils.sort(this.serializedData);
9091
items.forEach(function (node, i) {
9192
this.grid.addWidget($('<div><div class="grid-stack-item-content">' + i + '</div></div>'), node);
9293
}.bind(this));
94+
this.grid.commit();
9395
return false;
9496
}.bind(this);
9597

demo/serialization.html

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,19 @@ <h1>Serialization demo</h1>
2727

2828
<script type="text/javascript">
2929
$(function () {
30-
var options = {
31-
};
32-
$('.grid-stack').gridstack(options);
30+
var $grid = $('.grid-stack');
31+
32+
$grid.on('added', function(e, items) { log(' added ', items) });
33+
$grid.on('removed', function(e, items) { log(' removed ', items) });
34+
$grid.on('change', function(e, items) { log(' change ', items) });
35+
function log(type, items) {
36+
if (!items) { return; }
37+
var str = '';
38+
for (let i=0; i<items.length && items[i]; i++) { str += ' (x,y)=' + items[i].x + ',' + items[i].y; }
39+
console.log(type + items.length + ' items.' + str );
40+
}
41+
42+
$grid.gridstack();
3343

3444
new function () {
3545
this.serializedData = [
@@ -46,11 +56,13 @@ <h1>Serialization demo</h1>
4656
this.grid = $('.grid-stack').data('gridstack');
4757

4858
this.loadGrid = function () {
59+
this.grid.batchUpdate();
4960
this.grid.removeAll();
5061
var items = GridStackUI.Utils.sort(this.serializedData);
5162
items.forEach(function (node) {
5263
this.grid.addWidget($('<div><div class="grid-stack-item-content"></div></div>'), node);
5364
}, this);
65+
this.grid.commit();
5466
return false;
5567
}.bind(this);
5668

demo/two.html

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,9 @@ <h1>Two grids demo</h1>
8181
<a class="btn btn-primary" id="float2" href="#">float: true</a>
8282
<div class="grid-stack grid-stack-6" id="grid2">
8383
</div>
84+
</div>
8485
</div>
8586
</div>
86-
</div>
87-
8887

8988
<script type="text/javascript">
9089
$(function () {
@@ -109,12 +108,25 @@ <h1>Two grids demo</h1>
109108
];
110109

111110
$('.grid-stack').each(function () {
112-
var grid = $(this).data('gridstack');
111+
var $grid = $(this);
112+
var id = $grid.attr('id');
113+
$grid.on('added', function(e, items) { log(id, ' added ', items) });
114+
$grid.on('removed', function(e, items) { log(id, ' removed ', items) });
115+
$grid.on('change', function(e, items) { log(id, ' change ', items) });
116+
function log(id, type, items) {
117+
if (!items) { return; }
118+
var str = '';
119+
for (let i=0; i<items.length && items[i]; i++) { str += ' (x,y)=' + items[i].x + ',' + items[i].y; }
120+
console.log(id + type + items.length + ' items.' + str );
121+
}
113122

123+
var grid = $grid.data('gridstack');
124+
grid.batchUpdate();
114125
items.forEach(function (node) {
115126
grid.addWidget($('<div><div class="grid-stack-item-content">'
116127
+ (node.text? node.text : '') + '</div></div>'), node);
117128
});
129+
grid.commit();
118130
});
119131

120132
$('.sidebar .grid-stack-item').draggable({

doc/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Change log
2929

3030
- add `float(val)` to set/get the grid float mode [#1088](https://github.com/gridstack/gridstack.js/pull/1088)
3131
- Allow percentage as a valid unit for height [#1093](https://github.com/gridstack/gridstack.js/pull/1093)
32+
- fixed callbacks to get either `added, removed, change` or combination if adding a node require also to change its (x,y) for example.
33+
Also you can now call `batchUpdate()` before calling a bunch of `addWidget()` and get a single event callback (more efficient).
34+
[#1096](https://github.com/gridstack/gridstack.js/pull/1096)
3235

3336
## v0.5.5 (2019-11-27)
3437

doc/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ grid.addWidget(el, 0, 0, 3, 2, true);
250250

251251
### batchUpdate()
252252

253-
Initializes batch updates. You will see no changes until `commit` method is called.
253+
starts batch updates. You will see no changes until `commit()` method is called.
254254

255255
### cellHeight()
256256

@@ -271,7 +271,7 @@ Gets current cell width.
271271

272272
### commit()
273273

274-
Finishes batch updates. Updates DOM nodes. You must call it after `batchUpdate`.
274+
Ends batch updates. Updates DOM nodes. You must call it after `batchUpdate()`.
275275

276276
### destroy([detachGrid])
277277

@@ -355,7 +355,7 @@ Parameters:
355355
```javascript
356356
$('.grid-stack').gridstack();
357357

358-
$('.grid-stack').append('<div id="gsi-1" data-gs-x="0" data-gs-y="0" data-gs-width="3" data-gs-height="2" data-gs-auto-position="1"></div>')
358+
$('.grid-stack').append('<div id="gsi-1" data-gs-x="0" data-gs-y="0" data-gs-width="3" data-gs-height="2" data-gs-auto-position="true"></div>')
359359
var grid = $('.grid-stack').data('gridstack');
360360
grid.makeWidget('gsi-1');
361361
```

spec/e2e/html/column.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ <h1>setColumn() grid demo</h1>
3737
{x: 2, y: 0, width: 2, height: 1},
3838
{x: 5, y: 0, width: 1, height: 1},
3939
];
40+
self.grid.batchUpdate();
4041
items.forEach(function (node, i) {
4142
self.grid.addWidget($('<div><div class="grid-stack-item-content">' + i + '</div></div>'), node);
4243
});
44+
self.grid.batchUpdate();
4345

4446
$('#1column').click(function() { self.grid.setColumn(1); });
4547
$('#2column').click(function() { self.grid.setColumn(2); });

spec/e2e/html/gridstack-with-height.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ <h1>gridstack.js tests</h1>
3535
];
3636

3737
this.grid = $('.grid-stack').data('gridstack');
38+
this.grid.batchUpdate();
3839
this.grid.removeAll();
3940
items = GridStackUI.Utils.sort(items);
4041
var id = 0;
@@ -43,6 +44,7 @@ <h1>gridstack.js tests</h1>
4344
w.attr('id', 'item-' + (++id));
4445
this.grid.addWidget(w, node);
4546
}, this);
47+
this.grid.commit();
4648
};
4749
});
4850
</script>

spec/gridstack-spec.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -979,12 +979,45 @@ describe('gridstack', function() {
979979
afterEach(function() {
980980
document.body.removeChild(document.getElementById('gs-cont'));
981981
});
982-
it('should keep all widget options the same (autoPosition off)', function() {
982+
it('should autoPosition (missing X,Y)', function() {
983983
$('.grid-stack').gridstack();
984984
var grid = $('.grid-stack').data('gridstack');
985-
var widget = grid.addWidget(widgetHTML, {x: 8, height: 2, id: 'optionWidget'});
985+
var widget = grid.addWidget(widgetHTML, {height: 2, id: 'optionWidget'});
986+
var $widget = $(widget);
987+
expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(8);
988+
expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(0);
989+
expect(parseInt($widget.attr('data-gs-width'), 10)).toBe(1);
990+
expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(2);
991+
expect($widget.attr('data-gs-auto-position')).toBe(undefined);
992+
expect($widget.attr('data-gs-min-width')).toBe(undefined);
993+
expect($widget.attr('data-gs-max-width')).toBe(undefined);
994+
expect($widget.attr('data-gs-min-height')).toBe(undefined);
995+
expect($widget.attr('data-gs-max-height')).toBe(undefined);
996+
expect($widget.attr('data-gs-id')).toBe('optionWidget');
997+
});
998+
it('should autoPosition (missing X)', function() {
999+
$('.grid-stack').gridstack();
1000+
var grid = $('.grid-stack').data('gridstack');
1001+
var widget = grid.addWidget(widgetHTML, {y: 9, height: 2, id: 'optionWidget'});
1002+
var $widget = $(widget);
1003+
expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(8);
1004+
expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(0);
1005+
expect(parseInt($widget.attr('data-gs-width'), 10)).toBe(1);
1006+
expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(2);
1007+
expect($widget.attr('data-gs-auto-position')).toBe(undefined);
1008+
expect($widget.attr('data-gs-min-width')).toBe(undefined);
1009+
expect($widget.attr('data-gs-max-width')).toBe(undefined);
1010+
expect($widget.attr('data-gs-min-height')).toBe(undefined);
1011+
expect($widget.attr('data-gs-max-height')).toBe(undefined);
1012+
expect($widget.attr('data-gs-id')).toBe('optionWidget');
1013+
});
1014+
it('should autoPosition (missing Y)', function() {
1015+
$('.grid-stack').gridstack();
1016+
var grid = $('.grid-stack').data('gridstack');
1017+
var widget = grid.addWidget(widgetHTML, {x: 9, height: 2, id: 'optionWidget'});
9861018
var $widget = $(widget);
9871019
expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(8);
1020+
expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(0);
9881021
expect(parseInt($widget.attr('data-gs-width'), 10)).toBe(1);
9891022
expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(2);
9901023
expect($widget.attr('data-gs-auto-position')).toBe(undefined);
@@ -994,6 +1027,23 @@ describe('gridstack', function() {
9941027
expect($widget.attr('data-gs-max-height')).toBe(undefined);
9951028
expect($widget.attr('data-gs-id')).toBe('optionWidget');
9961029
});
1030+
it('should not autoPosition (correct X, missing Y)', function() {
1031+
$('.grid-stack').gridstack();
1032+
var grid = $('.grid-stack').data('gridstack');
1033+
var widget = grid.addWidget(widgetHTML, {x: 8, height: 2, id: 'optionWidget'});
1034+
var $widget = $(widget);
1035+
expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(8);
1036+
expect($widget.attr('data-gs-y')).toBe(undefined);
1037+
expect($widget.attr('data-gs-width')).toBe(undefined);
1038+
expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(2);
1039+
expect($widget.attr('data-gs-auto-position')).toBe(undefined);
1040+
expect($widget.attr('data-gs-min-width')).toBe(undefined);
1041+
expect($widget.attr('data-gs-max-width')).toBe(undefined);
1042+
expect($widget.attr('data-gs-min-height')).toBe(undefined);
1043+
expect($widget.attr('data-gs-max-height')).toBe(undefined);
1044+
expect($widget.attr('data-gs-id')).toBe('optionWidget');
1045+
});
1046+
9971047
});
9981048

9991049
describe('addWidget() with bad string value widget options', function() {

0 commit comments

Comments
 (0)