Skip to content

Commit aed3252

Browse files
authored
Merge pull request #1278 from adumesny/develop
fix 'addWidget' ignores data attributes #1276
2 parents afc2170 + 7fbbb9c commit aed3252

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Change log
3939
- fix [1195](https://github.com/gridstack/gridstack.js/issues/1195) options broken with ember hash helper - thanks [@btecu](https://github.com/btecu)
4040
- fix [1250](https://github.com/gridstack/gridstack.js/issues/1250) don't remove item from another grid
4141
- fix [1261](https://github.com/gridstack/gridstack.js/issues/1261) `init()` clones passed options so second doesn't affect first one
42+
- fix [1276](https://github.com/gridstack/gridstack.js/issues/1276) `addWidget()` ignores data attributes
4243

4344
## 1.1.1 (2020-03-17)
4445

spec/gridstack-spec.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -899,14 +899,14 @@ describe('gridstack', function() {
899899

900900
});
901901

902-
describe('addWidget() with bad string value widget options', function() {
902+
describe('addWidget()', function() {
903903
beforeEach(function() {
904904
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
905905
});
906906
afterEach(function() {
907907
document.body.removeChild(document.getElementById('gs-cont'));
908908
});
909-
it('should use default', function() {
909+
it('bad string options should use default', function() {
910910
var grid = GridStack.init();
911911
var widget = grid.addWidget(widgetHTML, {x: 'foo', y: null, width: 'bar', height: ''});
912912
var $widget = $(widget);
@@ -915,23 +915,27 @@ describe('gridstack', function() {
915915
expect(parseInt($widget.attr('data-gs-width'), 10)).toBe(1);
916916
expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(1);
917917
});
918-
});
919-
920-
describe('addWidget with null options, ', function() {
921-
beforeEach(function() {
922-
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
923-
});
924-
afterEach(function() {
925-
document.body.removeChild(document.getElementById('gs-cont'));
926-
});
927-
it('should clear x position', function() {
918+
it('null options should clear x position', function() {
928919
var grid = GridStack.init({float: true});
929920
var widgetHTML = '<div class="grid-stack-item" data-gs-x="9"><div class="grid-stack-item-content"></div></div>';
930921
var widget = grid.addWidget(widgetHTML, null, null, undefined);
931922
var $widget = $(widget);
932923
expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(8);
933924
expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(0);
934925
});
926+
it('width attr should be retained', function() { // #1276
927+
var grid = GridStack.init({float: true});
928+
var widgetHTML = '<div class="grid-stack-item" data-gs-width="3" data-gs-max-width="4" data-gs-id="foo"><div class="grid-stack-item-content"></div></div>';
929+
var widget = grid.addWidget(widgetHTML, 1, 5);
930+
var $widget = $(widget);
931+
expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(1);
932+
expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(5);
933+
expect(parseInt($widget.attr('data-gs-width'), 10)).toBe(3);
934+
expect(parseInt($widget.attr('data-gs-max-width'), 10)).toBe(4);
935+
expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(1);
936+
expect($widget.attr('data-gs-id')).toBe('foo');
937+
});
938+
935939
});
936940

937941
describe('method float()', function() {

src/gridstack.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,8 +1425,8 @@
14251425

14261426
/** call to write any default attributes back to element */
14271427
GridStack.prototype._writeAttr = function(el, node) {
1428+
if (!node) { return; }
14281429
el = $(el);
1429-
node = node || {};
14301430
// Note: passing null removes the attr in jquery
14311431
if (node.x !== undefined) { el.attr('data-gs-x', node.x); }
14321432
if (node.y !== undefined) { el.attr('data-gs-y', node.y); }
@@ -1444,7 +1444,7 @@
14441444
if (node.id !== undefined) { el.attr('data-gs-id', node.id); }
14451445
};
14461446

1447-
/** call to write any default attributes back to element */
1447+
/** call to read any default attributes back to element */
14481448
GridStack.prototype._readAttr = function(el, node) {
14491449
el = $(el);
14501450
node = node || {};
@@ -1488,6 +1488,9 @@
14881488

14891489
el = $(el);
14901490
if (opt) { // see knockout above
1491+
// make sure we load any DOM attributes that are not specified in passed in options (which override)
1492+
domAttr = this._readAttr(el);
1493+
Utils.defaults(opt, domAttr);
14911494
this.engine._prepareNode(opt);
14921495
}
14931496
this._writeAttr(el, opt);

0 commit comments

Comments
 (0)