Skip to content

Commit c37513d

Browse files
authored
Merge pull request #1044 from adumesny/develop
null values to addWidget() exception fix
2 parents f2d9969 + aca2a3a commit c37513d

File tree

3 files changed

+59
-66
lines changed

3 files changed

+59
-66
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Change log
2525
## v0.5.2-dev (upcoming changes)
2626

2727
- switch to eslint ([#763](https://github.com/gridstack/gridstack.js/issues/763)).
28+
- null values to addWidget() exception fix ([#1042](https://github.com/gridstack/gridstack.js/issues/1042)).
2829

2930
## v0.5.2 (2019-11-13)
3031

spec/gridstack-spec.js

Lines changed: 56 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ describe('gridstack', function() {
33

44
var e;
55
var w;
6+
// grid has 4x2 and 4x4 top-left aligned - used all most test cases
67
var gridstackHTML =
78
'<div style="width: 992px; height: 800px" id="gs-cont">' +
89
' <div class="grid-stack">' +
9-
' <div class="grid-stack-item"' +
10-
' data-gs-x="0" data-gs-y="0"' +
11-
' data-gs-width="4" data-gs-height="2">' +
12-
' <div class="grid-stack-item-content"></div>' +
13-
' </div>' +
14-
' <div class="grid-stack-item"' +
15-
' data-gs-x="4" data-gs-y="0"' +
16-
' data-gs-width="4" data-gs-height="4">' +
17-
' <div class="grid-stack-item-content"></div>' +
18-
' </div>' +
10+
' <div class="grid-stack-item"' +
11+
' data-gs-x="0" data-gs-y="0"' +
12+
' data-gs-width="4" data-gs-height="2">' +
13+
' <div class="grid-stack-item-content"></div>' +
14+
' </div>' +
15+
' <div class="grid-stack-item"' +
16+
' data-gs-x="4" data-gs-y="0"' +
17+
' data-gs-width="4" data-gs-height="4">' +
18+
' <div class="grid-stack-item-content"></div>' +
19+
' </div>' +
1920
' </div>' +
2021
'</div>';
2122

@@ -104,8 +105,7 @@ describe('gridstack', function() {
104105

105106
describe('grid.setAnimation', function() {
106107
beforeEach(function() {
107-
document.body.insertAdjacentHTML(
108-
'afterbegin', gridstackHTML);
108+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
109109
});
110110
afterEach(function() {
111111
document.body.removeChild(document.getElementById('gs-cont'));
@@ -136,8 +136,7 @@ describe('gridstack', function() {
136136

137137
describe('grid._setStaticClass', function() {
138138
beforeEach(function() {
139-
document.body.insertAdjacentHTML(
140-
'afterbegin', gridstackHTML);
139+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
141140
});
142141
afterEach(function() {
143142
document.body.removeChild(document.getElementById('gs-cont'));
@@ -170,8 +169,7 @@ describe('gridstack', function() {
170169

171170
describe('grid.getCellFromPixel', function() {
172171
beforeEach(function() {
173-
document.body.insertAdjacentHTML(
174-
'afterbegin', gridstackHTML);
172+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
175173
});
176174
afterEach(function() {
177175
document.body.removeChild(document.getElementById('gs-cont'));
@@ -217,8 +215,7 @@ describe('gridstack', function() {
217215

218216
describe('grid.cellWidth', function() {
219217
beforeEach(function() {
220-
document.body.insertAdjacentHTML(
221-
'afterbegin', gridstackHTML);
218+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
222219
});
223220
afterEach(function() {
224221
document.body.removeChild(document.getElementById('gs-cont'));
@@ -249,8 +246,7 @@ describe('gridstack', function() {
249246

250247
describe('grid.cellHeight', function() {
251248
beforeEach(function() {
252-
document.body.insertAdjacentHTML(
253-
'afterbegin', gridstackHTML);
249+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
254250
});
255251
afterEach(function() {
256252
document.body.removeChild(document.getElementById('gs-cont'));
@@ -281,8 +277,7 @@ describe('gridstack', function() {
281277

282278
describe('grid.minWidth', function() {
283279
beforeEach(function() {
284-
document.body.insertAdjacentHTML(
285-
'afterbegin', gridstackHTML);
280+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
286281
});
287282
afterEach(function() {
288283
document.body.removeChild(document.getElementById('gs-cont'));
@@ -306,8 +301,7 @@ describe('gridstack', function() {
306301

307302
describe('grid.maxWidth', function() {
308303
beforeEach(function() {
309-
document.body.insertAdjacentHTML(
310-
'afterbegin', gridstackHTML);
304+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
311305
});
312306
afterEach(function() {
313307
document.body.removeChild(document.getElementById('gs-cont'));
@@ -331,8 +325,7 @@ describe('gridstack', function() {
331325

332326
describe('grid.minHeight', function() {
333327
beforeEach(function() {
334-
document.body.insertAdjacentHTML(
335-
'afterbegin', gridstackHTML);
328+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
336329
});
337330
afterEach(function() {
338331
document.body.removeChild(document.getElementById('gs-cont'));
@@ -356,8 +349,7 @@ describe('gridstack', function() {
356349

357350
describe('grid.maxHeight', function() {
358351
beforeEach(function() {
359-
document.body.insertAdjacentHTML(
360-
'afterbegin', gridstackHTML);
352+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
361353
});
362354
afterEach(function() {
363355
document.body.removeChild(document.getElementById('gs-cont'));
@@ -381,8 +373,7 @@ describe('gridstack', function() {
381373

382374
describe('grid.isAreaEmpty', function() {
383375
beforeEach(function() {
384-
document.body.insertAdjacentHTML(
385-
'afterbegin', gridstackHTML);
376+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
386377
});
387378
afterEach(function() {
388379
document.body.removeChild(document.getElementById('gs-cont'));
@@ -411,8 +402,7 @@ describe('gridstack', function() {
411402

412403
describe('grid method obsolete warnings', function() {
413404
beforeEach(function() {
414-
document.body.insertAdjacentHTML(
415-
'afterbegin', gridstackHTML);
405+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
416406
});
417407
afterEach(function() {
418408
document.body.removeChild(document.getElementById('gs-cont'));
@@ -682,8 +672,7 @@ describe('gridstack', function() {
682672

683673
describe('grid opts obsolete warnings', function() {
684674
beforeEach(function() {
685-
document.body.insertAdjacentHTML(
686-
'afterbegin', gridstackHTML);
675+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
687676
});
688677
afterEach(function() {
689678
document.body.removeChild(document.getElementById('gs-cont'));
@@ -810,8 +799,7 @@ describe('gridstack', function() {
810799

811800
describe('grid method _packNodes with float', function() {
812801
beforeEach(function() {
813-
document.body.insertAdjacentHTML(
814-
'afterbegin', gridstackHTML);
802+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
815803
});
816804
afterEach(function() {
817805
document.body.removeChild(document.getElementById('gs-cont'));
@@ -858,8 +846,7 @@ describe('gridstack', function() {
858846

859847
describe('grid method addWidget with all parameters', function() {
860848
beforeEach(function() {
861-
document.body.insertAdjacentHTML(
862-
'afterbegin', gridstackHTML);
849+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
863850
});
864851
afterEach(function() {
865852
document.body.removeChild(document.getElementById('gs-cont'));
@@ -885,8 +872,7 @@ describe('gridstack', function() {
885872

886873
describe('grid method addWidget with autoPosition true', function() {
887874
beforeEach(function() {
888-
document.body.insertAdjacentHTML(
889-
'afterbegin', gridstackHTML);
875+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
890876
});
891877
afterEach(function() {
892878
document.body.removeChild(document.getElementById('gs-cont'));
@@ -904,13 +890,12 @@ describe('gridstack', function() {
904890

905891
describe('grid method addWidget with widget options', function() {
906892
beforeEach(function() {
907-
document.body.insertAdjacentHTML(
908-
'afterbegin', gridstackHTML);
893+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
909894
});
910895
afterEach(function() {
911896
document.body.removeChild(document.getElementById('gs-cont'));
912897
});
913-
it('should keep all widget options the same (autoPosition off', function() {
898+
it('should keep all widget options the same (autoPosition off)', function() {
914899
$('.grid-stack').gridstack();
915900
var grid = $('.grid-stack').data('gridstack');
916901
var widgetHTML = '<div class="grid-stack-item"><div class="grid-stack-item-content"></div></div>';
@@ -928,11 +913,27 @@ describe('gridstack', function() {
928913
});
929914
});
930915

916+
describe('addWidget with null options, ', function() {
917+
beforeEach(function() {
918+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
919+
});
920+
afterEach(function() {
921+
document.body.removeChild(document.getElementById('gs-cont'));
922+
});
923+
it('should clear x position', function() {
924+
$('.grid-stack').gridstack({float: true});
925+
var grid = $('.grid-stack').data('gridstack');
926+
var widgetHTML = '<div class="grid-stack-item" data-gs-x="9"><div class="grid-stack-item-content"></div></div>';
927+
var widget = grid.addWidget(widgetHTML, null, null, undefined);
928+
var $widget = $(widget);
929+
expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(8);
930+
expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(0);
931+
});
932+
});
931933

932934
describe('grid.destroy', function() {
933935
beforeEach(function() {
934-
document.body.insertAdjacentHTML(
935-
'afterbegin', gridstackHTML);
936+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
936937
});
937938
afterEach(function() {
938939
//document.body.removeChild(document.getElementsByClassName('grid-stack')[0]);
@@ -965,8 +966,7 @@ describe('gridstack', function() {
965966

966967
describe('grid.resize', function() {
967968
beforeEach(function() {
968-
document.body.insertAdjacentHTML(
969-
'afterbegin', gridstackHTML);
969+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
970970
});
971971
afterEach(function() {
972972
document.body.removeChild(document.getElementById('gs-cont'));
@@ -987,8 +987,7 @@ describe('gridstack', function() {
987987

988988
describe('grid.move', function() {
989989
beforeEach(function() {
990-
document.body.insertAdjacentHTML(
991-
'afterbegin', gridstackHTML);
990+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
992991
});
993992
afterEach(function() {
994993
document.body.removeChild(document.getElementById('gs-cont'));
@@ -1010,8 +1009,7 @@ describe('gridstack', function() {
10101009

10111010
describe('grid.moveNode', function() {
10121011
beforeEach(function() {
1013-
document.body.insertAdjacentHTML(
1014-
'afterbegin', gridstackHTML);
1012+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
10151013
});
10161014
afterEach(function() {
10171015
document.body.removeChild(document.getElementById('gs-cont'));
@@ -1050,8 +1048,7 @@ describe('gridstack', function() {
10501048

10511049
describe('grid.update', function() {
10521050
beforeEach(function() {
1053-
document.body.insertAdjacentHTML(
1054-
'afterbegin', gridstackHTML);
1051+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
10551052
});
10561053
afterEach(function() {
10571054
document.body.removeChild(document.getElementById('gs-cont'));
@@ -1075,8 +1072,7 @@ describe('gridstack', function() {
10751072

10761073
describe('grid.verticalMargin', function() {
10771074
beforeEach(function() {
1078-
document.body.insertAdjacentHTML(
1079-
'afterbegin', gridstackHTML);
1075+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
10801076
});
10811077
afterEach(function() {
10821078
document.body.removeChild(document.getElementById('gs-cont'));
@@ -1138,8 +1134,7 @@ describe('gridstack', function() {
11381134

11391135
describe('grid.opts.rtl', function() {
11401136
beforeEach(function() {
1141-
document.body.insertAdjacentHTML(
1142-
'afterbegin', gridstackHTML);
1137+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
11431138
});
11441139
afterEach(function() {
11451140
document.body.removeChild(document.getElementById('gs-cont'));
@@ -1165,8 +1160,7 @@ describe('gridstack', function() {
11651160

11661161
describe('grid.enableMove', function() {
11671162
beforeEach(function() {
1168-
document.body.insertAdjacentHTML(
1169-
'afterbegin', gridstackHTML);
1163+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
11701164
});
11711165
afterEach(function() {
11721166
document.body.removeChild(document.getElementById('gs-cont'));
@@ -1207,8 +1201,7 @@ describe('gridstack', function() {
12071201

12081202
describe('grid.enableResize', function() {
12091203
beforeEach(function() {
1210-
document.body.insertAdjacentHTML(
1211-
'afterbegin', gridstackHTML);
1204+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
12121205
});
12131206
afterEach(function() {
12141207
document.body.removeChild(document.getElementById('gs-cont'));
@@ -1249,8 +1242,7 @@ describe('gridstack', function() {
12491242

12501243
describe('grid.enable', function() {
12511244
beforeEach(function() {
1252-
document.body.insertAdjacentHTML(
1253-
'afterbegin', gridstackHTML);
1245+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
12541246
});
12551247
afterEach(function() {
12561248
document.body.removeChild(document.getElementById('gs-cont'));
@@ -1280,8 +1272,7 @@ describe('gridstack', function() {
12801272

12811273
describe('grid.enable', function() {
12821274
beforeEach(function() {
1283-
document.body.insertAdjacentHTML(
1284-
'afterbegin', gridstackHTML);
1275+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
12851276
});
12861277
afterEach(function() {
12871278
document.body.removeChild(document.getElementById('gs-cont'));

src/gridstack.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,11 +1438,12 @@
14381438
GridStack.prototype.addWidget = function(el, x, y, width, height, autoPosition, minWidth, maxWidth, minHeight, maxHeight, id) {
14391439

14401440
// instead of passing all the params, the user might pass an object with all fields instead, if so extract them and call us back
1441-
if (typeof x === 'object') {
1441+
if (x !== null && typeof x === 'object') {
14421442
return this.addWidget(el, x.x, x.y, x.width, x.height, x.autoPosition, x.minWidth, x.maxWidth, x.minHeight, x.maxHeight, x.id);
14431443
}
14441444

14451445
el = $(el);
1446+
// Note: passing null removes the attr in jquery
14461447
if (typeof x != 'undefined') { el.attr('data-gs-x', x); }
14471448
if (typeof y != 'undefined') { el.attr('data-gs-y', y); }
14481449
if (typeof width != 'undefined') { el.attr('data-gs-width', width); }

0 commit comments

Comments
 (0)