Skip to content

Commit 2631f71

Browse files
author
Alain Dumesny
authored
Merge pull request #1069 from adumesny/develop
griditems with x=0 placement fix
2 parents 0258954 + 7463cef commit 2631f71

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

doc/CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ Change log
2828
- add `gridstack.poly.js` for IE and older browsers, removed `core-js` lib from samples (<1k vs 85k), and all IE8 mentions ([#1061](https://github.com/gridstack/gridstack.js/pull/1061)).
2929
- add `jquery-ui.js` (and min.js) as minimal subset we need (55k vs 248k), which is now part of `gridstack.all.js`. Include individual parts if you need your own lib instead of all.js
3030
([#1064](https://github.com/gridstack/gridstack.js/pull/1064)).
31-
- changed jquery dep to lowest we can use (>=1.8) ([#629](https://github.com/gridstack/gridstack.js/issues/629)).
31+
- changed jquery dependency to lowest we can use (>=1.8) ([#629](https://github.com/gridstack/gridstack.js/issues/629)).
32+
- fix for griditems with x=0 placement wrong order (introduced for #1017) ([#1054](https://github.com/gridstack/gridstack.js/issues/1054)).
3233

3334
## v0.5.3 (2019-11-20)
3435

spec/gridstack-spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,4 +1376,41 @@ describe('gridstack', function() {
13761376
}
13771377
});
13781378
});
1379+
1380+
describe('custom grid placement #1054', function() {
1381+
var HTML =
1382+
'<div style="width: 992px; height: 800px" id="gs-cont">' +
1383+
' <div class="grid-stack">' +
1384+
' <div class="grid-stack-item" data-gs-x="0" data-gs-y="0" data-gs-width="12" data-gs-height="9">' +
1385+
' <div class="grid-stack-item-content"></div>' +
1386+
' </div>' +
1387+
' <div class="grid-stack-item" data-gs-x="0" data-gs-y="9" data-gs-width="12" data-gs-height="5">' +
1388+
' <div class="grid-stack-item-content"></div>' +
1389+
' </div>' +
1390+
' <div class="grid-stack-item" data-gs-x="0" data-gs-y="14" data-gs-width="7" data-gs-height="6">' +
1391+
' <div class="grid-stack-item-content"></div>' +
1392+
' </div>' +
1393+
' <div class="grid-stack-item" data-gs-x="7" data-gs-y="14" data-gs-width="5" data-gs-height="6">' +
1394+
' <div class="grid-stack-item-content"></div>' +
1395+
' </div>' +
1396+
' </div>' +
1397+
'</div>';
1398+
var pos = [{x:0, y:0, w:12, h:9}, {x:0, y:9, w:12, h:5}, {x:0, y:14, w:7, h:6}, {x:7, y:14, w:5, h:6}];
1399+
beforeEach(function() {
1400+
document.body.insertAdjacentHTML('afterbegin', HTML);
1401+
});
1402+
afterEach(function() {
1403+
document.body.removeChild(document.getElementById('gs-cont'));
1404+
});
1405+
it('should have correct position', function() {
1406+
var items = $('.grid-stack-item');
1407+
for (var i = 0; i < items.length; i++) {
1408+
var item = $(items[i]);
1409+
expect(parseInt(item.attr('data-gs-x'))).toBe(pos[i].x);
1410+
expect(parseInt(item.attr('data-gs-y'))).toBe(pos[i].y);
1411+
expect(parseInt(item.attr('data-gs-width'))).toBe(pos[i].w);
1412+
expect(parseInt(item.attr('data-gs-height'))).toBe(pos[i].h);
1413+
}
1414+
});
1415+
});
13791416
});

src/gridstack.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -801,11 +801,12 @@
801801
this.container.children('.' + this.opts.itemClass + ':not(.' + this.opts.placeholderClass + ')')
802802
.each(function(index, el) {
803803
el = $(el);
804+
var x = parseInt(el.attr('data-gs-x'));
805+
var y = parseInt(el.attr('data-gs-y'));
804806
elements.push({
805807
el: el,
806-
// if x,y are missing (autoPosition) add them to end of list - keep their respective DOM order
807-
i: (parseInt(el.attr('data-gs-x')) || 100) +
808-
(parseInt(el.attr('data-gs-y')) || 100) * _this.opts.column
808+
// if x,y are missing (autoPosition) add them to end of list - but keep their respective DOM order
809+
i: (Number.isNaN(x) ? 1000 : x) + (Number.isNaN(y) ? 1000 : y) * _this.opts.column
809810
});
810811
});
811812
Utils.sortBy(elements, function(x) { return x.i; }).forEach(function(item) {

0 commit comments

Comments
 (0)